29 lines
1.1 KiB
Bash
29 lines
1.1 KiB
Bash
#!/bin/bash
|
|
# /opt/topfans/loadtest/monitor/sample.sh
|
|
# 后台采样,写到 metrics-feed.jsonl
|
|
set -e
|
|
|
|
OUT="/opt/topfans/loadtest/metrics-feed.jsonl"
|
|
INTERVAL=${INTERVAL:-5}
|
|
PG_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.Names}}' | grep -v exporter | head -1)
|
|
[ -z "$PG_CONTAINER" ] && { echo "❌ 找不到 postgres 容器"; exit 1; }
|
|
export PGPASSWORD="${DB_PASSWORD:-postgres123}"
|
|
|
|
echo "📊 sampling to $OUT every ${INTERVAL}s, pid $$"
|
|
: > "$OUT"
|
|
while true; do
|
|
TS=$(date +%s)
|
|
PG_ACTIVE=$(docker exec -e PGPASSWORD="$PGPASSWORD" "$PG_CONTAINER" psql -U postgres -d topfans -tA -c "SELECT count(*) FROM pg_stat_activity WHERE state='active'" 2>/dev/null || echo 0)
|
|
DISK_FREE=$(df -B1G /opt | tail -1 | awk '{print $4}')
|
|
OOM=$(docker events --filter event=oom --since 5s --until 0s --format '{{.Actor.Attributes.name}}' 2>/dev/null | head -1)
|
|
if [ -n "$OOM" ]; then
|
|
OOM_FLAG="true"
|
|
OOM_NAME="$OOM"
|
|
else
|
|
OOM_FLAG="false"
|
|
OOM_NAME=""
|
|
fi
|
|
echo "$TS pg_active=$PG_ACTIVE disk_free=$DISK_FREE oom=$OOM_FLAG oom_container=$OOM_NAME" >> "$OUT"
|
|
sleep "$INTERVAL"
|
|
done
|