zabbix_version:3.0.22

1.web模板

zbx_export_templates_redis.xml

2.配置文件:userparameter_redis.conf

1
2
UserParameter=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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# Modify by lyc at 2018-12-03
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# Modify by lyc at 2018-12-03
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"