Skip to content

Instantly share code, notes, and snippets.

@ckdake
Created October 15, 2010 21:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckdake/629014 to your computer and use it in GitHub Desktop.
Save ckdake/629014 to your computer and use it in GitHub Desktop.
munin plugin for monitoring redis
#!/usr/bin/perl -w
#
## Copyright (C) 2009 Gleb Voronich <http://stanly.net.ua/>, 2010 Chris Kelly <http://ckdake.com/>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; version 2 dated June,
## 1991.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
##
## $Log$
##
## Based on Redis module code v0.08 2009/from http://svn.rot13.org/index.cgi/Redis
## Based on redis_ from http://stanly.net.ua/en/monitoring-dlya-redis/
use strict;
use IO::Socket::INET;
use Switch;
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
my $PASS = exists $ENV{'pass'} ? $ENV{'pass'} : "";
my $server = "$HOST:$PORT";
my $sock = IO::Socket::INET->new(
PeerAddr => $server,
Proto => 'tcp'
);
if ( $PASS ) {
print $sock "AUTH $PASS \r\n";
my $res = <$sock> || die "can't read socket: $!"
}
print $sock "INFO\r\n";
my $result = <$sock> || die "can't read socket: $!";
my $rep;
read($sock, $rep, substr($result,1)) || die "can't read from socket: $!";
my $hash;
foreach (split(/\r\n/, $rep)) {
my ($key,$val) = split(/:/, $_, 2);
$hash->{$key} = $val;
}
close ($sock);
$0 =~ s/(.+)redis_//g;
switch ($0) {
case "connected_clients" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Connected clients\n";
print "graph_vlabel Connected clients\n";
print "connected_clients.label connected clients\n";
print "graph_category redis\n";
exit 0;
}
print "connected_clients.value " . $hash->{'connected_clients'} . "\n";
}
case "per_sec" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Per second\n";
print "graph_vlabel per \${graph_period}\n";
print "graph_category redis\n";
print "requests.label requests\n";
print "requests.type COUNTER\n";
print "connections.label connections\n";
print "connections.type COUNTER\n";
exit 0;
}
print "requests.value ". $hash->{'total_commands_processed'} ."\n";
print "connections.value ". $hash->{'total_connections_received'} ."\n";
}
case "used_memory" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Used memory\n";
print "graph_vlabel Used memory\n";
print "used_memory.label used memory\n";
print "graph_category redis\n";
exit 0;
}
print "used_memory.value ". $hash->{'used_memory'} ."\n";
}
case "unsaved_changes" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Changes since last save\n";
print "graph_vlabel Unsaved changes\n";
print "unsaved_changes.label unsaved changes\n";
print "graph_category redis\n";
exit 0;
}
print "unsaved_changes.value ". $hash->{'changes_since_last_save'} ."\n";
}
case "keys" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Keys\n";
print "graph_vlabel Keys\n";
print "keys.label Active Keys\n";
print "expired_keys.label Expired Keys\n";
print "graph_category redis\n";
exit 0;
}
$_ = $hash->{'db0'};
if (/keys=(\d+),expires=(\d)/) {
print "keys.value ". $1 ."\n";
print "expired_keys.value ". $2 ."\n";
}
}
case "server_role" {
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title Server Role\n";
print "graph_vlabel is role\n";
print "master.label master\n";
print "slave.label slave\n";
print "graph_category redis\n";
exit 0;
}
if ( $hash->{'role'} eq 'master' ) {
print "master.value 1\n";
print "slave.value 0\n";
} elsif ( $hash->{'role'} eq 'slave' ) {
print "master.value 0\n";
print "slave.value 1\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment