zabbix实现对nginx监控并实现nginx服务自愈

zabbix实现对nginx监控并实现nginx服务自愈

1.zabbix-server端(ubuntu1804)

1)必要的软件包

zabbix-server-mysql 
zabbix-frontend-php

2)启动的服务

State                    Recv-Q                    Send-Q                             Local Address:Port                               Peer Address:Port                                                                                                                                                                                                                  
LISTEN                   0                           128                                       0.0.0.0:10051                                     0.0.0.0:*                       
users:(("zabbix_server",pid=3991,fd=4),("zabbix_server",pid=3990,fd=4),("zabbix_server",pid=3989,fd=4)……
……
LISTEN                   0                           80                                          192.168.1.14:3306                              0.0.0.0:*                     
    users:(("mysqld",pid=1461,fd=20))   
……                                                                                                                               
LISTEN                   0                         128                                           *:80                                                    *:*                      
 users:(("apache2",pid=4105,fd=4),("apache2",pid=4100,fd=4),("apache2",pid=4091,fd=4),("apache2",pid=3904,fd=4),
("apache2",pid=3900,fd=4),("apache2",pid=3801,fd=4),("apache2",pid=3576,fd=4),("apache2",pid=3501,fd=4),("apache2",
pid=3404,fd=4),("apache2",pid=1620,fd=4),("apache2",pid=1587,fd=4))
……

3)建立proxy代理端的受权数据库

MariaDB [(none)]> create database zabbix_proxy_active character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix_proxy_active.* to zabbix_proxy_active@192.168.1.20 identified by 'zabbix_proxy_active';

2.zabbix-proxy代理服务端(ubuntu1804)

1)必要的软件包

zabbix-proxy-mysql

2)配置proxy配置文件

root@ubuntu1804:/etc/zabbix# grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf
ProxyMode=0  #主动模式
Server=192.168.1.14
Hostname=zabbix_proxy_active
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
EnableRemoteCommands=1 #开启容许远程命令
LogRemoteCommands=1
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBHost=192.168.1.14
DBName=zabbix_proxy_active
DBUser=zabbix_proxy_active
DBPassword=zabbix_proxy_active
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=30
ExternalScripts=/usr/lib/zabbix/externalscripts
FpingLocation=/usr/bin/fping
Fping6Location=/usr/bin/fping6
LogSlowQueries=3000

3)测试是否可以登陆数据库

root@ubuntu1804:~# mysql -uzabbix_proxy_active -pzabbix_proxy_active -h192.168.1.14
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6951
Server version: 10.1.40-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+---------------------+
| Database            |
+---------------------+
| information_schema  |
| zabbix_proxy_active |
+---------------------+
2 rows in set (0.00 sec)

4)数据库初始化

zcat /usr/share/doc/zabbix-proxy-mysql/schema.sql.gz | mysql -uzabbix_proxy_active -pzabbix_proxy_active 
-h192.168.1.14 zabbix_proxy_active

5)启动服务

root@ubuntu1804:~# systemctl restart zabbix-proxy
root@ubuntu1804:~# systemctl enable zabbix-proxy

6)查看启动的服务

root@ubuntu1804:~# ss -tnlp
State                    Recv-Q                    Send-Q                                        Local Address:Port                                        Peer Address:Port                                                                                                                                                                                                                  
LISTEN                   0                         128                                                 0.0.0.0:10051                                            0.0.0.0:*                      
 users:(("zabbix_proxy",pid=3559,fd=4),("zabbix_proxy",pid=3558,fd=4),("zabbix_proxy",pid=3557,fd=4),("zabbix_proxy",pid=3556,fd=4),("zabbix_proxy",pid=3555,fd=4),("zabbix_proxy",pid=3554,fd=4),("zabbix_proxy",pid=3553,fd=4),("zabbix_proxy",pid=3552,fd=4),("zabbix_proxy",pid=3551,fd=4),("zabbix_proxy",pid=3550,fd=4),("zabbix_proxy",pid=3549,fd=4),("zabbix_proxy",pid=3548,fd=4),("zabbix_proxy",pid=3547,fd=4),("zabbix_proxy",pid=3546,fd=4),("zabbix_proxy",pid=3545,fd=4),("zabbix_proxy",pid=3544,fd=4),("zabbix_proxy",pid=3543,fd=4),("zabbix_proxy",pid=3542,fd=4),("zabbix_proxy",pid=3541,fd=4),("zabbix_proxy",pid=3540,fd=4),("zabbix_proxy",pid=3539,fd=4),("zabbix_proxy",pid=3538,fd=4),("zabbix_proxy",pid=3537,fd=4),("zabbix_proxy",pid=3536,fd=4),("zabbix_proxy",pid=3525,fd=4))
LISTEN                   0                         80                                                  0.0.0.0:3306                                             0.0.0.0:*                      
 users:(("mysqld",pid=4094,fd=17))
……

3.zabbix-agent客户端(centos7.6)

1)安装必要的包

zabbix-agent
nginx

2)配置agent配置文件

