参考文章:http://www.ttlsa.com/zabbix/zabbix-monitor-nginx-performance/
环境:
Zabbix Server: 172.16.0.174
Nginx Server(Zabbix agent): 172.16.0.233
一、准备工作:
Nginx编译安装时,要加入以下两个参数
--with-http_ssl_module
--with-http_stub_status_module
没加的话,重新编译安装
Nginx开启status状态信息
在nginx.conf配置文件中加入
server {
listen 80;
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 172.16.0.174; #允许zabbix server访问
deny all;
}
}
配置完重新加载nginx,然后测试效果
[root@localhost conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload
[root@localhost conf]# curl http://127.0.0.1/nginx_status
Active connections: 72
server accepts handled requests
540651 540651 1613382
Reading: 0 Writing: 1 Waiting: 71
二、编写客户端脚本
[root@localhost ~]# mkdir /usr/local/zabbix/scripts
[root@localhost ~]# cat /usr/local/zabbix/scripts/nginx-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/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading'| awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing'| awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting'| awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
#执行function
$1
给脚本加入权限
[root@localhost ~]# chmod +x /usr/local/zabbix/scripts/nginx-status.sh
三、修改zabbix_agentd.conf,指定参数和脚本路径,重启zabbix_agentd
# UserParameter=
UserParameter=nginx.status[*],/usr/local/zabbix/scripts/nginx-status.sh $1
四、测试配置效果
在Zabbix Server上运行以下命令
[root@ZabbixServer etc]# zabbix_get -s 172.16.0.233 -k 'nginx.status[accepts]'
534458
[root@ZabbixServer etc]# zabbix_get -s 172.16.0.233 -k 'nginx.status[requests]'
1621474
有数据返回,说明配置成功
五、导入模板,并将模板添加到主机中
模板下载地址Template App NGINX
主机添加模板后,过一会就会出现图形