首次提交

This commit is contained in:
2026-09-28 16:09:31 +08:00
commit 4a6839319a
71 changed files with 12144 additions and 0 deletions

33
scripts/monitor.sh Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# 每隔 N 秒采集 CPU/IO/负载/网络,带时间戳写入一个文件。
# 用法: bash monitor.sh [间隔秒,默认5] [输出文件,默认 monitor.log]
# 停止: Ctrl-C 或 kill $(cat monitor.pid)
INTERVAL="${1:-5}"
OUT="${2:-monitor.log}"
echo $$ > monitor.pid
echo "===== 监控开始 $(date) 间隔${INTERVAL}s 输出 $OUT =====" | tee -a "$OUT"
command -v iostat >/dev/null 2>&1 && HAS_IOSTAT=1 || HAS_IOSTAT=0
command -v sar >/dev/null 2>&1 && HAS_SAR=1 || HAS_SAR=0
[ "$HAS_IOSTAT" = 0 ] && echo "(无 iostat, 只记 vmstat; 装: yum/apt install sysstat)" | tee -a "$OUT"
trap 'echo "===== 监控停止 $(date) =====" >> "$OUT"; rm -f monitor.pid; exit 0' INT TERM
while true; do
{
echo "----- $(date '+%Y-%m-%d %H:%M:%S') -----"
echo "[vmstat] r=运行队列 b=阻塞(等IO) wa=iowait% id=空闲%"
vmstat 1 2 | tail -1
if [ "$HAS_IOSTAT" = 1 ]; then
echo "[iostat] %util>90=磁盘吃满 w/s=写次数 w_await=写延迟ms"
# 取第二份报告(间隔值, 非开机累计), 只留设备行
iostat -dx 1 2 | awk '/Device/{c++} c==2'
fi
if [ "$HAS_SAR" = 1 ]; then
echo "[net] 网卡收发 kB/s:"
sar -n DEV 1 1 | awk '/Average:/ && $2!="lo" && $2!="IFACE" {printf " %s rx=%s tx=%s kB/s\n",$2,$5,$6}'
fi
echo ""
} >> "$OUT" 2>&1
sleep "$INTERVAL"
done