这次要监控redis的状态,由于一台机器上起了多个redis实例,所以需要自动发现

1.redis端口自动发现脚本

vim redis_discovery.sh 
#!/bin/bash
redisarray=(`netstat -ntpl | grep "redis" | awk -F'[ :]+' '{print $5}'`)
 
length=${#redisarray[@]}
printf "{\n"
printf  '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
        printf '\n\t\t{'
        printf "\"{#PORT_NAME}\":\"${redisarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
                printf ','
        fi
done
printf  "\n\t]\n"
printf "}\n"

2.redis状态提取脚本


#!/bin/bash
# by ckl893
ARG1=$1
ARG2=$2

func_connected_clients () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "connected_clients" | awk -F':' '{print $2}'
}


func_mem_unilization () {
        MEM_USE=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "\" | awk -F':' '{print $2}')
        MEM_TOT=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "\" | awk -F':' '{print $2}')

        MEM_USE1=`echo $MEM_USE | tr -d '\r'`
        MEM_TOT1=`echo $MEM_TOT | tr -d '\r'`
        mem_unilization=`echo "scale=2;$MEM_USE1 / $MEM_TOT1" | bc | awk '{printf "%.2f", $0}'`
        echo $mem_unilization
}

func_cpu_sys_used () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "\" | awk -F':' '{print $2}'
}

func_cpu_user_userd () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "\" | awk -F':' '{print $2}'
}


func_hit_rate () {
        HIT=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "keyspace_hits" | awk -F':' '{print $2}')
        MISS=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "keyspace_misses" | awk -F':' '{print $2}')

        HIT1=`echo $HIT | tr -d '\r'`
        MISS1=`echo $MISS | tr -d '\r'`
        ALL=`expr $HIT1 + $MISS1`
        hit_rate=`echo "scale=2;$HIT1 / $ALL" | bc | awk '{printf "%.2f", $0}'`
        echo $hit_rate
}

func_total_connections_received () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "total_connections_received" | awk -F':' '{print $2}'
}

func_total_commands_processed () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep "total_commands_processed" | awk -F':' '{print $2}'
}

func_db_keys () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep db0 | awk -F'[:=,]+' '{print $3}'
}

func_db_expires () {
        /app/local/redis/bin/redis-cli -h 127.0.0.1 -p ${ARG1} info | grep db0 | awk -F'[:=,]+' '{print $5}'
}

case $ARG2 in 
connected_clients)
        func_connected_clients
        ;;
mem_unilization)
        func_mem_unilization
        ;;
cpu_sys_used)
        func_cpu_sys_used
        ;;
cpu_user_userd)
        func_cpu_user_userd
        ;;
hit_rate)
        func_hit_rate
        ;;
total_connections_received)
        func_total_connections_received
        ;;
total_commands_processed)
        func_total_commands_processed
        ;;
db_keys)
        func_db_keys
        ;;
db_expires)
        func_db_expires
        ;;
*)
        echo "what can i do for you!"
        ;;
esac
exit 0

2.添加配置文件参数

vim /usr/local/zabbix-agent/etc/zabbix_agentd.conf
#redis_stat
UserParameter=redis.status[*],/bin/bash /usr/local/zabbix-agent/scripts/redis_stat.sh $1 $2

3.重启zabbix进程

/etc/init.d/zabbix_agentd restart

4.添加模板

zabbix监控redis状态(五)_第1张图片

5.添加discovery rule

wKiom1h3OvWx6wmoAACPXw2X29M015.png

此处的key为配置文件定义的

zabbix监控redis状态(五)_第2张图片


6.添加item prototypes

7.查看添加的item prototypes

zabbix监控redis状态(五)_第3张图片8.注意

zabbix监控redis状态(五)_第4张图片

7.添加graph

zabbix监控redis状态(五)_第5张图片zabbix监控redis状态(五)_第6张图片

8.查看图形

查看发现状态:

查看状态:

zabbix监控redis状态(五)_第7张图片

远程获取数据报错:

wKiom1h3Q33z0wIDAAAcVzGnP0U171.png

解决:

1.添加sudoer

vi /etc/sudoers 
Cmnd_Alias MONITORING = /bin/netstat,/sbin/sudo   
zabbix       ALL=(root) NOPASSWD:MONITORING

2.添加权限

设置SUID/SGID程序允许普通用户以root身份暂时执行该程序

chmod +s /bin/netstat

3.测试:

zabbix监控redis状态(五)_第8张图片