zabbix监控redis状态

redis是我们常用的数据中间件,在目前大多项目中经常会使用到,所以使用zabbix对他进行监控还是非常又必须要的,我们这里来看看具体操作过程,以下操作过程目录约束为/usr/local/redis,admin123为我的redis认证密码

获取redis状态和性能有一个命令可以直接获取

/usr/local/redis/src/redis-cli -a admin123 info

返回

# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c311aecb128c60c7
redis_mode:standalone
os:Linux 3.10.0-1062.4.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:8464
run_id:1a4ccec5e9afabc0795c33706a6b640eb7f24d36
tcp_port:6379
uptime_in_seconds:240688
uptime_in_days:2
hz:10
configured_hz:10
lru_clock:12985805
executable:/usr/local/redis/src/redis-server
config_file:/usr/local/redis/redis.conf

# Clients
connected_clients:3
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:897792
used_memory_human:876.75K
used_memory_rss:2420736
used_memory_rss_human:2.31M
used_memory_peak:958824
used_memory_peak_human:936.35K
used_memory_peak_perc:93.63%
used_memory_overhead:875178
used_memory_startup:791408
used_memory_dataset:22614
used_memory_dataset_perc:21.26%
allocator_allocated:908448
allocator_active:1200128
allocator_resident:3637248
total_system_memory:3973505024
total_system_memory_human:3.70G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.32
allocator_frag_bytes:291680
allocator_rss_ratio:3.03
allocator_rss_bytes:2437120
rss_overhead_ratio:0.67
rss_overhead_bytes:-1216512
mem_fragmentation_ratio:2.90
mem_fragmentation_bytes:1586088
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:83538
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:9
rdb_bgsave_in_progress:0
rdb_last_save_time:1573047413
rdb_last_bgsave_status:err
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:327680
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
aof_last_cow_size:0

# Stats
total_connections_received:85
total_commands_processed:29329
instantaneous_ops_per_sec:0
total_net_input_bytes:15500685
total_net_output_bytes:15831330
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:1
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:234
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:9517449c3c3cb4612c6a2f1ba8c70be50ace5cec
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:140.940998
used_cpu_user:167.580463
used_cpu_sys_children:6.004816
used_cpu_user_children:10.418545

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=5,expires=0,avg_ttl=0

如果是单纯的获取redis是否在线可以使用

/usr/local/redis/src/redis-cli -a admin123 ping

了解了以上的一些基础命令,我们就要开始配置zabbix-agent和数据收集脚本了

主要添加/etc/zabbix/zabbix_agentd.d/redis_status.conf,内容如下

UserParameter=redis.ping,/usr/local/redis/src/redis-cli -a admin123 ping 2>&1 | grep -c PONG
UserParameter=redis.status[*],/etc/zabbix/script/resdis_status.sh $1 $2

别忘记如果需要使用自定义的参数还需要修改/etc/zabbix/zabbix_agentd.conf文件

UnsafeUserParameters=1

然后我们开始编写脚本

#vi /etc/zabbix/script/resdis_status.sh
#! /bin/bash

redis_cli="/usr/local/redis/src/redis-cli" 
redis_host="127.26.61.61"
redis_port=6379
if [[ $# == 1 ]];then
    case $1 in
        version)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "redis_version" | awk -F':' '{print $2}'`
            echo $result
        ;;
        uptime)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
            echo $result
        ;;
        connected_clients)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        rdb_last_bgsave_status)
            result=`$redis_cli -h $redis_host -p $redis_port info  | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_bgrewrite_status)
            result=`$redis_cli -h $redis_host -p $redis_port info  | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        aof_last_write_status)
            result=`$redis_cli -h $redis_host -p $redis_port info  | grep -w "aof_last_write_status" | awk -F':' '{print $2}' | grep -c ok`
            echo $result
        ;;
        *)
            echo -e "Usage: $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}" 
        ;;
    esac
elif [[ $# == 2 ]];then
    case $2 in
        keys)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl)
            result=`$redis_cli -h $redis_host -p $redis_port info | grep -w "$1" | grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
            echo $result
        ;;
        *)
            echo -e "Usage: $0 {db0 keys|db0 expires|db0 avg_ttl}" 
        ;;
    esac
fi

以上脚本编写好了以后,我们可以使用zabbix_get进行测试下

zabbix_get -s 127.26.61.61 -k redis.ping
#返回
1

返回1说明就是redis状态时在线

最后就是zabbix监控模板

redis.xml


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/758.html