[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

memcached monitoring script



Hi All,

I wrote a quick script to monitor memcached on a hobbitclient.
(it needs perl's Cache::Memcached)


Olivier
#!/usr/bin/perl

# 20090327; obeau
# quick script for hobbitclient to monitor memcached 

require Cache::Memcached;


# if you want to gets graphs, here are the definitions for the hobbitserver
# NCV_memcache="reqs:DERIVE,hitratio:GAUGE,conn:GAUGE,storage:GAUGE,items:GAUGE,*:NONE"
# [memcache]
#        TITLE Memcache Utilization
#        YAXIS 
#        DEF:reqs=memcache.rrd:reqs:AVERAGE
#        DEF:hitratio=memcache.rrd:hitratio:AVERAGE
#        DEF:conn=memcache.rrd:conn:AVERAGE
#        DEF:storage=memcache.rrd:storage:AVERAGE
#        DEF:items=memcache.rrd:items:AVERAGE
#        LINE1:reqs# (at) COLOR@:Requests      
#        GPRINT:reqs:LAST: \: %5.1lf%s (cur)
#        GPRINT:reqs:MAX: \: %5.1lf%s (max)
#        GPRINT:reqs:MIN: \: %5.1lf%s (min)
#        GPRINT:reqs:AVERAGE: \: %5.1lf%s (avg)\n
#        LINE1:conn# (at) COLOR@:% Used Clients
#        GPRINT:conn:LAST: \: %5.1lf%s (cur)
#        GPRINT:conn:MAX: \: %5.1lf%s (max)
#        GPRINT:conn:MIN: \: %5.1lf%s (min)
#        GPRINT:conn:AVERAGE: \: %5.1lf%s (avg)\n
#        LINE1:hitratio# (at) COLOR@:CacheHitRatio 
#        GPRINT:hitratio:LAST: \: %5.1lf%s (cur)
#        GPRINT:hitratio:MAX: \: %5.1lf%s (max)
#        GPRINT:hitratio:MIN: \: %5.1lf%s (min)
#        GPRINT:hitratio:AVERAGE: \: %5.1lf%s (avg)\n
#        LINE1:storage# (at) COLOR@:% Used Storage
#        GPRINT:storage:LAST: \: %5.1lf%s (cur)
#        GPRINT:storage:MAX: \: %5.1lf%s (max)
#        GPRINT:storage:MIN: \: %5.1lf%s (min)
#        GPRINT:storage:AVERAGE: \: %5.1lf%s (avg)\n
#        COMMENT:Items Cached    
#        GPRINT:items:LAST: \: %5.1lf%s (cur)
#        GPRINT:items:MAX: \: %5.1lf%s (max)
#        GPRINT:items:MIN: \: %5.1lf%s (min)
#        GPRINT:items:AVERAGE: \: %5.1lf%s (avg)\n



# variables
my $TEST="memcache";
my $HITSWARNING=50;
my $CONNWARNING=90;
my $HOST="127.0.0.1";
my $PORT=11211;
my $COLOR="green";
my $REPORT="";


# connect to memcache
my $mc = new Cache::Memcached { 'servers' => [ "$HOST:$PORT" ] };
my $stats = $mc->stats ('misc');
	

#check if memcache looks up and running
if ( keys( %$stats ) == 0 )
{
	$REPORT .= "memcache seems down\n";
	$COLOR="red";
}
else {	
		
	# get stuff for graphing purpose..
	$REPORT .= "<!--\n";
	my $memcache_cache_hits = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{get_hits};
	my $memcache_cache_misses = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{get_misses};
	my $memcachereqs = $memcache_cache_hits +  $memcache_cache_misses;
	$REPORT .= "reqs : $memcachereqs\n";
	
	if ($memcachereqs < 1) { $memcachereqs=1; }
	if ($memcache_cache_hits < 1) { $memcache_cache_hits=1; }
	my $memcachehit = $memcache_cache_hits  * 100 / $memcachereqs;
	$REPORT .= "hitratio : $memcachehit\n";
	
	my $memcacheconn = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{curr_connections};
	my $ps = `ps auxw | grep memcached | grep -v grep`;
	my @ps = split (/-c /, $ps);
	my @maxconn = split (/ /, @ps[1]);
	my $maxconn = @maxconn[0];
	$memcacheconn = $memcacheconn * 100 / $maxconn;
	$REPORT .= "conn : $memcacheconn\n";
	
	my $memcache_bytes_used = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{bytes};
	my $memcache_limit_maxbytes = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{limit_maxbytes};
	my $memcachestor = $memcache_bytes_used * 100 / $memcache_limit_maxbytes;
	$REPORT .= "storage : $memcachestor\n";
	
	my $memcacheitem = $stats->{hosts}->{"$HOST:$PORT"}->{misc}->{curr_items};
	$REPORT .= "items : $memcacheitem\n";
	
	$REPORT .= "-->\n";
	
	
	#check cachehitratio and connections
	$REPORT .= "Cache Hit Ratio : $memcachehit % ";
	if ( $memcachehit < $HITSWARNING ) {
		$COLOR="yellow";
		$REPORT .= "<img src=/hobbit/gifs/yellow.gif>\n";
	}
	else { $REPORT .= "<img src=/hobbit/gifs/green.gif>\n"; }
			
	$REPORT .= "Used Connection : $memcacheconn % ";
	if ( $memcacheconn > $CONNWARNING ) {
		$COLOR="yellow";
		$REPORT .= "<img src=/hobbit/gifs/yellow.gif>\n";
	}
	else { $REPORT .= "<img src=/hobbit/gifs/green.gif>\n"; }
}
		

#send status 
$STATUS="status $ENV{HOSTNAME}.$TEST $COLOR ".localtime()." $TEST is $COLOR\n\n$REPORT\n";
system "$ENV{BB} $ENV{BBDISP} \'$STATUS\'";
#print "$ENV{BB} $ENV{BBDISP} \'$STATUS\'";