zabbix版本:V2.2
nginx版本:V1.8.0
一、Nginx基础活跃指标
图解一个客户端连接开源版本的Nginx情况:
Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减。
名称 | 描述 | 指标类型 |
---|---|---|
Accepts(接受) | NGINX 所接受的客户端连接数 | 资源: 功能 |
Handled(已处理) | 成功的客户端连接数 | 资源: 功能 |
Dropped(已丢弃,计算得出) | 丢弃的连接数(接受 - 已处理) | 工作:错误* |
Requests(请求数) | 客户端请求数 | 工作:吞吐量 |
NGINX worker 进程接受 OS 的连接请求时 Accepts 计数器增加,而Handled 是当实际的请求得到连接时(通过建立一个新的连接或重新使用一个空闲的)。这两个计数器的值通常都是相同的,如果它们有差别则表明连接被Dropped,往往这是由于资源限制,比如已经达到 NGINX 的worker_connections的限制。
一旦 NGINX 成功处理一个连接时,连接会移动到Active状态,在这里对客户端请求进行处理:
Active状态
Waiting: 活跃的连接也可以处于 Waiting 子状态,如果有在此刻没有活跃请求的话。新连接可以绕过这个状态并直接变为到 Reading 状态,最常见的是在使用“accept filter(接受过滤器)” 和 “deferred accept(延迟接受)”时,在这种情况下,NGINX 不会接收 worker 进程的通知,直到它具有足够的数据才开始响应。如果连接设置为 keep-alive ,那么它在发送响应后将处于等待状态。
Reading: 当接收到请求时,连接离开 Waiting 状态,并且该请求本身使 Reading 状态计数增加。在这种状态下 NGINX 会读取客户端请求首部。请求首部是比较小的,因此这通常是一个快速的操作。
Writing: 请求被读取之后,其使 Writing 状态计数增加,并保持在该状态,直到响应返回给客户端。这意味着,该请求在 Writing 状态时, 一方面 NGINX 等待来自上游系统的结果(系统放在 NGINX “后面”),另外一方面,NGINX 也在同时响应。请求往往会在 Writing 状态花费大量的时间。
通常,一个连接在同一时间只接受一个请求。在这种情况下,Active 连接的数目 == Waiting 的连接 + Reading 请求 + Writing 。
二、Nginx相关指标的收集
首先在nginx配置文件中打开stub_status功能,并通过nginx -s reload命令重新加载配置文件
vim /etc/nginx/nginx.conf
server {
#zabbix监控nginx基本活跃指标
location /ngx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
nginx -s reload
通过curl命令,获取指标
curl http://127.0.0.1/ngx_status
Active connections: 1
server accepts handled requests
13791578 13791578 13796087
Reading: 0 Writing: 1 Waiting: 0
三、zabbix监控脚本
编辑的时候没有找到上传附件的地方,就直接将脚本和模板贴在下面了。
1.配置zabbix_agent.conf
vim /etc/zabbix/zabbix_agent.conf
UnsafeUserParameters=1
UserParameter=nginx.status[*],/opt/zabbix/ngx-status.sh $1
service zabbix-agent restart
2.配置ngx-status.sh
touch /opt/zabbix/ngx-status.sh
chown zabbix:zabbix /opt/zabbix/ngx-status.sh
chmow u+x /opt/zabbix/ngx-status.sh
vim /opt/zabbix/ngx-status.sh
#!/bin/bash
###########################nginx_check###########################
# V20170404 #
#active|reading|writing|waiting|accepts|handled|requests|dropped#
###########################nginx_check###########################
HOST="127.0.0.1"
PORT="80"
# 检测nginx相关参数
case $1 in
ping)
result=`/bin/pidof nginx 2>/dev/null| wc -l`
echo $result
;;
active)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'`
echo $result
;;
reading)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'`
echo $result
;;
writing)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'`
echo $result
;;
waiting)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'`
echo $result
;;
accepts)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'`
echo $result
;;
handled)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'`
echo $result
;;
requests)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'`
echo $result
;;
dropped)
result=`/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1-$2}'`
echo $result
;;
*)
echo "Usage:$0(ping|active|reading|writing|waiting|accepts|handled|requests|dropped)"
;;
esac
3.zabbix_web界面加载template
将ngx-statust_templates.xml导入zabbix里,再关联host即可
2.0
2017-04-04T07:15:54Z
Templates
Template App NGINX
Template App NGINX
Templates
nginx
-
nginx status connections active
0
0
nginx.status[active]
30
90
365
0
3
0
0
0
0
1
0
0
active
0
nginx
-
nginx status connections reading
0
0
nginx.status[reading]
30
90
365
0
3
0
0
0
0
1
0
0
reading
0
nginx
-
nginx status connections waiting
0
0
nginx.status[waiting]
30
90
365
0
3
0
0
0
0
1
0
0
waiting
0
nginx
-
nginx status connections writing
0
0
nginx.status[writing]
30
90
365
0
3
0
0
0
0
1
0
0
writing
0
nginx
-
nginx status PING
0
0
nginx.status[ping]
30
30
365
0
3
0
0
0
0
1
0
0
is live
0
nginx
Service state
-
nginx status server accepts
0
0
nginx.status[accepts]
30
90
365
0
3
2
0
0
0
1
0
0
accepts
0
nginx
-
nginx status server dropped
0
0
nginx.status[dropped]
30
90
365
0
3
0
0
0
0
1
0
0
active
0
nginx
-
nginx status server handled
0
0
nginx.status[handled]
30
90
365
0
3
2
0
0
0
1
0
0
handled
0
nginx
-
nginx status server requests
0
0
nginx.status[requests]
30
90
365
0
3
2
0
0
0
1
0
0
requests
0
nginx
Nginx performance
2
1
0
500
100
0
0
1
1
0
0
0
0
0
nginx status server
Template App NGINX
0
500
100
1
0
1
1
0
0
0
0
0
nginx status connections
Template App NGINX
{Template App NGINX:nginx.status[ping].last()}=0
nginx was down!
0
4
NGINX进程数:0,请注意
0
{Template App NGINX:nginx.status[accepts].avg(3m)}>400
Too many accepts nginx connections on {HOST.NAME}
0
4
accepts nginx connections is too many
0
{Template App NGINX:nginx.status[active].avg(3m)}>150
Too many active nginx connections on {HOST.NAME}
0
4
nginx active connections is too many
0
{Template App NGINX:nginx.status[dropped].avg(3m)}>10
Too many dropped nginx connections on {HOST.NAME}
0
4
accepts nginx connections is too many
0
nginx status connections
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
2
00C800
0
2
0
-
Template App NGINX
nginx.status[active]
1
2
C80000
0
2
0
-
Template App NGINX
nginx.status[reading]
2
2
0000C8
0
2
0
-
Template App NGINX
nginx.status[waiting]
3
2
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
2
00C800
0
2
0
-
Template App NGINX
nginx.status[accepts]
1
2
C80000
0
2
0
-
Template App NGINX
nginx.status[handled]
2
2
0000C8
0
2
0
-
Template App NGINX
nginx.status[requests]
3
2
C800C8
0
2
0
-
Template App NGINX
nginx.status[dropped]
四、参考资料
Nginx主要指标
Nginx指标收集