Prometheus监控nginx

prometheus监控nginx

①nginx-module-vts

1.编译安装nginx

1.1 安装编译工具
apt -y install git wget gcc make zlib1g-dev build-essential libtool openssl libssl-dev
1.2 克隆nginx-module-vts模块
#要FQ
[root@elk91 ~]# git clone https://github.com/vozlt/nginx-module-vts.git
1.3 下载nginx软件包
[root@elk91 ~]# wget http://nginx.org/download/nginx-1.27.3.tar.gz
1.4 解压nginx
[root@elk91 ~]# tar xf nginx-1.27.3.tar.gz 
1.5 配置nginx
[root@elk91 ~]# cd nginx-1.27.3/
[root@elk91 nginx-1.27.3]# ./configure --prefix=/dezyan/softwares/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --without-http_rewrite_module --with-http_stub_status_module --without-http_gzip_module  --with-file-aio --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/nginx-module-vts
1.6 编译并安装nginx
make -j 2 && make install


2.修改nginx的配置文件

[root@elk91 nginx-1.27.3]# vim /dezyan/softwares/nginx/conf/nginx.conf
...
http {
    vhost_traffic_status_zone;
    upstream dezyan-promethues {
       server 10.0.0.31:9090;
    }
    ...
    server {
        ...
        location / {
            root   html;
            # index  index.html index.htm;
            proxy_pass http://dezyan-promethues;
        }

        location /nginx_status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

1.8 检查配置文件语法
[root@elk91 nginx-1.27.3]# /dezyan/softwares/nginx/sbin/nginx -t
nginx: the configuration file /dezyan/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /dezyan/softwares/nginx/conf/nginx.conf test is successful

3.启动nginx

[root@elk91 nginx-1.27.3]# systemctl disable --now nginx
[root@elk91 nginx-1.27.3]# /dezyan/softwares/nginx/sbin/nginx 

4.访问nginx的状态页面

http://10.0.0.91/nginx_status/
http://10.0.0.91/nginx_status/format/prometheus

5.修改Prometheus的配置文件

[root@prometheus-server31 ~]# tail -6 /dezyan/softwares/prometheus-2.53.4.linux-amd64/prometheus.yml 
  - job_name: dezyan-linux96-nginx-vts-modules
    metrics_path: "/nginx_status/format/prometheus"
    static_configs:
      - targets: 
        - 10.0.0.91:80
        
[root@prometheus-server31 ~]# curl -X POST http://10.0.0.31:9090/-/reload

6.导入模板ID

9785

②nginx-vtx-exporter

1.下载nginx-vtx-exporter

[root@elk92 ~]# wget https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.8/nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz

2.解压软件包到path路径

[root@elk92 ~]# tar xf nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz  -C /usr/local/bin/ nginx-vtx-exporter
[root@elk92 ~]# ll /usr/local/bin/nginx-vtx-exporter 
-rwxr-xr-x 1 1001 avahi 7950336 Jul 11  2023 /usr/local/bin/nginx-vtx-exporter*

3.运行nginx-vtx-exporter

[root@elk92 ~]# nginx-vtx-exporter -nginx.scrape_uri=http://10.0.0.91/nginx_status/format/json
2025/03/27 16:25:19 Starting nginx_vts_exporter (version=, branch=, revision=)
2025/03/27 16:25:19 Build context (go=go1.20.5, user=, date=)
2025/03/27 16:25:19 Starting Server at : :9913
2025/03/27 16:25:19 Metrics endpoint: /metrics
2025/03/27 16:25:19 Metrics namespace: nginx
2025/03/27 16:25:19 Scraping information from : http://10.0.0.91/nginx_status/format/json

4.访问nginx exporter的WebUI

http://10.0.0.92:9913/metrics

5.配置prometheus采集nginx数据

[root@prometheus-server31 ~]# tail -12  /dezyan/softwares/prometheus-2.53.4.linux-amd64/prometheus.yml 

  - job_name: dezyan-linux96-nginx-vts-modules
    metrics_path: "/nginx_status/format/prometheus"
    static_configs:
      - targets: 
        - 10.0.0.91:80

  - job_name: dezyan-linux96-nginx-vtx-exporter
    static_configs:
      - targets: 
        - 10.0.0.92:9913
        
        
[root@prometheus-server31 ~]# curl -X POST http://10.0.0.31:9090/-/reload

6.导入grafana模板

2949
posted @ 2025-04-18 16:38  丁志岩  阅读(29)  评论(0)    收藏  举报