Bootstrap

实战案例:Zabbix对Redis的监控

Zabbix对Redis的监控的思路:Redis-cli 提供了查询状态的命令info,通过脚本调用此命令并进行数据抽取完成对Redis的可选监控。

基本步骤: 自定义监控项 → 通过脚本采集监控项数据 → zabbix agent获取监控项数据 → 自定义模板和图形及触发器 → 验证数据

1. Redis服务器的准备

#### Redis服务器必须安装并配置好Redis,还需要安装zabbix agent
## 配置zabbix 的yum源并安装zabbix agent
[root@CentOS84-IP88 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm

[root@CentOS84-IP88 ]#dnf clean all
43 files removed
[root@CentOS84-IP88 ]#dnf -y install zabbix-agent 

########################################################################
## 安装redis及抽取数据用的 nmap
[root@CentOS84-IP88 ]#yum -y install redis nmap

[root@CentOS84-IP88 ]#yum info redis
.......................
Name         : redis
Version      : 5.0.3
.......................

[root@CentOS84-IP88 ]#yum -y install redis

[root@CentOS84-IP88 ]#systemctl enable --now redis

[root@CentOS84-IP88 ]#ss -tln | grep 6379
LISTEN 0      511        127.0.0.1:6379       0.0.0.0:*          
[root@CentOS84-IP88 ]#

[root@CentOS84-IP88 ]#find / -name redis.conf
/etc/redis.conf
[root@CentOS84-IP88 ]#vim /etc/redis.conf
..................
bind 0.0.0.0 
..................

[root@CentOS84-IP88 ]#systemctl restart redis     
[root@CentOS84-IP88 ]#ss -tln | grep 6379    
LISTEN 0      511          0.0.0.0:6379       0.0.0.0:*          
[root@CentOS84-IP88 ]#

2. 准备监控脚本

########################################################################
## redis-cli命令方式查看redis info信息
[root@CentOS84-IP88 ]#redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9529b692c0384fb7
redis_mode:standalone
os:Linux 4.18.0-305.3.1.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.4.1
process_id:264852
run_id:0274acd335810ba7212c90f2bcb1ba84a0dbbfcd
tcp_port:6379
uptime_in_seconds:711
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9105971
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

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

# Memory
used_memory:853888
used_memory_human:833.88K
used_memory_rss:17264640
used_memory_rss_human:16.46M
used_memory_peak:853896
used_memory_peak_human:833.88K
used_memory_peak_perc:100.00%
used_memory_overhead:840686
used_memory_startup:790992
used_memory_dataset:13202
used_memory_dataset_perc:20.99%
allocator_allocated:1458584
allocator_active:1822720
allocator_resident:11051008
total_system_memory:3917111296
total_system_memory_human:3.65G
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.25
allocator_frag_bytes:364136
allocator_rss_ratio:6.06
allocator_rss_bytes:9228288
rss_overhead_ratio:1.56
rss_overhead_bytes:6213632
mem_fragmentation_ratio:21.26
mem_fragmentation_bytes:16452752
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.1.0
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:1653272428
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:2
total_commands_processed:2
instantaneous_ops_per_sec:0
total_net_input_bytes:228
total_net_output_bytes:15347
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:34753de3b1fb378615282384de595af96794edaa
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.400871
used_cpu_user:0.442322
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> 


[root@CentOS84-IP88 ]#cd /etc/zabbix/zabbix_agentd.d/
#### 脚本大概思路是将 redis-cli 命令info信息保存到一个文件,并对这个文件内的内容进行抽取,获得想要的监控的redis参数数据
[root@CentOS84-IP88 ]#vim 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_"$R_PORT".tmp
        REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2)
        echo $REDIS_STAT_VALUE  
}

help(){
        echo "${0} + redis_status + PORT + COMMAND"
}

main(){
    case $1 in
        redis_status)
            redis_status $2 $3
                ;;
        *)
            help
                ;;
        esac
}
main $1 $2 $3

