zabbix自定义脚本监控redis


编写监控redis qps 脚本结合zabbix展示

之前公司的redis info显示执行了6亿次命令,造成cpu过高问题,现写下shell采集redis执行命令次数和ops然后通过zabbix做监控

监控脚本

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

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
HOST="127.0.0.1"
PORT=7007
PASS="redis_pwd"

if [[ $# == 1 ]];then
case $1 in
version)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info server | grep -w "redis_version" | awk -F':' '{print $2}'`
echo $result
;;
connected_clients)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "connected_clients" | awk -F':' '{print $2}'`
echo $result
;;
instantaneous_ops_per_sec)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Stats | grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
echo $result
;;

total_commands_processed)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Stats | grep -w "total_commands_processed" | awk -F':' '{print $2}'`
echo $result
;;

*)
echo -e "\033[33mUsage: $0 {version}\033[0m"
;;
esac
elif [[ $# == 2 ]];then
case $2 in
keys)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
echo $result
;;
*)
echo -e "33[33mUsage: $0 {db0 keys|db0 expires|db0 avg_ttl}33[0m"
;;
esac
fi

在本地做测试

1
2
3
4
[work@scripts]$ sh /data/backup/zabbix/scripts/redis_status_7007.sh total_commands_processed
11958
[work@scripts]$ sh /data/backup/zabbix/scripts/redis_status_7007.sh instantaneous_ops_per_sec
0

修改zabbix_agent配置文件

1
2
3
4
5
6
7
vim /etc/zabbix/zabbix_agentd.conf

Include=/etc/zabbix/zabbix_agentd.d/ # 简易脚本的执行目录
...
# 自定义脚本执行
UserParameter=Redis_7007.Info[*],/data/backup/zabbix/scripts/redis_status_7007.sh $1 $2
UserParameter=Redis_7010.Info[*],/data/backup/zabbix/scripts/redis_status_7010.sh $1 $2

重启zabbix agent服务

1
sudo /etc/init.d/zabbix-agent restart

在zabbix server端通过get测试

1
2
[work@scripts]$ zabbix_get -s 内网监控IP -p 10050 -k "Redis_7007.Info[total_commands_processed]"
12051
1
2
[work@scripts]$ zabbix_get -s 内网监控IP -p 10050 -k "Redis_7007.Info[instantaneous_ops_per_sec]"
0

在zabbix平台配置监控项和触发器