Linux專案之zabbix監控腦裂
阿新 • • 發佈:2020-08-06
目錄
環境需求
主機 | IP地址 | 所需服務 |
---|---|---|
zabbix server端 | 192.168.159.135 | lamp、zabbix |
主keepalived端 | 192.168.159.137 | keepalived |
備keepalived端 | 192.168.159.144 | keepalived、zabbix |
1. 配置
配置server端的lamp和zabbix,我這裡就不過多闡述了,有疑問可以參考Liunx服務管理之lamp 和Linux監控服務之zabbix
配置主備keepalived的步驟,我上篇部落格Linux服務之keepalived高可用裡就有寫到,這裡也不做詳細講解了
2. 檢視主備伺服器上的vip狀態
配置監控前,主keepalived端
//VIP還是在主這裡 [root@master ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@master ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b2:1a:0e brd ff:ff:ff:ff:ff:ff inet 192.168.159.137/24 brd 192.168.159.255 scope global dynamic eno16777736 valid_lft 1647sec preferred_lft 1647sec inet 192.168.159.250/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feb2:1a0e/64 scope link valid_lft forever preferred_lft forever
配置監控前,備keepalived端
//目前,VIP不在備上面 [root@slave ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:10050 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@slave ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:23:3e:ef brd ff:ff:ff:ff:ff:ff inet 192.168.159.144/24 brd 192.168.159.255 scope global dynamic eth0 valid_lft 1521sec preferred_lft 1521sec inet6 fe80::250:56ff:fe23:3eef/64 scope link valid_lft forever preferred_lft forever
3. 在備上安裝、配置zabbix
//安裝一些常用的工具
[root@slave ~]# yum -y install vim wget gcc gcc-c++ net-snmp-devel libevent-devel
//下載zabbix安裝包,進行編譯、安裝
[root@slave ~]# cd /usr/src/
[root@slave src]# ls
debug kernels zabbix-4.4.7 zabbix-4.4.7.tar.gz
[root@slave src]# cd zabbix-4.4.7
[root@slave zabbix-4.4.7]# ./configure --enable-agent
[root@slave zabbix-4.4.7]# make install
//修改配置檔案
[root@slave ~]# cd /usr/local/etc/
[root@slave etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@slave etc]# vim zabbix_agentd.conf
..........
Server=192.168.159.135
..........
ServerActive=192.168.159.135
..........
Hostname=Zabbix client
//建立zabbix使用者
[root@slave ~]# useradd -r -M -s /sbin/nologin zabbix
[root@slave ~]# id zabbix
uid=996(zabbix) gid=994(zabbix) groups=994(zabbix)
//啟動服務
[root@slave ~]# zabbix_agentd
[root@slave ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//寫入監控腦裂的指令碼
[root@slave ~]# cd /scripts/
[root@slave scripts]# ls
notify.sh
[root@slave scripts]# vim check_keepalived.sh
[root@slave scripts]# cat check_keepalived.sh
#!/bin/bash
if [ `ip a show eth0 |grep 192.168.159.250|wc -l` -ne 0 ]
then
echo "1"
else
echo "0"
fi
[root@slave scripts]# chmod +x check_keepalived.sh
[root@slave scripts]# ll
total 8
-rwxr-xr-x 1 root root 110 Jul 31 18:01 check_keepalived.sh
-rwxr-xr-x 1 root root 451 Jul 31 17:32 notify.sh
[root@slave scripts]# ./check_keepalived.sh
0
//把指令碼寫入配置檔案
[root@slave ~]# vim /usr/local/etc/zabbix_agentd.conf
.............
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_keepalived.sh $1
//重啟服務
[root@slave ~]# pkill zabbix
[root@slave ~]# zabbix_agentd
4. 在server端,手動測試指令碼有沒有問題
[root@Server ~]# zabbix_get -s 192.168.159.144 -k check_process
0 //能看到0就是沒錯的!!
5. 在web介面配置監控項(zabbix server端)
5.1 建立主機
5.2 新增監控項
5.3 新增觸發器
5.4 新增媒介
5.5 新增動作
5.6 進入Latest data檢視
6. 觸發監控
修改備上的配置檔案,讓備與主的virtual_router_id不一樣,就是爭搶VIP,導致腦裂
[root@slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
...............
virtual_router_id 52
...............
//重啟服務
[root@slave ~]# systemctl restart keepalived
[root@slave ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:23:3e:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.159.144/24 brd 192.168.159.255 scope global dynamic eth0
valid_lft 1681sec preferred_lft 1681sec
inet 192.168.159.250/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe23:3eef/64 scope link
valid_lft forever preferred_lft forever
檢視有沒有監控到
因為我這裡,郵箱沒設白名單的緣故,所以未收到郵件,但這裡已經顯示傳送成功,無關大礙!
到此,通過zabbix監控腦裂的實驗就做完啦!