Skip to content

Commit 15f41c4

Browse files
author
Greg Rice
committed
made port addition, started using urllib2
1 parent 442444b commit 15f41c4

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

rabbit/python_modules/rabbitmq.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import sys
33
import os
44
import simplejson as json
5-
import urllib
5+
import urllib2
66
import time
77
from string import Template
88
import itertools
99
import 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
1212
INTERVAL = 10
1313
descriptors = list()
1414
username, password = "guest", "guest"
@@ -99,7 +99,7 @@ def refreshStats(stats = ('nodes', 'queues'), vhosts = ['/']):
9999
result_dict = {}
100100
urlstring = url_template.safe_substitute(stats = stat, vhost = vhost)
101101
print urlstring
102-
result = json.load(urllib.urlopen(urlstring, None, 2))
102+
result = json.load(urllib2.urlopen(urlstring))
103103
# Rearrange results so entry is held in a dict keyed by name - queue name, host name, etc.
104104
if stat in ("queues", "nodes", "exchanges"):
105105
for entry in result:
@@ -183,19 +183,34 @@ def metric_init(params):
183183
print 'received the following params:'
184184
#Set this globally so we can refresh stats
185185
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"
187187

188188
# Set the vhosts as a list split from params
189189
vhosts = params['vhost'].split(',')
190190
username, password = params['username'], params['password']
191191
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)
194202
url_template = Template(url)
195203
print params
196204

197205
refreshStats(stats = STATS, vhosts = vhosts)
198206

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+
199214
def create_desc(prop):
200215
d = {
201216
'name' : 'XXX',
@@ -258,12 +273,12 @@ def metric_cleanup():
258273

259274

260275
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)
262277
url_template = Template(url)
263278
parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq"}
264279
metric_init(parameters)
265280
result = refreshStats(stats = ('queues', 'nodes'), vhosts = ('/'))
266281
print '***'*10
267282
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

Comments
 (0)