服务监控

Prometheus

版本大于 v2.24 使用Docker搭建

docker-compose.yaml

version: "3"

services:
  prometheus:
    image: prom/prometheus:v3.4.0
    container_name: prometheus
    restart: on-failure:3
    ports: 
      - "9090:9090"
    volumes:
      - /docker-data/prometheus/config:/etc/prometheus
      - /docker-data/prometheus/data:/prometheus
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command:
      - '--web.config.file=/etc/prometheus/web.yml'
      - '--config.file=/etc/prometheus/prometheus.yml'
    privileged: true
    network_mode: host
    user: root
    environment:
      - STORAGE_TSDB_RETENTION_TIME=60d
      - TZ=Asia/Shanghai
    hostname: prometheus

prometheus.yml

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    scrape_interval: 5s
    basic_auth:
      username: "name"
      password: "pass"
    static_configs:
      - targets: ["ip:port"]

  - job_name: "mysql_exporter"
    scrape_interval: 5s
    basic_auth:
      username: "name"
      password: "pass"
    static_configs:
      - targets: ["ip:port"]

  - job_name: "rabbitmq"
    scrape_interval: 5s
    static_configs:
      - targets: ["ip:port"]

  - job_name: "youcats_mgt"
    metrics_path: "/monitor/prometheus"
    scrape_interval: 5s
    basic_auth:
      username: "name"
      password: "pass"
    static_configs:
      - targets: ["ip:port"]

  - job_name: "nginx_exporter"
    scrape_interval: 5s
    basic_auth:
      username: "name"
      password: "pass"
    static_configs:
      - targets: ["ip:port"]

web.yml

basic_auth_users:
  name: $2a$12$..........

密码生成方法->方法1:网页上的 bcrypt 生成器

htpasswd -nBC 8 name
New password: 
Re-type new password: 
name:$2y$08..........

MySQL-exporter

网上寻找适合 Linux-amd64 的版本

my.cnf

# my.cnf
[client]
# MySQL数据库实例,name pass均为数据库授权的用户
host = localhost
port = 3306
user = name
password = pass

web.yml

basic_auth_users:
  name: $2a$12$..........

mysql-exporter.service

[Unit]
Description=mysqld_exporter
After=network.target

[Service]
ExecStart=/opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/my.cnf --web.listen-address=:port --web.config.file=/opt/mysqld_exporter/web.yml
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

Nginx-exporter

网上寻找适合 Linux-amd64 的版本

web.yml

basic_auth_users:
  name: $2a$12$..........

nginx.conf

# nginx监控
server {
  listen 8080;
  server_name 192.168.1.5;

  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    allow 192.168.1.5;
    deny all;
  }
}

nginx-exporter.service 调用上面 nginx 的地址,nginxapt-get 安装

[Unit]
Description=NGINX Prometheus Exporter MKU
After=network.target

[Service]
ExecStart=/opt/nginx-exporter/nginx-prometheus-exporter -nginx.scrape-uri=http://192.168.1.5:8080/nginx_status -web.config.file=/opt/nginx-exporter/web.yml -web.listen-address=:9113
Restart=always

[Install]
WantedBy=multi-user.target

Nginx-vts-exporter

使用 nginx-prometheus-exporter 得到的数据有限,建议使用 nginx-vts-exporter 配合nginx加载 nginx-module-vts 采集更多数据,适用于apt安装的nginx。

编译安装配置正向代理HTTPS流程:nginx编译

加载模块

# 下载模块
git clone https://github.com/vozlt/nginx-module-vts.git

# 下载对应的nginx版本源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz

# 备份原nginx编译参数,configure arguments:后的
nginx -v

# 进入对应nginx版本源码编译,x为上一步的数据,并添加新的模块
./configure x --add-dynamic-module=../nginx-module-vts

# 编译module
make modules

# 复制插件,需要提前创建/etc/nginx/modules
cp /path/nginx-1.24.0/objs/ngx_http_vhost_traffic_status_module.so /etc/nginx/modules/

# 在nginx.conf中加载module
load_module /etc/nginx/modules/ngx_http_vhost_traffic_status_module.so

数据监控配置

http {
  # nginx监控
  vhost_traffic_status_zone;
  # 开启此功能,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个上。
  # vhost_traffic_status_filter_by_set_key 可以使用nginx内置的变量
  vhost_traffic_status_filter_by_host on;
  vhost_traffic_status_filter on;
  vhost_traffic_status_filter_by_set_key $status status::$server_name;
  vhost_traffic_status_filter_by_set_key $uri uri::$server_name;
  vhost_traffic_status_filter_by_set_key $server_port server_port::$server_name;

  server {
    listen 8070;
    server_name 192.168.1.4;

    location /status {
      vhost_traffic_status_display;
      vhost_traffic_status_display_format html;
      access_log off;
      allow 127.0.0.1;
      allow 192.168.1.5;
      deny all;
    }
  }
}

数据采集

# 下载并解压
wget https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.8/nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz

tar -zxvf nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz

# 守护进程 替换端口启用多个采集进程
[Unit]
Description=NGINX VTS Exporter MKU
After=network.target

[Service]
ExecStart=/opt/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://192.168.1.5:8070/status/format/json -telemetry.address=":9115"
Restart=always

[Install]
WantedBy=multi-user.target

# prometheus配置
scrape_configs:
  - job_name: "nginx_exporter"
    scrape_interval: 5s
    basic_auth:
      username: "name"
      password: "pass"
    static_configs:
      - targets: ["ip:port"]