Skip to content

Instantly share code, notes, and snippets.

@bionix
Last active August 29, 2015 14:16
Show Gist options
  • Save bionix/3558a923b75391a6f857 to your computer and use it in GitHub Desktop.
Save bionix/3558a923b75391a6f857 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
#
## Copyright (C) 2009 Gleb Voronich <http://stanly.net.ua/>
##
## 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
##
## Installation process:
##
## 1. Download the plugin to your plugins directory (e.g. /usr/share/munin/plugins)
## 2. Create 3 symlinks at the directory that us used by munin for plugins detection (e.g. /etc/munin/plugins): redis_connected_clients, redis_per_sec and and redis_used_memory
## 3. Edit plugin-conf.d/munin-node if it is needed (env.host and env.port variables are accepted; set env.password for password protected Redis server)
## 4. Restart munin-node service
use strict;
use IO::Socket::INET;
use Switch;
use File::Basename;
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef;
my $server = "$HOST:$PORT";
my $sock = IO::Socket::INET->new(
PeerAddr => $server,
Proto => 'tcp'
);
if ( defined( $PASSWORD ) ) {
print $sock "AUTH ", $PASSWORD, "\r\n";
my $result = <$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);
my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );
$0 =~ /redis(?:_([^_]+)|)_(.+)\s*$/;
my $id = $1;
my $graph = $2;
switch ($graph) {
case "clients" {
if ( $config ) {
print "graph_title ";
if ($id) { print "$id - "; }
print "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 "connections" {
if ( $config ) {
print "graph_title ";
if ($id) { print "$id - "; }
print "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 "memory" {
if ( $config ) {
print "graph_title ";
if ($id) { print "$id - "; }
print "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 "used_keys" {
my $dbs;
foreach my $key (keys %{$hash}) {
if ( $key =~ /^db\d+$/ && $hash->{$key} =~ /keys=(\d+),expires=(\d+)/ ) {
$dbs->{$key} = [ $1, $2 ];
}
}
if ( $config ) {
print "graph_title ";
if ($id) { print "$id - "; }
print "Used keys\n";
print "graph_vlabel Used keys\n";
print "graph_category redis\n";
foreach my $db (keys %{$dbs}) {
printf "%s_keys.label %s keys\n", $db, $db;
printf "%s_expires.label %s expires\n", $db, $db;
}
exit 0;
}
foreach my $db (keys %{$dbs}) {
printf "%s_keys.value %d\n", $db, $dbs->{$db}[0];
printf "%s_expires.value %d\n", $db, $dbs->{$db}[1];
}
}
}
# vim: ft=perl ai ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment