监控nginx

这篇博客更加详细:https://www.cnblogs.com/zmh520/p/17758730.html

1、nginx安装

1.1、创建nginx目录

mkdir ~/docker/monitor_alarm/docker-server/nginx/conf.d -p
cd ~/docker/monitor_alarm/docker-server/nginx/conf.d

在~/docker/monitor_alarm/docker-server/nginx/conf.d 目录里面新增加nginx的配置文件

cat > server.conf<<"EOF"
server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
EOF

1.2、docker-compose 安装rabbitmq、nginx、mongo、redis

mkdir ~/docker/monitor_alarm/docker-server
cd  ~/docker/monitor_alarm/docker-server

通过cat创建docker-compose.yaml文件

cat > docker-compose.yaml <<"EOF"
version: '3'
services:
  redis:
    image: redis:5
    container_name: redis_prom
    command: redis-server --requirepass 123456 --maxmemory 512mb
    restart: always
    volumes:
      - /home/lihaiyang/docker/monitor_alarm/docker-server/redis/data:/data
    ports:
      - 6380:6379
  nginx:
    image: nginx:1.21.6
    container_name: nginx_prom
    restart: always
    volumes:
      - /home/lihaiyang/docker/monitor_alarm/docker-server/nginx/conf.d:/etc/nginx/conf.d
      - /home/lihaiyang/docker/monitor_alarm/docker-server/nginx/html:/usr/share/nginx/html
      - /home/lihaiyang/docker/monitor_alarm/docker-server/nginx/log:/var/log/nginx
    ports:
      - 8081:80

  rabbitmq:
    image: rabbitmq:3.7.15-management
    container_name: rabbitmq_prom
    restart: always
    volumes:
      - /home/lihaiyang/docker/monitor_alarm/docker-server/rabbitmq/data:/var/lib/rabbitmq
      - /home/lihaiyang/docker/monitor_alarm/docker-server/rabbitmq/log:/var/log/rabbitmq
    ports:
      - 5673:5672
      - 15672:15672

  mongo:
    image: mongo:4.2.5
    container_name: mongo_prom
    restart: always
    volumes:
      - /home/lihaiyang/docker/monitor_alarm/docker-server/mongo/db:/data/db
    ports:
      - 27017:27017
    command: [--auth]
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 123456
EOF

2、监控nginx

2.1、nginx开启stub_status

注:nginx需要with-http_stub_status_module
检查是否安装有with-http_stub_status_module模块

docker exec -it nginx_prom nginx -V 2>&1 | grep -o with-http_stub_status_module

2.2、nginx开启stub_status配置

server {
    location /stub_status {
        stub_status on;
        access_log off;
        allow 0.0.0.0/0;
        deny all;
    }
}

2.3、重新加载配置文件

docker exec -it nginx_prom nginx -s reload

2.4、检查

curl http://ip:8081/stub_status

正常如下图所示
image
参数解释
Active connections 活动连接数
accepts 接收请求书
handled 成功处理请求数
requests 总请求数
reading 正在进行读操作的请求数
writing 正在进行写操作的请求数
waiting 正在等待的请求数

2.5、nginx_exporter安装

nginx_exporter 下载地址:https://github.com/nginxinc/nginx-prometheus-exporter/releases

2.5.1、二进制安装

# 下载
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_arm64.tar.gz

# 创建文件夹
mkdir /opt/prometheus/nginx_exporter -p

# 解压到/opt/prometheus/nginx_exporter
tar -zxvf nginx-prometheus-exporter_0.11.0_linux_arm64.tar.gz -C /opt/prometheus/nginx_exporter

# 查看
ls -l /opt/prometheus/nginx_exporter

# 创建用户
useradd -M -s /usr/sbin/nologin prometheus

# 更改/opt/prometheus权限
chown -R prometheus:prometheus /opt/prometheus

# 创建systemd服务
cat > /etc/systemd/system/nginx_exporter.service <<"EOF"
[Unit]
Description=nginx-prometheus-exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always
ExecStart=/opt/prometheus/nginx_exporter/nginx-prometheus-exporter -nginx.scrape-uri=http://192.168.209.102/stub_status

[Install]
WantedBy=multi-user.target
EOF

# 重载
systemctl daemon-reload
# 启动
systemctl start nginx_exporter
# 查看状态
systemctl status nginx_exporter
# 开机自启动
systemctl enable nginx_exporter

# 启动不了检查日志
journalctl -u nginx_exporter -f

2.5.2、docker-compose安装

mkdir ~/docker/monitor_alarm/nginx_exporter -p
cd ~/docker/monitor_alarm/nginx_exporter
cat > docker-compose.yaml <<"EOF"
version: '3.3'
services:
  nginx_exporter:
    image: nginx/nginx-prometheus-exporter:0.11
    container_name: nginx_exporter
    hostname: nginx_exporter
    command:
      - '-nginx.scrape-uri=http://192.168.209.102:8081/stub_status'
    restart: always
    ports:
      - "9113:9113"
EOF

# 启动
docker-compose up -d
# 检查
docker-compose ps 或 docker-compose logs nginx_exporter

3、参数解释

Environment variable 命令行参数 description
-nginx.scrape-uri nginx stub_status

4、metrics地址

http://ip:9113/metrics

5、Prometheus配置

配置prometheus去采集(拉取)nginx_exporter的监控样本数据

cd /opt/prometheus/prometheus

# 在scrape_config(搜刮配置)下面增加如下配置
  - job_name: "nginx_exporter"
    static_configs:
    - targets: ["192.168.68.200:9113"]
      labels:
        instance: test nginx服务器

6、添加触发器

cd /opt/prometheus/prometheus
vi alert.yml
  - name: nginx
    rules:
      # 对任何实力超过30秒无法访问的情况发出警报
      - alert: nginxDown
        expr: nginx_up == 0
        for: 30s
        labels:
          severity: critical
        annotations:
          summary: "nginx异常,实例:{{ $labels.instance }}"
          description: "{{ $labels.job }} nginx已关闭"

6.1、检查配置

# 二进制
./promtool check config prometheus.yml
# docker
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

6.2、重新加载配置

curl -X POST http://localhost:9090/-/reload

7、dashboard

grafana展示prometheus从nginx_exporter收集到的数据
https://grafana.com/grafana/dashboards/12708

posted @ 2023-10-24 22:50  尐海爸爸  阅读(49)  评论(0编辑  收藏  举报