|
41 | 41 | keyToPath['rmq_consumers'] = "%s{0}consumers".format(JSON_PATH_SEPARATOR) |
42 | 42 | keyToPath['rmq_messages'] = "%s{0}messages".format(JSON_PATH_SEPARATOR) |
43 | 43 |
|
| 44 | +RATE_METRICS = [ |
| 45 | + 'rmq_backing_queue_ack_egress_rate', |
| 46 | + 'rmq_backing_queue_ack_ingress_rate', |
| 47 | + 'rmq_backing_queue_egress_rate', |
| 48 | + 'rmq_backing_queue_ingress_rate' |
| 49 | +] |
| 50 | + |
44 | 51 | QUEUE_METRICS = ['rmq_messages_ready', |
45 | 52 | 'rmq_messages_unacknowledged', |
46 | 53 | 'rmq_backing_queue_ack_egress_rate', |
@@ -77,8 +84,6 @@ def metric_cleanup(): |
77 | 84 | def dig_it_up(obj,path): |
78 | 85 | try: |
79 | 86 | path = path.split(JSON_PATH_SEPARATOR) |
80 | | - print "obj is", obj |
81 | | - print "path is", path |
82 | 87 | return reduce(lambda x,y:x[y],path,obj) |
83 | 88 | except: |
84 | 89 | print "Exception" |
@@ -141,18 +146,14 @@ def getQueueStat(name): |
141 | 146 | print name |
142 | 147 | stat_name, queue_name, vhost = name.split(METRIC_TOKEN_SEPARATOR) |
143 | 148 |
|
144 | | - print "vhost is ", vhost |
145 | | - print "stat_name is ", stat_name |
146 | | - print "queue_name is ", queue_name |
147 | 149 | vhost = vhost.replace('-', '/') #decoding vhost from metric name |
148 | 150 | # Run refreshStats to get the result object |
149 | 151 | result = compiled_results[('queues', vhost)] |
150 | 152 |
|
151 | | - print "keyToPath[stat_name]", keyToPath[stat_name] |
152 | | - tmp_res = keyToPath[stat_name] % queue_name |
153 | | - print "tmp_res is ", tmp_res |
154 | 153 | value = dig_it_up(result, keyToPath[stat_name] % queue_name) |
155 | | - print name, value |
| 154 | + |
| 155 | + if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[queue_name].keys(): |
| 156 | + value = 0 |
156 | 157 |
|
157 | 158 | #Convert Booleans |
158 | 159 | if value is True: |
@@ -189,20 +190,31 @@ def product(*args, **kwds): |
189 | 190 | result = [x+[y] for x in result for y in pool] |
190 | 191 | for prod in result: |
191 | 192 | yield tuple(prod) |
| 193 | + |
| 194 | +def str2bool(string): |
| 195 | + if string.lower() in ("yes", "true"): |
| 196 | + return True |
| 197 | + if string.lower() in ("no", "false"): |
| 198 | + return False |
| 199 | + raise Exception("Invalid value of the 'zero_rates_when_idle' param, use one of the ('true', 'yes', 'false', 'no')") |
192 | 200 |
|
193 | 201 | def metric_init(params): |
194 | 202 | ''' Create the metric definition object ''' |
195 | | - global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results, STATS, vhosts |
| 203 | + global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results, STATS, vhosts, zero_rates_when_idle |
196 | 204 | print 'received the following params:' |
197 | 205 | #Set this globally so we can refresh stats |
198 | 206 | if 'host' not in params: |
199 | 207 | params['host'], params['vhost'],params['username'],params['password'],params['port'] = "localhost", "/", "guest", "guest", "15672" |
| 208 | + if 'zero_rates_when_idle' not in params: |
| 209 | + params['zero_rates_when_idle'] = "false" |
200 | 210 |
|
201 | 211 | # Set the vhosts as a list split from params |
202 | 212 | vhosts = params['vhost'].split(',') |
203 | 213 | username, password = params['username'], params['password'] |
204 | 214 | host = params['host'] |
205 | 215 | port = params['port'] |
| 216 | + |
| 217 | + zero_rates_when_idle = str2bool(params['zero_rates_when_idle']) |
206 | 218 |
|
207 | 219 | url = 'http://%s:%s/api/$stats/$vhost' % (host,port) |
208 | 220 | base_url = 'http://%s:%s/api' % (host,port) |
@@ -290,10 +302,10 @@ def metric_cleanup(): |
290 | 302 | url_template = Template(url) |
291 | 303 | print "url_template is ", url_template |
292 | 304 | ### in config files we use '/' in vhosts names but we should convert '/' to '-' when calculating a metric |
293 | | - parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq"} |
| 305 | + parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq", "zero_rates_when_idle": "yes"} |
294 | 306 | metric_init(parameters) |
295 | 307 | result = refreshStats(stats = ('queues', 'nodes'), vhosts = ('/')) |
296 | 308 | print '***'*20 |
297 | | - getQueueStat('rmq_messages_ready___hello___-') |
298 | | -# getNodeStat('rmq_disk_free.rmqone@inrmq01d1#/') |
299 | | -# getNodeStat('rmq_mem_used.rmqone@inrmq01d1#/') |
| 309 | + getQueueStat('rmq_backing_queue_ack_egress_rate___nfl_client___-') |
| 310 | + getNodeStat('rmq_disk_free___rmqone@inrmq01d1___-') |
| 311 | + getNodeStat('rmq_mem_used___rmqone@inrmq01d1___-') |
0 commit comments