zabbix自定义监控nginx
agent端配置
1.nginx编译安装需要加上该选项--with-http_stub_status_module
2.修改nginx配置文件
[root@cwcfsnginx01-21 ~]# vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
[root@cwcfsnginx01-21 ~]# nginx -s reload
3.nginx参数说明
[root@cwcfsnginx01-21 ~]# curl 127.0.0.1/ngx_status
Active connections: 1
server accepts handled requests
292 292 356
Reading: 0 Writing: 1 Waiting: 0
Active connections:Nginx正处理的活动链接数个数;重要
Server accepts handled requests:Nginx总共处理了292个连接,成功创建292次握手(证明中间没有失败的),总共处理了356个请求。
Reading:Nginx读取到客户端的Header信息数。
Writing:Nginx 返回给客户端的Header信息数。
Waiting:开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接。
4.获取nginx状态的shell脚本
[root@cwcfsnginx01-21 ~]# cat /usr/local/zabbix-agent/shell/nginx_check.sh
#!/bin/bash
HOST="127.0.0.1"
PORT="80"
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
case $1 in
ping)
ping;
;;
active)
active;
;;
reading)
reading;
;;
writing)
writing;
;;
waiting)
waiting;
;;
accepts)
accepts;
;;
handled)
handled;
;;
requests)
requests;
;;
*)
echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac
添加脚本执行权限:
chmod +x /usr/local/zabbix-agent/shell/nginx_check.sh
重启zabbix-agent服务:
[root@cwcfsnginx01-21 ~]# systemctl restart zabbix-agent
5.修改zabbix agent的配置文件
[root@cwcfsnginx01-21 ~]# cat /etc/zabbix/zabbix_agentd.conf |grep -vE "#|^$"
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.30.54
ServerActive=192.168.30.54
Hostname=agent_21
Include=/etc/zabbix/zabbix_agentd.d/
UnsafeUserParameters=1
UserParameter=nginx.status[*],/usr/local/zabbix-agent/shell/nginx_check.sh $1
服务端配置
1.server端验证是否能获取agent端的key值
[root@cwcfsapp010-54 ~]# zabbix_get -s 192.168.31.21 -k nginx.status[accepts]
508
2.添加监控项
创建模板
创建应用集
创建监控项(这里只添加了一个)
创建图形
添加到指定主机
查看最新数据