设为首页 收藏本站
查看: 1095|回复: 0

[经验分享] 线上zabbix监控redis和redis集群

[复制链接]

尚未签到

发表于 2019-1-17 12:58:33 | 显示全部楼层 |阅读模式
  2018-10-31 15:25:46
公司最近网站改版,增加了redis服务器,现领导要求需要测试redis的监控,于是从网上找了个redis的监控脚本,简单的修改了一下,测试中没出现任何问题。生产环境中,可根据实际要监控的参数值修改脚本内容,并添加相应的触发器。
Redis有自带的redis-cli客户端,通过info命令可以查询到redis的运行情况,我们可以写个shell脚本,通过zabbix来调用这个脚本实现redis的监控。
  一、info命令的使用
要获得redis的当前情况,可以使用info命令。
命令格式:
  redis-cli -h [hostname] -p [port] -a [password] info [参数]
redis-cli -h [hostname] -p [port] -c  [参数]
  1、查询server信息
  redis-cli -h 127.0.0.1 -p 6379 -a 'password' info server

  2、查询客户端连接情况
  redis-cli -h 127.0.0.1 -p 6379 -a 'password' info clients

  3、查询内存使用情况
  redis-cli -h 127.0.0.1 -p 6379 -a 'password' info memory

  4、查询CPU使用情况
  redis-cli -h 127.0.0.1 -p 6379 -a 'password' info cpu

  5、查询redis集群情况
  redis-cli -h 127.0.0.1 -p 6379 -c cluster nodes
redis-cli -h 127.0.0.1 -p 6379 -c info server


  二、创建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
  第二个根据自己系统环境改编的脚本:
  vim /etc/zabbix/zabbix_agentd.d/redis_status.sh
  #!/bin/bash
REDISCLI="/usr/bin/redis-cli"
HOST="127.0.0.1"
PORT=6379
  if [[ $# == 1 ]];then
case $1 in
cluster_state)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_state" | awk -F':' '{print $2}'| grep -c ok
echo $result
;;
cluster_slots_assigned)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_slots_assigned" | awk -F':' '{print $2}'
echo $result
;;
cluster_slots_ok)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_slots_ok" | awk -F':' '{print $2}'
echo $result
;;
cluster_slots_pfail)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_slots_pfail" | awk -F':' '{print $2}'
echo $result
;;
cluster_slots_fail)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_slots_fail" | awk -F':' '{print $2}'
echo $result
;;
cluster_known_nodes)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_known_nodes" | awk -F':' '{print $2}'
echo $result
;;
cluster_size)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_size" | awk -F':' '{print $2}'
echo $result
;;
cluster_current_epoch)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_current_epoch" | awk -F':' '{print $2}'
echo $result
;;
cluster_my_epoch)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_my_epoch" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_ping_sent)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_ping_sent" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_pong_sent)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_pong_sent" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_sent)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_sent" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_ping_received)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_ping_received" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_pong_received)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_pong_received" | awk -F':' '{print $2}'
echo $result
;;
cluster_stats_messages_received)
result=$REDISCLI -h $HOST  -p $PORT -c cluster info  | grep -w "cluster_stats_messages_received" | awk -F':' '{print $2}'
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received}\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

  三、创建redis监控配置文件
