Skip to content

Commit 9953654

Browse files
committed
mostly conforment to pep8
1 parent 25523ec commit 9953654

1 file changed

Lines changed: 80 additions & 75 deletions

File tree

rabbit/python_modules/rabbitmq.py

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
keyToPath = {}
2828
last_update = None
2929
#last_update = {}
30-
compiled_results = {"nodes" : None, "queues" : None, "connections" : None, "exchanges": None}
30+
compiled_results = {"nodes": None, "queues": None, "connections": None, "exchanges": None}
3131
#Make initial stat test time dict
3232
#for stat_type in ('queues', 'connections','exchanges', 'nodes'):
3333
# last_update[stat_type] = None
@@ -57,15 +57,15 @@
5757
]
5858

5959
QUEUE_METRICS = ['rmq_messages_ready',
60-
'rmq_messages_unacknowledged',
61-
'rmq_backing_queue_ack_egress_rate',
62-
'rmq_backing_queue_ack_ingress_rate',
63-
'rmq_backing_queue_egress_rate',
64-
'rmq_backing_queue_ingress_rate',
65-
'rmq_backing_queue_mirror_senders',
66-
'rmq_memory',
67-
'rmq_consumers',
68-
'rmq_messages']
60+
'rmq_messages_unacknowledged',
61+
'rmq_backing_queue_ack_egress_rate',
62+
'rmq_backing_queue_ack_ingress_rate',
63+
'rmq_backing_queue_egress_rate',
64+
'rmq_backing_queue_ingress_rate',
65+
'rmq_backing_queue_mirror_senders',
66+
'rmq_memory',
67+
'rmq_consumers',
68+
'rmq_messages']
6969

7070
# NODE METRICS #
7171
keyToPath['rmq_disk_free'] = "%s{0}disk_free".format(JSON_PATH_SEPARATOR)
@@ -75,11 +75,11 @@
7575
keyToPath['rmq_mem_used'] = "%s{0}mem_used".format(JSON_PATH_SEPARATOR)
7676
keyToPath['rmq_proc_used'] = "%s{0}proc_used".format(JSON_PATH_SEPARATOR)
7777
keyToPath['rmq_sockets_used'] = "%s{0}sockets_used".format(JSON_PATH_SEPARATOR)
78-
keyToPath['rmq_mem_alarm'] = "%s{0}mem_alarm".format(JSON_PATH_SEPARATOR) #Boolean
78+
keyToPath['rmq_mem_alarm'] = "%s{0}mem_alarm".format(JSON_PATH_SEPARATOR) # Boolean
7979
keyToPath['rmq_mem_binary'] = "%s{0}mem_binary".format(JSON_PATH_SEPARATOR)
8080
keyToPath['rmq_mem_code'] = "%s{0}mem_code".format(JSON_PATH_SEPARATOR)
8181
keyToPath['rmq_mem_proc_used'] = "%s{0}mem_proc_used".format(JSON_PATH_SEPARATOR)
82-
keyToPath['rmq_running'] = "%s{0}running".format(JSON_PATH_SEPARATOR) #Boolean
82+
keyToPath['rmq_running'] = "%s{0}running".format(JSON_PATH_SEPARATOR) # Boolean
8383

8484
NODE_METRICS = ['rmq_disk_free', 'rmq_mem_used', 'rmq_disk_free_alarm', 'rmq_running', 'rmq_proc_used', 'rmq_mem_proc_used', 'rmq_fd_used', 'rmq_mem_alarm', 'rmq_mem_code', 'rmq_mem_binary', 'rmq_sockets_used']
8585

@@ -94,18 +94,19 @@
9494
def metric_cleanup():
9595
pass
9696

97-
def dig_it_up(obj,path):
97+
98+
def dig_it_up(obj, path):
9899
try:
99100
path = path.split(JSON_PATH_SEPARATOR)
100-
return reduce(lambda x,y:x[y],path,obj)
101+
return reduce(lambda x, y: x[y], path, obj)
101102
except Exception, e:
102103
# not WARN because the False return is used for control flow
103104
# (zero assumed)
104105
log.debug('dig_it_up Exception %r path: %s' % (e, path))
105106
return False
106107

107-
def refreshStats(stats = ('nodes', 'queues'), vhosts = ['/']):
108108

109+
def refreshStats(stats=('nodes', 'queues'), vhosts=['/']):
109110
global url_template
110111
global last_update, url, compiled_results
111112

@@ -124,7 +125,7 @@ def refreshStats(stats = ('nodes', 'queues'), vhosts = ['/']):
124125
if stat in ('nodes'):
125126
vhost = '/'
126127
result_dict = {}
127-
urlstring = url_template.safe_substitute(stats = stat, vhost = vhost)
128+
urlstring = url_template.safe_substitute(stats=stat, vhost=vhost)
128129
log.debug('urlspring: %s' % urlstring)
129130
result = json.load(urllib2.urlopen(urlstring))
130131
# Rearrange results so entry is held in a dict keyed by name - queue name, host name, etc.
@@ -143,85 +144,90 @@ def validatedResult(value):
143144
else:
144145
return None
145146

147+
146148
def list_queues(vhost):
147149
global compiled_results
148150
queues = compiled_results[('queues', vhost)].keys()
149151
return queues
150152

153+
151154
def list_nodes():
152155
global compiled_results
153156
nodes = compiled_results[('nodes', '/')].keys()
154157
return nodes
155158

159+
156160
def list_exchanges(vhost):
157161
global compiled_results
158162
exchanges = compiled_results[('exchanges', vhost)].keys()
159163
return exchanges
160164

161165

162166
def getQueueStat(name):
163-
refreshStats(stats = STATS, vhosts = vhosts)
164-
#Split a name like "rmq_backing_queue_ack_egress_rate.access"
167+
refreshStats(stats=STATS, vhosts=vhosts)
168+
# Split a name like "rmq_backing_queue_ack_egress_rate.access"
165169

166-
#handle queue names with . in them
170+
# handle queue names with . in them
167171

168172
log.debug(name)
169173
stat_name, queue_name, vhost = name.split(METRIC_TOKEN_SEPARATOR)
170174

171-
vhost = vhost.replace('-', '/') #decoding vhost from metric name
175+
vhost = vhost.replace('-', '/') # decoding vhost from metric name
172176
# Run refreshStats to get the result object
173177
result = compiled_results[('queues', vhost)]
174178

175179
value = dig_it_up(result, keyToPath[stat_name] % queue_name)
176180

177-
if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[queue_name].keys():
181+
if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[queue_name].keys():
178182
value = 0
179183

180-
#Convert Booleans
184+
# Convert Booleans
181185
if value is True:
182186
value = 1
183187
elif value is False:
184188
value = 0
185189

186190
return float(value)
187191

192+
188193
def getNodeStat(name):
189-
refreshStats(stats = STATS, vhosts = vhosts)
190-
#Split a name like "rmq_backing_queue_ack_egress_rate.access"
194+
refreshStats(stats=STATS, vhosts=vhosts)
195+
# Split a name like "rmq_backing_queue_ack_egress_rate.access"
191196
stat_name, node_name, vhost = name.split(METRIC_TOKEN_SEPARATOR)
192-
vhost = vhost.replace('-', '/') #decoding vhost from metric name
197+
vhost = vhost.replace('-', '/') # decoding vhost from metric name
193198

194199
result = compiled_results[('nodes', '/')]
195200
value = dig_it_up(result, keyToPath[stat_name] % node_name)
196201

197202
log.debug('name: %r value: %r' % (name, value))
198-
#Convert Booleans
203+
# Convert Booleans
199204
if value is True:
200205
value = 1
201206
elif value is False:
202207
value = 0
203208

204209
return float(value)
205210

211+
206212
def getExchangeStat(name):
207-
refreshStats(stats = STATS, vhosts = vhosts)
208-
#Split a name like "rmq_backing_queue_ack_egress_rate.access"
213+
refreshStats(stats=STATS, vhosts=vhosts)
214+
# Split a name like "rmq_backing_queue_ack_egress_rate.access"
209215

210-
#handle queue names with . in them
216+
# handle queue names with . in them
211217

212218
log.debug(name)
213219
stat_name, exchange_name, vhost = name.split(METRIC_TOKEN_SEPARATOR)
214220

215-
vhost = vhost.replace('-', '/') #decoding vhost from metric name
221+
vhost = vhost.replace('-', '/') # decoding vhost from metric name
216222
# Run refreshStats to get the result object
217223
result = compiled_results[('exchanges', vhost)]
218224

219225
value = dig_it_up(result, keyToPath[stat_name] % exchange_name)
220226

