nginx状态页面开启和监控

 环境

主机ip 系统
192.168.170.132 centos8

开启状态页面

开启status:

location /status {
  stub_status {on | off};
  allow 172.16.0.0/16;
  deny all;
}

访问状态页面的方式:http://server_ip/status

 

 

状态页面信息详解:

 

状态码表示的意义
Active connections 2 当前所有处于打开状态的连接数
accepts 总共处理了多少个连接
handled 成功创建多少握手
requests 总共处理了多少个请求
Reading nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数
Writing nginx返回给客户端的Header信息数,表示请求已经接收完成,
且正处于处理请求或发送响应的过程中的连接数
Waiting 开启keep-alive的情况下,这个值等于active - (reading + writing),
意思就是Nginx已处理完正在等候下一次请求指令的驻留连接

 

 

 

 

 

//开启状态页面
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
...
        location /status {
            stub_status on;
        }
...
...

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port     
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                      [::]:22                   [::]:*   

 

 

访问一下网页

 

 

 

如果不限制的话用其他主机也能访问到他的状态页面

[root@localhost nginx-1.20.0]# ip a show ens160
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:81:b8:da brd ff:ff:ff:ff:ff:ff
    inet 192.168.170.134/24 brd 192.168.170.255 scope global dynamic noprefixroute ens160
       valid_lft 983sec preferred_lft 983sec
    inet6 fe80::c8b5:ee83:7837:cb77/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::76dd:4d80:292a:6a7a/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::7675:93c:79de:89ad/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@localhost nginx-1.20.0]# curl 192.168.170.132/status
Active connections: 1 
server accepts handled requests
 38 38 21 
Reading: 0 Writing: 1 Waiting: 0 

 

 

// deny all 是拒绝所有访问,allow则是允许访问
[root@localhost ssl]# vim ../conf/nginx.conf
        location /status {
            stub_status on;
            allow 192.168.170.0/24;
            deny all;
        }

 

 

 

 

 

        location /status {
            stub_status on;
            deny all;
        }

 

 

 

 

 

 

 

既然其他主机可以访问他的状态页面,那么也可以用zabbix给他监控起来

 

环境

      

主机 IP地址 安装
master 192.168.170.133

lamp架构

nginx

zabbix_server

zabbix_agentd

 

 

准备工作请看之前的文章

//为了防止端口冲突,将nginx端口改为81
[root@master ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port          Peer Address:Port    
LISTEN    0          128                   0.0.0.0:81                 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                   0.0.0.0:10051              0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:9000               0.0.0.0:*       
LISTEN    0          128                         *:80                       *:*       
LISTEN    0          128                      [::]:81                    [::]:*       
LISTEN    0          128                      [::]:22                    [::]:*       
LISTEN    0          80                          *:3306                     *:* 




//开启状态页面
[root@master ~]# vim /etc/nginx/nginx.conf
        location /status {
            stub_status on;
        }

 

 

 

 

 

 

 

开启自定义监控的功能

//创建执行脚本
#!/bin/bash

nginx_server=192.168.170.133

requests=$(curl -s http://${nginx_server}:81/status | awk 'NR==3{print $3}')

Reading=$(curl -s http://${nginx_server}:81/status | awk 'NR==4{print $2}')

Writing=$(curl -s http://${nginx_server}:81/status | awk 'NR==4{print $4}')

Waiting=$(curl -s http://${nginx_server}:81/status | awk 'NR==4{print $6}')

echo "requests:$requests Reading:$Reading Writing:$Writing Waiting:$Waiting"







//开启自定义监控功能
...
...
UnsafeUserParameters=1
UserParameter=check_nginx_status,/scripts/check_nginx.sh
...
...



//测试能否取到值
[root@master scripts]# zabbix_get -s 192.168.170.133 -k check_nginx_status
requests:22 Reading:0 Writing:1 Waiting:0

 

 

 

 

添加监控项

 

 

 

 

添加触发器

 

 

posted @ 2021-05-31 20:10  取个名字真滴难  阅读(516)  评论(0)    收藏  举报