11#!/usr/bin/python2.4
22import sys
33import os
4- import simplejson as json
4+ import json
55import urllib2
66import time
77from string import Template
88import itertools
99import threading
1010
11- global url , descriptors , last_update , vhost , username , password , url_template , result , result_dict , keyToPath
11+ global url , descriptors , last_update , vhost , username , password , url_template , result , result_dict , keyToPath
12+
13+
14+ JSON_PATH_SEPARATOR = "?"
15+ METRIC_TOKEN_SEPARATOR = "___"
16+
1217INTERVAL = 10
1318descriptors = list ()
1419username , password = "guest" , "guest"
2530STATS = ['nodes' , 'queues' ]
2631
2732# QUEUE METRICS #
28- keyToPath ['rmq_messages_ready' ] = "%s. messages_ready"
29- keyToPath ['rmq_messages_unacknowledged' ] = "%s. messages_unacknowledged"
30- keyToPath ['rmq_backing_queue_ack_egress_rate' ] = "%s. backing_queue_status. avg_ack_egress_rate"
31- keyToPath ['rmq_backing_queue_ack_ingress_rate' ] = "%s. backing_queue_status. avg_ack_ingress_rate"
32- keyToPath ['rmq_backing_queue_egress_rate' ] = "%s. backing_queue_status. avg_egress_rate"
33- keyToPath ['rmq_backing_queue_ingress_rate' ] = "%s. backing_queue_status. avg_ingress_rate"
34- keyToPath ['rmq_backing_queue_mirror_senders' ] = "%s. backing_queue_status. mirror_senders"
35- keyToPath ['rmq_memory' ] = "%s. memory"
36- keyToPath ['rmq_consumers' ] = "%s. consumers"
37- keyToPath ['rmq_messages' ] = "%s. messages"
33+ keyToPath ['rmq_messages_ready' ] = "%s{0} messages_ready" . format ( JSON_PATH_SEPARATOR )
34+ keyToPath ['rmq_messages_unacknowledged' ] = "%s{0} messages_unacknowledged" . format ( JSON_PATH_SEPARATOR )
35+ keyToPath ['rmq_backing_queue_ack_egress_rate' ] = "%s{0} backing_queue_status{0} avg_ack_egress_rate" . format ( JSON_PATH_SEPARATOR )
36+ keyToPath ['rmq_backing_queue_ack_ingress_rate' ] = "%s{0} backing_queue_status{0} avg_ack_ingress_rate" . format ( JSON_PATH_SEPARATOR )
37+ keyToPath ['rmq_backing_queue_egress_rate' ] = "%s{0} backing_queue_status{0} avg_egress_rate" . format ( JSON_PATH_SEPARATOR )
38+ keyToPath ['rmq_backing_queue_ingress_rate' ] = "%s{0} backing_queue_status{0} avg_ingress_rate" . format ( JSON_PATH_SEPARATOR )
39+ keyToPath ['rmq_backing_queue_mirror_senders' ] = "%s{0} backing_queue_status{0} mirror_senders" . format ( JSON_PATH_SEPARATOR )
40+ keyToPath ['rmq_memory' ] = "%s{0} memory" . format ( JSON_PATH_SEPARATOR )
41+ keyToPath ['rmq_consumers' ] = "%s{0} consumers" . format ( JSON_PATH_SEPARATOR )
42+ keyToPath ['rmq_messages' ] = "%s{0} messages" . format ( JSON_PATH_SEPARATOR )
3843
3944QUEUE_METRICS = ['rmq_messages_ready' ,
4045 'rmq_messages_unacknowledged' ,
4853 'rmq_messages' ]
4954
5055# NODE METRICS #
51- keyToPath ['rmq_disk_free' ] = "%s. disk_free"
52- keyToPath ['rmq_disk_free_alarm' ] = "%s. disk_free_alarm"
53- keyToPath ['rmq_fd_used' ] = "%s. fd_used"
54- keyToPath ['rmq_fd_used' ] = "%s. fd_used"
55- keyToPath ['rmq_mem_used' ] = "%s. mem_used"
56- keyToPath ['rmq_proc_used' ] = "%s. proc_used"
57- keyToPath ['rmq_sockets_used' ] = "%s. sockets_used"
58- keyToPath ['rmq_mem_alarm' ] = "%s. mem_alarm" #Boolean
59- keyToPath ['rmq_mem_binary' ] = "%s. mem_binary"
60- keyToPath ['rmq_mem_code' ] = "%s. mem_code"
61- keyToPath ['rmq_mem_proc_used' ] = "%s. mem_proc_used"
62- keyToPath ['rmq_running' ] = "%s. running" #Boolean
56+ keyToPath ['rmq_disk_free' ] = "%s{0} disk_free" . format ( JSON_PATH_SEPARATOR )
57+ keyToPath ['rmq_disk_free_alarm' ] = "%s{0} disk_free_alarm" . format ( JSON_PATH_SEPARATOR )
58+ keyToPath ['rmq_fd_used' ] = "%s{0} fd_used" . format ( JSON_PATH_SEPARATOR )
59+ keyToPath ['rmq_fd_used' ] = "%s{0} fd_used" . format ( JSON_PATH_SEPARATOR )
60+ keyToPath ['rmq_mem_used' ] = "%s{0} mem_used" . format ( JSON_PATH_SEPARATOR )
61+ keyToPath ['rmq_proc_used' ] = "%s{0} proc_used" . format ( JSON_PATH_SEPARATOR )
62+ keyToPath ['rmq_sockets_used' ] = "%s{0} sockets_used" . format ( JSON_PATH_SEPARATOR )
63+ keyToPath ['rmq_mem_alarm' ] = "%s{0} mem_alarm" . format ( JSON_PATH_SEPARATOR ) #Boolean
64+ keyToPath ['rmq_mem_binary' ] = "%s{0} mem_binary" . format ( JSON_PATH_SEPARATOR )
65+ keyToPath ['rmq_mem_code' ] = "%s{0} mem_code" . format ( JSON_PATH_SEPARATOR )
66+ keyToPath ['rmq_mem_proc_used' ] = "%s{0} mem_proc_used" . format ( JSON_PATH_SEPARATOR )
67+ keyToPath ['rmq_running' ] = "%s{0} running" . format ( JSON_PATH_SEPARATOR ) #Boolean
6368
6469NODE_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' ]
6570
@@ -71,7 +76,9 @@ def metric_cleanup():
7176
7277def dig_it_up (obj ,path ):
7378 try :
74- path = path .split ('.' )
79+ path = path .split (JSON_PATH_SEPARATOR )
80+ print "obj is" , obj
81+ print "path is" , path
7582 return reduce (lambda x ,y :x [y ],path ,obj )
7683 except :
7784 print "Exception"
@@ -109,6 +116,7 @@ def refreshStats(stats = ('nodes', 'queues'), vhosts = ['/']):
109116
110117 return compiled_results
111118
119+
112120def validatedResult (value ):
113121 if not isInstance (value , bool ):
114122 return float (value )
@@ -131,14 +139,18 @@ def getQueueStat(name):
131139
132140 #handle queue names with . in them
133141 print name
134- split_name , vhost = name .split ("#" )
135- split_name = split_name .split ("." )
136- stat_name = split_name [0 ]
137- queue_name = "." .join (split_name [1 :])
142+ stat_name , queue_name , vhost = name .split (METRIC_TOKEN_SEPARATOR )
138143
144+ print "vhost is " , vhost
145+ print "stat_name is " , stat_name
146+ print "queue_name is " , queue_name
147+ vhost = vhost .replace ('-' , '/' ) #decoding vhost from metric name
139148 # Run refreshStats to get the result object
140149 result = compiled_results [('queues' , vhost )]
141150
151+ print "keyToPath[stat_name]" , keyToPath [stat_name ]
152+ tmp_res = keyToPath [stat_name ] % queue_name
153+ print "tmp_res is " , tmp_res
142154 value = dig_it_up (result , keyToPath [stat_name ] % queue_name )
143155 print name , value
144156
@@ -153,8 +165,9 @@ def getQueueStat(name):
153165def getNodeStat (name ):
154166 refreshStats (stats = STATS , vhosts = vhosts )
155167 #Split a name like "rmq_backing_queue_ack_egress_rate.access"
156- stat_name = name .split ("." )[0 ]
157- node_name , vhost = name .split ("." )[1 ].split ("#" )
168+ stat_name , node_name , vhost = name .split (METRIC_TOKEN_SEPARATOR )
169+ vhost = vhost .replace ('-' , '/' ) #decoding vhost from metric name
170+
158171 result = compiled_results [('nodes' , '/' )]
159172 value = dig_it_up (result , keyToPath [stat_name ] % node_name )
160173
@@ -233,7 +246,7 @@ def buildQueueDescriptors():
233246 for vhost , metric in product (vhosts , QUEUE_METRICS ):
234247 queues = list_queues (vhost )
235248 for queue in queues :
236- name = "%s.%s#%s" % ( metric , queue , vhost )
249+ name = "{1}{0}{2}{0}{3}" . format ( METRIC_TOKEN_SEPARATOR , metric , queue , vhost . replace ( '/' , '-' ))
237250 print name
238251 d1 = create_desc ({'name' : name .encode ('ascii' ,'ignore' ),
239252 'call_back' : getQueueStat ,
@@ -248,10 +261,10 @@ def buildQueueDescriptors():
248261
249262 def buildNodeDescriptors ():
250263 for metric in NODE_METRICS :
251- for node in list_nodes ():
252- name = '%s.%s#%s' % ( metric , node , '/' )
253- print name
254- d2 = create_desc ({'name' : name .encode ('ascii' ,'ignore' ),
264+ for node in list_nodes ():
265+ name = "{1}{0}{2}{0}-" . format ( METRIC_TOKEN_SEPARATOR , metric , node )
266+ print name
267+ d2 = create_desc ({'name' : name .encode ('ascii' ,'ignore' ),
255268 'call_back' : getNodeStat ,
256269 'value_type' : 'float' ,
257270 'units' : 'N' ,
@@ -260,7 +273,7 @@ def buildNodeDescriptors():
260273 'description' : 'Node_Metric' ,
261274 'groups' : 'rabbitmq,node' })
262275 print d2
263- descriptors .append (d2 )
276+ descriptors .append (d2 )
264277
265278 buildQueueDescriptors ()
266279 buildNodeDescriptors ()
@@ -275,10 +288,12 @@ def metric_cleanup():
275288if __name__ == "__main__" :
276289 url = 'http://%s:%s@localhost:15672/api/$stats' % (username , password )
277290 url_template = Template (url )
291+ print "url_template is " , url_template
292+ ### in config files we use '/' in vhosts names but we should convert '/' to '-' when calculating a metric
278293 parameters = {"vhost" :"/" , "username" :"guest" ,"password" :"guest" , "metric_group" :"rabbitmq" }
279294 metric_init (parameters )
280295 result = refreshStats (stats = ('queues' , 'nodes' ), vhosts = ('/' ))
281- print '***' * 10
282- getQueueStat ('rmq_backing_queue_ack_egress_rate.nfl_client#/ ' )
283- getNodeStat ('rmq_disk_free.rmqone@inrmq01d1#/' )
284- getNodeStat ('rmq_mem_used.rmqone@inrmq01d1#/' )
296+ print '***' * 20
297+ getQueueStat ('rmq_messages_ready___hello___- ' )
298+ # getNodeStat('rmq_disk_free.rmqone@inrmq01d1#/')
299+ # getNodeStat('rmq_mem_used.rmqone@inrmq01d1#/')
0 commit comments