1.在nginx中开启状态模板

[root@web03 ~]# vim /usr/local/nginx-1.8.0/conf/nginx.conf

    location /stat {
        stub_status on;
        access_log off;
        allow 本机的IP地址;          #只允许本机访问
        deny all;
    }
wKiom1cz9vjTnqJrAAAnL0sJH_k427.png

2.写出监控nginx的脚本

[root@web03 ~]# vim /home/shell/nc_nginx_check.sh
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: Dick
# Data: 2016-05-12
HOST=网站的域名


# Functions to return nginx stats
function active {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Active' | awk '{print $NF}'
  }
function reading {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Reading' | awk '{print $2}'
  }
function writing {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Writing' | awk '{print $4}'
  }
function waiting {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
  }
function accepts {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $1}'
  }
function handled {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $2}'
  }
function requests {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $3}'
  }
# Run the requested function
$1               #接收第一个参数

[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf      #添加如下参数

UnsafeUserParameters=1
UserParameter=nginx.accepts,/home/shell/nc_nginx_check.sh accepts
UserParameter=nginx.handled,/home/shell/nc_nginx_check.sh handled
UserParameter=nginx.requests,/home/shell/nc_nginx_check.sh requests
UserParameter=nginx.connections.active,/home/shell/nc_nginx_check.sh active
UserParameter=nginx.connections.reading,/home/shell/nc_nginx_check.sh reading
UserParameter=nginx.connections.writing,/home/shell/nc_nginx_check.sh writing
UserParameter=nginx.connections.waiting,/home/shell/nc_nginx_check.sh waiting

[root@web03 ~]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:         [  OK  ]
Starting Zabbix agent:                 [  OK  ]
[root@web03 ~]#

3.在zabbix服务端测试是否有数据

[root@db02 ~]# yum install zabbix-get -y

[root@db02 ~]# zabbix_get -s 被监控端的IP地址  -k "nginx.accepts"
16187540
[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.handled"
16187585
[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.requests"
55652248
[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.active"
349
[root@db02 ~]#


4.在zabbix窗口添加模板,分类,项目

(1).创建模板

wKiom1cz-6fRHMWEAAB-d8EmsaE174.png

(2).创建分类

wKiom1cz-6ihLq0ZAABKtWmmSZA353.png

(3).创建应用集

wKiom1cz-6iBxpa3AAA5xk_xj6A897.png

(4).创建项目

wKioL1cz_InR9meHAABIFVaf9uM307.png

(5).创建监控项

wKioL1cz_InhPLXJAAAo8_97nSA366.png

wKiom1cz-6nBQO90AABDSJzrMm8978.png


5.查看监控的状态图

wKioL1cz_enz7HUpAAB3DBdMlrc654.png


错误总结:

[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.writing"
ZBX_NOTSUPPORTED: Timeout while executing a shell script.

[root@db02 ~]#

解决方法:

    在zabbix_agent端修改Timeout参数

[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf
Timeout=20                    #默认是关闭,去掉注释,将值修改为20


[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.writing"

ZBX_NOTSUPPORTED
[root@db02 ~]#

解决方法:

    在zabbix_agent端修改Timeout参数

[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf

AllowRoot=1

UnsafeUserParameters=1

EnableRemoteCommands=1

[root@web03 ~]#