使用Zabbix3.4监控工具创建 redis监控模板并实现redis监控 – 21运维
通知: .-...

使用Zabbix3.4监控工具创建 redis监控模板并实现redis监控

Zabbix 21运维 16654浏览 0评论

本案例以Zabbix-2.2.2为监控平台进行创建,该方法对于zabbix 2.X以及3.X的方法也是一样可以使用,只不过zabbix 2.x平台导入模板的时候,需要编辑模板xml文件将version 3.0改成2.0才可以导入,否则出错。如zabbix  V3.X,则可以直接导入并按照如下方法配置使用。这里只是一个通用操作,如需更加完善功能可以自己添加监控项以及触发器等。
1,导入模板
下载链接: http://download.21yunwei.com/zabbix/redis/zbx_template_redis.tar.gz  导入模板成功以后,将需要监控redis的主机添加这个模板。
注意:需 确认监控平台版本,低于3.0的需要编辑xml改成2.0后再去导入。

 

2,创建采集脚本
我的监控采集脚本都放到了/home/yunwei/redis_zbx.sh,在这里创建脚本保存shell脚本:
vim redis_zbx.sh

#!/bin/bash
REDISPATH="/usr/bin/redis-cli"
HOST="127.0.0.1"
PORT="6379"
REDIS_INFO="$REDISPATH -h $HOST -p $PORT info"
if [[ $# == 1 ]];then
case $1 in
cluster)
        result=`$REDIS_INFO|/bin/grep cluster|awk -F":" '{print $NF}'`
        echo $result 
;; 
uptime_in_seconds)
        result=`$REDIS_INFO|/bin/grep uptime_in_seconds|awk -F":" '{print $NF}'`
        echo $result 
;; 
connected_clients)
        result=`$REDIS_INFO|/bin/grep connected_clients|awk -F":" '{print $NF}'`
        echo $result 
;; 
client_longest_output_list)
        result=`$REDIS_INFO|/bin/grep client_longest_output_list|awk -F":" '{print $NF}'`
        echo $result 
;; 
client_biggest_input_buf)
        result=`$REDIS_INFO|/bin/grep client_biggest_input_buf|awk -F":" '{print $NF}'`
        echo $result 
;; 
blocked_clients)
        result=`$REDIS_INFO|/bin/grep blocked_clients|awk -F":" '{print $NF}'`
        echo $result 
;; 
#内存
used_memory)
        result=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result 
;; 
used_memory_human)
        result=`$REDIS_INFO|/bin/grep used_memory_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'` 
        echo $result 
;; 
used_memory_rss)
        result=`$REDIS_INFO|/bin/grep used_memory_rss|awk -F":" '{print $NF}'`
        echo $result 
;; 
used_memory_peak)
        result=`$REDIS_INFO|/bin/grep used_memory_peak|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result 
;; 
used_memory_peak_human)
        result=`$REDIS_INFO|/bin/grep used_memory_peak_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'`
        echo $result 
;; 
used_memory_lua)
        result=`$REDIS_INFO|/bin/grep used_memory_lua|awk -F":" '{print $NF}'`
        echo $result 
;;     
mem_fragmentation_ratio)
        result=`$REDIS_INFO|/bin/grep mem_fragmentation_ratio|awk -F":" '{print $NF}'`
        echo $result 
;;   
#rdb
rdb_changes_since_last_save)
        result=`$REDIS_INFO|/bin/grep rdb_changes_since_last_save|awk -F":" '{print $NF}'`
        echo $result 
;;   
rdb_bgsave_in_progress)
        result=`$REDIS_INFO|/bin/grep rdb_bgsave_in_progress|awk -F":" '{print $NF}'`
        echo $result 
;;   
rdb_last_save_time)
        result=`$REDIS_INFO|/bin/grep rdb_last_save_time|awk -F":" '{print $NF}'`
        echo $result 
;;   
rdb_last_bgsave_status)
        result=`$REDIS_INFO|/bin/grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
        echo $result 
;;   
rdb_current_bgsave_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "rdb_current_bgsave_time_sec" | awk -F':' '{print $2}'`
        echo $result 
;; 
#rdbinfo
aof_enabled)
        result=`$REDIS_INFO|/bin/grep -w "aof_enabled" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_rewrite_scheduled)
        result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_scheduled" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_last_rewrite_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "aof_last_rewrite_time_sec" | awk -F':' '{print $2}'`
        echo $result 
            ;; 
aof_current_rewrite_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "aof_current_rewrite_time_sec" | awk -F':' '{print $2}'`
        echo $result 
            ;; 
aof_last_bgrewrite_status)
        result=`$REDIS_INFO|/bin/grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
        echo $result 
;; 
#aofinfo
aof_current_size)
        result=`$REDIS_INFO|/bin/grep -w "aof_current_size" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_base_size)
        result=`$REDIS_INFO|/bin/grep -w "aof_base_size" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_pending_rewrite)
        result=`$REDIS_INFO|/bin/grep -w "aof_pending_rewrite" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_buffer_length)
        result=`$REDIS_INFO|/bin/grep -w "aof_buffer_length" | awk -F':' '{print $2}'`
        echo $result 
;; 
aof_rewrite_buffer_length)
        result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_buffer_length" | awk -F':' '{print $2}'`
        echo $result 
;;   
aof_pending_bio_fsync)
        result=`$REDIS_INFO|/bin/grep -w "aof_pending_bio_fsync" | awk -F':' '{print $2}'`
        echo $result 
;;
aof_delayed_fsync)
        result=`$REDIS_INFO|/bin/grep -w "aof_delayed_fsync" | awk -F':' '{print $2}'`
        echo $result 
