Zabbix实现对Nginx的监控
环境准备
五台机器:
zabbix_agent 5.0 被动模式(已安装) Zabbix_java_gateway 5.0 (已安装) 10.0.0.7
MariaDB-10.4.22(已安装) zabbix_server 5.0(已安装) 10.0.0.17
Tomcat-8.5(已安装) 10.0.0.27
zabbix_agent 5.0 主动模式(已安装)Redis-6.2.4(已安装)Nginx-1.18(已安装)10.0.0.37
MariaDB-10.4.22(已安装) zabbix_proxy 5.0 主动模式(已安装) 10.0.0.47
Nginx 状态页
基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误
注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态
[root@centos7 ~]
[root@centos7 nginx-1.18.0]
--with-http_stub_status_module enable ngx_http_stub_status_module
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections:
accepts:
handled:
requests:
Reading:
Writing:
Waiting:
1.添加Nginx状态页
[root@centos7 ~]
location / {
root html;
index index.html index.htm;
}
location /nginx_status {
stub_status;
allow 10.0.0.0/24;
allow 127.0.0.1;
deny all;
}
[root@centos7 ~]
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos7 ~]
[root@centos7 ~]
Active connections: 1
server accepts handled requests
8 8 9
Reading: 0 Writing: 1 Waiting: 0
2.Nginx监控脚本
[root@centos7 ~]
[root@centos7 zabbix_agentd.d]
nginx_status_fun(){
NGINX_PORT=$1
NGINX_COMMAND=$2
nginx_active(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
nginx_reading(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
nginx_writing(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
nginx_waiting(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
nginx_accepts(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
nginx_handled(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
nginx_requests(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{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;
esac
}
main(){
case $1 in
nginx_status)
nginx_status_fun $2 $3;
;;
*)
echo $"Usage: $0 {nginx_status key}"
esac
}
main $1 $2 $3
[root@centos7 zabbix_agentd.d]
[root@centos7 zabbix_agentd.d]
1
[root@centos7 zabbix_agentd.d]
1
3.zabbix agent添加自定义监控项
[root@centos7 zabbix_agentd.d]
UserParameter=redis_monitor[*],/bin/bash /etc/zabbix/zabbix_agentd.d/redis_monitor.sh $1 $2 $3
UserParameter=nginx.status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1 $2 $3
[root@centos7 zabbix_agentd.d]
UserParameter=redis_monitor[*],/bin/bash /etc/zabbix/zabbix_agentd.d/redis_monitor.sh $1 $2 $3
[root@centos7 zabbix_agentd.d]
4.zabbix server测试监控项数据
[root@zabbix_server ~]
1
[root@zabbix_server ~]
1
5.导入Nginx监控模板
点击【配置-模板-导入】
6.查看监控项
点击【配置-模板-nginx-template-magedu-jiege-监控项】
7.模板关联主机
点击【配置-主机-10.0.0.37-web2-active-模板】
8.验证监控项数据
|