一、为nginx开启status状态
在server中添加如下代码,xxx.xxx.xxx.xxx填写你的监控服务器地址。
location /nginx_status{ stub_status on; access_log off; allow 127.0.0.1; allow xxx.xxx.xxx.xxx; deny all; }
二、写监控脚本
大家可以根据要监控的主机地址及端口进行调节脚本。
# vim /usr/local/zabbix/scripts/nginx
#!/bin/bash HOST="10.0.0.10" PORT="80" 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}' } # Run the requested function $1
三、配置Key
# vim /etc/zabbix/zabbix_agentd.conf
# monitor nginx status UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx $1
四、导入模版
模版在我的附件中,导入即可直接使用。
五、结果查看
六、状态参数讲解
Active connections: 11921 server accepts handled requests 11989 11989 11991 Reading: 0 Writing: 7 Waiting: 42
active connections – 活跃的连接数量
server accepts handled requests — 总共处理了11989个连接 , 成功创建11989次握手, 总共处理了11991个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
所以,在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量。