;;                     
#stats
total_connections_received)
        result=`$REDIS_INFO|/bin/grep -w "total_connections_received" | awk -F':' '{print $2}'`
        echo $result 
;; 
total_commands_processed)
        result=`$REDIS_INFO|/bin/grep -w "total_commands_processed" | awk -F':' '{print $2}'`
        echo $result 
;; 
instantaneous_ops_per_sec)
        result=`$REDIS_INFO|/bin/grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
        echo $result 
;; 
rejected_connections)
        result=`$REDIS_INFO|/bin/grep -w "rejected_connections" | awk -F':' '{print $2}'` 
        echo $result 
;; 
expired_keys)
        result=`$REDIS_INFO|/bin/grep -w "expired_keys" | awk -F':' '{print $2}'`
        echo $result 
;; 
evicted_keys)
        result=`$REDIS_INFO|/bin/grep -w "evicted_keys" | awk -F':' '{print $2}'` 
        echo $result 
;; 
keyspace_hits)
        result=`$REDIS_INFO|/bin/grep -w "keyspace_hits" | awk -F':' '{print $2}'` 
        echo $result 
;; 
keyspace_misses)
        result=`$REDIS_INFO|/bin/grep -w "keyspace_misses" | awk -F':' '{print $2}'`
        echo $result 
;;
pubsub_channels)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
        echo $result 
;;
pubsub_channels)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
        echo $result 
;;
pubsub_patterns)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_patterns" | awk -F':' '{print $2}'`
        echo $result 
;;
latest_fork_usec)
        result=`$REDIS_INFO|/bin/grep -w "latest_fork_usec" | awk -F':' '{print $2}'`
        echo $result 
;;           
connected_slaves)
        result=`$REDIS_INFO|/bin/grep -w "connected_slaves" | awk -F':' '{print $2}'`
        echo $result 
;;
master_link_status)
        result=`$REDIS_INFO|/bin/grep -w "master_link_status"|awk -F':' '{print $2}'|/bin/grep -c up`
        echo $result 
;;
master_last_io_seconds_ago)
        result=`$REDIS_INFO|/bin/grep -w "master_last_io_seconds_ago"|awk -F':' '{print $2}'`
        echo $result 
;;
master_sync_in_progress)
        result=`$REDIS_INFO|/bin/grep -w "master_sync_in_progress"|awk -F':' '{print $2}'`
        echo $result 
;;
slave_priority)
        result=`$REDIS_INFO|/bin/grep -w "slave_priority"|awk -F':' '{print $2}'`
        echo $result 
;;
#cpu
used_cpu_sys)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys"|awk -F':' '{print $2}'`
        echo $result 
;;
used_cpu_user)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_user"|awk -F':' '{print $2}'`
        echo $result 
;;
used_cpu_sys_children)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys_children"|awk -F':' '{print $2}'`
        echo $result 
;;
used_cpu_user_children)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_user_children"|awk -F':' '{print $2}'`
        echo $result 
;;
*)
	echo "argu error"
;;
esac
#db0:key
   elif [[ $# == 2 ]];then
case $2 in
keys)
        result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "keys" | awk -F'=|,' '{print $2}'`
        echo $result 
;;
expires)
        result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "expires" | awk -F'=|,' '{print $4}'`
        echo $result 
;;
avg_ttl)
        result=`$REDIS_INFO|/bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
        echo $result 
;;
*)
     echo "argu error" ;;
esac
fi

赋予权限:chmod +x redis_zbx.sh

3,编辑agentd配置文件

#监控redis状态,我们可以根据这个参数对应的监控项创建redis状态触发器。
#redis monitor
UserParameter=redis.status,/usr/bin/redis-cli -h 127.0.0.1 -p 6379 ping |grep -c PONG 
UserParameter=redis_info[*],/home/yunwei/redis_zbx.sh $1 $2 

重启agentd服务,使之生效。

好了,我们到administraton-general-other刷新下被监控主机不支持的监控项,使之生效后测试看下效果(太多数据了,这里只举例几个):
内存使用情况:
2

redis status状态信息:
1

说明:
1,这个模板redis监控的参数比较多,具体根据自己的实际情况启用redis里边的应用集或者监控项,也可以自己再添加,。
2,默认aof未开启 以及slave这里都有监控,用不到的就停掉,这个模板。当前模板四个触发器,其中关于aof和主从的关了,只保留了redis存活状态和redis磁盘回写失败与否两个触发器。
更多
监控应用方面,请查看21运维网站zabbix相关教程。

转载请注明:21运维 » 使用Zabbix3.4监控工具创建 redis监控模板并实现redis监控

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (5)

  1. 重启了zabbix 还是有很多监控项显示不支持,而且单独执行redis_zbx.sh 也没有东西输出 ,+x输出“argu error”
    orange2018-05-22 14:36 回复
  2. 标签无效 "/zabbix_export/templates/template(1)/items/item(1)": 标签 "preprocessing" 异常
    alsmn2018-01-17 15:21 回复
  3. 您好我的 zabbix3.4导入您的模板失败
    koby2017-12-21 16:47 回复
    • 更新了下模板,可以重新测试下,原因是自定义的聚合图形放入模板导入有点问题。到时看下具体报错,导入模板的地方可以勾选导入选项。
      21运维2017-12-22 09:56 回复
  4. Zabbix 中国区合作伙伴OneOaaS团队 为企业级用户提供Zabbix监控架构设计,部署,培训服务 1.提供监控架构设计,包括高可用以及分布式部署 2.定制安装包,主要为windows,Linux, Suse 3.提供告警方式的对接,如邮箱,短信,微信,电话等 4.提供二次开发功能服务
    oneoaas2016-09-19 11:01 回复