Skip to content

Instantly share code, notes, and snippets.

@danielbeardsley
Created August 2, 2012 08:05
Show Gist options
  • Save danielbeardsley/3235099 to your computer and use it in GitHub Desktop.
Save danielbeardsley/3235099 to your computer and use it in GitHub Desktop.
Pipe memcache stats into Graphite
#!/bin/bash
if [ "$1" != "report" ]; then
echo "Usage:" >&2
script="`basename $0`"
echo " nohup $script report > /var/log/memcache-stats.log &" >&2
exit 1
fi
GRAPHITE_SERVER=localhost
GRAPHITE_PORT=2003
GRAPHITE_INTERVAL=10
while true; do
# Do it in a backgrounded subshell so we can move
# directly on to sleeping for $GRAPHITE_INTERVAL
(
# Get a timestamp for sending to graphite
ts=`date +%s`
# memcache gives us some decent stats in the form of
# STAT bytes_read 4535820
output=`service memcached status 2>/dev/null |
grep STAT |
grep -v version |
sed "s/STAT /memcache\./"`
# Pipe the output through sed, using a regex to
# append a $ts timestamp to the end of each line,
# and then to the correct server and port using netcat
echo "$output" |
sed "s/\$/ $ts/" |
nc $GRAPHITE_SERVER $GRAPHITE_PORT
# Echo this data too in case we want to record
# it to a log
echo `date "+%Y-%m-%d_%H:%M:%S"`
echo "$output"
echo; echo
) &
sleep $GRAPHITE_INTERVAL
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment