一. 环境
Centos 7.2
Zabbix 3.4
Redis 5.0.0
二 zabbix-agent配置
1. 编辑zabbix-agent的zabbix_agentd.conf
vim /etc/zabbix/zabbix_agentd.conf
添加:
UserParameter=Redis.Info[*],/etc/zabbix/script/redis/redismonitor.sh 2>/dev/null $1 $2
# 通过脚本监控信息
UserParameter=Redis.Status,(/datastore/redis/redis-5.0.0/src/redis-cli -h 192.168.126.128 -p 6379 -a 密码 ping)2>/dev/null | grep -c PONG
使用错误重定向(2>/dev/null)是防止报错导致监控项失效无法告警,具体错误为 : 538840574" of type “string” is not suitable for value type "Numeric (unsign)"
设置AllowRoot=1,目的是以root用户去执行脚本
2. 添加脚本
新建redismonitor.sh,放在/etc/zabbix/script/redis目录下,当然这个目录你可以随便改
脚本内容为:
#! /bin/bash
#Name: redismontior.sh
#From: liuhaibin 2018.5.29
#Action: Zabbix monitoring redis plug-in
#根据自己的安装情况填写命令位置主机ip和端口
REDISCLI="/datastore/redis/redis-5.0.0/src/redis-cli"
HOST="192.168.126.128"
PORT=6379
PASS="密码"
if [[ $# == 1 ]];then
case $1 in
version)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "redis_version" | awk -F':' '{print $2}'`
echo $result
;;
uptime)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
echo $result
;;
connected_clients)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "connected_clients" | awk -F':' '{print $2}'`
echo $result
;;
blocked_clients)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "blocked_clients" | awk -F':' '{print $2}'`
echo $result
;;
used_memory)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_memory" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_rss)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_peak)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
echo $result
;;
used_memory_lua)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_sys)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_user)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_sys_children)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_user_children)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
echo $result
;;
rdb_last_bgsave_status)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_bgrewrite_status)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_write_status)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | 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 -p $PORT -a $PASS info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
echo $result
;;
expires)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
echo $result
;;
avg_ttl)
result=`$REDISCLI -h $HOST -p $PORT -a $PASS 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
zabbix监控redis只要是通过“info”查看信息, 如果redis没有密码的话,记得把$Pass去掉
修改脚本的可执行权限
chmod +x redismonitor.sh
重启 agent
service zabbix-agent restart
3. 导入模板
在zabbix web端导入web模板 [链接:https://pan.baidu.com/s/1NjGyxqJcy4YmMBDZUcJGRg
提取码:sktv ]
三 测试
在zabbix 服务端用zabbix_get命令测试是否配置成功
zabbix_get -s 192.168.126.128 -k Redis.Info[used_memory]
前端