| 1 |
# ## DESCRIPTION |
| 2 |
# Redis plugin for Ganglia that exposes most of the counters in the |
| 3 |
# Redis `INFO` command to Ganglia for all your graphing needs. The |
| 4 |
# metrics it comes with are pretty rudimentary but they get the job |
| 5 |
# done. |
| 6 |
# |
| 7 |
# ## FILES |
| 8 |
# ## THEME SONG |
| 9 |
# |
| 10 |
# The Arcade Fire - "Wake Up" |
| 11 |
# |
| 12 |
# ## SEE ALSO |
| 13 |
# The Redis `INFO` command is described at <http://code.google.com/p/redis/wiki/InfoCommand>. |
| 14 |
# |
| 15 |
# The original blog post on this plugin is at |
| 16 |
# <http://rcrowley.org/2010/06/24/redis-in-ganglia.html>. Gil |
| 17 |
# Raphaelli's MySQL plugin, on which this one is based can be found at |
| 18 |
# <http://g.raphaelli.com/2009/1/5/ganglia-mysql-metrics>. |
| 19 |
|
| 20 |
import socket |
| 21 |
import time |
| 22 |
#import logging |
| 23 |
|
| 24 |
#logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log', filemode='w') |
| 25 |
#logging.debug('starting up') |
| 26 |
|
| 27 |
|
| 28 |
def metric_handler(name): |
| 29 |
|
| 30 |
# Update from Redis. Don't thrash. |
| 31 |
if 15 < time.time() - metric_handler.timestamp: |
| 32 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 33 |
s.connect((metric_handler.host, metric_handler.port)) |
| 34 |
if metric_handler.auth is not None: |
| 35 |
s.send("*2\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n" % (len(metric_handler.auth), metric_handler.auth)) |
| 36 |
result = s.recv(100) |
| 37 |
if not 'OK' in result: |
| 38 |
return 0 |
| 39 |
s.send("*1\r\n$4\r\nINFO\r\n") |
| 40 |
#logging.debug("sent INFO") |
| 41 |
info = s.recv(4096) |
| 42 |
#logging.debug("rcvd INFO") |
| 43 |
if "$" != info[0]: |
| 44 |
return 0 |
| 45 |
msglen = int(info[1:info.find("\n")]) |
| 46 |
if 4096 < msglen: |
| 47 |
info += s.recv(msglen - 4096) |
| 48 |
metric_handler.info = {} |
| 49 |
try: |
| 50 |
for line in info.splitlines()[1:]: |
| 51 |
#logging.debug("line is %s done" % line) |
| 52 |
if "" == line: |
| 53 |
continue |
| 54 |
if "#" == line[0]: |
| 55 |
continue |
| 56 |
n, v = line.split(":") |
| 57 |
if n in metric_handler.descriptors: |
| 58 |
if n == "master_sync_status": |
| 59 |
v = 1 if v == 'up' else 0 |
| 60 |
if n == "db0": |
| 61 |
v = v.split('=')[1].split(',')[0] |
| 62 |
if n == "used_memory": |
| 63 |
v = int(int(v) / 1000) |
| 64 |
if n == "total_connections_received": |
| 65 |
# first run, zero out and record total connections |
| 66 |
if metric_handler.prev_total_connections == 0: |
| 67 |
metric_handler.prev_total_connections = int(v) |
| 68 |
v = 0 |
| 69 |
else: |
| 70 |
# calculate connections per second |
| 71 |
cps = (int(v) - metric_handler.prev_total_connections) / (time.time() - metric_handler.timestamp) |
| 72 |
metric_handler.prev_total_connections = int(v) |
| 73 |
v = cps |
| 74 |
if n == "total_commands_processed": |
| 75 |
# first run, zero out and record total commands |
| 76 |
if metric_handler.prev_total_commands == 0: |
| 77 |
metric_handler.prev_total_commands = int(v) |
| 78 |
v = 0 |
| 79 |
else: |
| 80 |
# calculate commands per second |
| 81 |
cps = (int(v) - metric_handler.prev_total_commands) / (time.time() - metric_handler.timestamp) |
| 82 |
metric_handler.prev_total_commands = int(v) |
| 83 |
v = cps |
| 84 |
#logging.debug("submittincg metric %s is %s" % (n, int(v))) |
| 85 |
metric_handler.info[n] = int(v) # TODO Use value_type. |
| 86 |
except Exception, e: |
| 87 |
#logging.debug("caught exception %s" % e) |
| 88 |
pass |
| 89 |
s.close() |
| 90 |
metric_handler.timestamp = time.time() |
| 91 |
|
| 92 |
#logging.debug("returning metric_handl: %s %s %s" % (metric_handler.info.get(name, 0), metric_handler.info, metric_handler)) |
| 93 |
return metric_handler.info.get(name, 0) |
| 94 |
|
| 95 |
|
| 96 |
def metric_init(params={}): |
| 97 |
metric_handler.host = params.get("host", "127.0.0.1") |
| 98 |
metric_handler.port = int(params.get("port", 6379)) |
| 99 |
metric_handler.auth = params.get("auth", None) |
| 100 |
metric_handler.timestamp = 0 |
| 101 |
metric_handler.prev_total_commands = 0 |
| 102 |
metric_handler.prev_total_connections = 0 |
| 103 |
metrics = { |
| 104 |
"connected_clients": {"units": "clients"}, |
| 105 |
"connected_slaves": {"units": "slaves"}, |
| 106 |
"blocked_clients": {"units": "clients"}, |
| 107 |
"used_memory": {"units": "KB"}, |
| 108 |
"rdb_changes_since_last_save": {"units": "changes"}, |
| 109 |
"rdb_bgsave_in_progress": {"units": "yes/no"}, |
| 110 |
"master_sync_in_progress": {"units": "yes/no"}, |
| 111 |
"master_link_status": {"units": "yes/no"}, |
| 112 |
#"aof_bgrewriteaof_in_progress": {"units": "yes/no"}, |
| 113 |
"total_connections_received": {"units": "connections/sec"}, |
| 114 |
"instantaneous_ops_per_sec": {"units": "ops"}, |
| 115 |
"total_commands_processed": {"units": "commands/sec"}, |
| 116 |
"expired_keys": {"units": "keys"}, |
| 117 |
"pubsub_channels": {"units": "channels"}, |
| 118 |
"pubsub_patterns": {"units": "patterns"}, |
| 119 |
#"vm_enabled": {"units": "yes/no"}, |
| 120 |
"master_last_io_seconds_ago": {"units": "seconds ago"}, |
| 121 |
"db0": {"units": "keys"}, |
| 122 |
} |
| 123 |
metric_handler.descriptors = {} |
| 124 |
for name, updates in metrics.iteritems(): |
| 125 |
descriptor = { |
| 126 |
"name": name, |
| 127 |
"call_back": metric_handler, |
| 128 |
"time_max": 90, |
| 129 |
"value_type": "int", |
| 130 |
"units": "", |
| 131 |
"slope": "both", |
| 132 |
"format": "%d", |
| 133 |
"description": "http://code.google.com/p/redis/wiki/InfoCommand", |
| 134 |
"groups": "redis", |
| 135 |
} |
| 136 |
descriptor.update(updates) |
| 137 |
metric_handler.descriptors[name] = descriptor |
| 138 |
return metric_handler.descriptors.values() |
| 139 |
|
| 140 |
|
| 141 |
def metric_cleanup(): |
| 142 |
pass |