公司最近网站改版,增加了redis服务器,现领导要求需要测试redis的监控,于是从网上找了个redis的监控脚本,简单的修改了一下,测试中没出现任何问题。生产环境中,可根据实际要监控的参数值修改脚本内容,并添加相应的触发器。
Redis有自带的redis-cli客户端,通过info命令可以查询到redis的运行情况,我们可以写个shell脚本,通过zabbix来调用这个脚本实现redis的监控。

一、info命令的使用

要获得redis的当前情况,可以使用info命令。
命令格式:

redis-cli -h [hostname] -p [port] -a [password] info [参数]

1、查询server信息

redis-cli -h 127.0.0.1 -p 6379 -a 'password' info server

Zabbix3.4通过shell脚本监控redis服务_第1张图片
2、查询客户端连接情况

redis-cli -h 127.0.0.1 -p 6379 -a 'password' info clients

Zabbix3.4通过shell脚本监控redis服务_第2张图片
3、查询内存使用情况

redis-cli -h 127.0.0.1 -p 6379 -a 'password' info memory

Zabbix3.4通过shell脚本监控redis服务_第3张图片
4、查询CPU使用情况

redis-cli -h 127.0.0.1 -p 6379 -a 'password' info cpu

Zabbix3.4通过shell脚本监控redis服务_第4张图片

二、创建redis监控脚本

1、编写监控脚本
vim /etc/zabbix/zabbix_agentd.d/redis_status.sh

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
HOST="127.0.0.1"
PORT=6379
PASS="password"

if [[ $# == 1 ]];then
    case $1 in
        version)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info server | grep -w "redis_version" | awk -F':' '{print $2}'`
            echo $result
        ;;
        uptime)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info server | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
            echo $result
        ;;
        connected_clients)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        rdb_last_bgsave_status)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_bgrewrite_status)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_write_status)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | 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 -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl)
            result=`$REDISCLI -h $HOST -a $PASS -p $PORT 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

2、赋予脚本可执行权限

chmod +x /etc/zabbix/zabbix_agentd.d/redis_status.sh

3、脚本测试
查看redis的客户端连接数

/etc/zabbix/zabbix_agentd.d/redis_status.sh connected_clients

Zabbix3.4通过shell脚本监控redis服务

三、创建redis监控配置文件

1、编写redis监控配置文件
vim /etc/zabbix/zabbix_agentd.d/redis.conf

UserParameter=Redis.Status,status=`/usr/local/bin/redis-cli -h 127.0.0.1 -a 'password' -p 6379 ping|grep -c PONG` &&echo $status
UserParameter=Redis.Info[*],/etc/zabbix/zabbix_agentd.d/redis_status.sh $1 $2

2、重启zabbix-agent

systemctl restart zabbix-agent

3、在zabbix server端测试

zabbix_get -s 192.168.2.235 -p 10050 -k "Redis.Info[used_cpu_user]"

Zabbix3.4通过shell脚本监控redis服务

四、创建并导入监控模板

1、创建监控模板文件
redis-template.xml文件内容如下



    2.0
    2014-08-07T10:04:35Z
    
        
            RedisMontior
        
        
            Templates
        
    
    
        
    
    
        
            {RedisMontior:Redis.Status.last(0)}=0
            Redis is down
            
            0
            5
            
            0
            
        
    
    
        
            Redis Client
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    0
                    C80000
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[blocked_clients]
                    
                
                
                    1
                    0
                    00C800
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[connected_clients]
                    
                
            
        
        
            Redis CPU
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    2
                    C80000
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_cpu_sys]
                    
                
                
                    1
                    2
                    00C800
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_cpu_user]
                    
                
                
                    2
                    2
                    0000C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_cpu_sys_children]
                    
                
                
                    3
                    2
                    C800C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_cpu_user_children]
                    
                
            
        
        
            Redis DbKeys
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    2
                    C80000
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[db0,avg_ttl]
                    
                
                
                    1
                    2
                    00C800
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[db0,expires]
                    
                
                
                    2
                    2
                    0000C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[db0,keys]
                    
                
            
        
        
            Redis Memory
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    2
                    C80000
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_memory]
                    
                
                
                    1
                    2
                    00C800
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_memory_lua]
                    
                
                
                    2
                    2
                    0000C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_memory_peak]
                    
                
                
                    3
                    2
                    C800C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[used_memory_rss]
                    
                
            
        
        
            Redis WriteStatus
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    2
                    C80000
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[aof_last_bgrewrite_status]
                    
                
                
                    1
                    2
                    0000C8
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[rdb_last_bgsave_status]
                    
                
                
                    2
                    2
                    00C800
                    0
                    2
                    0
                    
                        RedisMontior
                        Redis.Info[aof_last_write_status]
                    
                
            
        
    

2、导入监控模板
配置—模板—导入
Zabbix3.4通过shell脚本监控redis服务_第5张图片
点击“选择文件”,找到redis-template.xml文件,将其导入
Zabbix3.4通过shell脚本监控redis服务_第6张图片

五、给主机添加监控模板

Zabbix3.4通过shell脚本监控redis服务_第7张图片
Zabbix3.4通过shell脚本监控redis服务_第8张图片

六、监控效果图

Zabbix3.4通过shell脚本监控redis服务_第9张图片
Zabbix3.4通过shell脚本监控redis服务_第10张图片
Zabbix3.4通过shell脚本监控redis服务_第11张图片
附:配置文件、脚本和模板下载地址
http://down.51cto.com/data/2454656