monit 安装 GPT解释
好的。按“从零到跑通”的顺序,把 Monit(无 HTTP 面板、阈值→Webhook) 全流程整理如下。每一步都有明确命令与目标。
0)可选:清理旧环境(已有就跳过)
systemctl stop monit 2>/dev/null || true
systemctl disable monit 2>/dev/null || true
dnf remove -y monit 2>/dev/null || true
rm -rf /etc/monitrc /etc/monit.d /var/lib/monit /var/log/monit.log /usr/local/bin/monit-webhook.sh
1)安装
dnf install -y monit
monit -V # 确认版本(你的是 5.33.0)
2)准备 Webhook 发送脚本(统一出口)
将 WEBHOOK_URL 改成你的接口(可带固定 token)。
cat >/usr/local/bin/monit-webhook.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
WEBHOOK_URL="https://your-api.example.com/monitor/hook?token=MY_SECRET_TOKEN" # TODO: 改成你的
payload="${1:-{}}"
/usr/bin/curl -sS -f --connect-timeout 3 --max-time 5 \
-H 'Content-Type: application/json' \
-X POST "$WEBHOOK_URL" -d "$payload" >/dev/null || true
EOF
chmod +x /usr/local/bin/monit-webhook.sh
快速自测脚本是否可用:
/usr/local/bin/monit-webhook.sh '{"alert":"SelfTest","status":"firing","host":"'"$(hostname)"'"}'
3)主配置
/etc/monitrc
(关闭 HTTP;30s/轮)
cat >/etc/monitrc <<'EOF'
set daemon 30
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
include /etc/monit.d/*
EOF
chmod 600 /etc/monitrc
mkdir -p /var/lib/monit
说明:monit status/summary 在 5.33 需要 HTTP 面板;我们此处关闭,所以查询状态请看日志(或见下方“可选开启 HTTP 仅限本机”)。
建议开启http服务
可以添加内容
set httpd port 2812 and
use address 127.0.0.1
allow localhost
allow admin:monit
4)规则
/etc/monit.d/alerts.conf
(含抖动抑制与恢复事件)
mkdir -p /etc/monit.d
cat >/etc/monit.d/alerts.conf <<'EOF'
# 1) CPU:5 分钟平均负载 > 1.2 连续 3 轮
check system cpu_high
if loadavg (5min) > 1.2 for 3 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"HighCPU\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"detail\":\"loadavg5m>1.2\"}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"HighCPU\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"detail\":\"recovered\"}'"
# 2) 内存:> 80% 连续 3 轮
check system mem_high
if memory usage > 80% for 3 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"LowMemory\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"detail\":\"mem>80%\"}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"LowMemory\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"detail\":\"recovered\"}'"
# 3) SWAP:> 20% 连续 5 轮
check system swap_high
if swap usage > 20% for 5 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"HighSwap\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"detail\":\"swap>20%\"}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"HighSwap\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"detail\":\"recovered\"}'"
# 4) 根分区:> 80% 连续 10 轮
check filesystem rootfs with path /
if space usage > 80% for 10 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"DiskUsage\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"path\":\"/\",\"detail\":\">80%\"}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"DiskUsage\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"path\":\"/\",\"detail\":\"recovered\"}'"
# 5) 数据盘:/mnt/blockstorage > 80% 连续 10 轮
check filesystem blockstorage with path /mnt/blockstorage
if space usage > 80% for 10 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"DiskUsage\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"path\":\"/mnt/blockstorage\",\"detail\":\">80%\"}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"DiskUsage\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"path\":\"/mnt/blockstorage\",\"detail\":\"recovered\"}'"
# 6) MySQL 端口:127.0.0.1:3306 连续 2 轮失败
check host mysql_tcp with address 127.0.0.1
if failed port 3306 for 2 cycles then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"MySQLPortDown\",\"status\":\"firing\",\"host\":\"$(hostname)\",\"port\":3306}'"
else if succeeded then exec "/usr/local/bin/monit-webhook.sh '{\"alert\":\"MySQLPortDown\",\"status\":\"resolved\",\"host\":\"$(hostname)\",\"port\":3306}'"
EOF
5)语法检查 → 启动
monit -t # 看到“Control file syntax OK”即可
systemctl enable --now monit
monit reload
6)验证运行
关闭 HTTP 面板时,以下方式验证。
ps -ef | grep -E '[m]onit' # 进程常驻
tail -n 50 /var/log/monit.log # 启动/重载/告警日志
模拟触发(任选其一,验证 Webhook):
-
临时把某条磁盘阈值改成 > 1% → monit reload → 你的接口应收到 firing;改回→resolved。
-
暂停 MySQL 容器:docker compose down → 约 1 分钟内收到 MySQLPortDown;恢复后收到 resolved。
7)可选:仅本机启用 HTTP(方便
monit status/summary
)
若需要命令行状态查询或本地 Web 面板:
sed -i '/include \/etc\/monit.d\/\*/i set httpd port 2812 and\n use address 127.0.0.1\n allow localhost' /etc/monitrc
monit -t && systemctl restart monit
之后可用:
monit summary
monit status
# Web 面板(仅本机):http://127.0.0.1:2812
常见问题速解
-
/etc/monitrc:…: syntax error 'env':5.33 不支持 set env,已用脚本代替。
-
Program does not exist: '$CURL':不要用 $CURL 变量;改为调用 /usr/local/bin/monit-webhook.sh。
-
Error opening idfile:创建 /var/lib/monit 目录即可。
-
monit status 提示未启用 HTTP:见第 7 步,启用仅本机 HTTP。
需要我把你当前机器的 WEBHOOK_URL 代入,并生成“可直接复制”的两段文件内容(monitrc 与 alerts.conf)吗?
浙公网安备 33010602011771号