#!/usr/bin/perl
use Cache::Memcached;
use Time::HiRes qw(gettimeofday tv_interval);

$0 =~ /memcached_responsetime_(\d+_\d+_\d+_\d+)_(\d+)$/;
my ($ip, $port) = ($1, $2);
$ip =~ s/_/./g;
my $address = "$ip:$port";

my $cmd = shift || '';
if ($cmd eq 'config') {
    print "graph_title Memcached Response Time for $address \n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel response time\n";
    print "graph_category memcached\n";
    print "graph_scale yes\n";
    print "time.label time\n";
    print "time.info Response time in seconds\n";
    print "time.min 0\n";
    print "time.type GAUGE\n";
    print "time.draw AREA\n";
    exit 0;
}

#print "I have $address \n";
my $cache = Cache::Memcached->new( { servers => [ $address ], debug => 1 } );

my $start = [gettimeofday];
my $stats = $cache->stats( qw[misc] );
my $elapsed = tv_interval($start, [gettimeofday]);

die "I couldn't connect to memcached and run stats misc" unless $stats;

print "time.value " . $elapsed . "\n";
