Skip to content

Instantly share code, notes, and snippets.

@rcrowley
Created June 22, 2010 04:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcrowley/448007 to your computer and use it in GitHub Desktop.
Save rcrowley/448007 to your computer and use it in GitHub Desktop.
Redis in Ganglia
modules {
module {
name = "python_module"
path = "/usr/lib/ganglia/modpython.so"
params = "/usr/lib/ganglia/python_modules"
}
}
include ('/etc/ganglia/conf.d/*.pyconf')
import socket
import time
def metric_handler(name):
# Update from Redis. Don't thrash.
if 15 < time.time() - metric_handler.timestamp:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((metric_handler.host, metric_handler.port))
s.send("INFO\n")
info = s.recv(4096)
if "$" != info[0]:
return 0
len = int(info[1:info.find("\n")])
if 4096 < len:
info += s.recv(len - 4096)
metric_handler.info = {}
for line in info.splitlines()[1:]:
if "" == line:
continue
n, v = line.split(":")
if n in metric_handler.descriptors:
metric_handler.info[n] = int(v) # TODO Use value_type.
s.close()
metric_handler.timestamp = time.time()
return metric_handler.info.get(name, 0)
def metric_init(params={}):
metric_handler.host = params.get("host", "127.0.0.1")
metric_handler.port = int(params.get("port", 6379))
metric_handler.timestamp = 0
metrics = {
"connected_clients": {"units": "clients"},
"connected_slaves": {"units": "slaves"},
"blocked_clients": {"units": "clients"},
"used_memory": {"units": "bytes"},
"changes_since_last_save": {"units": "changes"},
"bgsave_in_progress": {"units": "yes/no"},
"bgrewriteaof_in_progress": {"units": "yes/no"},
"total_connections_received": {
"units": "connections",
"slope": "positive",
},
"total_commands_processed": {
"units": "commands",
"slope": "positive",
},
"expired_keys": {"units": "keys"},
"pubsub_channels": {"units": "channels"},
"pubsub_patterns": {"units": "patterns"},
"vm_enabled": {"units": "yes/no"},
"master_last_io_seconds_ago": {"units": "seconds ago"},
}
metric_handler.descriptors = {}
for name, updates in metrics.iteritems():
descriptor = {
"name": name,
"call_back": metric_handler,
"time_max": 90,
"value_type": "int",
"units": "",
"slope": "both",
"format": "%d",
"description": "http://code.google.com/p/redis/wiki/InfoCommand",
"groups": "redis",
}
descriptor.update(updates)
metric_handler.descriptors[name] = descriptor
return metric_handler.descriptors.values()
def metric_cleanup():
pass
modules {
module {
name = "redis"
language = "python"
param host { value = "127.0.0.1" }
param port { value = 6379 }
}
}
collection_group {
collect_every = 10
time_threshold = 60
metric { name = "connected_clients" }
metric { name = "connected_slaves" }
metric { name = "blocked_clients" }
metric { name = "used_memory" }
metric { name = "changes_since_last_save" }
metric { name = "bgsave_in_progress" }
metric { name = "bgrewriteaof_in_progress" }
metric { name = "total_connections_received" }
metric { name = "total_commands_processed" }
metric { name = "expired_keys" }
metric { name = "pubsub_channels" }
metric { name = "pubsub_patterns" }
metric { name = "vm_enabled" }
metric { name = "master_last_io_seconds_ago" }
}
@jeffomatic
Copy link

People arriving from Google will want to take note that the above script may not work correctly with modern versions of Redis; in particular, Redis 2.6 emits comment lines (prefixed with #) with the INFO command. At a minimum, you'll want to modify lines redis.py 14-21 to something like the following:

        info_length = int(info[1:info.find("\n")])
        if 4096 < info_length:
            info += s.recv(info_length - 4096)
        metric_handler.info = {}
        for line in info.splitlines()[1:]:
            if "" == line:
                continue
            tokens = line.split(":")
            if len(tokens) < 2:
                continue
            n, v = tokens

@oraeven
Copy link

oraeven commented Mar 20, 2018

hello,I want to ask a question, at present this script only support ganglia 3.1 X version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment