Skip to content

Instantly share code, notes, and snippets.

@t0saki
Last active March 29, 2025 16:48
Show Gist options
  • Save t0saki/f0dbabcc09dd20004ea5a199d7a3a0f1 to your computer and use it in GitHub Desktop.
Save t0saki/f0dbabcc09dd20004ea5a199d7a3a0f1 to your computer and use it in GitHub Desktop.
incused monitor
#!/bin/bash
# 脚本文件:/root/monitor_incusd_top.sh
# 功能:每分钟检查incusd进程的CPU占用率(使用top采样),如果连续3分钟超过80%,则重启服务
LOG_FILE="/root/incusd_monitor.log"
COUNTER_FILE="/tmp/incusd_high_cpu_counter"
if [ ! -f "$COUNTER_FILE" ]; then
echo "0" > "$COUNTER_FILE"
fi
CURRENT_TIME=$(date "+%Y-%m-%d %H:%M:%S")
# 使用top采样,注意:不同系统中top的输出字段可能有所不同,一般CPU使用率在第9列,可根据实际情况调整
sleep 1
CPU_USAGE=$(top -b -n1 | grep incusd | grep -v grep | awk '{print $9}' | sort -nr | head -n 1)
if [ -z "$CPU_USAGE" ]; then
echo "$CURRENT_TIME - incusd进程未运行" >> "$LOG_FILE"
echo "0" > "$COUNTER_FILE"
exit 0
fi
# 如果可能,打印一下采样值格式(有百分号时需要去掉)
CPU_USAGE_NUM=$(echo "$CPU_USAGE" | sed 's/%//g')
if [ "$(echo "$CPU_USAGE_NUM > 80.0" | bc -l)" -eq 1 ]; then
COUNTER=$(<"$COUNTER_FILE")
COUNTER=$((COUNTER + 1))
echo "$COUNTER" > "$COUNTER_FILE"
echo "$CURRENT_TIME - incusd进程最高CPU使用率: $CPU_USAGE_NUM%,计数器: $COUNTER" >> "$LOG_FILE"
if [ "$COUNTER" -ge 3 ]; then
echo "$CURRENT_TIME - incusd进程CPU使用率连续3分钟超过80%,正在重启服务…" >> "$LOG_FILE"
systemctl restart incus >> "$LOG_FILE" 2>&1
echo "$CURRENT_TIME - incus服务重启完成" >> "$LOG_FILE"
echo "0" > "$COUNTER_FILE"
fi
else
echo "0" > "$COUNTER_FILE"
echo "$CURRENT_TIME - incusd进程最高CPU使用率: $CPU_USAGE_NUM%,状态正常" >> "$LOG_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment