1.部署Nginx服务
#安装nginx [root@web01 ~]# yum install -y nginx #配置nginx文件 [root@web01 ~]# cat /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /nginx_status { stub_status on; allow 127.0.0.1; deny all; } } #检查nginx配置文件 [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #重启nginx服务 [root@web01 ~]# systemctl restart nginx #curl访问测试 [root@web01 ~]# curl http://127.0.0.1/nginx_status Active connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0
2.编写监听脚本并赋予执行权限
[root@web01 ~]# mkdir -p /server/scripts [root@web01 ~]# cd /server/scripts [root@web01 /server/scripts]# vim nginx_status.sh #!/bin/bash NGINX_PORT=80 #如果端口不同仅需要修改脚本即可,否则修改xml很麻烦 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 #赋予脚本执行权限 [root@web01 /server/scripts]# chmod +x nginx_status.sh
3.编写zabbix监控项
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf UserParameter=nginx_status[*],/bin/bash /server/scripts/nginx_status.sh "$1" #重启nginx客户端 [root@web01 ~]# systemctl restart zabbix-agent #客户端获取值测试 [root@web01 ~]# zabbix_agentd -t nginx_status[active] nginx_status[active] [t|1] #zabbix服务端获取值测试 [root@zabbix-server ~]# zabbix_get -s 172.16.1.7 -k nginx_status[active] 1
4.导入监控nginx状态模板
5.选择Nginx Start模板并导入(自行找我索要)
6.发现已经导入成功了,选择监控项
7.创建监控项
这里每个监控nginx状态后面必须是绿色已启动的
然后创建监控项,来监控nginx的端口
然后创建监控项,来监控nginx的端口