Gatus 健康检查仪表板搭建教程

Gatus 是一款用 Go 语言编写的现代化健康检查仪表板,通过简洁的 YAML 配置即可监控 HTTP、TCP、DNS、ICMP 等多种协议的服务。它的核心优势在于强大的条件表达式引擎,可以对响应时间、状态码、响应体内容等多个维度进行精细化监控。Gatus 的界面简洁美观,资源占用极低。

一、Docker 部署

mkdir -p /opt/gatus && cd /opt/gatus

1.1 创建配置文件

创建 config.yaml

storage:
  type: sqlite
  path: /data/gatus.db

ui:
  title: "服务状态监控"
  header: "所有系统运行状态"

endpoints:
  - name: 主站
    group: 网站
    url: "https://example.com"
    interval: 60s
    conditions:
      - "[STATUS] == 200"
      - "[RESPONSE_TIME] < 2000"
      - "[BODY].status == UP"
    alerts:
      - type: email
        enabled: true
        failure-threshold: 3
        success-threshold: 2

  - name: API 服务
    group: 后端
    url: "https://api.example.com/health"
    interval: 30s
    conditions:
      - "[STATUS] == 200"
      - "[RESPONSE_TIME] < 1000"

  - name: 数据库
    group: 基础设施
    url: "tcp://127.0.0.1:3306"
    interval: 60s
    conditions:
      - "[CONNECTED] == true"

  - name: Redis
    group: 基础设施
    url: "tcp://127.0.0.1:6379"
    interval: 60s
    conditions:
      - "[CONNECTED] == true"

  - name: DNS 解析
    group: 网络
    url: "8.8.8.8"
    dns:
      query-name: "example.com"
      query-type: "A"
    interval: 120s
    conditions:
      - "[DNS_RCODE] == NOERROR"

alerting:
  email:
    from: "monitor@example.com"
    host: "smtp.example.com"
    port: 587
    username: "monitor@example.com"
    password: "邮箱密码"
    to: "admin@example.com"

  telegram:
    token: "你的Bot Token"
    id: "你的Chat ID"

1.2 Docker Compose 配置

version: '3.8'

services:
  gatus:
    image: twinproduction/gatus:latest
    restart: always
    ports:
      - "8087:8080"
    volumes:
      - ./config.yaml:/config/config.yaml:ro
      - gatus_data:/data

volumes:
  gatus_data:
docker compose up -d

二、Nginx 反向代理

server {
    listen 80;
    listen 443 ssl http2;
    server_name monitor.example.com;

    ssl_certificate /etc/letsencrypt/live/monitor.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/monitor.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8087;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
nginx -t && systemctl reload nginx

三、高级条件表达式

Gatus 的条件表达式非常灵活:

endpoints:
  - name: JSON API 检查
    url: "https://api.example.com/status"
    interval: 30s
    conditions:
      - "[STATUS] == 200"
      - "[RESPONSE_TIME] < 500"
      - "[BODY].status == ok"
      - "[BODY].version == any(1.0, 1.1, 1.2)"
      - "len([BODY].errors) == 0"

  - name: SSL 证书检查
    url: "https://example.com"
    interval: 3600s
    conditions:
      - "[STATUS] == 200"
      - "[CERTIFICATE_EXPIRATION] > 720h"  # 证书有效期大于 30 天

  - name: 响应体包含检查
    url: "https://example.com"
    interval: 120s
    conditions:
      - "[STATUS] == 200"
      - "[BODY] == pat(*正常运行*)"  # 响应体包含特定字符串

四、多通知渠道

alerting:
  slack:
    webhook-url: "https://hooks.slack.com/services/xxx/xxx/xxx"

  discord:
    webhook-url: "https://discord.com/api/webhooks/xxx/xxx"

  pagerduty:
    integration-key: "你的PagerDuty集成密钥"

  custom:
    url: "https://your-webhook.example.com/alert"
    method: "POST"
    body: |
      {"service":"[ENDPOINT_NAME]","status":"[ALERT_TRIGGERED_OR_RESOLVED]","message":"[ENDPOINT_GROUP] - [ENDPOINT_NAME]: [ALERT_DESCRIPTION]"}

五、API 和徽章

# 获取端点状态
curl https://monitor.example.com/api/v1/endpoints/网站_主站/statuses

# 获取 SVG 徽章(可嵌入到 README 中)
# https://monitor.example.com/api/v1/endpoints/网站_主站/uptimes/7d/badge.svg
# https://monitor.example.com/api/v1/endpoints/网站_主站/response-times/24h/badge.svg

六、数据备份

# 备份 SQLite 数据库
docker cp gatus-gatus-1:/data/gatus.db /root/backups/gatus_$(date +%Y%m%d).db

# 备份配置文件
cp /opt/gatus/config.yaml /root/backups/gatus_config_$(date +%Y%m%d).yaml

总结

Gatus 是新一代的服务健康检查工具,YAML 配置方式简洁直观,条件表达式引擎功能强大。在搬瓦工 VPS 上运行资源占用极低,非常适合监控小到中等规模的服务集群。相比 Statping,Gatus 的配置更灵活;相比 Cachet,Gatus 自带监控能力。

选购搬瓦工 VPS 请查看 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。