Zabbix--Zabbix-Redis状态监控
Posted IT科技 不拘一格
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix--Zabbix-Redis状态监控相关的知识,希望对你有一定的参考价值。
续:
推荐阅读:
安装 redis服务
通过自定义监控模板对memcache进行监控
为了测试效果,在主动模式下,因为主动模式获取数据比较快一些
环境:在这里复用mysql-slave的主机
安装 redis
root@Mysql-slave:~# apt install redis -y
root@Mysql-slave:~# vim /etc/redis/redis.conf
root@Mysql-slave:~# grep ^[a-Z] /etc/redis/redis.conf
bind 0.0.0.0 #修改监听地址为0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
root@Mysql-slave:~# systemctl restart redis-server
root@Mysql-slave:~# systemctl enable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable redis-server
root@Mysql-slave:~# ss -ntl |grep 6379
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
root@Mysql-slave:~# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key1 vaule1
OK
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379>
编写监控脚本
脚本编写思路
root@Mysql-slave:~# echo -en "INFO
" | ncat 127.0.0.1 6379
$2759
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9435c3c2879311f3
redis_mode:standalone
os:Linux 4.15.0-29-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:7.4.0
process_id:59969
run_id:d43a1454bccb9c1d72cd634fa5a66a3d2a832c26
······
#获取某一条信息
root@Mysql-slave:~# echo -en "INFO
" | ncat 127.0.0.1 6379 | grep role:
role:master
root@Mysql-slave:~# echo -en "INFO
" | ncat 127.0.0.1 6379 | grep role: | awk -F: '{print $2}'
master
脚本编写
在机器172.20.32.105上面编写脚本
一般脚本放在/etc/zabbix/zabbix_agentd.d/这个文件目录下
方法一: redis_monitor.sh
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# vim redis_monitor.sh
#!/bin/bash
#
# Edited on 20200501 by likai.tech
#
redis_status(){
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO
";sleep 1;) | 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@Mysql-slave:/etc/zabbix/zabbix_agentd.d# chmod a+x redis_monitor.sh
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# bash redis_monitor.sh redis_status 6379 connected_clients
1
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# bash redis_monitor.sh redis_status 6379 used_memory
840352
方法二: redis_monitor1.sh
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# vim redis_monitor1.sh
#!/bin/bash
#
# Edited on 20200501 by likai.tech
#
get_redis_status(){
echo -en "INFO
" | ncat 127.0.0.1 $1 | grep -w "$2" | awk -F: '{print $2}'
}
main(){
# install ncat
cat /etc/issue | grep -iq "ubuntu"
[[ $? -ne 0 ]] && yum install nmap-ncat -y &> /dev/null || apt install nmap-ncat -y &> /dev/null
# prompt usage
if [[ $# -eq 0 ]]; then
echo "`basename $0` redis_status <PORT> <STATUS>"
fi
# do the f* thing
if [[ $1 = "redis_status" ]]; then
get_redis_status $2 $3
fi
}
main $1 $2 $3
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# chmod a+x redis_monitor1.sh
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# bash redis_monitor1.sh redis_status 6379 connected_clients
1
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# bash redis_monitor1.sh redis_status 6379 used_memory
840352
增加监控项
这里以方法一的脚本做自定义监控项目
在 agent 配置文件增加监控项
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# vim /etc/zabbix/zabbix_agentd.conf
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
#添加自定义监控项
UserParameter=redis_status[*],bin/bash /etc/zabbix/zabbix_agentd.d/redis_monitor.sh "$1" "$2" "$3"
root@Mysql-slave:/etc/zabbix/zabbix_agentd.d# systemctl restart zabbix-agent.service
在 zabbix server 测试获取数据
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","role"]"
master
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","uptime_in_seconds"]"
7054
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory_rss"]"
4128768
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory"]"
840352
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","total_connections_received"]"
848
制作模板
创建模板
可以看到上面的Redis templet 后面的数据都是空的,依次创建一些监控项以及图形和触发器
键值:redis_status["redis_status","6379","used_memory"]
同理在创建一个新的监控项
键值:redis_status["redis_status","6379","total_connections_received"]
创建图形
关联到主机
数据查看
root@Zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory"]"
840352
触发器
这里的触发器需要基于前面自定义的监控项来进行配置。
如果添加了图形界面的,在监控图形界面就能看到一个触发器
内存使用触发器
需要将已用内存单位转换,从 G 转换为字节,例如当内存使用达 2G 时就进行触发,则换算方式如下:
2GB=21024(MB)1024(KB)*1024(B)
查看监控图形
客户端连接数量触发器
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
创建客户端连接数量监控项:
添加触发器
创建图形:
在监测图形中查看:
在终端上多一个终端访问redis-cli,则connected_clients会增加1
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
2
root@Zabbix-server:/apps#
发送邮件信息
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
4
root@Zabbix-server:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
5
超过触发器会发送邮件信息:
推荐阅读:
以上是关于Zabbix--Zabbix-Redis状态监控的主要内容,如果未能解决你的问题,请参考以下文章