簡單的redis服務狀態監控插件: q-check-redis-server-status

jopen 11年前發布 | 27K 次閱讀 系統監控 q-check-redis-server-status

模仿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

項目主頁:http://www.baiduhome.net/lib/view/home/1381584093458

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!