一、开始之前
本文内容是使用Zabbix 5.0自带的Nginx Template进行的监控和图形展示。监控之后,绘制出的Nginx运行时状态的图形如下:
二、Nginx启用状态统计模块
状态统计模块“ngx_http_stub_status_module”需要在编译Nginx时,使用“
--with-http_stub_status_module
”。使用以下命令判断模块是否可用:
# /usr/local/nginx/sbin/nginx -V 2>&1 | grep -o 'with-http_stub_status_module'
with-http_stub_status_module
配置Nginx,开启统计:
# cat /usr/local/nginx/conf/nginx.conf
···
server {
···
location = /nginx-status {
stub_status;
access_log off;
allow 127.0.0.1;
deny all;
}
}
重新加载Nginx:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
测试:
# curl http://127.0.0.1/nginx-status
Active connections: 5
server accepts handled requests
672 672 3416
Reading: 0 Writing: 1 Waiting: 4
各字段的含义:
- Active connections:当前活跃的客户端连接数,包括Waiting连接数;
- accepts:接受的客户端连接总数;
- handled:已处理的连接总数。通常与“accepts”的值相同,除非达到某些资源限制,如“worker_connections”;
- requests:客户端请求总数,一个客户端连接可能包含多个请求;
- Reading:Nginx正在读取请求标头的当前连接数;
- Writing:Nginx正在将响应写回给客户端的当前连接数;
- Waiting:当前等待请求的空闲连接数。
三、Zabbix前端页面链接模板
在Zabbix前端页面,点击“Configuration” -> “Templates”,找到名称为“Template App Nginx by Zabbix agent”的模板。
该模板定义了三个重要的参数:
-
:Nginx地址,默认值是“localhost”;{$NGINX.STUB_STATUS.HOST}
-
:状态统计路径,默认值是“basic_status”;{$NGINX.STUB_STATUS.PATH}
-
:Nginx端口,默认值是“80”。{$NGINX.STUB_STATUS.PORT}
如果需要更改某个参数的值,需要点击模板名称,选择“Macros(宏)”,见下图:
将该模板链接到对应的Nginx服务器上,等待采集数据即可。
注意事项:
- 如果在Nginx配置文件中,包含“server_tokens off;”,则该模板无法正常获取到Nginx版本号。
参考链接:https://www.zabbix.com/documentation/5.0/manual/config/templates_out_of_the_box/zabbix_agent