|
2 | 2 | import sys |
3 | 3 | import os |
4 | 4 | import simplejson as json |
5 | | -import urllib |
| 5 | +import urllib2 |
6 | 6 | import time |
7 | 7 | from string import Template |
8 | 8 | import itertools |
9 | 9 | import threading |
10 | 10 |
|
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 | 12 | INTERVAL = 10 |
13 | 13 | descriptors = list() |
14 | 14 | username, password = "guest", "guest" |
@@ -99,7 +99,7 @@ def refreshStats(stats = ('nodes', 'queues'), vhosts = ['/']): |
99 | 99 | result_dict = {} |
100 | 100 | urlstring = url_template.safe_substitute(stats = stat, vhost = vhost) |
101 | 101 | print urlstring |
102 | | - result = json.load(urllib.urlopen(urlstring, None, 2)) |
| 102 | + result = json.load(urllib2.urlopen(urlstring)) |
103 | 103 | # Rearrange results so entry is held in a dict keyed by name - queue name, host name, etc. |
104 | 104 | if stat in ("queues", "nodes", "exchanges"): |
105 | 105 | for entry in result: |
@@ -183,19 +183,34 @@ def metric_init(params): |
183 | 183 | print 'received the following params:' |
184 | 184 | #Set this globally so we can refresh stats |
185 | 185 | if 'host' not in params: |
186 | | - params['host'], params['vhost'],params['username'],params['password'] = "localhost", "/", "guest", "guest" |
| 186 | + params['host'], params['vhost'],params['username'],params['password'],params['port'] = "localhost", "/", "guest", "guest", "15672" |
187 | 187 |
|
188 | 188 | # Set the vhosts as a list split from params |
189 | 189 | vhosts = params['vhost'].split(',') |
190 | 190 | username, password = params['username'], params['password'] |
191 | 191 | host = params['host'] |
192 | | - |
193 | | - url = 'http://%s:%s@%s:55672/api/$stats/$vhost' % (username, password, host) |
| 192 | + port = params['port'] |
| 193 | + |
| 194 | + url = 'http://%s:%s/api/$stats/$vhost' % (host,port) |
| 195 | + base_url = 'http://%s:%s/api' % (host,port) |
| 196 | + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 197 | + password_mgr.add_password(None, base_url, username, password) |
| 198 | + handler = urllib2.HTTPBasicAuthHandler(password_mgr) |
| 199 | + opener = urllib2.build_opener(handler) |
| 200 | + opener.open(base_url) |
| 201 | + urllib2.install_opener(opener) |
194 | 202 | url_template = Template(url) |
195 | 203 | print params |
196 | 204 |
|
197 | 205 | refreshStats(stats = STATS, vhosts = vhosts) |
198 | 206 |
|
| 207 | + def metric_handler(name): |
| 208 | + if 15 < time.time() - metric_handler.timestamp: |
| 209 | + metric_handler.timestamp = time.time() |
| 210 | + return refreshStats(stats = STATS, vhosts = vhosts) |
| 211 | + |
| 212 | + |
| 213 | + |
199 | 214 | def create_desc(prop): |
200 | 215 | d = { |
201 | 216 | 'name' : 'XXX', |
@@ -258,12 +273,12 @@ def metric_cleanup(): |
258 | 273 |
|
259 | 274 |
|
260 | 275 | if __name__ == "__main__": |
261 | | - url = 'http://%s:%s@localhost:55672/api/$stats' % (username, password) |
| 276 | + url = 'http://%s:%s@localhost:15672/api/$stats' % (username, password) |
262 | 277 | url_template = Template(url) |
263 | 278 | parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq"} |
264 | 279 | metric_init(parameters) |
265 | 280 | result = refreshStats(stats = ('queues', 'nodes'), vhosts = ('/')) |
266 | 281 | print '***'*10 |
267 | 282 | getQueueStat('rmq_backing_queue_ack_egress_rate.nfl_client#/') |
268 | | - getNodeStat('rmq_disk_free.rmqtwo@inrmq02d1#/') |
269 | | - getNodeStat('rmq_mem_used.rmqtwo@inrmq02d1#/') |
| 283 | + getNodeStat('rmq_disk_free.rmqone@inrmq01d1#/') |
| 284 | + getNodeStat('rmq_mem_used.rmqone@inrmq01d1#/') |
0 commit comments