Skip to content

Commit 98a6787

Browse files
committed
2 parents ddfe8e7 + 962e6e0 commit 98a6787

8 files changed

Lines changed: 156 additions & 112 deletions

File tree

rabbit/README.mkdn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ To use multiple vhosts, separate them by comma in the vhosts file.
1717

1818
To get metrics besides nodes or queues, either check out how the buildQueueDescriptors and buildNodeDescriptors were set up and make a new descriptor builder/modify stats at the top of the python file and contribute the changes, or ask for my assistance and I'll see what I can do.
1919

20+
## UPDATE
21+
22+
Just added Port parameter to the pyconf. If your broker is pre-3.0, use 55672. If you're on 3.0, use 15672.
23+
2024
## AUTHORS
2125

2226
Gregory Rice <[email protected]>

rabbit/conf.d/rabbitmq.pyconf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ modules {
2121
param password {
2222
value = "guest"
2323
}
24+
param port {
25+
value = "15672"
26+
}
2427

2528
param metric_group {
2629
value = "rmq"

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#/')
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
modules {
22
module {
3-
name = "redis"
3+
name = "redis-gmond"
44
language = "python"
55
param host { value = "127.0.0.1" }
66
param port { value = 6379 }
@@ -13,14 +13,18 @@ collection_group {
1313
metric { name = "connected_slaves" }
1414
metric { name = "blocked_clients" }
1515
metric { name = "used_memory" }
16-
metric { name = "changes_since_last_save" }
17-
metric { name = "bgsave_in_progress" }
18-
metric { name = "bgrewriteaof_in_progress" }
16+
metric { name = "rdb_changes_since_last_save" }
17+
metric { name = "rdb_bgsave_in_progress" }
18+
metric { name = "master_sync_in_progress" }
19+
metric { name = "master_link_status" }
1920
metric { name = "total_connections_received" }
21+
metric { name = "instantaneous_ops_per_sec" }
2022
metric { name = "total_commands_processed" }
2123
metric { name = "expired_keys" }
2224
metric { name = "pubsub_channels" }
2325
metric { name = "pubsub_patterns" }
24-
metric { name = "vm_enabled" }
2526
metric { name = "master_last_io_seconds_ago" }
27+
metric { name = "db0" }
2628
}
29+
30+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import socket
2+
import time
3+
#import logging
4+
5+
#logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log', filemode='w')
6+
#logging.debug('starting up')
7+
8+
def metric_handler(name):
9+
10+
# Update from Redis. Don't thrash.
11+
if 15 < time.time() - metric_handler.timestamp:
12+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13+
s.connect((metric_handler.host, metric_handler.port))
14+
s.send("INFO\r\n")
15+
#logging.debug("sent INFO")
16+
info = s.recv(4096)
17+
#logging.debug("rcvd INFO")
18+
if "$" != info[0]:
19+
return 0
20+
len = int(info[1:info.find("\n")])
21+
if 4096 < len:
22+
info += s.recv(len - 4096)
23+
metric_handler.info = {}
24+
try:
25+
for line in info.splitlines()[1:]:
26+
#logging.debug("line is %s done" % line)
27+
if "" == line:
28+
continue
29+
if "#" == line[0]:
30+
continue
31+
n, v = line.split(":")
32+
if n in metric_handler.descriptors:
33+
if n == "master_sync_status":
34+
v = 1 if v == 'up' else 0
35+
if n == "db0":
36+
v = v.split('=')[1].split(',')[0]
37+
if n == "used_memory":
38+
v = int(int(v) / 1000)
39+
if n == "total_connections_received":
40+
# first run, zero out and record total connections
41+
if metric_handler.prev_total_connections == 0:
42+
metric_handler.prev_total_connections = int(v)
43+
v = 0
44+
else:
45+
# calculate connections per second
46+
cps = (int(v) - metric_handler.prev_total_connections) / (time.time() - metric_handler.timestamp)
47+
metric_handler.prev_total_connections = int(v)
48+
v = cps
49+
if n == "total_commands_processed":
50+
# first run, zero out and record total commands
51+
if metric_handler.prev_total_commands == 0:
52+
metric_handler.prev_total_commands = int(v)
53+
v = 0
54+
else:
55+
# calculate commands per second
56+
cps = (int(v) - metric_handler.prev_total_commands) / (time.time() - metric_handler.timestamp)
57+
metric_handler.prev_total_commands = int(v)
58+
v = cps
59+
#logging.debug("submittincg metric %s is %s" % (n, int(v)))
60+
metric_handler.info[n] = int(v) # TODO Use value_type.
61+
except Exception, e:
62+
#logging.debug("caught exception %s" % e)
63+
pass
64+
s.close()
65+
metric_handler.timestamp = time.time()
66+
67+
#logging.debug("returning metric_handl: %s %s %s" % (metric_handler.info.get(name, 0), metric_handler.info, metric_handler))
68+
return metric_handler.info.get(name, 0)
69+
70+
def metric_init(params={}):
71+
metric_handler.host = params.get("host", "127.0.0.1")
72+
metric_handler.port = int(params.get("port", 6379))
73+
metric_handler.timestamp = 0
74+
metric_handler.prev_total_commands = 0
75+
metric_handler.prev_total_connections = 0
76+
metrics = {
77+
"connected_clients": {"units": "clients"},
78+
"connected_slaves": {"units": "slaves"},
79+
"blocked_clients": {"units": "clients"},
80+
"used_memory": {"units": "KB"},
81+
"rdb_changes_since_last_save": {"units": "changes"},
82+
"rdb_bgsave_in_progress": {"units": "yes/no"},
83+
"master_sync_in_progress": {"units": "yes/no"},
84+
"master_link_status": {"units": "yes/no"},
85+
#"aof_bgrewriteaof_in_progress": {"units": "yes/no"},
86+
"total_connections_received": { "units": "connections/sec" },
87+
"instantaneous_ops_per_sec": {"units": "ops"},
88+
"total_commands_processed": { "units": "commands/sec" },
89+
"expired_keys": {"units": "keys"},
90+
"pubsub_channels": {"units": "channels"},
91+
"pubsub_patterns": {"units": "patterns"},
92+
#"vm_enabled": {"units": "yes/no"},
93+
"master_last_io_seconds_ago": {"units": "seconds ago"},
94+
"db0": {"units": "keys"},
95+
}
96+
metric_handler.descriptors = {}
97+
for name, updates in metrics.iteritems():
98+
descriptor = {
99+
"name": name,
100+
"call_back": metric_handler,
101+
"time_max": 90,
102+
"value_type": "int",
103+
"units": "",
104+
"slope": "both",
105+
"format": "%d",
106+
"description": "http://code.google.com/p/redis/wiki/InfoCommand",
107+
"groups": "redis",
108+
}
109+
descriptor.update(updates)
110+
metric_handler.descriptors[name] = descriptor
111+
return metric_handler.descriptors.values()
112+
113+
def metric_cleanup():
114+
pass

redis/python_modules/redis.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

system/mem_stats/python_modules/mem_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def metric_init(params):
110110
}))
111111

112112
descriptors.append(create_desc(Desc_Skel, {
113-
"name" : "mem_total",
113+
"name" : "mem_total_anon",
114114
"orig_name" : "Active(anon)",
115115
"units" : "Bytes",
116116
"description": "Active(anon)",
@@ -365,4 +365,4 @@ def metric_cleanup():
365365
metric_init({})
366366
for d in descriptors:
367367
v = d['call_back'](d['name'])
368-
print 'value for %s is %f' % (d['name'], v)
368+
print 'value for %s is %f' % (d['name'], v)

0 commit comments

Comments
 (0)