1、制作redis_low_discovery.sh脚本
mkdir -p /data/service/script/zabbix
cd /data/service/script/zabbix
cat redis_low_discovery.sh
#!/bin/bash
#Fucation:mysql low-level discovery#Script_name redis_low_discovery.shredis() { port=($(sudo netstat -tpln | awk -F "[ :]+" '/redis/ && /0.0.0.0/ {print $5}')) printf '{\n' printf '\t"data":[\n' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1` printf '\t {\n' printf "\t\t\t\"{#REDISPORT}\":\"${port[${key}]}\"},\n" else [[ "${key}" -eq "((${#port[@]}-1))" ]] socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1` printf '\t {\n' printf "\t\t\t\"{#REDISPORT}\":\"${port[${key}]}\"}\n" fi done printf '\t ]\n' printf '}\n'}$1验证脚本是否正常
[root@l-redis1 zabbix]# sh redis_low_discovery.sh redis
{ "data":[ { "{#REDISPORT}":"7111"}, { "{#REDISPORT}":"7112"}, { "{#REDISPORT}":"7113"}, { "{#REDISPORT}":"7114"}, { "{#REDISPORT}":"7115"}, { "{#REDISPORT}":"7116"} ]}
2、添加UserParameter
cd /etc/zabbix/zabbix_agentd.d/
vim zabbix_redis.conf
UserParameter=zabbix_low_discovery[*],/bin/bash /data/service/script/zabbix/redis_low_discovery.sh $1UserParameter=redis_stats[*],(echo info; sleep 1) | telnet 127.0.0.1 $1 2>&1 |grep $2|cut -d : -f2zabbix_agentd.conf配置文件中把UnsafeUserParameters=1设置为1并打开注释即可,这里我的redis示例没有设置密码,如果有密码就加上-a password,telnet对空密码可以。有密码的话telnet换成$(which redis-cli)。然后重启agent即可
把redis_low_discovery.sh文件存放到data/service/script/zabbix/目录下,然后给与755权限,并修改用户与组为zabbix,同时允许zabbix用户无密码运行
echo "zabbix ALL=(root) NOPASSWD:/bin/netstat">>/etc/sudoers 关闭requiretty
sed -i 's/^Defaults.*.requiretty/#Defaults requiretty/' /etc/sudoers
3、server 客户端操作 使用zabbix_get 获取redis 键值 重启zabbix-agent zabbix-server 上面测试:
zabbix_get -s 10.5.58.241 -k redis_stats[7111,uptime_in_seconds]
26002584、导入模板 Template Redis Auto Discovery导入到zabbix里(模板在附件),具体操作不介绍 5、设置正则 adminstration--->general___右上角-->Regular expressions--->new Regular expressions name:Redis Regex master Result is True ^(7111|7112|7113|7114|7115|7116)$
6、模板调用正则 Configuration ----> Host---->Discovery rules ----->Redis Port Discovery------>
7、引用模板 配置-主机-Templates--添加模板 模板地址: https://files-cdn.cnblogs.com/files/yaxuan/zabbix_redis_templates.xml
17:09:30