nginx内置了一个status状态的功能,通过配置可以看到nginx的运行情况,status显示的内容包括当前连接数,处于活动状态的连接数,已经处理的请求数等等,可以利用这个功能编写zabbix监控nginx的脚本。
1 nginx配置
nginx配置文件,开启status功能
location / {
root html;
index index.php index.html index.htm;
}
#nginx status
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
allow 127.0.0.1;
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
nginx状态解释
Active connections Nginx正处理的活动链接数个数;重要
server Nginx启动到现在共处理了多少个连接。
accepts Nginx启动到现在共成功创建几次握手。
handled requests Nginx总共处理了几次请求。
Reading Nginx读取到客户端的 Header 信息数。
Writing Nginx返回给客户端的 Header 信息数。
Waiting Nginx已经处理完正在等候下一次请求指令的驻留链接,开启。
Keep-alive的情况下,Waiting这个值等于active-(reading + writing)。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。
2 zabbix-agent配置
添加监控脚本
nginx_status.sh 放于 /etc/zabbix/zabbix_agentd.d/目录下
#!/bin/bash
#Script to fetch nginx statuses for monitoring systems
#Author Tony
HOST="127.0.0.1"
PORT="80"
function ping {
/sbin/pidof nginx | wc -l
}
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}'
}
$1
添加zabbix配置文件,放于 /etc/zabbix/zabbix_agentd.d/目录下(agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录)创建配置文件nginx_status.conf
cat nginx_status.conf
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1
确保配置Agent配置文件开启自定义参数UnsafeUserParameters=1
[root@Node1 zabbix_agentd.d]# !grep
grep -n '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
57:DebugLevel=3
97:Server=172.17.21.208
138:ServerActive=172.17.21.208
149:Hostname=Node1.contoso.com
267:Include=/etc/zabbix/zabbix_agentd.d/*.conf
286:UnsafeUserParameters=1 //1代表允许,0代表关闭
重启zabbix-agent服务
systemctl restart zabbix-agent.service
在zabbix servere服务器上测试,是否能正常获取数据
zabbix_get -s
172.17.21.206 -p 10050 -k nginx.status[ping]
1
4. 监控页面导入tcp连接状态模板
模板
3.4
2018-01-18T11:00:49Z
Templates
Template App NGINX
Template App NGINX
nginx statuses
Author:371304
Templates
Nginx
-
nginx status server accepts
7
nginx.status[accepts]
30s
90d
365d
0
3
0
0
0
0
accepts
0
Nginx
-
nginx status connections active
7
nginx.status[active]
30s
90d
365d
0
3
0
0
0
0
acitve
0
Nginx
-
nginx status server handled
7
nginx.status[handled]
30s
90d
365d
0
3
0
0
0
0
handled
0
Nginx
-
nginx status PING
7
nginx.status[ping]
30s
90d
365d
0
3
0
0
0
0
ping
0
Nginx
-
nginx status connections reading
7
nginx.status[reading]
30s
90d
365d
0
3
0
0
0
0
reading
0
Nginx
-
nginx status server requests
7
nginx.status[requests]
30s
90d
365d
0
3
0
0
0
0
requests
0
Nginx
-
nginx status connections waiting
7
nginx.status[waiting]
30s
90d
365d
0
3
0
0
0
0
waiting
0
Nginx
-
nginx status connections writing
7
nginx.status[writing]
30s
90d
365d
0
3
0
0
0
0
writing
0
Nginx
{Template App NGINX:nginx.status[ping].last()}=0
0
nginx was down!
0
0
4
nginx was down!
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
1A7C11
0
2
0
-
Template App NGINX
nginx.status[active]
1
0
F63100
0
2
0
-
Template App NGINX
nginx.status[reading]
2
0
2774A4
0
2
0
-
Template App NGINX
nginx.status[waiting]
3
0
A54F10
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
1A7C11
0
2
0
-
Template App NGINX
nginx.status[accepts]
1
0
F63100
0
2
0
-
Template App NGINX
nginx.status[handled]
2
0
2774A4
0
2
0
-
Template App NGINX
nginx.status[requests]