一 nginx配置

(1)在nginx配置文件中,添加status配置:

location /nginx-status {
          stub_status on;
          access_log  off;
          allow 127.0.0.1;
          allow 192.168.1.0/24;
          deny all;
}

注:这是用于查看nginx的运行参数

然后访问:http://192.168.1.30/nginx-status

输出如下格式的数据:

Active connections: 4
server accepts handled requests
7 7 10
Reading: 0 Writing: 1 Waiting: 3

注:关于上面参数的详细说明:

  • Active connections    对后端发起的活动连接数;

  • server accepts 7    nginx 总共处理了7个连接;

  • handled    成功创建了7次握手;

  • requests    总共处理了10请求。

  • Reading    nginx读取客户端的header数;

  • Writing    nginx 返回给客户端的header数;

  • Waiting    nginx 请求处理完成,正在等待下一请求指令的连接

二 zabbix客户端配置

(1)编写客户端脚本nginx_status.sh

[root@localhost scripts]# vim /usr/local/zabbix/scripts/nginx_status.sh

其内容如下:

#!/bin/bash
# Description:zabbix监控nginx性能以及进程状态
# Note:此脚本需要配置在被监控端,否则ping检测将会得到不符合预期的结果

HOST="127.0.0.1"
PORT="80"

# 检测nginx进程是否存在
function ping {
    /sbin/pidof nginx | wc -l 
}
# 检测nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1

给脚本添加执行权限:

[root@localhost scripts]# chmod a+x /usr/local/zabbix/scripts/nginx_status.sh

(2)修改zabbix_agentd.conf并重启zabbix_agentd:

[root@localhost zabbix]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

添加:

UserParameter=nginx.status[*],/usr/local/zabbix/scripts/nginx_status.sh $1

重启zabbix_agentd:

[root@localhost zabbix]# service zabbix_agentd restart

三 zabbix_server测试获取数据以及配置web界面

(1)在zabbix server使用zabbix_get测试获取数据:

[root@localhost ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.30 -k 'nginx.status[accepts]'

输出如下:

48

如果有数据输出,则说明上面的配置已经成功了,接下来就是配置web界面了

(2)web界面配置:

由于上面已经可以通过zabbix_get获取到nginx的一些状态参数了,因此在配置web界面时就可以通过新建模板,并新建一些参数对应的监控项,以及添加显示“图形”等。具体实现思路可以参考我以前写过的这篇文章:https://www.zifangsky.cn/582.html

如果嫌麻烦的话也可以直接导入现成的模板:

模板文件Template App NGINX.xml:



    3.0
    2015-10-25T01:29:50Z
    
        
            Templates
        
    
    
        
    
    
        
            {Template App NGINX:nginx.status[ping].last()}=0
            nginx was down!
            
            0
            4
            NGINX进程数:0,请注意
            0
            
        
    
    
        
            nginx status connections
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    0
                    00C800
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[active]
                    
                
                
                    1
                    0
                    C80000
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[reading]
                    
                
                
                    2
                    0
                    0000C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[waiting]
                    
                
                
                    3
                    0
                    C800C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[writing]
                    
                
            
        
        
            nginx status server
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    0
                    00C800
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[accepts]
                    
                
                
                    1
                    0
                    C80000
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[handled]
                    
                
                
                    2
                    0
                    0000C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[requests]
                    
                
            
        
    

zabbix监控nginx_第1张图片

选择模板文件后选择“导入”即可

主机链接到模板:

配置->主机->点击nginx所在的主机->点击模板->添加”Template App NGINX”

zabbix监控nginx_第2张图片

最后效果如下:

zabbix监控nginx_第3张图片

参考文章:

  • http://www.ttlsa.com/zabbix/zabbix-monitor-nginx-performance/


PS:上面图片中的水印是我个人博客的域名,因此还请管理员手下留情不要给我标为“转载文章”,谢谢!!!