Using MRTG to monitor Memcached

Russell Garrett rg@tcslon.com
Wed, 29 Oct 2003 12:23:00 -0000


I hacked together a perl script quickly to grab hit and miss info from
memcached and output it in an MRTG-friendly way. Since we only use 2 servers
at the moment (and they're not doing exactly the same thing) it grabs it per
server. It's left as a very easy exercise to the reader to aggregate all the
servers' stats. So here's the script (yes, it's hacky, but it works):

--
#!/usr/bin/perl
use IO::Socket;
my $socket = IO::Socket::INET->new( PeerAddr => $ARGV[0],
                                    PeerPort => $ARGV[1],
                                    Proto    => "tcp",
                                    Type     => SOCK_STREAM)
					or die($@);
print $socket "stats\n";
$out = <$socket>;
while ($out ne "END\r\n") {
    if ($out =~ m/STAT get_hits ([0-9]+)/) {
        print "$1\n";
    }
    if ($out =~ m/STAT get_misses ([0-9]+)/) {
        print "$1\n";
    }
    $out = <$socket>;
}
my ($uptime, undef) = split /,/, `uptime`;
$host = $ARGV[0];
print("$uptime\n$host\n");
close($socket);
--

And you can get MRTG to graph it nicely by doing something like this:

Target[st0_memcache]: `/home/mrtg/memcached.pl st0 11211`
YLegend[st0_memcache]: Requests/Second
ShortLegend[st0_memcache]: r/s
XSize[st0_memcache]: 400
YSize[st0_memcache]: 175
Options[st0_memcache]: growright, nopercent
MaxBytes[st0_memcache]: 12500000
Title[st0_memcache]: Memcached Stats: st0
PageTop[st0_memcache]: <h1>Memcached Stats: st0</h1>
Legend1[st0_memcache]: Cache Hits
Legend2[st0_memcache]: Cache Misses
Legend3[st0_memcache]: Maximal 5 Minute Hits
Legend4[st0_memcache]: Maximal 5 Minute Misses
LegendI[st0_memcache]: &nbsp;Hits:
LegendO[st0_memcache]: &nbsp;Misses:

--------------------------------------------------------------------
Russ Garrett                                            russ@last.fm
                                                      http://last.fm