221-
if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[exchange_name].keys():
227+
if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[exchange_name].keys():
222228
value = 0
223229

224-
#Convert Booleans
230+
# Convert Booleans
225231
if value is True:
226232
value = 1
227233
elif value is False:
@@ -236,26 +242,28 @@ def product(*args, **kwds):
236242
pools = map(tuple, args) * kwds.get('repeat', 1)
237243
result = [[]]
238244
for pool in pools:
239-
result = [x+[y] for x in result for y in pool]
245+
result = [x + [y] for x in result for y in pool]
240246
for prod in result:
241247
yield tuple(prod)
242248

249+
243250
def str2bool(string):
244251
if string.lower() in ("yes", "true"):
245252
return True
246253
if string.lower() in ("no", "false"):
247254
return False
248255
raise Exception("Invalid value of the 'zero_rates_when_idle' param, use one of the ('true', 'yes', 'false', 'no')")
249256

257+
250258
def metric_init(params):
251259
''' Create the metric definition object '''
252260
global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results, STATS, vhosts, zero_rates_when_idle
253261
if log is None:
254-
setup_logging('syslog', params['syslog_facility'], params['log_level'])
262+
setup_logging('syslog', params['syslog_facility'], params['log_level'])
255263
log.info('received the following params: %r' % params)
256-
#Set this globally so we can refresh stats
264+
# Set this globally so we can refresh stats
257265
if 'host' not in params:
258-
params['host'], params['vhost'],params['username'],params['password'],params['port'] = "localhost", "/", "guest", "guest", "15672"
266+
params['host'], params['vhost'], params['username'], params['password'], params['port'] = "localhost", "/", "guest", "guest", "15672"
259267
if 'zero_rates_when_idle' not in params:
260268
params['zero_rates_when_idle'] = "false"
261269

@@ -268,8 +276,8 @@ def metric_init(params):
268276

269277
zero_rates_when_idle = str2bool(params['zero_rates_when_idle'])
270278

271-
url = 'http://%s:%s/api/$stats/$vhost' % (host,port)
272-
base_url = 'http://%s:%s/api' % (host,port)
279+
url = 'http://%s:%s/api/$stats/$vhost' % (host, port)
280+
base_url = 'http://%s:%s/api' % (host, port)
273281
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
274282
password_mgr.add_password(None, base_url, username, password)
275283
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
@@ -278,14 +286,12 @@ def metric_init(params):
278286
urllib2.install_opener(opener)
279287
url_template = Template(url)
280288

281-
refreshStats(stats = STATS, vhosts = vhosts)
289+
refreshStats(stats=STATS, vhosts=vhosts)
282290

283291
def metric_handler(name):
284292
if 15 < time.time() - metric_handler.timestamp:
285293
metric_handler.timestamp = time.time()
286-
return refreshStats(stats = STATS, vhosts = vhosts)
287-
288-
294+
return refreshStats(stats=STATS, vhosts=vhosts)
289295

290296
def create_desc(prop):
291297
d = {
@@ -300,25 +306,24 @@ def create_desc(prop):
300306
'groups' : params["metric_group"],
301307
}
302308

303-
for k,v in prop.iteritems():
309+
for k, v in prop.iteritems():
304310
d[k] = v
305311
return d
306312

307-
308313
def buildQueueDescriptors():
309314
for vhost, metric in product(vhosts, QUEUE_METRICS):
310315
queues = list_queues(vhost)
311316
for queue in queues:
312317
name = "{1}{0}{2}{0}{3}".format(METRIC_TOKEN_SEPARATOR, metric, queue, vhost.replace('/', '-'))
313318
log.debug(name)
314-
d1 = create_desc({'name': name.encode('ascii','ignore'),
315-
'call_back': getQueueStat,
316-
'value_type': 'float',
317-
'units': 'N',
318-
'slope': 'both',
319-
'format': '%f',
320-
'description': 'Queue_Metric',
321-
'groups' : 'rabbitmq,queue'})
319+
d1 = create_desc({'name': name.encode('ascii', 'ignore'),
320+
'call_back': getQueueStat,
321+
'value_type': 'float',
322+
'units': 'N',
323+
'slope': 'both',
324+
'format': '%f',
325+
'description': 'Queue_Metric',
326+
'groups': 'rabbitmq,queue'})
322327
log.debug(d1)
323328
descriptors.append(d1)
324329