#### 授予运行权限
[root@CentOS84-IP88 ]#chmod a+x redis_monitor.sh 
[root@CentOS84-IP88 ]#ll
total 8
-rwxr-xr-x 1 root root 514 May 23 10:25 redis_monitor.sh
[root@CentOS84-IP88 ]#

#### 测试脚本抽取数据是否成功,和redis-cli下的info获取的信息比对
[root@CentOS84-IP88 ]#bash redis_monitor.sh redis_status 6379 used_memory
853896
[root@CentOS84-IP88 ]#

3. 添加zabbix agent自定义监控项

#### 前面已经安装好zabbix_agent,修改并配置好zabbix_agent
## 抽取出的yum方式默认安装zabbix_agent的配置行

[root@CentOS84-IP88 ]#vim /etc/zabbix/zabbix_agentd.conf
......................
# UserParameter=
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_monitor.sh "$1" "$2" "$3"                                               
......................

[root@CentOS84-IP88 ]#
[root@CentOS84-IP88 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.250.18
ListenPort=10050
ServerActive=127.0.0.1
Hostname=192.168.250.88
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_monitor.sh "$1" "$2" "$3"
[root@CentOS84-IP88 ]#

[root@CentOS84-IP88 ]#systemctl restart zabbix-agent

## 验证配置
[root@CentOS84-IP88 ]#ss -tln
State          Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port         Process         
LISTEN         0               511                            0.0.0.0:6379                         0.0.0.0:*                            
LISTEN         0               1024                           0.0.0.0:11211                        0.0.0.0:*                            
LISTEN         0               128                            0.0.0.0:111                          0.0.0.0:*                            
LISTEN         0               128                            0.0.0.0:22                           0.0.0.0:*                            
LISTEN         0               5                            127.0.0.1:631                          0.0.0.0:*                            
LISTEN         0               128                            0.0.0.0:10050                        0.0.0.0:*                            
LISTEN         0               1024                              [::]:11211                           [::]:*                            
LISTEN         0               128                               [::]:111                             [::]:*                            
LISTEN         0               128                               [::]:22                              [::]:*                            
LISTEN         0               5                                [::1]:631                             [::]:*                            
LISTEN         0               128                               [::]:10050                           [::]:*                            
[root@CentOS84-IP88 ]#

4. zabbix server测试监控项数据

## 安装 zabbix-get 工具
[root@CentOS84-IP18 ]#yum install zabbix-get.x86_64
[root@CentOS84-IP18 ]#find / -name zabbix_get
/usr/bin/zabbix_get
[root@CentOS84-IP18 ]#
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.88 -p 10050 -k "redis_status["redis_status","6379","used_memory"]"
/etc/zabbix/zabbix_agentd.d/redis_monitor.sh: line 5: /tmp/redis_6379.tmp: Permission denied
853896

#### 出现上面的错误是因为在IP88 redis主机上已经运行过redis,用root账号创建了  /tmp/redis_6379.tmp 这个文件,需要修改成权属授权zabbix能修改
####################################################################################
[root@CentOS84-IP88 ]#ll /tmp/redis_6379.tmp
-rw-r--r-- 1 root root 3263 May 23 10:27 /tmp/redis_6379.tmp
[root@CentOS84-IP88 ]#chown zabbix.zabbix /tmp/redis_6379.tmp
[root@CentOS84-IP88 ]#ll /tmp/redis_6379.tmp
-rw-r--r-- 1 zabbix zabbix 3275 May 23 11:50 /tmp/redis_6379.tmp
[root@CentOS84-IP88 ]#
####################################################################################

[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.88 -p 10050 -k "redis_status["redis_status","6379","used_memory"]"
853896
[root@CentOS84-IP18 ]#
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.88 -p 10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
[root@CentOS84-IP18 ]#

####  从zabbix服务器测试性访问 redis主机,已经完成正常了,就可以去zabbix服务器端进行模板创建和图形展示了。


如果本文对你有帮助,别忘记给我个3连 ,点赞,转发,评论,
咱们下期见。

;