zabbix添加Nginx-stub_status监控
Nginx性能监控健康检查
1.安装Nginx-stub_status_module
首先要安装Nginx的--with-http_stub_status_module
#查看这里是没有安装启用stub_status_module
]# ./nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments:
#注意的是如果之前启用了其它的模块一定要在升级Nginx时候都要一样
下载相应的Nginx版本 进行升级配置 ,这里用Nginx的1.140,查看官网没有1.140,使用1.14.2
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xf nginx-1.14.2.tar.gz
cd nginx-1.14.2/
#这里我同时安装了 http_ssl_module http_stub_status_module这两个模块
#由于原来的安装没有安装这些 Nginx ssl 依赖库没有,通过yum 解决了依赖包
yum install openssl openssl-devel -y
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#进行编译
make
#注意 只是编译不安装 不要执行make install
cd /usr/local/nginx/sbin/
#备份原来的Nginx主程序
cp nginx nginx.bak
#停止Nginx
./nginx -s stop
#覆盖原来的Nginx程序
\cp /opt/nginx-1.14.2/objs/nginx /usr/local/nginx/sbin/
#启动Nginx
./nginx
#检查安装模块
]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
2.添加Nginx监控用 location
#vim /usr/local/nginx/conf/nginx.conf
location = /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
#nginx -s reload
3.准备监控脚本
#我的agent是zabbix_agent2,要注意自己的版本
[root@p-f zabbix_agent2.d]# pwd
/etc/zabbix/zabbix_agent2.d
[root@p-f zabbix_agent2.d]# cat nginx_monitor.sh
#!/bin/bash
#注意Nginx监控的端口
NGINX_PORT=80
NGINX_COMMAND=$1
nginx_active(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk '/Active/ {print $NF}'
}
nginx_reading(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk '/Reading/ {print $2}'
}
nginx_writing(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk '/Writing/ {print $4}'
}
nginx_waiting(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk '/Waiting/ {print $6}'
}
nginx_accepts(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk 'NR==3 {print $1}'
}
nginx_handled(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk 'NR==3 {print $2}'
}
nginx_requests(){
/usr/bin/curl -s http://127.0.0.1:$NGINX_PORT/nginx_status |awk 'NR==3 {print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
*)
echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac
5.agent的配置文件
[root@p-f zabbix_agent2.d]# cat nginx_monitor.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agent2.d/nginx_monitor.sh "$1"
#重启
systemctl restart zabbix-agent2.service
#服务器端zabbix_server命令测试,server端验证是否能获取agent端的key值。
~]# zabbix_get -s 192.168.9.10 -k nginx_status[active]
2
所有键值
nginx_status[active]
nginx_status[reading]
nginx_status[writing]
nginx_status[waiting]
nginx_status[accepts]
nginx_status[handled]
nginx_status[requests]
6.web页面添加监控
添加模板
定义应用集
定义监控项
监控项要添加所有键值对 这里共7个,截图示例: