在location添加下列内容:
server {
location /ngx_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
示例:
······
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
}
location /goaccess {
root /usr/local/src;
}
location /ngx_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
}
······
ngixn -s reload
curl http://127.0.0.1/ngx_status
Active connections: 3
server accepts handled requests
123630 123630 477618
Reading: 0 Writing: 1 Waiting: 2
若启用https和域名访问,则直接将IP地址换成域名:
curl https://domain/ngx_status
以上为nginx性能计数,我们除了监控以上数据,还需要监控nginx进程状态,并且配置触发器
#!/bin/bash
HOST="127.0.0.1"
PORT="80"
# 检测nginx进程是否存在
function ping() {
/usr/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
$1
若采用
https
的域名方式访问,则将脚本中的http
替换成https
,IP地址替换成域名即可
chmod +x /etc/zabbix/zabbix_agentd.d/nginx_status.sh
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1
systemctl restart zabbix-agent
zabbix_get -s 127.0.0.1 -p 10050 -k 'nginx.status[accepts]'
123833
zabbix_get -s 127.0.0.1 -p 10050 -k 'nginx.status[ping]'
1
注意:不要添加错了,zabbix自带nginx模板不过我们不用
本文摘自:Zabbix监控Nginx性能的实现方式
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:zabbix4.4.7监控nginx状态