Files
membank/auto-check/scripts/full.sh
12600k-rog-d4 2aaaa16ae2 0813
2026-08-13 22:49:50 +08:00

72 lines
2.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
echo "===TEST:full==="
# 读取温度sensors 不可用时输出空)
read_temp() {
sensors 2>/dev/null | grep -oP '\+?\d+(\.\d+)?(?=°C)' | head -1
}
# 后台采样 CPU + 内存 + 温度
SAMPLE_LOG=/tmp/auto-check-full-sample.log
: > "$SAMPLE_LOG"
(
while true; do
TS=$(date +%H:%M:%S)
CPU=$(top -bn1 2>/dev/null | grep "Cpu(s)" | sed -n 's/.*,\s*\([0-9.]*\)%*\s*id.*/\1/p' | awk '{printf "%.1f", 100-$1}' 2>/dev/null)
MEM_TOTAL=$(awk '/^MemTotal:/{print $2}' /proc/meminfo)
MEM_AVAIL=$(awk '/^MemAvailable:/{print $2}' /proc/meminfo)
if [ -z "$MEM_AVAIL" ] || [ "$MEM_AVAIL" = "0" ]; then
MEM_AVAIL=$(awk '/^MemFree:/{print $2}' /proc/meminfo)
fi
MEM_PCT=$(awk -v t="$MEM_TOTAL" -v a="$MEM_AVAIL" 'BEGIN{if(t>0) printf "%.1f", (t-a)/t*100}')
TEMP=$(read_temp)
echo "${TS},${CPU},${MEM_PCT},${TEMP}" >> "$SAMPLE_LOG"
sleep "${AUTOCHECK_SAMPLE_INTERVAL:-2}"
done
) &
SAMPLE_PID=$!
START_TIME=$(date +%s%N)
set +e
OUTPUT=$(stress-ng \
--cpu "${AUTOCHECK_THREADS:-4}" --cpu-method all \
--vm "${AUTOCHECK_THREADS:-4}" --vm-bytes "${AUTOCHECK_MEM_MB:-256}M" --vm-method all \
--iomix 2 --iomix-bytes "${AUTOCHECK_DISK_SIZE_MB:-512}M" \
--timeout "${AUTOCHECK_DURATION:-30}s" --temp-path /tmp --metrics-brief 2>&1)
EXIT_CODE=$?
set -e
END_TIME=$(date +%s%N)
kill $SAMPLE_PID 2>/dev/null || true
wait $SAMPLE_PID 2>/dev/null || true
DURATION_MS=$(( (END_TIME - START_TIME) / 1000000 ))
if [ "$EXIT_CODE" -eq 0 ]; then echo 'status:pass'; else echo 'status:fail'; fi
echo "duration:${DURATION_MS}ms"
# 跑分:各 stressor 的 bogo ops/s 之和
SCORE=$(echo "$OUTPUT" | awk '
/^stress-ng:/ && ($4=="cpu" || $4=="vm" || $4=="iomix" || $4=="memrate") {
v=$(NF-1)+0; if (v>0) s+=v
}
END { printf "%.1f", s+0 }')
echo "score:${SCORE}"
# 关键指标
AVG_CPU=$(awk -F',' '$2!="" {s+=$2; n++} END{printf "%.1f", (n?s/n:0)}' "$SAMPLE_LOG")
echo "metric:avg_cpu=${AVG_CPU}"
MAX_MEM=$(awk -F',' '$3!="" && $3+0>m {m=$3} END{printf "%.1f", m+0}' "$SAMPLE_LOG")
echo "metric:mem_used_pct=${MAX_MEM}"
MAX_TEMP=$(awk -F',' '$4!="" && $4+0>m {m=$4} END{printf "%.1f", m+0}' "$SAMPLE_LOG")
HAS_TEMP=$(awk -F',' '$4!="" && $4+0>0{f=1} END{print f+0}' "$SAMPLE_LOG")
if [ "$HAS_TEMP" = "1" ]; then echo "metric:max_temp=${MAX_TEMP}"; fi
echo "output:${OUTPUT}"
# 采样数据(首行表头)
echo "===SAMPLES==="
echo "time,cpu,mem_used_pct,temp"
cat "$SAMPLE_LOG"
echo "===END_SAMPLES==="
echo ''