zabbix监控redis

百度“zabbix监控redis”有说官方有redis的监控脚本和模版,

https://github.com/adubkov/zbx_redis_template

下面为自己手动写脚本添加

将redis监控项都放在一个模版里面

一、监控redis的6379端口

使用自带的监控项:net.tcp.port[127.0.0.1,6379]

二、使用下面这个命令监控redis的状态

root@zabbix:/opt/zabbix# redis-cli -h 127.0.0.1 -p 6379 info
# Server
redis_version:3.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:28b6715d3583bf8e
redis_mode:standalone
os:Linux 4.4.0-140-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:5.4.0
process_id:6309
run_id:03bb7cc863e58c26856565ab603ba8e1200f4adc
tcp_port:6379
uptime_in_seconds:1610
uptime_in_days:0
hz:10
lru_clock:2126654
config_file:/etc/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:508976
used_memory_human:497.05K
used_memory_rss:3112960
used_memory_peak:508976
used_memory_peak_human:497.05K
used_memory_lua:36864
mem_fragmentation_ratio:6.12
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1545628916
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:11
total_commands_processed:10
instantaneous_ops_per_sec:0
total_net_input_bytes:184
total_net_output_bytes:13917
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
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
used_cpu_sys:1.46
used_cpu_user:0.48
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace

三、zabbix自定义监控项

root@zabbix:~# cat /etc/zabbix/zabbix_agentd.d/userparameter_redis.conf 
UserParameter=redis[*],/opt/zabbix/zabbix_redis.sh $1

四、脚本

懒得写了,使用下面这个大佬的:

https://www.cnblogs.com/qinyujie/p/8567355.html

root@zabbix:~# chmod +x /opt/zabbix/zabbix_redis.sh 
root@zabbix:~# cat /opt/zabbix/zabbix_redis.sh 
#!/bin/bash
REDISPATH="/usr/bin/redis-cli"
HOST="127.0.0.1"
PORT="6379"
REDIS_INFO="$REDISPATH -h $HOST -p $PORT -a xxx info"   ##-a 后面跟的是redis密码
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

五、手动测试,没毛病

root@zabbix:~# systemctl restart zabbix-agent.service 
root@zabbix:~# zabbix_get -s 127.0.0.1 -p 10050 -k redis[used_cpu_sys]
4.14
root@zabbix:~# zabbix_get -s 127.0.0.1 -p 10050 -k redis[used_cpu_user]
1.27
root@zabbix:~# zabbix_get -s 127.0.0.1 -p 10050 -k redis[used_memory]
508976

六、web界面添加监控项,放到一个模版里面

随便加了几个

zabbix监控redis_第1张图片

触发器:

当redis的6379检测不到就报警

当redis使用内存大于1048576字节(1MB)就报警,

七、关联模版到对应的agent客户端

八、停止redis服务测试

root@zabbix:~# date;systemctl stop redis-server
2018年 12月 24日 星期一 15:45:06 CST

报警了:

zabbix监控redis_第2张图片

没问题

内存不足的报警:

zabbix监控redis_第3张图片

九、测试那个所谓的官网模版,用python写的

可以用,很强

127.0.0.1:主机

used_cpu_sys:要监控的东西。可以通过redis info命令出来的哪些选项参数

none:脚本里面写的是db选项的内容,默认为none

root@zabbix:~# wget https://raw.githubusercontent.com/adubkov/zbx_redis_template/master/zbx_redis_stats.pyhttps://raw.githubusercontent.com/adubkov/zbx_redis_template/master/zbx_redis_stats.py -O /opt/zabbix/zbx_redis_stats.py
--2018-12-24 16:27:46--  https://raw.githubusercontent.com/adubkov/zbx_redis_template/master/zbx_redis_stats.pyhttps://raw.githubusercontent.com/adubkov/zbx_redis_template/master/zbx_redis_stats.py
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.108.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.108.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 4925 (4.8K) [text/plain]
正在保存至: “/opt/zabbix/zbx_redis_stats.py”

/opt/zabbix/zbx_redis_stats.py    100%[==========================================================>]   4.81K  25.0KB/s    in 0.2s    

2018-12-24 16:27:48 (25.0 KB/s) - 已保存 “/opt/zabbix/zbx_redis_stats.py” [4925/4925])
root@zabbix:/opt/zabbix# python3 zbx_redis_stats.py 127.0.0.1 used_cpu_sys none
2.34
root@zabbix:/opt/zabbix# python3 zbx_redis_stats.py 127.0.0.1 used_memory none
508240

 

 

你可能感兴趣的:(ubuntu,nginx,zabbix,redis)