feat: add lightweight monitoring scripts#2255
feat: add lightweight monitoring scripts#2255dev-Flyblue wants to merge 1 commit intorouter-for-me:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求为 CLIProxyAPI 项目引入了一个新的轻量级监控解决方案。该系统通过 shell 脚本实现,提供对 API 健康、系统资源、Docker 容器、网络、安全配置(UFW、fail2ban)和 TLS 证书的全面检查。它还包括自动化安装过程,并与飞书集成以发送告警通知,从而提高了项目的运行可靠性。 Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
| else | ||
| record_warn "API" "接口异常但未达到告警阈值,HTTP=${http_code},连续失败=${fail_count}" | ||
| fi | ||
| send_recovery "api_slow" "API" "响应时间已恢复正常" |
| 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 |
There was a problem hiding this comment.
当前的 cron 任务设置为每分钟执行一次。对于一个“轻量级”监控脚本来说,这个频率可能太高了,特别是考虑到某些检查(如证书到期)不需要频繁运行,而且 CPU 检查本身包含1秒的 sleep。过于频繁的执行可能会消耗不必要的系统资源。建议将执行频率降低到每5分钟一次。
| * * * * * root /opt/CLIProxyAPI/monitor/monitor.sh >> /var/log/cliproxyapi-monitor-run.log 2>&1 | |
| */5 * * * * root /opt/CLIProxyAPI/monitor/monitor.sh >> /var/log/cliproxyapi-monitor-run.log 2>&1 |
| set_kv() { | ||
| local file="$1" key="$2" value="$3" | ||
| if grep -qE "^${key}=" "$file"; then | ||
| sed -i "s#^${key}=.*#${key}=${value}#g" "$file" | ||
| else | ||
| echo "${key}=${value}" >> "$file" | ||
| fi | ||
| } |
There was a problem hiding this comment.
当前的 set_kv 函数实现不够健壮,存在一些问题:
- 它不是原子操作。在
grep和sed/echo之间如果发生中断,可能会导致状态不一致。 sed命令对value中的特殊字符(如&)很敏感,可能导致状态文件损坏。grep命令对key中的正则表达式元字符也很敏感。
虽然当前的使用场景似乎是安全的,但为了未来的可维护性和健壮性,建议使用一个更安全的方法,例如使用 awk 和原子文件替换(mv)。
set_kv() {
local file="$1" key="$2" value="$3"
local temp_file
temp_file=$(mktemp)
# Use awk to robustly update or append the key. It handles special characters
# in values and avoids complex shell quoting issues with sed.
# It uses index() to match the start of the line to avoid regex issues with `key`
# and to correctly handle keys that are prefixes of other keys.
awk -v k="$key" -v v="$value" '
BEGIN { found = 0 }
index($0, k "=") == 1 {
print k "=" v
found = 1
next
}
{ print }
END {
if (!found) {
print k "=" v
}
}
' "$file" > "$temp_file"
# Atomically replace the original file
mv "$temp_file" "$file"
}
No description provided.