• Zabbix监控Redis状态


    监控redis通过info信息来监控redis的状态
    自动发现脚本编写

    cat /etc/zabbix/scripts/redis_parameter #自动发现redis脚本参数的脚本

    #!/bin/bash
    /usr/local/bin/redis-cli -p 3000 -a redis66cs "info">/home/zabbix/tmp/info.txt
    cat /tmp/info.txt |grep -v "^#"|awk -F ":" {'print $1'}|grep -v [[:space:]] >/home/zabbix/tmp/info_parameter
    Redis_parameter_discovery () {
    Redis_Site=`cat /home/zabbix/tmp/info_parameter`
    Redis_Site_Num=`echo ${Redis_Site}|awk {'print NF'}`
    Redis_Site_parameter=($Redis_Site)
    printf '{ '
    printf ' "data":[ '
    for((i=0;i<${Redis_Site_Num};i++))
    {
    if [ `expr $i + 1 ` != ${Redis_Site_Num} ];then
    printf " {"{#SITENAME}":"${Redis_Site_parameter[$i]}"}, "
    else
    printf " {"{#SITENAME}":"${Redis_Site_parameter[$i]}"} "
    fi
    }
    printf ' ] '
    printf '} '
    }
    case "$1" in
    redis_parameter_discovery)
    Redis_parameter_discovery
    ;;
    *)
    echo "Usage:$0 redis_parameter_discovery"
    ;;
    esac
    创建上面脚本对应的目录和文件:

    #mkdir /home/zabbix/tmp
    #touch /home/zabbix/tmp/info.txt
    #touch /home/zabbix/tmp/info_parameter
    #chown -R zabbix:zabbix /home/zabbix
    #chmod 777 /home/zabbix/tmp/{info.txt,info_parameter}
    编写redis的info取值的脚本

    cat /etc/zabbix/scripts/redis_info.sh #这个脚本就是根据自动发现传过来的info的参数名进行grep取值

    #!/bin/bash
    /usr/local/bin/redis-cli -p 3000 -a redis66cs "info">/home/zabbix/tmp/info.txt
    Redis_Site=`cat /home/zabbix/tmp/info_parameter`
    Redis_info_value=$(grep "$1": /home/zabbix/tmp/info.txt|awk -F ":" {'print $NF'})
    echo $Redis_info_value
    注意这里一定grep 一定要是"$1":这种以:结尾,比如:used_memory:和 used_memory_human:,会精确匹配而非模糊匹配

    编写自定义的key:

    cat /etc/zabbix/zabbix_agentd.conf.d/redis_parameter_discovery.conf

    脚本中的redis.parameter.discovery就是我们自定义的key

    UserParameter=redis.parameter.discovery,/etc/zabbix/scripts/redis_parameter redis_parameter_discovery
    redis.parameter.discovery这个key的作用就是调用redis_parameter这个脚本,将redis的所有info的参数名提取出来。

    cat /etc/zabbix/zabbix_agentd.conf.d/redis_info_value.conf
    通过上面自动发现提取出来的参数名作为$1传递给下面的脚本

    UserParameter=redis.info.value[*],/etc/zabbix/scripts/redis_info.sh $1
    给对应的脚本授权

    #chown -R zabbix:zabbix /etc/zabbix/*
    #chmod +x zabbix:zabbix /etc/zabbix/scripts/*
    重启客户端服务

    #/etc/init.d/zabbix/zabbix_agentd restart
    zabbix_proxy代理的操作

    zabbix_get -s 192.168.14.56 -k redis.parameter.discovery #测试redis的自动发现info参数的脚本
    {
    "data":[
    {"{#SITENAME}":"redis_version"},
    {"{#SITENAME}":"redis_git_sha1"},
    {"{#SITENAME}":"redis_git_dirty"},
    {"{#SITENAME}":"redis_build_id"},
    {"{#SITENAME}":"redis_mode"},
    {"{#SITENAME}":"os"},
    {"{#SITENAME}":"arch_bits"},
    {"{#SITENAME}":"multiplexing_api"},
    {"{#SITENAME}":"gcc_version"},
    {"{#SITENAME}":"process_id"},
    {"{#SITENAME}":"run_id"},
    {"{#SITENAME}":"tcp_port"},
    {"{#SITENAME}":"uptime_in_seconds"},
    {"{#SITENAME}":"uptime_in_days"},
    {"{#SITENAME}":"hz"},
    {"{#SITENAME}":"lru_clock"},
    {"{#SITENAME}":"executable"},
    {"{#SITENAME}":"config_file"},
    {"{#SITENAME}":"connected_clients"},
    {"{#SITENAME}":"client_longest_output_list"},
    {"{#SITENAME}":"client_biggest_input_buf"},
    {"{#SITENAME}":"blocked_clients"},
    {"{#SITENAME}":"used_memory"},
    {"{#SITENAME}":"used_memory_human"},
    {"{#SITENAME}":"used_memory_rss"},
    {"{#SITENAME}":"used_memory_rss_human"},
    {"{#SITENAME}":"used_memory_peak"},
    {"{#SITENAME}":"used_memory_peak_human"},
    {"{#SITENAME}":"total_system_memory"},
    {"{#SITENAME}":"total_system_memory_human"},
    {"{#SITENAME}":"used_memory_lua"},
    {"{#SITENAME}":"used_memory_lua_human"},
    {"{#SITENAME}":"maxmemory"},
    {"{#SITENAME}":"maxmemory_human"},
    {"{#SITENAME}":"maxmemory_policy"},
    {"{#SITENAME}":"mem_fragmentation_ratio"},
    {"{#SITENAME}":"mem_allocator"},
    {"{#SITENAME}":"loading"},
    {"{#SITENAME}":"rdb_changes_since_last_save"},
    {"{#SITENAME}":"rdb_bgsave_in_progress"},
    {"{#SITENAME}":"rdb_last_save_time"},
    {"{#SITENAME}":"rdb_last_bgsave_status"},
    {"{#SITENAME}":"rdb_last_bgsave_time_sec"},
    {"{#SITENAME}":"rdb_current_bgsave_time_sec"},
    {"{#SITENAME}":"aof_enabled"},
    {"{#SITENAME}":"aof_rewrite_in_progress"},
    {"{#SITENAME}":"aof_rewrite_scheduled"},
    {"{#SITENAME}":"aof_last_rewrite_time_sec"},
    {"{#SITENAME}":"aof_current_rewrite_time_sec"},
    {"{#SITENAME}":"aof_last_bgrewrite_status"},
    {"{#SITENAME}":"aof_last_write_status"},
    {"{#SITENAME}":"total_connections_received"},
    {"{#SITENAME}":"total_commands_processed"},
    {"{#SITENAME}":"instantaneous_ops_per_sec"},
    {"{#SITENAME}":"total_net_input_bytes"},
    {"{#SITENAME}":"total_net_output_bytes"},
    {"{#SITENAME}":"instantaneous_input_kbps"},
    {"{#SITENAME}":"instantaneous_output_kbps"},
    {"{#SITENAME}":"rejected_connections"},
    {"{#SITENAME}":"sync_full"},
    {"{#SITENAME}":"sync_partial_ok"},
    {"{#SITENAME}":"sync_partial_err"},
    {"{#SITENAME}":"expired_keys"},
    {"{#SITENAME}":"evicted_keys"},
    {"{#SITENAME}":"keyspace_hits"},
    {"{#SITENAME}":"keyspace_misses"},
    {"{#SITENAME}":"pubsub_channels"},
    {"{#SITENAME}":"pubsub_patterns"},
    {"{#SITENAME}":"latest_fork_usec"},
    {"{#SITENAME}":"migrate_cached_sockets"},
    {"{#SITENAME}":"role"},
    {"{#SITENAME}":"connected_slaves"},
    {"{#SITENAME}":"master_repl_offset"},
    {"{#SITENAME}":"repl_backlog_active"},
    {"{#SITENAME}":"repl_backlog_size"},
    {"{#SITENAME}":"repl_backlog_first_byte_offset"},
    {"{#SITENAME}":"repl_backlog_histlen"},
    {"{#SITENAME}":"used_cpu_sys"},
    {"{#SITENAME}":"used_cpu_user"},
    {"{#SITENAME}":"used_cpu_sys_children"},
    {"{#SITENAME}":"used_cpu_user_children"},
    {"{#SITENAME}":"cluster_enabled"}
    ]
    }
    redis的Info信息总共有82个参数,用上面的自定义key已经都取出来了。

    /usr/local/zabbix/bin/zabbix_get -s 192.168.14.56 -k redis.info.value[used_memory] #测试redis.info.value传参也可以
    821512
    创建redis模板

    1.创建模板


    2.创建应用集


    3.添加自动发现规则


    选择主机添加模板

    上面已经将一个简单的Redis模板创建完成了,现在找一个主机进行添加吧。


    点击此主机的自动发现,如果最后边的状态有红色的叹号,说明哪里会有问题,鼠标移动到叹号哪里也会有提示


    点击此主机的监控项选择Redis应用集,可以看到我们设置的自动发现规则里面的监控项原型生效了。一共两页82行。

    检查是否有数据

    查看最新数据,选择主机以及应用集,查看是否能采集到数据。


    从上图可以看到我们已经可以对redis的info信息的每一项参数进行数据采集了.

  • 相关阅读:
    将博客搬至CSDN
    NOIP2018酱油记
    CF 1039D You Are Given a Tree && CF1059E Split the Tree 的贪心解法
    最大异或子序列问题
    UVa 10615
    UVa 1057
    用树状数组代替平衡树
    [CTSC2008]图腾totem
    POI2008 题解
    简便思路的题目别人的做法
  • 原文地址:https://www.cnblogs.com/guarderming/p/13238023.html
Copyright © 2020-2023  润新知