Skip to content

Instantly share code, notes, and snippets.

@alice-xu
Created July 1, 2014 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alice-xu/83701263c2c835aed8da to your computer and use it in GitHub Desktop.
Save alice-xu/83701263c2c835aed8da to your computer and use it in GitHub Desktop.
Memcached monitoring script for Zabbix
#!/usr/bin/env bash
#
# memcached_stats.sh:
# Memcached monitoring script for Zabbix
#
# $1: Memcached host(default: 127.0.0.1)
# $2: Memcached port(default: 11211)
# $3: Timeout(default: 3[sec])
#
# script settings.
cd `dirname $0`
source ./zbx_functions
# Default parameters
[ -z "$1" ] && MEMCACHED_HOST="127.0.0.1" || MEMCACHED_HOST="$1"
[ -z "$2" ] && MEMCACHED_PORT="11211" || MEMCACHED_PORT="$2"
[ -z "$3" ] && MEMCACHED_TIMEOUT="3" || MEMCACHED_TIMEOUT="$3"
POLLING_TIME=$( date '+%s' )
MEMCACHED_STATS=$( echo -e 'stats' \
| nc -w ${MEMCACHED_TIMEOUT} ${MEMCACHED_HOST} ${MEMCACHED_PORT} )
MEMCACHED_MAXCONNS=$( echo -e 'stats settings' \
| nc -w ${MEMCACHED_TIMEOUT} ${MEMCACHED_HOST} ${MEMCACHED_PORT}|grep maxconns )
if [ $? -ne 0 ]; then
echo "Error: Could not connect to memcached(Host:${MEMCACHED_HOST}, Port:${MEMCACHED_PORT}, Timeout:${MEMCACHED_TIMEOUT})."
exit 1
fi
RESULT=$( echo -e "${MEMCACHED_STATS}\n${MEMCACHED_MAXCONNS}" \
| grep -E '^STAT ' | tr -d '\r' \
| awk -v hostname="${MY_HOSTNAME}" -v time="${POLLING_TIME}" \
'{ print "\""hostname"\"","script.memcached."$2,time,$3 }' )
echo "${RESULT}" | grep script.memcached.version > /dev/null 2>&1
if [ ${PIPESTATUS[1]} -ne 0 ]; then
echo "Error: Memcached does not respond to stat command(Host:${MEMCACHED_HOST}, Port:${MEMCACHED_PORT}, Timeout:${MEMCACHED_TIMEOUT})."
exit 1
fi
echo "Info: Memcached is alive(Host:${MEMCACHED_HOST}, Port:${MEMCACHED_PORT}, Timeout:${MEMCACHED_TIMEOUT})"
echo "${RESULT}" | zbx_bulk_sender 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment