服务监控

Prometheus

搭建参考:LPG日志采集

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

在高版本的模块编译中会出现错误情况,问AI基本解决

root@MK-Y:/opt/nginx-1.28.1# make modules
make -f objs/Makefile modules
make[1]: Entering directory '/opt/nginx-1.28.1'
cc -c -fPIC -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/nginx-H9ztob/nginx-1.28.1=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/nginx-H9ztob/nginx-1.28.1=/usr/src/nginx-1.28.1-1+ubuntu24.04.1+deb.sury.org+1 -fPIC -Wdate-time -D_FORTIFY_SOURCE=3 -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I src/http/modules/perl -I /usr/include/libxml2 -I objs -I src/http -I src/http/modules -I src/http/v2 -I src/http/v3 -I src/mail -I src/stream \
        -o objs/src/http/modules/ngx_http_xslt_filter_module.o \
        src/http/modules/ngx_http_xslt_filter_module.c
<command-line>: error: "_FORTIFY_SOURCE" redefined [-Werror]
<built-in>: note: this is the location of the previous definition
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:1768: objs/src/http/modules/ngx_http_xslt_filter_module.o] Error 1
make[1]: Leaving directory '/opt/nginx-1.28.1'
make: *** [Makefile:16: modules] Error 2

# 解决方法 - 以下来自Kimi

# 清理之前的编译
make clean

# 重新配置,移除冲突的编译选项
./configure \
  --with-compat \
  --add-dynamic-module=/path/to/your/module \
  --with-cc-opt="-g -O2 -fPIC" \
  --with-ld-opt="-Wl,-z,relro"

# 编译模块
make modules

数据监控配置

http {
  # nginx监控
  vhost_traffic_status_zone shared:vhost_traffic_status:64m;
  # 开启此功能,会根据不同的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 $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;
    }
  }
}

采集数据删除

# 全部清除
curl "http://192.168.1.5:8070/status/control?cmd=delete&group=*"

数据采集

# 下载并解压
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

# 守护进程 替换端口启用多个采集进程,设置不同的nginx.scrape_uri监听不同的服务器nginx
[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"]

Node-exporter(服务器监控)

下载新版本: Node-exporter 并解压,多个服务器相同

web.yml

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

node-exporter.service

[Unit]
Description=NODE Exporter MKU
After=network.target

[Service]
ExecStart=/opt/node-exporter/node_exporter --web.config.file=/opt/node-exporter/web.yml --web.listen-address=:9100
Restart=always

[Install]
WantedBy=multi-user.target

Redis-exporter

下载: Redis-exporter 解压,此工具不适用 web.yml

ewdis-exporter.service

[Unit]
Description=REDIS Exporter MKU
After=network.target

[Service]
ExecStart=/opt/redis-exporter/redis_exporter \
        -web.listen-address=:9101 \
        -redis.addr=tcp://127.0.0.1:6379 \
        -redis.password=pass
Restart=always

[Install]
WantedBy=multi-user.target