1、编写redis监控配置文件
  vim /etc/zabbix/zabbix_agentd.d/redis.conf
  UserParameter=Redis.status
  • ,/data/zabbix/scripts/redis_status.sh $1
    UserParameter=Redisfile,redis-cli -h 127.0.0.1 -p 6379 -c cluster nodes | awk -F ',' '{print $2}' | grep -c 'fail'
  •   2、重启zabbix-agent
      ps -ef | grep zabbix_agentd
    kill -9 进程
    /data/zabbix/sbin/zabbix_agentd -c /data/zabbix/etc/zabbix_agentd.conf
      或者:
      systemctl restart zabbix-agent
      3、在zabbix server端测试
      zabbix_get -s 192.168.2.235 -p 10050 -k "Redis.Info[used_cpu_user]"

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

    2.0
    2014-08-07T10:04:35Z


    RedisMontior


    Templates




    RedisMontior
    RedisMontior


    RedisMontior


    Templates




    Redis Clients


    Redis CPU


    Redis DbKey


    Redis Memory


    Redis WriteStatus




    Redis.Info[aof_last_bgrewrite_status]
    0

    0

    Redis.Info[aof_last_bgrewrite_status]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis WriteStatus





    Redis.Info[aof_last_write_status]
    0

    0

    Redis.Info[aof_last_write_status]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis WriteStatus





    Redis.Info[blocked_clients]
    0

    0

    Redis.Info[blocked_clients]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Clients





    Redis.Info[connected_clients]
    0

    0

    Redis.Info[connected_clients]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Clients





    Redis.Info[db0,avg_ttl]
    0

    0

    Redis.Info[db0,avg_ttl]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis DbKey





    Redis.Info[db0,expires]
    0

    0

    Redis.Info[db0,expires]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis DbKey





    Redis.Info[db0,keys]
    0

    0

    Redis.Info[db0,keys]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis DbKey





    Redis.Info[rdb_last_bgsave_status]
    0

    0

    Redis.Info[rdb_last_bgsave_status]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis WriteStatus





    Redis.Info[uptime]
    0

    0

    Redis.Info[uptime]
    30
    90
    365
    0
    3

    uptime
    0


    0
    0

    0

    1



    0
    0






    0




    Redis.Info[used_cpu_sys]
    0

    0

    Redis.Info[used_cpu_sys]
    30
    90
    365
    0
    0


    0


    0
    0

    0

    1



    0
    0






    0


    Redis CPU





    Redis.Info[used_cpu_sys_children]
    0

    0

    Redis.Info[used_cpu_sys_children]
    30
    90
    365
    0
    0


    0


    0
    0

    0

    1



    0
    0






    0


    Redis CPU





    Redis.Info[used_cpu_user]
    0

    0

    Redis.Info[used_cpu_user]
    30
    90
    365
    0
    0


    0


    0
    0

    0

    1



    0
    0






    0


    Redis CPU





    Redis.Info[used_cpu_user_children]
    0

    0

    Redis.Info[used_cpu_user_children]
    30
    90
    365
    0
    0


    0


    0
    0

    0

    1



    0
    0






    0


    Redis CPU





    Redis.Info[used_memory]
    0

    0

    Redis.Info[used_memory]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Memory





    Redis.Info[used_memory_lua]
    0

    0

    Redis.Info[used_memory_lua]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Memory





    Redis.Info[used_memory_peak]
    0

    0

    Redis.Info[used_memory_peak]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Memory





    Redis.Info[used_memory_rss]
    0

    0

    Redis.Info[used_memory_rss]
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    Redis Memory





    Redis.Info[version]
    0

    0

    Redis.Info[version]
    30
    90
    365
    0
    1


    0


    0
    0

    0

    1



    0
    0






    0




    Redis Status
    0

    0

    Redis.Status
    30
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0












    {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]






      模板下载地址:
    https://pan.baidu.com/s/1Iqqr_ad1V_Vzt9H4DyDoeA
    密码:m15h
    2、导入监控模板
    配置—模板—导入

    点击“选择文件”,找到redis-template.xml文件,将其导入

      五、给主机添加监控模板


    六、监控效果图




      脚本、模板下载地址:
    https://pan.baidu.com/s/1UnS4e4FFv1rZW-ctdrdQHA
    密码:1glw
      以上内容,如有问题 随时沟通
    QQ: 936172842




    运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-664425-1-1.html 上篇帖子: Zabbix篇三:windows批量安装客户端Zabbix 下篇帖子: zabbix3.4安装及使用
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表