公司最近网站改版,增加了redis服务器,现领导要求需要测试redis的监控,于是从网上找了个redis的监控脚本,简单的修改了一下,测试中没出现任何问题。生产环境中,可根据实际要监控的参数值修改脚本内容,并添加相应的触发器。
Redis有自带的redis-cli客户端,通过info命令可以查询到redis的运行情况,我们可以写个shell脚本,通过zabbix来调用这个脚本实现redis的监控。
要获得redis的当前情况,可以使用info命令。
命令格式:
redis-cli -h [hostname] -p [port] -a [password] info [参数]
1、查询server信息
redis-cli -h 127.0.0.1 -p 6379 -a 'password' info server
redis-cli -h 127.0.0.1 -p 6379 -a 'password' info clients
redis-cli -h 127.0.0.1 -p 6379 -a 'password' info memory
redis-cli -h 127.0.0.1 -p 6379 -a 'password' info cpu
1、编写监控脚本vim /etc/zabbix/zabbix_agentd.d/redis_status.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
HOST="127.0.0.1"
PORT=6379
PASS="password"
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
;;
uptime)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info server | grep -w "uptime_in_seconds" | 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
;;
blocked_clients)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "blocked_clients" | awk -F':' '{print $2}'`
echo $result
;;
used_memory)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_rss)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_peak)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_lua)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_sys)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_user)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_sys_children)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_user_children)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
echo $result
;;
rdb_last_bgsave_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_bgrewrite_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_write_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "aof_last_write_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {connected_clients|blocked_clients|used_memory|used_memory_rss|used_memory_peak|used_memory_lua|used_cpu_sys|used_cpu_user|used_cpu_sys_children|used_cpu_user_children|rdb_last_bgsave_status|aof_last_bgrewrite_status|aof_last_write_status}\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
;;
expires)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
echo $result
;;
avg_ttl)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {db0 keys|db0 expires|db0 avg_ttl}\033[0m"
;;
esac
fi
2、赋予脚本可执行权限
chmod +x /etc/zabbix/zabbix_agentd.d/redis_status.sh
3、脚本测试
查看redis的客户端连接数
/etc/zabbix/zabbix_agentd.d/redis_status.sh connected_clients
1、编写redis监控配置文件vim /etc/zabbix/zabbix_agentd.d/redis.conf
UserParameter=Redis.Status,status=`/usr/local/bin/redis-cli -h 127.0.0.1 -a 'password' -p 6379 ping|grep -c PONG` &&echo $status
UserParameter=Redis.Info[*],/etc/zabbix/zabbix_agentd.d/redis_status.sh $1 $2
2、重启zabbix-agent
systemctl restart zabbix-agent
3、在zabbix server端测试
zabbix_get -s 192.168.2.235 -p 10050 -k "Redis.Info[used_cpu_user]"
1、创建监控模板文件
redis-template.xml文件内容如下
2.0
2014-08-07T10:04:35Z
RedisMontior
Templates
RedisMontior
RedisMontior
RedisMontior
Templates
Redis Clients
Redis CPU
Redis DbKey
Redis Memory
Redis WriteStatus
-
Redis.Info[aof_last_bgrewrite_status]
0
0
Redis.Info[aof_last_bgrewrite_status]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis WriteStatus
-
Redis.Info[aof_last_write_status]
0
0
Redis.Info[aof_last_write_status]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis WriteStatus
-
Redis.Info[blocked_clients]
0
0
Redis.Info[blocked_clients]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Clients
-
Redis.Info[connected_clients]
0
0
Redis.Info[connected_clients]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Clients
-
Redis.Info[db0,avg_ttl]
0
0
Redis.Info[db0,avg_ttl]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis DbKey
-
Redis.Info[db0,expires]
0
0
Redis.Info[db0,expires]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis DbKey
-
Redis.Info[db0,keys]
0
0
Redis.Info[db0,keys]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis DbKey
-
Redis.Info[rdb_last_bgsave_status]
0
0
Redis.Info[rdb_last_bgsave_status]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis WriteStatus
-
Redis.Info[uptime]
0
0
Redis.Info[uptime]
30
90
365
0
3
uptime
0
0
0
0
1
0
0
0
-
Redis.Info[used_cpu_sys]
0
0
Redis.Info[used_cpu_sys]
30
90
365
0
0
0
0
0
0
1
0
0
0
Redis CPU
-
Redis.Info[used_cpu_sys_children]
0
0
Redis.Info[used_cpu_sys_children]
30
90
365
0
0
0
0
0
0
1
0
0
0
Redis CPU
-
Redis.Info[used_cpu_user]
0
0
Redis.Info[used_cpu_user]
30
90
365
0
0
0
0
0
0
1
0
0
0
Redis CPU
-
Redis.Info[used_cpu_user_children]
0
0
Redis.Info[used_cpu_user_children]
30
90
365
0
0
0
0
0
0
1
0
0
0
Redis CPU
-
Redis.Info[used_memory]
0
0
Redis.Info[used_memory]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Memory
-
Redis.Info[used_memory_lua]
0
0
Redis.Info[used_memory_lua]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Memory
-
Redis.Info[used_memory_peak]
0
0
Redis.Info[used_memory_peak]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Memory
-
Redis.Info[used_memory_rss]
0
0
Redis.Info[used_memory_rss]
30
90
365
0
3
0
0
0
0
1
0
0
0
Redis Memory
-
Redis.Info[version]
0
0
Redis.Info[version]
30
90
365
0
1
0
0
0
0
1
0
0
0
-
Redis Status
0
0
Redis.Status
30
90
365
0
3
0
0
0
0
1
0
0
0
{RedisMontior:Redis.Status.last(0)}=0
Redis is down
0
5
0
Redis Client
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
0
C80000
0
2
0
-
RedisMontior
Redis.Info[blocked_clients]
1
0
00C800
0
2
0
-
RedisMontior
Redis.Info[connected_clients]
Redis CPU
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
2
C80000
0
2
0
-
RedisMontior
Redis.Info[used_cpu_sys]
1
2
00C800
0
2
0
-
RedisMontior
Redis.Info[used_cpu_user]
2
2
0000C8
0
2
0
-
RedisMontior
Redis.Info[used_cpu_sys_children]
3
2
C800C8
0
2
0
-
RedisMontior
Redis.Info[used_cpu_user_children]
Redis DbKeys
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
2
C80000
0
2
0
-
RedisMontior
Redis.Info[db0,avg_ttl]
1
2
00C800
0
2
0
-
RedisMontior
Redis.Info[db0,expires]
2
2
0000C8
0
2
0
-
RedisMontior
Redis.Info[db0,keys]
Redis Memory
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
2
C80000
0
2
0
-
RedisMontior
Redis.Info[used_memory]
1
2
00C800
0
2
0
-
RedisMontior
Redis.Info[used_memory_lua]
2
2
0000C8
0
2
0
-
RedisMontior
Redis.Info[used_memory_peak]
3
2
C800C8
0
2
0
-
RedisMontior
Redis.Info[used_memory_rss]
Redis WriteStatus
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
2
C80000
0
2
0
-
RedisMontior
Redis.Info[aof_last_bgrewrite_status]
1
2
0000C8
0
2
0
-
RedisMontior
Redis.Info[rdb_last_bgsave_status]
2
2
00C800
0
2
0
-
RedisMontior
Redis.Info[aof_last_write_status]
2、导入监控模板
配置—模板—导入
点击“选择文件”,找到redis-template.xml文件,将其导入