zabbix监控nginx

可以参考:http://www.ttlsa.com/nginx/nginx-status-detail/

可以参考:http://www.ttlsa.com/zabbix/zabbix-monitor-nginx-performance/

通过status 模块监控 nginx

zabbix监控nginx_第1张图片

一、nginx配置

root@zabbix:/etc/nginx/conf.d# cat  /etc/nginx/conf.d/ping.conf 
server {
       listen 8090 ;
       server_name _;
       location /stub_status {
              stub_status on;
              access_log off;
       }
}
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@zabbix:/etc/nginx/conf.d# nginx -s reload

二、测试

root@zabbix:/etc/nginx/conf.d# curl localhost:8090/stub_status
Active connections: 2 
server accepts handled requests
 16 16 336 
Reading: 0 Writing: 1 Waiting: 1 

Active connections: 当前nginx正在处理的活动连接数.
Server accepts handled requests request_time: nginx总共处理了16个连接,成功创建16握手(证明中间没有失败的),总共处理了336个请求。
Reading: nginx读取到客户端的Header信息数.
Writing: nginx返回给客户端的Header信息数.
Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是nginx已经处理完成,正在等候下一次请求指令的驻留连接。
所以,在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量非常大,正在处理过程中。

三、自定义监控项:

root@zabbix:~# cat /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf 
UserParameter=nginx.status[*],/opt/zabbix/zabbix_nginx.sh $1

监控脚本

root@zabbix:~# vim /opt/zabbix/zabbix_nginx.sh
root@zabbix:~# cat /opt/zabbix/zabbix_nginx.sh 
#!/bin/bash
##################################################
# Description:zabbix 通过 status 模块监控 nginx
# Note:Zabbix 3.2
# DateTime: 2016-11-22
##################################################

HOST="localhost"
PORT="8090"
stub_status=stub_status

#检测nginx是否挂掉
function check() {
	if [ -f /sbin/pidof ]; then
	   /sbin/pidof nginx | wc -w
	else
	   ps ax | grep "nginx:" | grep -v grep | wc -l
	fi
}

#检测nginx性能
function active() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function accepts() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
function reading() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting() {
	/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}

case "$1" in
	check)
		check
		;;
	active)
		active
		;;
	accepts)
		accepts
		;;
	handled)
		handled
		;;
	requests)
		requests
		;;
	reading)
		reading
		;;
	writing)
		writing
		;;
	waiting)
		waiting
		;;

	*)
		echo "Usage $0 {check|active|accepts|handled|requests|reading|writing|waiting}"
		exit		
esac

加执行权限:

root@zabbix:~# chmod +x /opt/zabbix/zabbix_nginx.sh

重启zabbix-agent

root@zabbix:~# systemctl restart zabbix-agent.service

手动测试:

root@zabbix:~# zabbix_get -s 127.0.0.1 -p10050 -k nginx.status[check]
2
root@zabbix:~# zabbix_get -s 127.0.0.1 -p10050 -k nginx.status[check]
2
root@zabbix:~# zabbix_get -s 127.0.0.1 -p10050 -k nginx.status[active]
6
root@zabbix:~# zabbix_get -s 127.0.0.1 -p10050 -k nginx.status[accepts]
32

四、web界面定义监控项及触发器,写成模版

zabbix监控nginx_第2张图片

五、应用到主机上

zabbix监控nginx_第3张图片

六、手动停掉nginx看是否报警

监控项内容:

root@zabbix:~# date;systemctl stop nginx
2018年 12月 24日 星期一 12:53:45 CST
root@zabbix:~# zabbix_get -s 127.0.0.1 -p10050 -k nginx.status[check]
0

zabbix监控nginx_第4张图片

12:53:45停掉,12:54:18检测到

你可能感兴趣的:(nginx,zabbix)