Skip to content

Instantly share code, notes, and snippets.

@nettedfish
Last active December 22, 2015 23:39
Show Gist options
  • Save nettedfish/6548192 to your computer and use it in GitHub Desktop.
Save nettedfish/6548192 to your computer and use it in GitHub Desktop.
模仿percona的nagios监控插件,实现一个简单的redis服务状态监控插件,判断redis服务器是否存活,是否因为未知原因自动重启过
#!/bin/bash
# ########################################################################
# This program is part of Qunar Monitoring Plugins
# Authors: yinggang.zhao@qunar.com
# ########################################################################
# ########################################################################
# Redirect STDERR to STDOUT; Nagios doesn't handle STDERR.
# ########################################################################
exec 2>&1
# ########################################################################
# Set up constants, etc.
# ########################################################################
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
redis_client="/usr/bin/redis-cli"
script_name="q-check-redis-server-status"
#下限,1800秒。用户也可以自己定义。默认uptime 为1800秒,就认为server的状态正常
server_uptime_low_limit=1800
# ########################################################################
# Run the program.
# ########################################################################
main() {
# Get options
for o; do
case "${o}" in
-l) shift; server_uptime_low_limit="${1}"; shift; ;;
-p) shift; OPT_PORT="${1}"; shift; ;;
--help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;;
-*) echo "Unknown option ${o}. Try --help."; exit 1; ;;
esac
done
if is_not_sourced; then
#如果输入不是以-开头的,就会走到这个分支
if [ -n "$1" ]; then
echo "WARN spurious command-line options: $@"
exit 1
fi
fi
#端口为必须的选项
if [ -z ${OPT_PORT} ]; then
echo "WARN please input your redis port number!"
exit 2
fi
if [ ! -x "${redis_client}" ]; then
echo "WARN please yum install redis first! ${redis_client} does't exist?"
exit 2
fi
if [ -z "$1" ]; then
#由于redis的info输出含有换行,此处必须使用tr来过滤
server_uptime=$(${redis_client} -h 127.0.0.1 -p ${OPT_PORT} info|grep -i -E "uptime_in_seconds"|tr -d "\n\r"|awk -F ":" '{print $2}')
fi
if [ ! -z ${server_uptime} ]; then
if [ ${server_uptime} -gt ${server_uptime_low_limit} ]; then
NOTE="OK redis server uptime is ${server_uptime}. uptime low limit is ${server_uptime_low_limit}. so status OK"
else
NOTE="CRIT redis server uptime is ${server_uptime}, less than ${server_uptime_low_limit}. please check"
fi
#如果redis连接不上
else
NOTE="CRIT fatal! redis server down!"
fi
echo $NOTE
}
# ########################################################################
# Determine whether this program is being executed directly, or sourced/included
# from another file.
# ########################################################################
is_not_sourced() {
[ "${0##*/}" = "${script_name}" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
}
# ########################################################################
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
# ########################################################################
#下面的语句,跟python的if __name__ == "__main__" 一样,可以便于调试,非常cool
if is_not_sourced; then
OUTPUT=$(main "$@")
EXITSTATUS=$STATE_UNKNOWN
case "${OUTPUT}" in
UNK*) EXITSTATUS=$STATE_UNKNOWN; ;;
OK*) EXITSTATUS=$STATE_OK; ;;
WARN*) EXITSTATUS=$STATE_WARNING; ;;
CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
esac
echo "${OUTPUT}"
exit $EXITSTATUS
fi
# ############################################################################
# Documentation
# ############################################################################
: <<'DOCUMENTATION'
Usage: ./script_name [OPTIONS]
Options:
-l uptime low limit
-p redis port. 6379...
--help show this help message
Options must be given as --option value, not --option=value or -Ovalue.
readme here...
DOCUMENTATION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment