-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add lightweight monitoring scripts #2255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # 轻量监控与告警(飞书) | ||
|
|
||
| ## 文件 | ||
| - `monitor.sh`:监控主脚本 | ||
| - `monitor.conf.example`:配置模板 | ||
| - `install.sh`:安装脚本 | ||
|
|
||
| ## 监控项 | ||
| - API 存活与响应时间 | ||
| - Docker 容器运行状态与重启次数 | ||
| - CPU / 内存 / 磁盘 | ||
| - 网络错误计数 | ||
| - UFW 状态与 80/443 暴露检查 | ||
| - fail2ban 与 sshd jail | ||
| - TLS 证书到期时间 | ||
| - 源站直连检查 | ||
|
|
||
| ## 增强点 | ||
| - 每次执行写心跳 | ||
| - 每次执行输出摘要:`status=OK|WARN|FAIL` | ||
| - 失败恢复后发送飞书恢复通知 | ||
| - cron 保留独立运行日志 | ||
| - 自动安装 logrotate,避免日志无限增长 | ||
| - 使用 `flock` 防止并发重入 | ||
|
|
||
| ## 安装 | ||
| ```bash | ||
| cd /opt/CLIProxyAPI/deploy/monitor | ||
| sudo bash install.sh | ||
| ``` | ||
|
|
||
| ## 配置 | ||
| ```bash | ||
| sudo vi /opt/CLIProxyAPI/monitor/monitor.conf | ||
| ``` | ||
|
|
||
| 至少填写: | ||
| - `FEISHU_WEBHOOK` | ||
| - `DOMAIN` | ||
| - `ORIGIN_IP` | ||
| - `API_KEY`(如接口需要鉴权) | ||
|
|
||
| 可选配置: | ||
| - `RECOVERY_ALERTS=1`:异常恢复后发送恢复通知 | ||
| - `ALERT_COOLDOWN=1800`:同类告警冷却时间 | ||
|
|
||
| ## 手动测试 | ||
| ```bash | ||
| /opt/CLIProxyAPI/monitor/monitor.sh | ||
| tail -n 50 /var/log/cliproxyapi-monitor.log | ||
| tail -n 50 /var/log/cliproxyapi-monitor-run.log | ||
| ``` | ||
|
|
||
| ## 默认阈值 | ||
| - CPU `>= 90%` | ||
| - 内存 `>= 90%` | ||
| - 磁盘 `>= 85%` | ||
| - API 连续失败 `>= 3` 次 | ||
| - API 响应时间 `>= 2000ms` | ||
| - 证书剩余天数 `<= 15` | ||
|
|
||
| ## 日志 | ||
| - 告警与摘要日志:`/var/log/cliproxyapi-monitor.log` | ||
| - cron 运行日志:`/var/log/cliproxyapi-monitor-run.log` | ||
|
|
||
| ## 日志轮转 | ||
| 安装脚本会写入: | ||
|
|
||
| `/etc/logrotate.d/cliproxyapi-monitor` | ||
|
|
||
| 策略: | ||
| - 每日轮转 | ||
| - 保留 14 份 | ||
| - 压缩旧日志 | ||
| - `copytruncate` 避免中断写入 | ||
|
|
||
| ## 定时任务 | ||
| `/etc/cron.d/cliproxyapi-monitor` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| if [[ "${EUID}" -ne 0 ]]; then | ||
| echo "请使用 root 执行" | ||
| exit 1 | ||
| fi | ||
|
|
||
| SRC_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| TARGET_DIR="/opt/CLIProxyAPI/monitor" | ||
| CONF_TARGET="${TARGET_DIR}/monitor.conf" | ||
| CRON_FILE="/etc/cron.d/cliproxyapi-monitor" | ||
| LOGROTATE_FILE="/etc/logrotate.d/cliproxyapi-monitor" | ||
|
|
||
| mkdir -p "$TARGET_DIR" | ||
| cp -f "${SRC_DIR}/monitor.sh" "${TARGET_DIR}/monitor.sh" | ||
| chmod +x "${TARGET_DIR}/monitor.sh" | ||
|
|
||
| if [[ ! -f "$CONF_TARGET" ]]; then | ||
| cp -f "${SRC_DIR}/monitor.conf.example" "$CONF_TARGET" | ||
| echo "已生成配置: ${CONF_TARGET}" | ||
| fi | ||
|
|
||
| mkdir -p /var/lib/cliproxyapi-monitor | ||
| touch /var/log/cliproxyapi-monitor.log | ||
| touch /var/log/cliproxyapi-monitor-run.log | ||
|
|
||
| cat > "$CRON_FILE" <<'EOF' | ||
| * * * * * root /opt/CLIProxyAPI/monitor/monitor.sh >> /var/log/cliproxyapi-monitor-run.log 2>&1 | ||
| EOF | ||
|
|
||
| cat > "$LOGROTATE_FILE" <<'EOF' | ||
| /var/log/cliproxyapi-monitor.log /var/log/cliproxyapi-monitor-run.log { | ||
| su root root | ||
| daily | ||
| rotate 14 | ||
| compress | ||
| delaycompress | ||
| missingok | ||
| notifempty | ||
| copytruncate | ||
| create 0640 root root | ||
| } | ||
| EOF | ||
|
|
||
| echo "已安装定时任务: ${CRON_FILE}" | ||
| echo "已安装日志轮转: ${LOGROTATE_FILE}" | ||
| echo "cron 输出已写入: /var/log/cliproxyapi-monitor-run.log" | ||
| echo "请编辑配置并手动测试:" | ||
| echo " vi ${CONF_TARGET}" | ||
| echo " /opt/CLIProxyAPI/monitor/monitor.sh" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # 监控与告警配置(复制为 monitor.conf 并修改) | ||
|
|
||
| # 基础 | ||
| DOMAIN="cpa.hasdg.fun" | ||
| ORIGIN_IP="142.171.216.112" | ||
| API_PATH="/v1/models" | ||
| API_KEY="" | ||
|
|
||
| # 阈值 | ||
| API_TIMEOUT=5 | ||
| API_SLOW_MS=2000 | ||
| API_FAIL_THRESHOLD=3 | ||
| CPU_WARN=90 | ||
| MEM_WARN=90 | ||
| DISK_WARN=85 | ||
| NET_ERR_INCR=1 | ||
| CERT_EXPIRE_DAYS=15 | ||
|
|
||
| # 组件 | ||
| CONTAINER_NAME="cliproxyapi" | ||
|
|
||
| # 开关 | ||
| CHECK_API=1 | ||
| CHECK_DOCKER=1 | ||
| CHECK_RESOURCE=1 | ||
| CHECK_NETWORK=1 | ||
| CHECK_UFW=1 | ||
| CHECK_FAIL2BAN=1 | ||
| CHECK_CERT=1 | ||
| CHECK_ORIGIN=1 | ||
|
|
||
| # 告警 | ||
| ALERT_COOLDOWN=1800 | ||
| RECOVERY_ALERTS=1 | ||
| FEISHU_WEBHOOK="https://open.feishu.cn/open-apis/bot/v2/hook/REPLACE_ME" | ||
|
|
||
| # 日志与状态 | ||
| LOG_FILE="/var/log/cliproxyapi-monitor.log" | ||
| RUN_LOG_FILE="/var/log/cliproxyapi-monitor-run.log" | ||
| STATE_DIR="/var/lib/cliproxyapi-monitor" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前的 cron 任务设置为每分钟执行一次。对于一个“轻量级”监控脚本来说,这个频率可能太高了,特别是考虑到某些检查(如证书到期)不需要频繁运行,而且 CPU 检查本身包含1秒的
sleep。过于频繁的执行可能会消耗不必要的系统资源。建议将执行频率降低到每5分钟一次。