zabbix服務監控、觸發器案例(自動重啓服務及郵件報警)超詳細

實驗案例

監控nginx服務,實現nginx宕機,zabbix自動重啓其服務,如若重啓失敗,則郵件報警。
環境:
server:
192.168.1.11
centos7 zabbix4.4
agent:
192.168.1.12
centos7 zabbix4.4

一、安裝zabbix服務端,見上篇文章

https://blog.csdn.net/oToyix/article/details/106853849
改字體

[root@localhost ~]# mv stkaiti.ttf /usr/local/nginx/html/zabbix/assets/fonts/DejaVuSans.ttf 

二、安裝配置zabbix-agent端

安裝zabbix源

[root@localhost ~]# yum install http://repo.zabbix.com/zabbix/4.5/rhel/7/x86_64/zabbix-release-4.5-2.el7.noarch.rpm

安裝zabbix-agent

[root@localhost ~]# yum install zabbix40 abbix40-agent -y
[root@localhost ~]# rpm -qa|grep zabbix
zabbix40-4.0.17-1.el7.x86_64
zabbix-release-4.5-2.el7.noarch
zabbix40-agent-4.0.17-1.el7.x86_64

客戶端口開啓主動模式

[root@localhost ~]# sed -i "s/ServerActive=127.0.0.1/ServerActive=192.168.1.11/g" /etc/zabbix_agentd.conf
[root@localhost ~]# sed -i "s/Hostname=Zabbix server/Hostname=192.168.1.12/" /etc/zabbix_agentd.conf
[root@localhost ~]# egrep -v '^#|^$' /etc/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.1.11
ServerActive=192.168.1.11
Hostname=192.168.1.12                                              

啓動zabbix-agent

[root@localhost ~]# systemctl start zabbix-agent
[root@localhost ~]# ps -ef|grep zabbix
zabbix    2312     1  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd -f
zabbix    2313  2312  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix    2314  2312  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix    2315  2312  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix    2316  2312  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix    2317  2312  0 18:37 ?        00:00:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root      2319  1961  0 18:37 pts/0    00:00:00 grep --color=auto zabbix                                                                                

三、zabbixserver添加客戶端主機-nginx監控項_nginx_port

配置-主機-創建主機
在這裏插入圖片描述

server端測試

[root@localhost ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.12 -k "net.tcp.listen[80]"
1

創建圖形

配置-主機-圖形-創建圖形
在這裏插入圖片描述

查看監控結果

在這裏插入圖片描述

四、zabbixserver添加 nginx監控項stub_status

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 48         location /status {
 49                 stub_status;
 50         }
 51 
 [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

查看狀態數據

[root@localhost ~]# curl -s 192.168.1.12/status
Active connections: 1 
server accepts handled requests
 32 32 39 
Reading: 0 Writing: 1 Waiting: 0 
[root@localhost ~]# curl -s 192.168.1.12/status 2>/dev/null |awk 'NR==3 {print $3}'
40   
注: 
NR==3意思是第三行
$3 表示第3個值即39(訪問數),因爲本次又執行了一遍curl,所以爲40

爲了更好的利用stub_status模塊,寫個腳本來傳遞參數

[root@localhost ~]# mkdir -p /data/sh
[root@localhost ~]# vim /data/sh/nginx_stub_status.sh
#!/bin/bash
# by toyix
#######################

function active {
        curl 192.168.1.12/status 2>/dev/null |awk 'NR==1{print $NF}'
        }
function accepts {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==3{print $1}'
        }
function handled {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==3{print $2}'
        }
function requests {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==3{print $3}'
}
function reading {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==4{print $2}'
}
function writing {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==4{print $4}'
}
function waiting {
        curl 192.168.1.12/status 2>/dev/null | awk 'NR==4{print $NF}'
}

$1

使用腳本,定義進zabbix_agent.conf中

[root@localhost sh]# vim /etc/zabbix_agentd.conf 
292 UserParameter=nginx.stub.status[*],bash /data/sh/nginx_stub_status.sh $1
[root@localhost sh]# systemctl restart zabbix-agent

server端測試腳本

[root@localhost sh]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.12 -k "nginx.stub.status[requests]"
42

web端添加此監控項(nginx.stub.status)

在這裏插入圖片描述
加入圖形
在這裏插入圖片描述
查看最新數據
在這裏插入圖片描述

五、zabbixserver添加 nginx監控項killall -0

客戶端安裝killall工具

[root@localhost sh]# yum install psmisc -y
[root@localhost ~]# chmod u+s /usr/bin/killall

編輯腳本

[root@localhost ~]# vim /data/sh/nginx.killall_echo.sh
#!/bin/bash
#by toyix
#############################

killall -0 nginx &>/dev/null

if [ $? -eq 0 ];then
        echo 1
else
        echo 0
fi
[root@localhost ~]# chmod +x /data/sh/nginx.killall_echo.sh 

加入zabbix_agent.conf

[root@localhost ~]# vim /etc/zabbix_agentd.conf 
UserParameter=nginx.killall,bash /data/sh/nginx.killall_echo.sh
[root@localhost ~]# systemctl restart zabbix-agent

server端腳本測試

[root@localhost sh]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.12 -k "nginx.killall"
1

加入監控項
在這裏插入圖片描述

在這裏插入圖片描述

六、創建觸發器

配置-主機-觸發器-創建觸發器
在這裏插入圖片描述
在這裏插入圖片描述

七、動作-郵件報警

1、設置報警媒介-設置郵件信息

管理-報警媒介類型(點擊email)
發件人:
在這裏插入圖片描述
收件人:
在這裏插入圖片描述

2、添加動作-發 郵件

配置-動作-創建動作
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
停止服務
[root@localhost ~]# pkill nginx
檢測是否會發送郵件
在這裏插入圖片描述

八、動作-自動啓動服務,啓動成功後不再發送郵件,啓動失敗,則發送郵件

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

1、啓動服務成功,不發送郵件

測試,關閉服務,看結果
[root@localhost ~]# pkill nginx
查看問題,右下角發現已執行遠程命令
在這裏插入圖片描述
查看進程是否成功啓動

[root@localhost ~]# ps -ef|grep nginx
root      6622     1  0 00:35 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     6623  6622  0 00:35 ?        00:00:00 nginx: worker process
root      6637  1961  0 00:36 pts/0    00:00:00 grep --color=auto nginx

上面爲啓動成功,沒有發送郵件

2、現在改配置文件,讓nginx啓動失敗,發送郵件

    keepalive_timeout  65;
dsfdsfdsfdsf   nginx.conf中隨便加幾個字
    #gzip  on;
[root@localhost ~]# pkill nginx

查看問題狀態,發送郵件了
在這裏插入圖片描述
在這裏插入圖片描述

注:

1、當監測問題時間與系統時間不一致時,改
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.d/default.conf
php_value[date.timezone] = Asia/Shanghai
2、創建觸發器規則時,T值最好等於1,如果=2,會執行完遠程命令後,直接發送問題郵件,因爲在檢查問題是否解決時,會認爲問題沒有解決,這是一個bug.
---------------end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章