[root@nginx zabbix_agentd.d]# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1  #必定要开启,不然致使没法执行远程命令
Server=192.168.1.14,192.168.1.20  #同时指定server端和proxy代理端
ServerActive=192.168.1.20  #指定主动模式的proxy代理端
Hostname=192.168.1.16  #给本机指定一个主机名,web端上天添加的主机名必须和此处主机名相同,不然会致使数据采集失败
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1  #支持远程命令传输的特殊字符,也必须开启

3)修改nginx配置文件,打开nginx访问状态页

[root@nginx ~]# vim /etc/nginx/nginx.conf
                ……
server {
                listen       8080 default_server;   #改成监听8080端口
                listen       [::]:8080 default_server;
                server_name  _;
                root         /usr/share/nginx/html;
                ……
                location / {
                }
             location /nginx_status {   #打开状态页
                     stub_status;
                     alllow 127.0.0.1;#容许本地访问
                    deny all;#为安全,除了本地,其余主机均不可访问状态页
             }
             ……

4)建立nginx的key值采集脚本,并赋予脚本执行权限

[root@nginx ~]# vim /etc/zabbix/zabbix_agentd.d/nginx.sh
nginx_status(){
nginx_port=$1
nginx_cmd=$2
ip=`ifconfig eth0 | awk NR==2 | awk '{print $2}'`   #取出本机的ip地址
nginx_active(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | grep Active | awk '{print $NF}'  #取出当前客户端活动链接数
}
nginx_accepts(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==3 | awk '{print $1}'  #取出累积接收客户端请求链接的链接数
}
nginx_handled(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==3 | awk '{print $2}'  #取出累积客户端请求处理数
}
nginx_requests(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==3 | awk '{print $3}'  #取出累积的客户端总共发送的请求数 
}
nginx_reading(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==4 | awk '{print $2}'  #取出当前正在读取客户端请求报文首部链接的链接数
}
nginx_writing(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==4 | awk '{print $4}'  #取出当前正在向客户端发送响应报文链接的链接数
}
nginx_waiting(){
curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | awk NR==4 | awk '{print $6}'  #取出当前正在等待客户端发来请求空闲链接的链接数
}
nginx_connecting(){
charactor=`curl ${ip}:${nginx_port}/nginx_status 2> /dev/null | grep -o Active`   #判断nginx服务是否为运行状态
if [ "$charactor" == "Active" ];then
 echo 0; #0表示能够链接nginx服务
else
 echo 10; #10表示是没法链接到nginx服务,服务关闭
fi
}
case $nginx_cmd in
         active)
         nginx_active
         ;;
         accepts)
         nginx_accepts
         ;;
         handled)
         nginx_handled
         ;;
         requests)
         nginx_requests
         ;;
         reading)
         nginx_reading
         ;;
         writing)
         nginx_writing
         ;;
         waiting)
         nginx_waiting
         ;;
         connecting)
         nginx_connecting
         ;;
esac
}
nginx_status $1 $2

5)配置监控项key值配置文件

[root@nginx ~]# vim /etc/zabbix/zabbix_agentd.d/nginx_status_monitor.conf
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx.sh $1 $2

6)查看编辑好的用于建立nginx模板所需的采集脚本文件和key值配置文件

[root@nginx ~]# ll /etc/zabbix/zabbix_agentd.d/
total 8
-rwxr-xr-x 1 root root 1350 Jul 19 20:37 nginx.sh
-rw-r--r-- 1 root root   74 Jul 19 20:43 nginx_status_monitor.conf

7)启动agent和nginx两个服务

[root@nginx ~]# systemctl restart zabbix-agent
[root@nginx ~]# systemctl enable zabbix-agent
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx

8)查看启动的服务

[root@nginx ~]# ss -tnlp
State       Recv-Q Send-Q                                                  Local Address:Port                                                 Peer Address:Port              
LISTEN      0      511                                                                 *:8080                                                                *:*                   
users:(("nginx",pid=50295,fd=6),("nginx",pid=50294,fd=6),("nginx",pid=50293,fd=6),("nginx",pid=50292,fd=6),
("nginx",pid=50291,fd=6))
LISTEN      0      128                                                                 *:10050                                                               *:*   
……              
    users:(("zabbix_agentd",pid=43802,fd=4),("zabbix_agentd",pid=43801,fd=4),("zabbix_agentd",pid=43800,fd=4),
("zabbix_agentd",pid=43799,fd=4),("zabbix_agentd",pid=43798,fd=4),("zabbix_agentd",pid=43797,fd=4))
……

9)测试访问nginx状态页

[root@nginx ~]# curl 127.0.0.1:8080/nginx_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0

10)将zabbix用户提高为sudo超级用户

[root@nginx ~]# vim /etc/sudoers
    #Defaults    requiretty   #必定要注释掉此行,会致使sudo超级用户没法执行远程命令
    ……
  ## Allow root to run any commands anywhere 
        root    ALL=(ALL)       ALL
        zabbix  ALL=(ALL)       NOPASSWD: ALL   
……

4.zabbix-server的web端建立一个主动模式的代理

zabbix实现对nginx监控并实现nginx服务自愈

