[转帖]prometheus监控nginxt的两种方法(vts)

prometheus,监控,nginxt,两种,方法,vts · 浏览次数 : 0

小编点评

**生成内容步骤** 1. **创建自动发现脚本** - 将自动发现脚本配置到`/data/prometheus/conf/rules/nginx.rules`` - 内容包含自动发现的配置,例如`docker run`、`metrics_path`和`file_sd_configs` 2. **添加自动发现脚本** - 将自动发现脚本配置到`/data/prometheus/conf/rules/nginx.rules`` - 内容包含自动发现的配置,例如`docker run`、`metrics_path`和`file_sd_configs` 3. **生成内容** - 根据`/data/prometheus/conf/rules/nginx.rules``内容,生成内容时需要带简单的排版

正文

方法一
使用nginx_ vts_exporter

mkdir -p /data/nginx/{log,conf/conf.d}

cat > /data/nginx/conf/nginx.conf << 'EOF'
user root;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
access_log  /var/log/nginx/access.log  main;
sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/conf.d/*.conf;

}
EOF

cat > /data/nginx/conf/conf.d/vts.conf << 'EOF'
server {
listen 9913;
server_name localhost;
proxy_ignore_client_abort on;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}

}

EOF

cat > /data/nginx/conf/conf.d/test.conf << 'EOF'
upstream backend {
server 192.168.11.202:31101;
}
server {
listen 80;
server_name localhost;
proxy_ignore_client_abort on;
location / {
proxy_redirect off;
proxy_set_header Host \(host:\)server_port;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "http";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
}
EOF

cat > /data/nginx/start.sh << 'EOF'
docker run -d
--restart=always
-p 80:80
-p 9913:9913
--name nginx
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d
-v /data/nginx/log:/var/log/nginx
-v /etc/localtime:/etc/localtime:ro
crunchgeek/nginx-pagespeed:latest
EOF

bash /data/nginx/start.sh

    mkdir -p /data/nginx_exporter
    cat > /data/nginx_exporter/start.sh << 'EOF'
    docker run -d \
    --restart=always \
    --name nginx_exporter \
    -p 9913:9913 \
    -e NGINX_STATUS="http://192.168.11.221:9913/status/format/json" \
    -e METRICS_NS=nginx \
    -v /etc/localtime:/etc/localtime:ro \
    sophos/nginx-vts-exporter
    EOF
    

    bash /data/nginx_exporter/start.sh

      cat > /data/prometheus/conf/rules/nginx.rules << 'EOF'	
      groups:
      - name: nginx-监控告警
        rules:
        - alert: 警报!Nginx http状态码4xx的占比高
          expr: sum(rate(nginx_server_requests{code="4xx",host="*"}[1m])) / sum(rate(nginx_server_requests{code="total",host="*"}[1m])) * 100 > 5
          for: 1m
          labels:
            severity: 严重告警
          annotations:
            summary: "{{ $labels.instance }} Nginx http状态码4xx的占比高"
            description: "http状态码4xx的占比(> 5%),请检查!\n 当前值 = {{ printf \"%.1f\" $value }}"
      
      • alert: 警报!Nginxhttp状态码5xx的占比高
        expr: sum(rate(nginx_server_requests{code="5xx",host=""}[1m])) / sum(rate(nginx_server_requests{code="total",host=""}[1m])) * 100 > 5
        for: 1m
        labels:
        severity: 严重告警
        annotations:
        summary: "{{ $labels.instance }} Nginx http状态码5xx的占比高"
        description: "http状态码5xx的占比(> 5%),请检查!(> 5%)\n 当前值 = {{ printf "%.1f" $value }}"

      • alert: 警报!Nginx响应延时高
        expr: histogram_quantile(0.99, sum(rate(nginx_http_request_duration_seconds_bucket[2m])) by (host, node)) > 3
        for: 2m
        labels:
        severity: 一般告警
        annotations:
        summary: "{{ $labels.instance }} Nginx响应延时高"
        description: "Nginx 99%响应延时高于3秒\n 当前值 = {{ printf "%.2f" $value }}"
        EOF

        #添加自动发现脚本
        cat >> /data/prometheus/conf/prometheus.yml << 'EOF'
        #nginx自动发现
          - job_name: 'nginx'
            file_sd_configs:
              - files:
                - /etc/prometheus/sd_config/nginx.yaml
                refresh_interval: 5s
            relabel_configs:
            #  - source_labels: [__address__]
            #    target_label: __param_target
            #  - source_labels: [__param_target]
            #    target_label: instance
            #  - target_label: __address__
            #    replacement: 192.168.11.221:9913
              - source_labels: [__address__]
                regex: (.*)
                target_label: instance
                replacement: $1
              - source_labels: [__address__]
                regex: (.*):(.*)
                target_label: __address__
                replacement: $1:9913
        EOF
        

        #自动发现配置
        cat >> /data/prometheus/conf/sd_config/nginx.yaml << 'EOF'

        nginx自动发现

        • labels:
          project: 网关nginx
          targets:
          • 192.168.11.221:9913
          • 192.168.11.192:9913
            EOF

          方法二
          使用vts自带的metrics

          mkdir -p /data/nginx/{log,conf/conf.d}
          

          cat > /data/nginx/conf/nginx.conf << 'EOF'
          user root;
          worker_processes auto;

          error_log /var/log/nginx/error.log notice;
          pid /var/run/nginx.pid;

          events {
          worker_connections 1024;
          }

          http {
          vhost_traffic_status_zone;
          vhost_traffic_status_filter_by_host on;

          include       /etc/nginx/mime.types;
          default_type  application/octet-stream;
          
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
          access_log  /var/log/nginx/access.log  main;
          sendfile        on;
          #tcp_nopush     on;
          keepalive_timeout  65;
          #gzip  on;
          include /etc/nginx/conf.d/*.conf;
          

          }
          EOF

          cat > /data/nginx/conf/conf.d/vts.conf << 'EOF'
          server {
          listen 9913;
          server_name localhost;
          proxy_ignore_client_abort on;
          location /status {
          vhost_traffic_status_display;
          vhost_traffic_status_display_format html;
          }

          }
          

          EOF

          cat > /data/nginx/conf/conf.d/test.conf << 'EOF'
          upstream backend {
          server 192.168.11.202:31101;
          }
          server {
          listen 80;
          server_name localhost;
          proxy_ignore_client_abort on;
          location / {
          proxy_redirect off;
          proxy_set_header Host \(host:\)server_port;
          proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto "http";
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass http://backend;
          }
          }
          EOF

          cat > /data/nginx/start.sh << 'EOF'
          docker run -d
          --restart=always
          -p 80:80
          -p 9913:9913
          --name nginx
          -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
          -v /data/nginx/conf/conf.d:/etc/nginx/conf.d
          -v /data/nginx/log:/var/log/nginx
          -v /etc/localtime:/etc/localtime:ro
          crunchgeek/nginx-pagespeed:latest
          EOF

          bash /data/nginx/start.sh

            cat > /data/prometheus/conf/rules/nginx.rules << 'EOF'	
            groups:
            - name: nginx-监控告警
              rules:
              - alert: 警报!Nginx http状态码4xx的占比高
                expr: sum(rate(nginx_vts_server_requests_total{code="4xx",host="*"}[1m])) / sum(rate(nginx_vts_server_requests_total{code="total",host="*"}[1m])) * 100 > 5
                for: 1m
                labels:
                  severity: 严重告警
                annotations:
                  summary: "{{ $labels.instance }} Nginx http状态码4xx的占比高"
                  description: "http状态码4xx的占比(> 5%),请检查!\n 当前值 = {{ printf \"%.1f\" $value }}"
            
            • alert: 警报!Nginxhttp状态码5xx的占比高
              expr: sum(rate(nginx_vts_server_requests_total{code="5xx",host=""}[1m])) / sum(rate(nginx_vts_server_requests_total{code="total",host=""}[1m])) * 100 > 5
              for: 1m
              labels:
              severity: 严重告警
              annotations:
              summary: "{{ $labels.instance }} Nginx http状态码5xx的占比高"
              description: "http状态码5xx的占比(> 5%),请检查!(> 5%)\n 当前值 = {{ printf "%.1f" $value }}"

            • alert: 警报!Nginx响应延时高
              expr: histogram_quantile(0.99, sum(rate(nginx_vts_server_request_duration_seconds[2m])) by (host, node)) > 3
              for: 2m
              labels:
              severity: 一般告警
              annotations:
              summary: "{{ $labels.instance }} Nginx响应延时高"
              description: "Nginx 99%响应延时高于3秒\n 当前值 = {{ printf "%.2f" $value }}"
              EOF

              #添加自动发现脚本
              cat >> /data/prometheus/conf/prometheus.yml << 'EOF'
              #nginx自动发现
                - job_name: 'nginx'
                  metrics_path: /status/format/prometheus
                  file_sd_configs:
                    - files:
                      - /etc/prometheus/sd_config/nginx.yaml
                      refresh_interval: 5s
                  relabel_configs:
                  #  - source_labels: [__address__]
                  #    target_label: __param_target
                  #  - source_labels: [__param_target]
                  #    target_label: instance
                  #  - target_label: __address__
                  #    replacement: 192.168.11.221:9913
                    - source_labels: [__address__]
                      regex: (.*)
                      target_label: instance
                      replacement: $1
                    - source_labels: [__address__]
                      regex: (.*):(.*)
                      target_label: __address__
                      replacement: $1:9913
              EOF
              

              #自动发现配置
              cat >> /data/prometheus/conf/sd_config/nginx.yaml << 'EOF'

              nginx自动发现

              • labels:
                project: 网关nginx
                targets:
                • 192.168.11.221:9913
                  EOF

                grafana_id : 9785

                在这里插入图片描述
                参考:https://blog.51cto.com/u_14601432/2447888

                与[转帖]prometheus监控nginxt的两种方法(vts)相似的内容:

                [转帖]prometheus监控nginxt的两种方法(vts)

                方法一 使用nginx_ vts_exporter mkdir -p /data/nginx/{log,conf/conf.d} cat > /data/nginx/conf/nginx.conf << 'EOF' user root; worker_processes auto; error_lo

                [转帖]Prometheus+Grafana+rabbitmq_prometheus 监控 RabbitMQ

                https://www.zhangbj.com/p/1065.html 关于 rabbitmq_prometheus rabbitmq_prometheus是RabbitMQ 3.8.0默认集成的监控插件。 相关文档 https://www.rabbitmq.com/monitoring.html

                [转帖]Prometheus监控系统存储容量优化攻略,让你的数据安心保存!

                云原生监控领域不可撼动,Prometheus 是不是就没缺点?显然不是。 一个软件如果什么问题都想解决,就会导致什么问题都解决不好。所以Prometheus 也存在不足,广受诟病的问题就是 单机存储不好扩展。 1 真的需要扩展容量吗? 大部分场景其实不需要扩展,因为一般的数据量压根达不到 Prome

                [转帖]使用Prometheus监控bind9的DNS服务

                https://www.cnblogs.com/charlieroro/p/11013428.html 首先编译bind_exporter,编译方式参见bind_exporter 创建一个systemd配置文件来运行bind_exporter vi /etc/systemd/system/bind_

                [转帖]使用Prometheus监控snmp

                获取snmp信息 首先获取需要监控的snmp的基本信息,假设基本信息如下: snmp服务IP: 1.1.1.1 snmp community: public snmp exportor部署地址: 2.2.2.2 配置snmp exporter 从官方下载snmp exporter的可执行文件。 此外

                [转帖]【最佳实践】prometheus 监控 sql server (使用sql_exporter)

                https://www.cnblogs.com/gered/p/13535212.html 目录 【0】核心参考 【简述】 【1】安装配置 sql_exporter 【1.1】下载解压 sql_exporter 【1.2】修改配置文件 【1.3】自带的sql server监控采集器 【2】整合 pr

                [转帖]使用Prometheus和Grafana监控RabbitMQ集群 (使用RabbitMQ自带插件)

                https://www.cnblogs.com/hahaha111122222/p/15683696.html 配置RabbitMQ集群 官方文档:https://www.rabbitmq.com/prometheus.html#quick-start 官方github地址:https://gith

                [转帖]【Windows 10】Prometheus监控平台安装以及配置windows Exporter探针

                Prometheus 简介 Prometheus是一个开放性的监控解决方案,用户可以非常方便的安装和使用Prometheus并且能够非常方便的对其进行扩展。 在Prometheus的架构设计中,Prometheus Server并不直接服务监控特定的目标,其主要任务负责数据的收集,存储并且对外提供数

                [转帖]Prometheus + Spring Boot 应用监控

                https://blog.51cto.com/u_15127622/2757942 1. Prometheus是什么Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包。一言以蔽之,它是一套开源监控解决方案。Prometheus主要特性:多维数据模型,其中包含由指标名称和键/值对标识

                [转帖]「开源摘星计划」Prometheus监控Harbor(二进制版)

                推荐 原创 键客李大白2022-08-08 11:35:07博主文章分类:Harbor进阶实战(企业实战)©著作权 文章标签云原生运维Harbor文章分类kubernetes云计算私藏项目实操分享阅读数10000+ 本文已参与「开源摘星计划」,欢迎正在阅读的你加入。活动链接:​ ​https://g