Nginx教程

监控Nginx

本文主要是介绍监控Nginx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

编译Nginx

[root@zabbix-web01 nginx-1.18.0]# ./configure --prefix=/apps/nginx --with-http_stub_status_module

编译安装

[root@zabbix-web01 nginx-1.18.0]# make && make install

如果是apt源安装的nginx默认路径在;优先级高的原因在于default_server,删除如下:

[root@zabbix-web02 ~]# vim /etc/nginx/sites-enabled/default

范例:

[root@zabbix-web02 ~]# cat /etc/nginx/conf.d/nginx-statu.conf
server{
   listen 80 default_server;
   server_name _;
   location / {
      root /var/www/html;
      index index.html;
   }
   location /ngx_status {
      stub_status;
      allow 172.31.1.0/21;
      allow 127.0.0.1;
      deny all;
   }
}

修改配置文件

[root@zabbix-web01 nginx-1.18.0]# vim /apps/nginx/conf/nginx.conf

    location /nginx_status {
        stub_status;
        allow 172.31.1.0/21;
        allow 127.0.0.1;
        deny all;
    }

检查语法

[root@zabbix-web01 nginx-1.18.0]# /apps/nginx/sbin/nginx -t
nginx: [warn] low address bits of 172.31.1.0/21 are meaningless in /apps/nginx/conf/nginx.conf:50
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

启动

[root@zabbix-web01 nginx-1.18.0]# /apps/nginx/sbin/nginx

浏览器访问
http://172.31.1.25/nginx_status

监控Nginx脚本

[root@zabbix-web01 zabbix_agentd.d]# cat nginx_status.sh
#!/bin/bash
#
#********************************************************************
#Author:                xuanlv
#QQ:                    360956175
#Date:                  2021-07-31
#FileName:             nginx_status.sh
#URL:                   https://www.cnblogs.com/xuanlv-0413/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
nginx_status_fun(){
    NG_PORT=$1
    NG_COMMAND=$2
    nginx_active(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | grep 'Active' | awk '{print $NF}'
    }
    nginx_reading(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | grep 'Reading' | awk '{print $2}'
    }
    nginx_writing(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | grep 'Writing' | awk '{print $4}'
    }
    nginx_waiting(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | grep 'Waiting' | awk '{print $6}'
    }
    nginx_accepts(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{print $1}'
    }
    nginx_handled(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{print $2}'
    }
    nginx_requests(){
      /usr/bin/curl "http://127.0.0.1:"$NG_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{print $3}'
    }
    case $NG_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@zabbix-web01 zabbix_agentd.d]# chmod a+x nginx_status.sh

测试

[root@zabbix-web01 zabbix_agentd.d]# bash nginx_status.sh nginx_status 80 active
1

修改agent配置文件

[root@zabbix-web01 zabbix]# vim /etc/zabbix/zabbix_agentd.conf

UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"

重启

[root@zabbix-web01 zabbix]# systemctl restart zabbix-agent.service

server测试

[root@zabbix-server ~]# zabbix_get -s 172.31.1.25 -p 10050 -k "nginx.status["nginx_status","80","active"]"
1

浏览器添加

创建模板---创建监控项---创建触发器---创建图形---主机关联模板---查看图形

这篇关于监控Nginx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!