Skip to content

Commit

Permalink
category and thread arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Tharp committed Jun 17, 2015
1 parent 07afeb7 commit 28a80e1
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions elasticstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class Elasticstat:
STATUS_COLOR = {'red': ESColors.RED, 'green': ESColors.GREEN, 'yellow': ESColors.YELLOW}

def __init__(self, host, port, username, password, delay_interval, categories, threadpools, no_color):

self.sleep_interval = delay_interval
self.node_counters = {}
self.node_counters['gc'] = {}
Expand All @@ -85,14 +84,9 @@ def __init__(self, host, port, username, password, delay_interval, categories, t
self.node_names = {} # node names, organized by id
self.new_nodes = [] # used to track new nodes that join the cluster
self.active_master = ""
self.threadpools = threadpools
self.no_color = no_color

# categories for display
if categories == 'all':
self.categories = CATEGORIES
else:
self.categories = ['general'] + categories
self.threadpools = self._parse_threadpools(threadpools)
self.categories = self._parse_categories(categories)

# check for port in host
if ':' in host:
Expand All @@ -108,6 +102,26 @@ def __init__(self, host, port, username, password, delay_interval, categories, t

self.es_client = Elasticsearch([host_dict])

def _parse_categories(self, categories):
if isinstance(categories, list):
if categories[0] == 'all':
return CATEGORIES
if ',' in categories[0]:
categories = categories[0].split(',')
else:
if categories == 'all':
return CATEGORIES
for category in categories:
if category not in CATEGORIES:
msg = "{0} is not valid, please choose categories from {1}".format(category, ', '.join(CATEGORIES[1:]))
raise argparse.ArgumentTypeError(msg)
return ['general'] + categories

def _parse_threadpools(self, threadpools):
if isinstance(threadpools, list) and ',' in threadpools[0]:
threadpools = threadpools[0].split(',')
return threadpools

def colorize(self, msg, color):
if self.no_color == True:
return(msg)
Expand Down Expand Up @@ -319,8 +333,6 @@ def format_headings(self):
self.node_headings = " ".join(node_heading_segments)

def print_stats(self):
counter = 0

# just run forever until ctrl-c
while True:
cluster_health = self.es_client.cluster.health()
Expand All @@ -330,10 +342,9 @@ def print_stats(self):
# Print cluster health
cluster_health['timestamp'] = self.thetime()
status = cluster_health['status']
#cluster_health['status'] = self.colorize(status, self.STATUS_COLOR[status])
print self.colorize(self.cluster_headings, ESColors.GRAY)
print self.colorize(CLUSTER_TEMPLATE.format(**cluster_health), self.STATUS_COLOR[status])
print "" # space for readability
#print "" # space for readability

# Nodes can join and leave cluster with each iteration -- in order to report on nodes
# that have left the cluster, maintain a list grouped by role.
Expand Down Expand Up @@ -395,7 +406,7 @@ def main():
default='all',
metavar='CATEGORY',
nargs='+',
help='Statistic categories to show')
help='Statistic categories to show [all or choose from {0}]'.format(', '.join(CATEGORIES[1:])))
parser.add_argument('-t',
'--threadpools',
dest='threadpools',
Expand Down

0 comments on commit 28a80e1

Please sign in to comment.