声明参考地址:http://www.ttlsa.com/zabbix/zabbix-monitor-nginx-performance/
1.zabbix需要监控nginx,则nginx需要配置ngx_status
(1) 编译安装时带上--with-http_stub_status_module参数:
./configure --prefix=/usr/local/nginx-1.11.5 --with-http_ssl_module --with-http_stub_status_module --with-pcre
(2)vim /usr/local/nginx-1.11.5/conf/nginx.conf 在server内添加以下内容:
location /ngx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.0.150; #(zabbix服务器的IP地址,一般是内网地址)
deny all;
}
(3)检测--with-http_stub_status_module参数是否生效
![](https://images2015.cnblogs.com/blog/948623/201612/948623-20161206171751616-1115883679.png)
2.编写客户端脚本 vim /usr/local/zabbix-3.2.1/scripts/ngx-status.sh 赋予执行权限chmod a+x /usr/local/zabbix-3.2.1/scripts/ngx-status.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
$1
3.将自定义的UserParameter加入zabbix_agentd.conf配置文件,然后重启agentd
vim /usr/local/zabbix-3.2.1/etc/zabbix_agentd.conf
UserParameter=nginx.status[*],/usr/local/zabbix-3.2.1/scripts/ngx-status.sh $1
重启agentd
4.zabbix_get 获取数据,检测配置是否正确
/usr/local/zabbix-3.2.1/bin/zabbix_get -s 127.0.0.1 -k 'nginx.status[accept]'
/usr/local/zabbix-3.2.1/bin/zabbix_get -s 127.0.0.1 -k 'nginx.status[ping]'
6.导入模板:配置、模板、导入模板
7.给该主机添加nginx的监控项:
![](https://images2015.cnblogs.com/blog/948623/201612/948623-20161206173253194-1714242844.png)
8.完成,查看图形:
![](https://images2015.cnblogs.com/blog/948623/201612/948623-20161206173351757-1453675530.png)
交流qq1071179133