前提
1.监控主机安装zabbix-agent
2.监控主机安装nginx

zabbix服务器端ip zabbix-agent客户端
192.168.1.240 192.168.1.228
[root@vrgv bin]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log                                                   --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-pat                                                  h=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp                                                   --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --wit                                                  h-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_mo                                                  dule --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --w                                                  ith-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecor                                                  d-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

其中必须有--with-http_stub_status_module --with-http_sub_module模块功能,就代表可以启动status功能,要不然不会监控上数据,没有的需要编译安装上

开始对zabbix-agent客户端192.168.1.228,被监控的nginx主机配置

nginx配置

新增nginx配置文件nginx.status.conf
[root@vrgv /]# vim /etc/nginx/conf.d/nginx.status.conf
server {
    listen 81;
    access_log off;
    server_name localhost;
    root /var/www/html;

    location /nginx_status {     #转发关键字
       stub_status on;             #打开status状态服务
       access_log off;             #关闭log功能
       allow 127.0.0.1;            #只允许本地访问
       deny all;                       #拒绝除了上面127.0.0.1独立设置的ip以外,所有的ip
    }
}

重新启动nginx
[root@vrgv bin]# systemctl restart nginx

访问测试两次
[root@vrgv bin]# curl http://127.0.0.1:81/nginx_status
Active connections: 1
server accepts handled requests
 1 1 1
Reading: 0 Writing: 1 Waiting: 0
[root@vrgv bin]# curl http://127.0.0.1:81/nginx_status
Active connections: 1
server accepts handled requests
 2 2 2
Reading: 0 Writing: 1 Waiting: 0

以上信息中:
#active   当前活动的客户端连接数,包括waiting连接数
#accepts  已接受的客户端连接数
#handled  已处理的连接总数
#requests 客户端请求的总数
#reading  正在读取请求头的当前连接数
#writing  将响应写回客户端的当前连接数
#waiting  等待请求空闲客户端的当前连接数

zabbix-agent端配置

新增脚本

根据自己实际情况,找到zabbix-agent安装目录下,创建一个脚本目录和脚本文件
[root@vrgv /]# cd /etc/zabbix/
[root@vrgv zabbix]# ls
zabbix_agentd.conf  zabbix_agentd.d  zabbix_java_gateway.conf.rpmsave
[root@vrgv zabbix]# mkdir scripts
[root@vrgv zabbix]# cd scripts/
[root@vrgv scripts]# vim nginx-status.sh

#!/bin/bash
result="/usr/bin/curl -s http://127.0.0.1:81/nginx_status"
case $1 in
    active)
        $result |awk '/Active/ {print $NF}'
    ;;
    accepts)
        $result |awk 'NR==3 {print $1}'
    ;;
    handled)
        $result |awk 'NR==3 {print $2}'
    ;;
    requests)
        $result |awk 'NR==3 {print $3}'
    ;;
    reading)
        $result |awk '/Reading/ {print $2}'
    ;;
    writing)
        $result |awk '/Writing/ {print $4}'
    ;;
    waiting)
         $result |awk '/Waiting/ {print $6}'
    ;;
          *)
    echo "USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac

配置zabbix-agent配置文件

[root@vrgv scripts]# cd ../
[root@vrgv zabbix]# ls
scripts  zabbix_agentd.conf  zabbix_agentd.d  zabbix_java_gateway.conf.rpmsave
[root@vrgv zabbix]# vim zabbix_agentd.conf
全局搜索UserParameter参数
修改成:
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx-status.sh $1  #nginx.status是参数,需要与后面的zabbix-web模板中对应的对应,后面是脚本路径,需要注意权限问题。

重新启动zabbix-agent服务
[root@vrgv zabbix]# systemctl restart zabbix-agent

zabbix-server端192.168.1.240操作

测试获取信息,可以获取到信息

[root@zabbix ~]# zabbix_get -s 192.168.1.228 -p 10050 -k 'nginx.status[active]'
1

zabbix-web端操作

导入模板
ZABBIX4.0监控nginx1.16_zabbix模板文件链接:javascript:void(0)(官方提供的下载要求一共两个选择,积分和粉丝,此资源需要关注下博主,无需积分)
选择一主机关联此模板
ZABBIX4.0监控nginx1.16_客户端_02在监测中—最新数据如图,可以看到数据了
ZABBIX4.0监控nginx1.16_zabbix_03