Prometheus通过Nginx-vts-exporter模块监控nginx服务

文章目录

    • 1、编译安装nginx
      • [1.1 下载nginx-module-vts 第三方模块](#1.1 下载nginx-module-vts 第三方模块)
      • [1.2 下载nginx源码包并编译安装](#1.2 下载nginx源码包并编译安装)
    • [2. 配置nginx](#2. 配置nginx)
      • [2.1 修改配置文件](#2.1 修改配置文件)
      • [2.2 检查语法,启动服务](#2.2 检查语法,启动服务)
      • [2.3 查看vhost_traffic_status的监控页面](#2.3 查看vhost_traffic_status的监控页面)
    • [3. 安装Nginx-vts-exporter](#3. 安装Nginx-vts-exporter)
      • [3.1 下载Nginx-exporter](#3.1 下载Nginx-exporter)
      • [3.2 准备nginx-vts-exporter启动文件,使用-nginx.scrape_uri指定url路径,同时指定为json格式,否则数据转换失败](#3.2 准备nginx-vts-exporter启动文件,使用-nginx.scrape_uri指定url路径,同时指定为json格式,否则数据转换失败)
      • [3.3 启动ningx-exporter, 检查9913端口是否存在](#3.3 启动ningx-exporter, 检查9913端口是否存在)
    • [4. 配置Prometheus](#4. 配置Prometheus)
      • [4.1 修改Prometheus配置文件](#4.1 修改Prometheus配置文件)
      • [4.2 加载 Prometheus 配置](#4.2 加载 Prometheus 配置)
      • [4.3 检查Prometheus是否将对应nginx节点纳入监控](#4.3 检查Prometheus是否将对应nginx节点纳入监控)
    • [5. 导入Grafana模板](#5. 导入Grafana模板)
      • [5.1 下载官方提供的Dashboard, 或者选择2949](#5.1 下载官方提供的Dashboard, 或者选择2949)
      • [5.2 在Grafana中导入该json文件](#5.2 在Grafana中导入该json文件)

1、编译安装nginx

1.1 下载nginx-module-vts 第三方模块

bash 复制代码
[root@node02 ~]#wget https://codeload.github.com/vozlt/nginx-module-vts/tar.gz/refs/tags/v0.2.1
[root@node02 ~]#tar xf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz 

1.2 下载nginx源码包并编译安装

bash 复制代码
[root@node02 ~]# yum install gcc pcre-devel openssl-devel zlib-devel -y
[root@node02 ~]# wget https://nginx.org/download/nginx-1.22.1.tar.gz
[root@node02 ~]# tar xf nginx-1.22.1.tar.gz
[root@node02 ~]# cd nginx-1.22.1
[root@node02 ~]# ./configure --prefix=/usr/local/nginx \
                             --with-http_ssl_module \
                             --add-module=../nginx-module-vts-0.2.1
[root@node02 ~]# make -j 2  && make install

2. 配置nginx

2.1 修改配置文件

bash 复制代码
 [root@node02 ~]# cat /usr/local/nginx/conf/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    vhost_traffic_status_zone; 

    server {           # 找一个稳定的虚拟主机添加如下location即可
        listen       80;
        server_name  www.test.com;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /nginx_vhost_status {
            vhost_traffic_status_display;    # 显示该虚拟主机的流量信息
            vhost_traffic_status_display_format html; # 显示格式为html
        }
    }
    
    server {
        listen       80;
        server_name  news.test.com;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

 # 定义一组测试使用的负载均衡
    upstream map {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }
    server {
        listen       80;
        server_name  map.test.com;
        location / {
            proxy_pass http://map;
            proxy_set_header Host $http_host;
        }
    }
}

2.2 检查语法,启动服务

bash 复制代码
[root@node02 ~]# /usr/local/nginx//sbin/nginx -t
[root@node02 ~]# /usr/local/nginx//sbin/nginx -s reload

2.3 查看vhost_traffic_status的监控页面

3. 安装Nginx-vts-exporter

3.1 下载Nginx-exporter

bash 复制代码
[root@node02 ~]# wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
[root@node02 ~]# tar xf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
[root@node02 ~]# cp nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/bin/

3.2 准备nginx-vts-exporter启动文件,使用-nginx.scrape_uri指定url路径,同时指定为json格式,否则数据转换失败

bash 复制代码
[root@node02 ~]# cat > /usr/lib/systemd/system/nginx-vts-exporter.service <<EOF
[Unit]
Description=nginx-vts-exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
ExecStart=/usr/local/bin/nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/nginx_vhost_status/format/json
Restart=on-failure
RestartSec=20

[Install]
WantedBy=multi-user.target
EOF

3.3 启动ningx-exporter, 检查9913端口是否存在

bash 复制代码
[root@node02 ~]# systemctl daemon-reload
[root@node02 ~]# systemctl restart nginx-vts-exporter
[root@node02 ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      878460/nginx: maste     
tcp6       0      0 :::9913                 :::*                    LISTEN      3943361/nginx-vts-e 

4. 配置Prometheus

4.1 修改Prometheus配置文件

bash 复制代码
 [root@node01 ~]# /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: "nginx-exporter"
    metrics_path: "/metrics"
    static_configs:
    - targets: ["node02:9913"]

4.2 加载 Prometheus 配置

bash 复制代码
 [root@node01 ~]# curl -X  POST http://node01:9090/-/reload

4.3 检查Prometheus是否将对应nginx节点纳入监控

5. 导入Grafana模板

5.1 下载官方提供的Dashboard, 或者选择2949

bash 复制代码
wget https://raw.githubusercontent.com/hnlq715/nginx-vts-exporter/master/dashboard/nginx-vts-exporter.json

5.2 在Grafana中导入该json文件

相关推荐
Decay__42 分钟前
PostgreSQL 告警与自动化运维 —— 告警分级收敛、Webhook、定时巡检、脚本库
运维·数据库·sql·postgresql
程序员agions2 小时前
Docker Compose 生产环境配置记录
运维·docker·容器
郝学胜-神的一滴2 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
Mikko72 小时前
Redis 主从切换期间可用吗?6.2 实测哨兵切换:应用不可写 33 秒,主从连不上、数据不一致,以及设了 ACL 后哨兵不切换
java·运维·redis·后端
进击的荆棘4 小时前
Linux网络——应用层协议HTTP
linux·运维·服务器·网络·http
迪康软件zz4 小时前
敏感文件判定引擎:从单词命中到加权打分
运维·安全·自动化运维
JasonChen@6665 小时前
Prometheus 存储实战:本地 TSDB 配置与 VictoriaMetrics 远端存储对接(18)
grafana·prometheus·victoria
jipinxiaofeihua5 小时前
(文章7) 04章-C++异常处理(下)
linux·运维·网络
yyzc1111116 小时前
当中小微实体店面临经营困境时如何借助顺成汇创旗下荟生意品牌实现数字化智慧营销
大数据·运维·人工智能
Rsingstarzengjx19 小时前
第三十九章 Linux PWM 驱动实验
linux·运维·服务器