zabbix监控nginx状态页面
文章目录
环境说明:
使用的系统平台 | IP | 需要安装的服务 |
---|---|---|
CentOS8 监控端 | 192.168.220.9 | zabbix-server zabbix-agentd |
CentOS8 被监控端 | 192.168.220.10 | nginx zabbix-agentd |
安装zabbix-server需要lamp架构支持,nginx的安装之前的文章也写有,这里就不再赘述
关闭两台防火墙和selinux
# zabbix-server端
[root@localhost ~]# hostnamectl set-hostname zabbix-server
[root@localhost ~]# bash
[root@zabbix-server ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zabbix-server ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@zabbix-server ~]# setenforce 0
[root@zabbix-server ~]# getenforce
Permissive
# nginx端
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# bash
[root@nginx ~]#
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
访问zabbix web界面是否正常
nginx端修改配置文件配置状态页面
# 查看nginx是否安装了状态页面的模块
[root@nginx ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
# 上面的 --with-http_stub_status_module 就是状态页面的模块,下面我们启用它
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
43 location / {
44 root html;
45 index index.html index.htm;
46 }
47
48 location /status { # 添加
49 stub_status; # 添加,开启nginx状态页面
50 allow 192.168.220.9/32; # 允许zabbix-server端访问nginx状态页面
51 allow 192.168.220.10/32; # 允许nginx端访问nginx状态页面
52 allow 192.168.220.1/32; # 允许真机访问
53 deny all; # 拒绝其他的所有访问nginx状态页面
54 } # 添加
......
[root@nginx ~]# nginx -s reload # 重新加载nginx配置文件
nginx端访问nginx的状态页面 (http://ip/status)
状态页面信息详解:
状态码 | 表示的意义 |
---|---|
Active connections 2 | 当前所有处于打开状态的连接数 |
accepts | 总共处理了多少个连接 |
handled | 成功创建多少握手 |
requests | 总共处理了多少个请求 |
Reading | nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数 |
Writing | nginx返回给客户端的Header信息数,表示请求已经接收完成, 且正处于处理请求或发送响应的过程中的连接数 |
Waiting | 开启keep-alive的情况下,这个值等于active - (reading + writing), 意思就是Nginx已处理完正在等候下一次请求指令的驻留连接 |
在nginx端编写脚本来监控nginx端的状态页面,我使用脚本监控的是Waiting的值
[root@nginx ~]# mkdir /scripts
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# ls
[root@nginx scripts]# touch check_status.sh
[root@nginx scripts]# chmod +x check_status.sh
[root@nginx scripts]# vi check_status.sh
#!/bin/bash
IP=$(ip a|grep 'inet ' |grep -v '127.0.0.1'|awk -F'[ /]+' '{print $3}') # 获取本机IP
# 取到对应的三个值
case $1 in
"Reading")
curl -s http://$IP/status |awk 'NF==6 {print $2}';;
"Writing")
curl -s http://$IP/status |awk 'NF==6 {print $4}';;
"Waiting")
curl -s http://$IP/status |awk 'NF==6 {print $6}';;
esac
修改nginx端的 zabbix_agtend.conf 配置文件
[root@nginx scripts]# vim /usr/local/etc/zabbix_agentd.conf
......
152 # ServerActive=
153
154 ServerActive=192.168.220.9 # zabbix-server端的IP
......
163 # Hostname=
164
165 Hostname=fgqwe # 唯一标识本机的名字,不能有重复(也是web界面添加此主机的key)
......
322 UnsafeUserParameters=1 # 取消注释,把默认的0改成1
323
324 ### Option: UserParameter
325 # User-defined parameter to monitor. There can be several user-def ined parameters.
326 # Format: UserParameter=<key>,<shell command>
327 UserParameter=check_nginx_status[*](此key必须与web界面创建监控项时输入的key一致),/scripts/check_status.sh $1 # 复制上面一行到这行进行修改,$1是传的参数
.......
[root@nginx scripts]# pkill zabbix_agentd # 重启zabbix-agentd
[root@nginx scripts]# zabbix_agentd
[root@nginx scripts]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
zabbix-server端测试是否有问题
[root@zabbix-server ~]# zabbix_get -s 192.168.220.10 -k check_nginx_status[Reading](nginx端的IP)
0
Web界面配置
web界面配置是在zabbix-server端的web界面
创建主机组
创建主机并把主机加入到主机组
创建自定义监控 Reading 值
创建触发器 Reading 值
创建自定义监控 Writing 值
创建触发器 Writing 值
创建自定义监控 Waiting 值
创建触发器 Waiting 值