-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_redis.sh
executable file
·99 lines (87 loc) · 2.05 KB
/
check_redis.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
#
# Author: Guanghongwei
# Email: [email protected]
cwdir=`dirname $0`
. $cwdir/function.sh
redis_cli=$(which redis-cli 2> /dev/null)
redis_cli=${redis_cli:=/opt/redis/bin/redis-cli}
port=${2:-6379}
redis_pwd_default=guanghongwei
if [ -f $check_list ];then
redis_pwd=$(grep $port $check_list | awk '{ print $4 }')
fi
redis_pwd=${redis_pwd:-$redis_pwd_default}
redis_cli="${redis_cli} -a $redis_pwd -p $port"
tmpfile=$tmp_dir/.$(str_md5 ${port}.redis).zbx
slave_discovery() {
ports=($(grep -v "^#" $check_list | grep '^redis:' | grep 'slave' | awk '{ print $2 }'))
discovery ${ports[@]}
}
redis_discovery() {
ports=($(grep -v "^#" $check_list | grep "^redis:" | awk '{ print $2 }'))
discovery ${ports[@]}
}
redis_ping() {
error=''
role=$(grep ^redis: $check_list | grep $port | awk '{ print $3 }')
role=$(low_case ${role:-slave})
$redis_cli ping 2> /dev/null | grep -i 'PONG' &> /dev/null || error='1'
if [ "$role" == 'master' ];then
$redis_cli set zbx_ping OK 2> /dev/null | grep -i 'OK' &> /dev/null || error='2'
$redis_cli expire zbx_ping 60 &> /dev/null
$redis_cli get zbx_ping 2> /dev/null | grep -i 'OK' &> /dev/null || error='3'
fi
if [ -z "$error" ];then
echo 1
else
echo 0
fi
$redis_cli info 2> /dev/null > $tmpfile
}
redis_perf() {
value=$(grep ^$1 $tmpfile | awk -F: '{ print $2 }')
if [ ! -z "$value" ];then
echo $value
fi
}
redis_conf() {
value=$($redis_cli config get $1 | tail -1)
if [ ! -z "$value" ];then
echo $value
fi
}
redis_size() {
$redis_cli dbsize | head -1
}
case $1 in
discovery)
redis_discovery
;;
slave_discovery)
slave_discovery
;;
ping)
redis_ping
;;
tmpfile_md5)
tmpfile_md5
;;
dbsize)
redis_size
;;
perf)
if [ ! -z "$3" ];then
redis_perf $3
fi
;;
conf)
if [ ! -z "$3" ];then
redis_conf $3
fi
;;
*)
msg="Usage: $0 discovery | ping | tmpfile_md5 | dbsize | conf port option | perf port option "
echo $msg
;;
esac