Created
April 15, 2011 14:00
-
-
Save kapilreddy/921745 to your computer and use it in GitHub Desktop.
A Munin plugin for Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
## Copyright (c) 2011, Kapil Reddy | |
## All rights reserved. | |
## Redistribution and use in source and binary forms, with or without | |
## modification, are permitted provided that the following conditions are met: | |
## * Redistributions of source code must retain the above copyright | |
## notice, this list of conditions and the following disclaimer. | |
## * Redistributions in binary form must reproduce the above copyright | |
## notice, this list of conditions and the following disclaimer in the | |
## documentation and/or other materials provided with the distribution. | |
## * Neither the name of the <organization> nor the | |
## names of its contributors may be used to endorse or promote products | |
## derived from this software without specific prior written permission. | |
## THIS SOFTWARE IS PROVIDED BY Kapil Reddy ''AS IS'' AND ANY | |
## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
## DISCLAIMED. IN NO EVENT SHALL Kapil Reddy BE LIABLE FOR ANY | |
## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
## | |
## | |
## Based on https://github.com/andymccurdy/redis-py | |
## Thanks greenmang0 and ghoseb | |
import socket | |
import sys | |
def redis_info(host, port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((host, port)) | |
sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) | |
sock.sendall("INFO\r\n") | |
resp = sock.recv(65565) | |
sock.close() | |
return parse_info(resp) | |
def parse_info(response): | |
"Parse the result of Redis's INFO command into a Python dict" | |
info = {} | |
def get_value(value): | |
if ',' not in value: | |
return value | |
sub_dict = {} | |
for item in value.split(','): | |
k, v = item.rsplit('=', 1) | |
try: | |
sub_dict[k] = int(v) | |
except ValueError: | |
sub_dict[k] = v | |
return sub_dict | |
for line in response.splitlines(): | |
if line and not line.startswith('$'): | |
key, value = line.split(':') | |
try: | |
if '.' in value: | |
info[key] = float(value) | |
else: | |
info[key] = int(value) | |
except ValueError: | |
info[key] = get_value(value) | |
return info | |
if __name__ == "__main__": | |
info = redis_info("127.0.0.1", 6379) | |
if len(sys.argv) == 2 and sys.argv[1] == "config": | |
print "graph_title redis_graph" | |
print "graph_vlabel redis_label" | |
print "hits.label Hits" | |
print "misses.label Misses" | |
print "graph_category redis" | |
else: | |
print "hits.value ", info['keyspace_hits'] | |
print "misses.value ", info['keyspace_misses'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment