Zabbix4.0.12通过shell脚本监控redis服务

由于公司需要监控redis的状态,并放在zabbix图形展示,所以就需要用到redis自带的redis-cli客户端,通过info命令查询redis的运行情况,以下是shell脚本,下一篇文章会介绍如何简单监控windows上的redis。

一、info命令的使用

如何搭建zabbix和安装redis这里就不介绍了。
1、首先要用到redis-cli命令,通过find查找本机命令所在目录,比如我的是在/usr/local/bin/redis-cli 通过命令redis-cli -h 127.0.0.1 -p 6379 -a ‘password’ info可以查看本机redis的详细信息,包括连接数、网络流量等。
2、所以我们可以在安装有redis的服务器上编写shell脚本,用来查询该服务器redis的状态,然后通过该服务器上zabbix客户端来读取脚本结果,返回给zabbix服务端数值,来展现在我们面前。

二、创建redis监控脚本

1.首先来到zabbix_agentd.d目录,比如我的是在/etc/zabbix/zabbix_agentd.d,然后该目录下默认会有一个userparameter_mysql.conf配置文件,该配置文件用来定义要执行的脚本和键值对应关系。
2.我们先创建一个redis监控脚本,用来筛选出我们需要的监控项目所对应的值。
3. vim redisStatus.sh 这里由于我每台机器都部署了两个redis,用来跟其他机器的redis做主从,所以我需要监听两个端口6379和6380.

#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
HOST="127.0.0.1"
PASS="password"
#这里我没有定义端口号的变量,有需要也可以定义的,会更方便
#以下筛选出每个字断所对应的值
if [[ $# == 1 ]];then
    case $1 in
        version)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info server 2>/dev/null | grep -w "redis_version" | awk -F':' '{print $2}'`
            echo $result
        ;;
        total_commands_processed6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "total_commands_processed" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_ops_per_sec6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_input_kbps6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "instantaneous_input_kbps" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_output_kbps6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "instantaneous_output_kbps" | awk -F':' '{print $2}'`
            echo $result
        ;;
        uptime6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info server 2>/dev/null | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
            echo $result
        ;;
        connected_clients6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info clients 2>/dev/null | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info clients 2>/dev/null | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info memory 2>/dev/null | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info memory 2>/dev/null | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info memory 2>/dev/null | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info memory 2>/dev/null | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        maxmemory6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info memory 2>/dev/null | grep -w "maxmemory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info cpu 2>/dev/null | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info cpu 2>/dev/null | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info cpu 2>/dev/null | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info cpu 2>/dev/null | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        rdb_last_bgsave_status6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info Persistence 2>/dev/null | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_bgrewrite_status6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info Persistence 2>/dev/null | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_write_status6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info Persistence 2>/dev/null | grep -w "aof_last_write_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        total_commands_processed6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "total_commands_processed" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_ops_per_sec6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_input_kbps6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "instantaneous_input_kbps" | awk -F':' '{print $2}'`
            echo $result
        ;;
        instantaneous_output_kbps6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "instantaneous_output_kbps" | awk -F':' '{print $2}'`
            echo $result
        ;;
        uptime6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info server 2>/dev/null | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
            echo $result
        ;;
        connected_clients6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info clients 2>/dev/null | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info clients 2>/dev/null | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info memory 2>/dev/null | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info memory 2>/dev/null | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info memory 2>/dev/null | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info memory 2>/dev/null | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        maxmemory6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info memory 2>/dev/null | grep -w "maxmemory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info cpu 2>/dev/null | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info cpu 2>/dev/null | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info cpu 2>/dev/null | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info cpu 2>/dev/null | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        rdb_last_bgsave_status6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info Persistence 2>/dev/null | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_bgrewrite_status6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info Persistence 2>/dev/null | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_write_status6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info Persistence 2>/dev/null | 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
        keys6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        keys6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
    esac
elif [[ $# == 2 ]];then
    case $2 in
        keys6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl6379)
            result=`$REDISCLI -h $HOST -a $PASS -p 6379 info 2>/dev/null | grep -w "$1" | grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
            echo $result
        ;;
        keys6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl6380)
            result=`$REDISCLI -h $HOST -a $PASS -p 6380 info 2>/dev/null | 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

4.赋予脚本可执行权限
chmod +x /etc/zabbix/zabbix_agentd.d/redisStatus.sh
5.脚本测试一下是否正确
/etc/zabbix/zabbix_agentd.d/redisStatus.sh version

三、修改配置文件

1.编写redis监控配置文件

  vim  /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

写入:

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

这里的两项,一个是监测redis的状态,用来zabbix触发报警;一个是用来匹配脚本的值。
2.重启zabbix-agent
systemctl restart zabbix-agent

四、创建并导入监控模板

1、创建监控模板文件
redis-template.xml文件下载地址如下,在我的github上。
[email protected]:sxswcq/zabbix.template.of.redis.monitoring.git
2.下载之后,导入监控模板
Zabbix4.0.12通过shell脚本监控redis服务_第1张图片

3.给主机添加监控项,选中刚才的模板就好。

以下写一些个人搭建中碰到的问题:
1.这个监控模板是网上通用的,一些基础项目的模板,不包含网络流量等,所以有需要大家自行添加,或者直接自己编写监控项目,根据自己写的脚本。如下是我自己写的监控项,其实只需要修改名称和对应的键值就行,名称自定义,键值就是脚本中case后面定义的:
Zabbix4.0.12通过shell脚本监控redis服务_第2张图片

你可能感兴趣的:(Zabbix,Redis)