@@ -327,14 +332,14 @@ def buildNodeDescriptors():
327332
for node in list_nodes():
328333
name = "{1}{0}{2}{0}-".format(METRIC_TOKEN_SEPARATOR, metric, node)
329334
log.debug(name)
330-
d2 = create_desc({'name': name.encode('ascii','ignore'),
331-
'call_back': getNodeStat,
332-
'value_type': 'float',
333-
'units': 'N',
334-
'slope': 'both',
335-
'format': '%f',
336-
'description': 'Node_Metric',
337-
'groups' : 'rabbitmq,node'})
335+
d2 = create_desc({'name': name.encode('ascii', 'ignore'),
336+
'call_back': getNodeStat,
337+
'value_type': 'float',
338+
'units': 'N',
339+
'slope': 'both',
340+
'format': '%f',
341+
'description': 'Node_Metric',
342+
'groups': 'rabbitmq,node'})
338343
log.debug(d2)
339344
descriptors.append(d2)
340345

@@ -344,14 +349,14 @@ def buildExchangeDescriptors():
344349
for exchange in exchanges:
345350
name = "{1}{0}{2}{0}{3}".format(METRIC_TOKEN_SEPARATOR, metric, exchange, vhost.replace('/', '-'))
346351
log.debug(name)
347-
d1 = create_desc({'name': name.encode('ascii','ignore'),
348-
'call_back': getExchangeStat,
349-
'value_type': 'float',
350-
'units': 'N',
351-
'slope': 'both',
352-
'format': '%f',
353-
'description': 'Exchange_Metric',
354-
'groups' : 'rabbitmq,exchange'})
352+
d1 = create_desc({'name': name.encode('ascii', 'ignore'),
353+
'call_back': getExchangeStat,
354+
'value_type': 'float',
355+
'units': 'N',
356+
'slope': 'both',
357+
'format': '%f',
358+
'description': 'Exchange_Metric',
359+
'groups': 'rabbitmq,exchange'})
355360
log.debug(d1)
356361
descriptors.append(d1)
357362

@@ -365,6 +370,7 @@ def buildExchangeDescriptors():
365370

366371
return descriptors
367372

373+
368374
def metric_cleanup():
369375
pass
370376

@@ -373,7 +379,7 @@ def setup_logging(handlers, facility, level):
373379
global log
374380

375381
log = logging.getLogger('gmond_python_rabbitmq')
376-
formatter = logging.Formatter(' | '.join(['%(asctime)s', '%(name)s', '%(levelname)s', '%(message)s']))
382+
formatter = logging.Formatter(' | '.join(['%(asctime)s', '%(name)s', '%(levelname)s', '%(message)s']))
377383
if handlers in ['syslog', 'both']:
378384
sh = logging.handlers.SysLogHandler(address='/dev/log', facility=facility)
379385
sh.setFormatter(formatter)
@@ -388,8 +394,7 @@ def setup_logging(handlers, facility, level):
388394
'WARNING': logging.WARNING,
389395
'INFO': logging.INFO,
390396
'DEBUG': logging.DEBUG,
391-
'NOTSET': logging.NOTSET
392-
}
397+
'NOTSET': logging.NOTSET}
393398
log.setLevel(lmap[level])
394399

395400

@@ -428,15 +433,15 @@ def main(argv):
428433
""" used for testing """
429434
(opts, args) = parse_args(argv)
430435
setup_logging(opts.log, opts.log_facility, opts.log_level)
431-
### in config files we use '/' in vhosts names but we should convert '/' to '-' when calculating a metric
432-
parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq",
436+
# in config files we use '/' in vhosts names but we should convert '/' to '-' when calculating a metric
437+
parameters = {"vhost": "/", "username": "guest", "password": "guest", "metric_group": "rabbitmq",
433438
"zero_rates_when_idle": "yes",
434439
"host": opts.admin_host, "port": opts.admin_port,
435440
"stats": opts.stats.split(','),
436441
"vhosts": opts.vhosts.split(',')}
437442
descriptors = metric_init(parameters)
438-
result = refreshStats(stats = parameters['stats'], vhosts = parameters['vhosts'])
439-
print '***'*20
443+
result = refreshStats(stats=parameters['stats'], vhosts=parameters['vhosts'])
444+
print '***' * 20
440445
if opts.list_only is True:
441446
print 'nodes:'
442447
pprint.pprint(list_nodes())

0 commit comments

Comments
 (0)