Zabbix监控Redis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix监控Redis相关的知识,希望对你有一定的参考价值。
监控redis服务可以使用redis自带的客户端命令来连接redis的服务,redis-cli连接redis服务在使用info命令来查询redis状态信息,在通过脚本利用此命令并结合其他命令将数据抽取有用的值来完成对redis的可选监控,这里我使用的Linux系统是ubuntu的20.04的版本。
1、准备Redis服务
#redis服务器也是必须安装redis和zabbix agent的
root@Linux:~# wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1%2Bfocal_all.deb
root@Linux:~# dpkg -i zabbix-release_5.0-1+focal_all.deb
root@Linux:~# apt update
root@Linux:~# apt -y install zabbix-agent redis nmap ncat
root@Linux:~# ss -tnl | grep 6379
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:*
LISTEN 0 511 [::1]:6379 [::]:*
root@Linux:~# vi /etc/redis/redis.conf
bind 0.0.0.0
root@Linux:~# systemctl restart redis
root@Linux:~# systemctl status redis
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-12-05 19:27:19 CST; 8s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 16164 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 16176 (redis-server)
Tasks: 4 (limit: 4575)
Memory: 2.1M
CGroup: /system.slice/redis-server.service
└─16176 /usr/bin/redis-server 0.0.0.0:6379
Dec 05 19:27:19 Linux.stars.com systemd[1]: Starting Advanced key-value store...
Dec 05 19:27:19 Linux.stars.com systemd[1]: redis-server.service: Cant open PID file /run/redis/redis-server.pid (yet?) after start: Operation not permitted
Dec 05 19:27:19 Linux.stars.com systemd[1]: Started Advanced key-value store.
root@Linux:~# ss -tnl | grep 6379
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
2、编辑监控使用的脚本
#可以先用redis-cli命令查看redis info的信息
root@Linux:~# redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:66bd629f924ac924
redis_mode:standalone
os:Linux 5.4.0-128-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.0
process_id:16176
run_id:3f7fa8cc8868feadeebe68b47a891ed10ffa7b37
tcp_port:6379
uptime_in_seconds:327
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9295582
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# Memory
used_memory:859136
used_memory_human:839.00K
used_memory_rss:6025216
used_memory_rss_human:5.75M
used_memory_peak:859136
used_memory_peak_human:839.00K
used_memory_peak_perc:100.12%
used_memory_overhead:845910
used_memory_startup:796216
used_memory_dataset:13226
used_memory_dataset_perc:21.02%
allocator_allocated:1570968
allocator_active:1892352
allocator_resident:9105408
total_system_memory:4090576896
total_system_memory_human:3.81G
used_memory_lua:41984
used_memory_lua_human:41.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.20
allocator_frag_bytes:321384
allocator_rss_ratio:4.81
allocator_rss_bytes:7213056
rss_overhead_ratio:0.66
rss_overhead_bytes:-3080192
mem_fragmentation_ratio:7.37
mem_fragmentation_bytes:5208088
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.2.1
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1670239639
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
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:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:11468
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:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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:c18e66707cf32c54fdea753cf39e04b110d19a5f
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:0.356384
used_cpu_user:0.036170
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
127.0.0.1:6379>
#脚本里面可以把redis-cli命令info的信息存放在一个文件里,在对这个文件内的内容进行抽取,获得自己想要的监控的redis参数数据
root@Linux:~# vi redis_monitor.sh
root@Linux:~# cat redis_monitor.sh
#!/bin/bash
redis_status()
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO \\r\\n";) | ncat 127.0.0.1 "$R_PORT" > /tmp/redis_status.tmp
REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_status.tmp | cut -d ":" -f2)
echo $REDIS_STAT_VALUE
#这的help可以省略去掉的
help()
echo "$0 + redis_status + PORT + COMMAND"
#如果把前面的help函数内容去掉后,也要把*到esac前面的;;去掉
main()
case $1 in
redis_status)
redis_status $2 $3
;;
*)
help
;;
esac
main $1 $2 $3
root@Linux:~# chmod a+x redis_monitor.sh
root@Linux:~# mv redis_monitor.sh /etc/zabbix/zabbix_agentd.d/
root@Linux:~# cd /etc/zabbix/zabbix_agentd.d/
root@Linux:/etc/zabbix/zabbix_agentd.d# ./redis_monitor.sh redis_status 6379 used_memory
859136
root@Linux:/etc/zabbix/zabbix_agentd.d# ./redis_monitor.sh redis_status 6379 connected_clients
1
3、添加zabbix agent自定义的监控项
root@Linux:~# vim /etc/zabbix/zabbix_agentd.conf
root@Linux:~# grep -Ev "#|^$" /etc/zabbix/zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid
LogFile=/tmp/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.100,10.0.0.104
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=3
ServerActive=10.0.0.104
Hostname=10.0.0.102
Timeout=30
AllowRoot=1
User=root
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=linux_status[*],/etc/zabbix/zabbix_agentd.d/tcp_conn_plugin.sh "$1" "$2"
UserParameter=memcache_status[*],/etc/zabbix/zabbix_agentd.d/memcache_monitor.sh "$1" "$2" "$3"
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_monitor.sh "$1" "$2" "$3"
root@Linux:~# systemctl restart zabbix-agent
4、在zabbix server上测试是否可以获取到监控数据
root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "redis_status["redis_status","6379","used_memory"]"
859136
root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
5、制作Redis模板
5.1、创建自定义模板
5.2、创建自定义监控项
5.2.1、连接数监控项
5.2.2、使用内存监控项
5.3、创建自定义的触发器
5.3.1、连接数触发器
5.3.2、使用内存触发器
这里是需要将已使用的内存做单位转换,从G装换为字节,假设当内存使用达到2G时就会触发,换算公式如下:
2*1024*1024*1024=2147483648
5.4、创建展示图形
5.4.1、redis连接数图形
5.4.2、redis使用内存图形
6、关联主机并验证数据
主机之前监控Linux主机是就添加好了,这里我就不演示了。
以上是关于Zabbix监控Redis的主要内容,如果未能解决你的问题,请参考以下文章