Tuesday, June 1, 2010

How to send Memcached stats to a monitoring tool using bash?

Zabbix is my network monitoring tool of choice. You can send Memcached stats to it using the simple code bash script below:


#!/bin/bash
ZABBIX_SENDER="/usr/local/sbin/zabbix_sender"
ZABBIX_TRAPPER="localhost"
ZABBIX_TRAPPER_PORT=10051
ZABBIX_ITEM_KEY_PREFIX="memcached_"
ZABBIX_HOST="Memcached"
MEMCACHED_SERVER="localhost"
MEMCACHED_SERVER_PORT=11211
STATS=`(sleep 1 ; echo "stats"; sleep 1; echo "quit") | telnet $MEMCACHED_SERVER $MEMCACHED_SERVER_PORT`
STAT //' | sed -e 's/ /:/g' | sed -e 's/:STAT:/\n/g')
do
OLD_IFS="$IFS"
IFS=":"
KEY_VALUE=($i)
IFS="$OLD_IFS"
ITEM_KEY=$ZABBIX_ITEM_KEY_PREFIX${KEY_VALUE[0]}
echo $ITEM_KEY:${KEY_VALUE[1]}
$ZABBIX_SENDER --zabbix-server $ZABBIX_TRAPPER --port $ZABBIX_TRAPPER_PORT --host $ZABBIX_HOST --key $ITEM_KEY --value ${KEY_VALUE[1]}
done

Save it to a file and schedule it with CronD. You will also have to add the Memcached template to all your memcached hosts.

No comments: