Zabbix监控Redis

??监控redis服务可以使用redis自带的客户端命令来连接redis的服务,redis-cli连接redis服务在使用info命令来查询redis状态信息,在通过脚本利用此命令并结合其他命令将数据抽取有用的值来完成对redis的可选监控,这里我使用的Linux系统是ubuntu的20.04的版本,redis服务我也安装Linux主机上了,监控的话也就在zabbix web界面上添加自定义的模板就好了。

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.debroot@Linux:~# dpkg -i zabbix-release_5.0-1+focal_all.debroot@Linux:~# apt updateroot@Linux:~# apt -y install zabbix-agent redis nmap ncatroot@Linux:~# ss -tnl | grep 6379LISTEN 0 511 127.0.0.1:6379 0.0.0.0:* LISTEN 0 511 [::1]:6379 [::]:*root@Linux:~# vi /etc/redis/redis.confbind 0.0.0.0root@Linux:~# systemctl restart redisroot@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:6379Dec 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: Can’t open PID file /run/redis/redis-server.pid (yet?) after start: Operation not permittedDec 05 19:27:19 Linux.stars.com systemd[1]: Started Advanced key-value store.root@Linux:~# ss -tnl | grep 6379LISTEN 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# Serverredis_version:5.0.7redis_git_sha1:00000000redis_git_dirty:0redis_build_id:66bd629f924ac924redis_mode:standaloneos:Linux 5.4.0-128-generic x86_64arch_bits:64multiplexing_api:epollatomicvar_api:atomic-builtingcc_version:9.3.0process_id:16176run_id:3f7fa8cc8868feadeebe68b47a891ed10ffa7b37tcp_port:6379uptime_in_seconds:327uptime_in_days:0hz:10configured_hz:10lru_clock:9295582executable:/usr/bin/redis-serverconfig_file:/etc/redis/redis.conf# Clientsconnected_clients:1client_recent_max_input_buffer:2client_recent_max_output_buffer:0blocked_clients:0# Memoryused_memory:859136used_memory_human:839.00Kused_memory_rss:6025216used_memory_rss_human:5.75Mused_memory_peak:859136used_memory_peak_human:839.00Kused_memory_peak_perc:100.12%used_memory_overhead:845910used_memory_startup:796216used_memory_dataset:13226used_memory_dataset_perc:21.02%allocator_allocated:1570968allocator_active:1892352allocator_resident:9105408total_system_memory:4090576896total_system_memory_human:3.81Gused_memory_lua:41984used_memory_lua_human:41.00Kused_memory_scripts:0used_memory_scripts_human:0Bnumber_of_cached_scripts:0maxmemory:0maxmemory_human:0Bmaxmemory_policy:noevictionallocator_frag_ratio:1.20allocator_frag_bytes:321384allocator_rss_ratio:4.81allocator_rss_bytes:7213056rss_overhead_ratio:0.66rss_overhead_bytes:-3080192mem_fragmentation_ratio:7.37mem_fragmentation_bytes:5208088mem_not_counted_for_evict:0mem_replication_backlog:0mem_clients_slaves:0mem_clients_normal:49694mem_aof_buffer:0mem_allocator:jemalloc-5.2.1active_defrag_running:0lazyfree_pending_objects:0# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1670239639rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:-1rdb_current_bgsave_time_sec:-1rdb_last_cow_size:0aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:okaof_last_cow_size:0# Statstotal_connections_received:1total_commands_processed:1instantaneous_ops_per_sec:0total_net_input_bytes:31total_net_output_bytes:11468instantaneous_input_kbps:0.00instantaneous_output_kbps:0.00rejected_connections:0sync_full:0sync_partial_ok:0sync_partial_err:0expired_keys:0expired_stale_perc:0.00expired_time_cap_reached_count:0evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:0pubsub_patterns:0latest_fork_usec:0migrate_cached_sockets:0slave_expires_tracked_keys:0active_defrag_hits:0active_defrag_misses:0active_defrag_key_hits:0active_defrag_key_misses:0# Replicationrole:masterconnected_slaves:0master_replid:c18e66707cf32c54fdea753cf39e04b110d19a5fmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0# CPUused_cpu_sys:0.356384used_cpu_user:0.036170used_cpu_sys_children:0.000000used_cpu_user_children:0.000000# Clustercluster_enabled:0# Keyspace127.0.0.1:6379> #脚本里面可以把redis-cli命令info的信息存放在一个文件里,在对这个文件内的内容进行抽取,获得自己想要的监控的redis参数数据root@Linux:~# vi redis_monitor.sh root@Linux:~# cat redis_monitor.sh#!/bin/bashredis_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 $3root@Linux:~# chmod a+x redis_monitor.shroot@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_memory859136root@Linux:/etc/zabbix/zabbix_agentd.d# ./redis_monitor.sh redis_status 6379 connected_clients13、添加zabbix agent自定义的监控项root@Linux:~# vim /etc/zabbix/zabbix_agentd.confroot@Linux:~# grep -Ev "#|^$" /etc/zabbix/zabbix_agentd.confPidFile=/tmp/zabbix_agentd.pidLogFile=/tmp/zabbix_agentd.logLogFileSize=0Server=10.0.0.100,10.0.0.104ListenPort=10050ListenIP=0.0.0.0StartAgents=3ServerActive=10.0.0.104Hostname=10.0.0.102Timeout=30AllowRoot=1User=rootInclude=/etc/zabbix/zabbix_agentd.d/*.confUserParameter=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-agent4、在zabbix server上测试是否可以获取到监控数据root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "redis_status["redis_status","6379","used_memory"]"859136root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "redis_status["redis_status","6379","connected_clients"]"15、制作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

相关文章:

你感兴趣的文章:

标签云: