我用 Linux程序员程序猿阵线联盟-汇总各类技术干货

Zabbix监控redis

2018-06-28  本文已影响72人  十二楼中月

1. Redis监控(shell脚本)

UserParameter=Redis.Info[*],/home/zkml/redismonitor.sh $1 $2 通过脚本监控信息 
UserParameter=Redis.Status,(/home/zkml/redis-3.2.9/src/redis-cli -h 192.168.199.95 -p 6379 -a 密码 ping)2>/dev/null |grep -c PONG 
通过命令检查redis状态,使用错误重定向(2>/dev/null)是防止报错导致监控项失效无法告警

增加时候需要特别注意: redismonitor.sh 放置的位置zabbix需要有权限执行. chmod +x redismonitor.sh 另外放置的文件夹也要有权限,否则是无监控信息的如果redis需要密码验证还需要设置 -a 密码
Zabbix-server端验证使用zabbix-get:
/usr/local/zabbix-3.0.3/bin/zabbix_get -s 192.168.199.95 -p10041 –k Redis.Status(ip是客户端的,端口zabbix-agent)

2. 增加监控项

# Server 服务器信息
redis_version:3.2.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9c93f7fea0328bce
redis_mode:standalone
os:Linux 2.6.32-431.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:30924
run_id:6e22d7abd8087f557c21dec43499c0c38a8a2749
tcp_port:6379
uptime_in_seconds:88
uptime_in_days:0
hz:10
lru_clock:925063
executable:/usr/local/redis/bin/./redis-server
config_file:

# Clients 客户端连接信息
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory 内存使用信息
used_memory:881336
used_memory_human:860.68K
used_memory_rss:2351104
used_memory_rss_human:2.24M
used_memory_peak:881336
used_memory_peak_human:860.68K
total_system_memory:4017008640
total_system_memory_human:3.74G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.67
mem_allocator:libc

# Persistence 持续
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1527651631
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats 状态信息
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:2
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication 集群信息
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU #CPU使用信息
used_cpu_sys:0.08
used_cpu_user:0.01
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster 是否使用集群
cluster_enabled:0

# Keyspace

根据以上信息使用shell命令可以获取自己想要检测的信息。

3. 脚本编写

主要命令:redis-cli -h IP -p 端口 -a 密码 info |grep xxx |awk xxx
下面的脚本是没有密码的如果redis有验证需要添加

#! /bin/bash
#Name: redismontior.sh
#From: zhangyl@izkml.com 2018.5.29
#Action: Zabbix monitoring redis plug-in
#根据自己的安装情况填写命令位置主机ip和端口
REDISCLI="/home/zkml/redis-3.2.9/src/redis-cli" 
HOST="192.168.199.95"
PORT=6379
根据参数信息获取监控信息
if [[ $# == 1 ]];then
    case $1 in
        version)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "redis_version" | awk -F':' '{print $2}'`
            echo $result
        ;;
        uptime)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
            echo $result
        ;;
        connected_clients)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        rdb_last_bgsave_status)
            result=`$REDISCLI -h $HOST -p $PORT info  | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_bgrewrite_status)
            result=`$REDISCLI -h $HOST -p $PORT info  | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_write_status)
            result=`$REDISCLI -h $HOST -p $PORT info  | 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
此处表示当有两个参数时使用,主要用来查询数据库的keys信息等
elif [[ $# == 2 ]];then
    case $2 in
        keys)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires)
            result=`$REDISCLI -h $HOST -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl)
            result=`$REDISCLI -h $HOST -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

4. 监控模版

在web界面导入模版:redis模版

导入模版
模版
此模版一共提供了19个监控项,其中有三个关于DbKey的检查可能无效.

Redis监控(Python脚本)

在zabbix官方给出一个python监控脚本,需要在监控端使用Python脚本。
监控的原理图:

原理图
Zabbix-server通过agent的配置文件不断读取文件,此文件有Python脚本每隔一段时间运行生成.文件内容是监控项的值.此处类似于percona监控mysql的方式类似.percona使用的php的脚本.这里使用的是Python脚本.
python监控redis
上一篇下一篇

猜你喜欢

热点阅读