1、nginx配置

server {
    listen 80 default_server;
    server_name 127.0.0.1;
    location /nginx_status 
    {
    stub_status on;   #nginx状态打开
    allow 127.0.0.1;  #只允许本机访问
    deny all;
    } 
    }

2、zabbix-agent端配置

1>nginx监控脚本

[root@prod-admin-01 script]# cat ngx_status.sh 
#!/bin/bash
HOST="127.0.0.1"   
PORT="80"
function ng_pid_count {
 /sbin/pidof nginx | wc -l 
}
# check nginx
active() {
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
reading (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
writing (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
waiting (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
accepts (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
handled (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
requests (){
 /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
#此处为输入参数,对应前面的函数
$1  

#脚本测试
~]# sh ngx_status.sh {request,active...}

zabbix 6.0 监控nginx_zabbix nginx监控

将脚本放置到zabbix-agentd 目录

cp ngx_status.sh /etc/zabbix/zabbix_agentd.d

2>zabbix agent安装配置


http://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/


yum install zabbix-agent-6.0.0-1.el7.x86_64 -y
[root@tke01 zabbix]# cat zabbix_agentd.conf | grep -v "#" | grep -v "^$"
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=x.x.x.x
ServerActive=x.x.x.x
Hostname=tke01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
UserParameter=ngx_status[*],/etc/zabbix/zabbix_agentd.d/ngx_status.sh "$1"

#启动
~]# systemctl enable zabbix-agent.service
~]# systemctl start zabbix-agent.service

3、zabbix-server nginx 配置模板

1>item配置

1)创建模板

zabbix 6.0 监控nginx_zabbix nginx监控_02

2)加入监控项

ngx_pid_count 监控项配置

zabbix 6.0 监控nginx_zabbix nginx监控_03

所有ngx_status所有监控项加入

zabbix 6.0 监控nginx_zabbix nginx监控_042> 图形聚合

zabbix 6.0 监控nginx_zabbix nginx监控_05

4、展示

zabbix 6.0 监控nginx_zabbix nginx监控_06

5、nginx报警

主要针对nginx进程的报警,先执行查看ng_pid_count进程数,小于这个数则报警

zabbix 6.0 监控nginx_zabbix nginx监控_07