Write by lyc at 2018-12-03
zabbix_version:3.0.22
1.zabbix 控制台上传监控模板
zbx_export_templates_redis.xml 下载模板
| 1
 | $ get https://res.lyc7456.com/zbx_export_templates_redis.xml
 | 
2.zabbix-agent 配置文件
| 12
 3
 
 | $ cat userparameter_redis.confUserParameter=redis_discovery[*],sudo /bin/sh /etc/zabbix/scripts/redis_discovery.sh $1
 UserParameter=redis_stats[*],sudo /bin/sh /etc/zabbix/scripts/redis_check.sh $1 $2
 
 | 
3.自动发现脚本:redis_discovery.sh
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | #!/bin/bash
 function redis() {
 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
 
 | 
4.监控脚本:redis_check.sh
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | #!/bin/bash
 port="$1"
 item="$2"
 
 function getIp(){
 if [[ -n "`grep 'release 7' /etc/redhat-release`" ]];then
 INNER_IP=`/sbin/ifconfig |egrep "inet 192\.168|inet 10\.|inet 172\."|head -1|awk '{print $2}'`
 else
 INNER_IP=`ifconfig |egrep "inet addr:192\.168\.|inet addr:10\.|inet addr:172\."|head -1|awk '{print $2}'|sed "s/addr://g"`
 fi
 }
 
 getIp
 if [[ "$2" == "maxmemory" ]];then
 value=`(echo "config get maxmemory";sleep 0.1) | sudo telnet ${INNER_IP} $port 2>/dev/null|tail -1`
 else
 value=`(echo "info";sleep 0.1) | sudo telnet ${INNER_IP} $port 2>/dev/null| grep $item |head -1|cut -d : -f2`
 fi
 echo "$value"
 
 |