Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.

Commit 2793fb8

Browse files
author
Greg Rice
committed
seems to be producing the right stat names
1 parent cdeefbe commit 2793fb8

1 file changed

Lines changed: 45 additions & 19 deletions

File tree

rabbitmq.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/python
22
from subprocess import Popen, PIPE
3-
import simplejson as json
3+
import json
44
import urllib
55
import time
6-
from String import Template
6+
from string import Template
77

88
global url, descriptors, last_update, vhost, username, password, url_template, result, result_dict, keyToPath
99
INTERVAL = 20
@@ -13,10 +13,10 @@
1313
RABBITMQCTL="/usr/sbin/rabbitmqctl"
1414
stats = {}
1515
last_update = {}
16-
compiled_results = {}
16+
compiled_results = {"nodes" : None, "queues" : None, "connections" : None}
1717
#Make initial stat test time dict
1818
for stat_type in ('queues', 'connections','exchanges', 'nodes'):
19-
last_update[stat_type] = time.time()
19+
last_update[stat_type] = None
2020

2121
keyToPath = {}
2222

@@ -64,26 +64,35 @@ def dig_it_up(obj,path):
6464
return False
6565

6666
def refreshGroup(group):
67+
6768

68-
69-
urlstring = url_template.safe_substitute(stat = group)
69+
global url_template
70+
urlstring = url_template.safe_substitute(stats = group)
7071

7172
global last_update, url, compiled_results
7273

7374
now = time.time()
74-
diff = now - last_update[group]
75+
if not last_update[group]:
76+
diff = INTERVAL
77+
else:
78+
diff = now - last_update[group]
7579

76-
if diff > INTERVAL:
80+
if diff >= INTERVAL or not last_update[group]:
81+
result_dict = {}
7782
print "Fetching stats after %d seconds" % INTERVAL
83+
print urlstring
7884
result = json.load(urllib.urlopen(urlstring))
85+
print urlstring
7986
compiled_results[group] = result
87+
print result
8088
last_update[group] = now
8189
#Refresh dict by names. We'll probably move this elsewhere.
82-
if group in (queues, nodes):
90+
if group in ('queues', 'nodes'):
8391
for entry in result:
8492
name_attribute = entry['name']
8593
result_dict[name_attribute] = entry
8694
compiled_results[group] = result_dict
95+
print compiled_results.keys()
8796

8897
return compiled_results[group]
8998

@@ -104,13 +113,11 @@ def validatedResult(value):
104113
def list_queues():
105114
# Make a list of queues
106115
results = refreshGroup('queues')
107-
queues = [queue["name"] for queue in results]
108-
return queues
116+
return results.keys()
109117

110118
def list_nodes():
111119
results = refreshGroup('nodes')
112-
nodes = [node['name'] for node in results]
113-
return nodes
120+
return results.keys()
114121

115122
def getQueueStat(name):
116123
#Split a name like "rmq_backing_queue_ack_egress_rate-access"
@@ -128,20 +135,39 @@ def getQueueStat(name):
128135
value = 0
129136

130137
return value
138+
139+
def getNodeStat(name):
140+
#Split a name like "rmq_backing_queue_ack_egress_rate-access"
141+
stat_name, node_name = name.split("-")
142+
143+
result = refreshGroup('nodes')
144+
145+
value = dig_it_up(result, keyToPath[stat_name] % node_name)
146+
print value
147+
148+
#Convert Booleans
149+
if value is True:
150+
value = 1
151+
elif value is False:
152+
value = 0
153+
154+
return value
131155

132156
def metric_init(params):
133157
''' Create the metric definition object '''
134-
global descriptors, params, stats, vhost, username, password, urlstring
158+
global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results
135159
print 'received the following params:'
136-
137160
#Set this globally so we can refresh stats
138161
vhost = params['vhost']
139-
stats = get_vhost_stats(params['vhost'])
140162
username, password = params['username'], params['password']
141163

142164
url = 'http://%s:%s@localhost:55672/api/$stats' % (username, password)
143165
url_template = Template(url)
144166

167+
refreshGroup("nodes")
168+
refreshGroup("queues")
169+
170+
145171
for queue in list_queues():
146172
for metric in QUEUE_METRICS:
147173
d1 = {'name': "%s-%s" % (metric, queue),
@@ -156,7 +182,7 @@ def metric_init(params):
156182
for node in list_nodes():
157183
for stat in NODE_METRICS:
158184
d1 = {'name': '%s-%s' % (stat, node),
159-
'call_back': get_messages_ready,
185+
'call_back': getNodeStat,
160186
'units': 'N',
161187
'slope': 'both',
162188
'format': '%d',
@@ -190,6 +216,6 @@ def get_vhost_stats(vhost, qtypes):
190216
return data
191217

192218
if __name__ == "__main__":
193-
params = {"vhost":"/", "username":"guest","password":"guest"}
194-
metric_init(params)
219+
parameters = {"vhost":"/", "username":"guest","password":"guest"}
220+
metric_init(parameters)
195221

0 commit comments

Comments
 (0)