5.zabbix-server的web端建立nginx模板

1)nginx_status模板建立

zabbix实现对nginx监控并实现nginx服务自愈

2)监控项建立

zabbix实现对nginx监控并实现nginx服务自愈

3)建立好的全部的nginx监控项

zabbix实现对nginx监控并实现nginx服务自愈

4)建立触发器

zabbix实现对nginx监控并实现nginx服务自愈

5)建立好的两个nginx触发器

zabbix实现对nginx监控并实现nginx服务自愈

6)建立当前状态图形

zabbix实现对nginx监控并实现nginx服务自愈

7)建立累计状态图形

zabbix实现对nginx监控并实现nginx服务自愈

8)再建立一个nginx监听端口状态图

zabbix实现对nginx监控并实现nginx服务自愈

9)建立聚合图形

zabbix实现对nginx监控并实现nginx服务自愈

10)将前面的监控图形添加聚合图形里,点击构造函数

zabbix实现对nginx监控并实现nginx服务自愈

11)点击更改进入添加界面

zabbix实现对nginx监控并实现nginx服务自愈

12)能够看到已成功添加监控图形到聚合图形里

zabbix实现对nginx监控并实现nginx服务自愈

13)查看建立好的图形

zabbix实现对nginx监控并实现nginx服务自愈

14)nginx_status状态模板建立完成

zabbix实现对nginx监控并实现nginx服务自愈

6.zabbix-server的web端添加监控主机

1)添加监控主机zabbix-agent并添加一个主动模式的代理

zabbix实现对nginx监控并实现nginx服务自愈

2)添加一个模板

zabbix实现对nginx监控并实现nginx服务自愈

3)添加完成

zabbix实现对nginx监控并实现nginx服务自愈

7.分别在zabbix-server端和zabbix-proxy端测试nginx服务的key值采集

1)分别下载zabbix-get测试工具

root@ubuntu1804:~# apt-get install zabbix-get -y

2)链接状态测试

root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k nginx_status[8080,connecting]
0

3)接收客户端发送的请求链接的链接数测试

root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k nginx_status[8080,accepts]
5

4)正在读取客户端请求报文首部链接的链接数测试

root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k nginx_status[8080,reading]
0

8.web端查看监控主机zabbix-agent采集数据图形

1)nginx状态页累积状态图

zabbix实现对nginx监控并实现nginx服务自愈

2)nginx状态页当前状态图

zabbix实现对nginx监控并实现nginx服务自愈

3)查看nginx监听端口状态图,显示正常

zabbix实现对nginx监控并实现nginx服务自愈

9.web端创建邮件通知和建立nginx服务自愈动做

1)点击建立媒介类型

zabbix实现对nginx监控并实现nginx服务自愈

2)建立媒介类型,填写媒介类型信息,要求qq邮箱预先开启smtp服务

zabbix实现对nginx监控并实现nginx服务自愈

3)能够查看到已成功添加媒介类型

zabbix实现对nginx监控并实现nginx服务自愈

4)进入用户界面选择admin帐户

选择admin帐户php

zabbix实现对nginx监控并实现nginx服务自愈

5)在选择报警媒介

zabbix实现对nginx监控并实现nginx服务自愈

6)添加建立好的媒介类型到此用户

zabbix实现对nginx监控并实现nginx服务自愈

7) 建立一个动做

zabbix实现对nginx监控并实现nginx服务自愈

8)在动做栏下添加触发条件

zabbix实现对nginx监控并实现nginx服务自愈

9)在操做栏添加一远程命令

zabbix实现对nginx监控并实现nginx服务自愈

10)操做栏再添加一个宕机通知信息,以及指定邮件的接收方

zabbix实现对nginx监控并实现nginx服务自愈

11)恢复操做栏添加恢复消息,一样指定邮件的接收者

zabbix实现对nginx监控并实现nginx服务自愈

10.测试nginx服务是否能接收到邮件通知以及实现自愈功能

1)先在在zabbix-agent客户端将nginx服务关闭

[root@nginx zabbix_agentd.d]# systemctl stop nginxhtml

2)再zabbix-agent客户端快速查看nginx服务的状态,服务已经被关闭

[root@nginx ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)mysql

3)nginx服务关闭后,在web端触了发仪表盘报警

zabbix实现对nginx监控并实现nginx服务自愈

4)接收到报警邮箱通知,nginx服务器断开链接

zabbix实现对nginx监控并实现nginx服务自愈

5)间隔3-5秒后,发现nginx服务自动重启,实现自愈

[root@nginx zabbix_agentd.d]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2019-07-19 16:43:10 CST; 12min ago
Process: 50272 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 50245 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 50243 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
……nginx

6)在web端查看nginx-8080监听图形,数值从超出警惕线到降低恢复到正常值

zabbix实现对nginx监控并实现nginx服务自愈

7)仪表盘又自动恢复正常

zabbix实现对nginx监控并实现nginx服务自愈

8)接收到故障自动恢复成功的邮箱通知

zabbix实现对nginx监控并实现nginx服务自愈

相关文章
相关标签/搜索