Zabbix學習(1)-監控服務與Zabbix介紹
Zabbix學習(2)-Zabbix安裝和基礎使用
Zabbix學習(3)-Zabbix監控入門
Zabbix學習(4)-Proxy主動被動部署
Zabbix學習案例(1)-TCP狀态監控
Zabbix學習案例(2)-memcached狀态監控
安裝 redis服務
通過自定義監控模闆對memcache進行監控
為了測試效果,在主動模式下,因為主動模式擷取資料比較快一些
環境:在這裡複用Mysql-slave的主機
zabbix server | zabbix agent |
---|---|
172.20.32.101:Zabbix-server | 172.20.32.105:Mysql-slave |
安裝 redis
[email protected]:~# apt install redis -y
[email protected]:~# vim /etc/redis/redis.conf
[email protected]:~# 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
[email protected]:~# systemctl restart redis-server
[email protected]:~# 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
[email protected]:~# ss -ntl |grep 6379
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
[email protected]:~# 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>
編寫監控腳本
腳本編寫思路
[email protected]:~# echo -en "INFO \r\n" | 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
······
#擷取某一條資訊
[email protected]:~# echo -en "INFO \r\n" | ncat 127.0.0.1 6379 | grep role:
role:master
[email protected]:~# echo -en "INFO \r\n" | 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
[email protected]:/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 \r\n";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
[email protected]:/etc/zabbix/zabbix_agentd.d# chmod a+x redis_monitor.sh
[email protected]:/etc/zabbix/zabbix_agentd.d# bash redis_monitor.sh redis_status 6379 connected_clients
1
[email protected]:/etc/zabbix/zabbix_agentd.d# bash redis_monitor.sh redis_status 6379 used_memory
840352
方法二: redis_monitor1.sh
[email protected]:/etc/zabbix/zabbix_agentd.d# vim redis_monitor1.sh
#!/bin/bash
#
# Edited on 20200501 by likai.tech
#
get_redis_status(){
echo -en "INFO \r\n" | 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
[email protected]:/etc/zabbix/zabbix_agentd.d# chmod a+x redis_monitor1.sh
[email protected]:/etc/zabbix/zabbix_agentd.d# bash redis_monitor1.sh redis_status 6379 connected_clients
1
[email protected]:/etc/zabbix/zabbix_agentd.d# bash redis_monitor1.sh redis_status 6379 used_memory
840352
增加監控項
這裡以方法一的腳本做自定義監控項目
在 agent 配置檔案增加監控項
[email protected]:/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"
[email protected]:/etc/zabbix/zabbix_agentd.d# systemctl restart zabbix-agent.service
在 zabbix server 測試擷取資料
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","role"]"
master
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","uptime_in_seconds"]"
7054
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory_rss"]"
4128768
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory"]"
840352
[email protected]:/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”]
建立圖形
關聯到主機
資料檢視
[email protected]:~# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","used_memory"]"
840352
觸發器
這裡的觸發器需要基于前面自定義的監控項來進行配置。
如果添加了圖形界面的,在監控圖形界面就能看到一個觸發器
記憶體使用觸發器
需要将已用記憶體機關轉換,從 G 轉換為位元組,例如當記憶體使用達 2G 時就進行觸發,則換算方式如下:
2GB=2*1024(MB)*1024(KB)*1024(B)
檢視監控圖形
用戶端連接配接數量觸發器
[email protected]:/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
[email protected]:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
[email protected]:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
2
[email protected]:/apps#
發送郵件資訊
[email protected]:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
4
[email protected]:/apps# /apps/zabbix_server/bin/zabbix_get -s 172.20.32.105 -p10050 -k "redis_status["redis_status","6379","connected_clients"]"
5
超過觸發器會發送郵件資訊: