第一步:

在监控的电脑上安装nginx及状态模块:

 yum install nginx.x86_64 nginx-all-modules.noarch

编辑nginx的配置文件:

vim /etc/nginx/nginx.conf

#关闭了ipv6    server {        listen       80 default_server;#        listen       [::]:80 default_server;

#插入状态吗的location配置

        location /nginx_status {                stub_status on;                access_log off;                allow 192.168.56.0/24;                allow 127.0.0.1;                deny all;        }

启动nginx服务

[root@zabbix-node2 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@zabbix-node2 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1544/sshd           tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2142/master         tcp6       0      0 :::22                   :::*                    LISTEN      1544/sshd           tcp6       0      0 ::1:25                  :::*                    LISTEN      2142/master         [root@zabbix-node2 ~]# nginx [root@zabbix-node2 ~]# !netnetstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2850/nginx: master  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1544/sshd           tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2142/master         tcp6       0      0 :::22                   :::*                    LISTEN      1544/sshd           tcp6       0      0 ::1:25                  :::*                    LISTEN      2142/master

下面要自定义一个conf文件,然后定义一个模板,然后倒入

[root@zabbix-node2 zabbix_agentd.d]# vim nginx_status.confUserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1#上传脚本到指定的目录下如果默认放在这个配置文件下面会当成配置文件,所以这里会修改agent的配置文件:[root@zabbix-node2 zabbix_agentd.d]# vim /etc/zabbix/zabbix_agentd.conf Include=/etc/zabbix/zabbix_agentd.d/*.conf #这里给配置文件添加*.conf 来指定以*.conf为配置文件[root@zabbix-node2 zabbix_agentd.d]# cat nginx_monitor.sh #!/bin/bashNGINX_COMMAND=$1NGINX_PORT=80CACHEFILE="/tmp/nginx_status.txt"CMD="/usr/bin/curl http://127.0.0.1:"$NGINX_PORT"/nginx_status/"if [ ! -f $CACHEFILE  ];then   $CMD >$CACHEFILE 2>/dev/nullfi# Check and run the scriptTIMEFLM=`stat -c %Y $CACHEFILE`TIMENOW=`date +%s`if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then    rm -f $CACHEFILEfiif [ ! -f $CACHEFILE  ];then   $CMD >$CACHEFILE 2>/dev/nullfinginx_active(){	 grep 'Active' $CACHEFILE| awk '{print $NF}'         exit 0;}nginx_reading(){	 grep 'Reading' $CACHEFILE| awk '{print $2}'         exit 0;}nginx_writing(){	 grep 'Writing' $CACHEFILE | awk '{print $4}'         exit 0;}nginx_waiting(){	 grep 'Waiting' $CACHEFILE| awk '{print $6}'         exit 0;}nginx_accepts(){	 awk NR==3 $CACHEFILE| awk '{print $1}'          exit 0;}nginx_handled(){	 awk NR==3 $CACHEFILE| awk '{print $2}'          exit 0;}nginx_requests(){	 awk NR==3 $CACHEFILE| awk '{print $3}'         exit 0;}case $NGINX_COMMAND inactive)nginx_active;;;reading)nginx_reading;;;writing)nginx_writing;;;waiting)nginx_waiting;;;accepts)nginx_accepts;;;handled)nginx_handled;;;requests)nginx_requests;;;*)echo 'Invalid credentials';exit 2;esac#给脚本授权[root@zabbix-node2 zabbix_agentd.d]# chmod +x nginx_monitor.sh #上传xml模板文件文件root@zabbix-node2 zabbix_agentd.d]# cat zbx_nginx_templates.xml 
    
3.0
    
2016-11-09T14:50:50Z
    
        
            
Templates
        
    
    
        
    
    
        
            
Nginx Clients Status
            
900
            
200
            
0.0000
            
100.0000
            
1
            
1
            
0
            
1
            
0
            
0.0000
            
0.0000
            
0
            
0
            
0
            
0
            
                
                    
0
                    
0
                    
1A7C11
                    
0
                    
2
                    
0
                    
                        
Template Nginx Status
                        
nginx_status[active]
                    
                
                
                    
1
                    
0
                    
F63100
                    
0
                    
2
                    
0
                    
                        
Template Nginx Status
                        
nginx_status[reading]
                    
                
                
                    
2
                    
0
                    
2774A4
                    
0
                    
2
                    
0
                    
                        
Template Nginx Status
                        
nginx_status[waiting]
                    
                
                
                    
3
                    
0
                    
A54F10
                    
0
                    
2
                    
0
                    
                        
Template Nginx Status
                        
nginx_status[writing]
                    
                
            
        
    
[root@zabbix-node2 zabbix_agentd.d]#

导入模板

下面我们新创建一个主机

验证