nomadro/apps/admin/deploy/_vip_track_assets/healthcheck.sh
Eric 3940a4ed3b Ship VIP wechat user CRUD and in-shell user-list nav.
Enrich vip-track ntfy/avatar lookup, add wechat-users API CRUD, and mount admin user-list inside the SPA shell instead of a fullscreen back-button overlay.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-20 09:29:32 +08:00

106 lines
3.2 KiB
Bash
Raw Permalink 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.

#!/bin/bash
# vip.hackrobot.cn health → ntfy on edge (down/up). Does not touch VIP process.
# Anti-flap: require N consecutive fails/oks; never append echo after curl -w.
STATE_FILE=/opt/vip-track/health.state
STREAK_FILE=/opt/vip-track/health.streak
NTFY_ENV=/opt/vip-track/ntfy.env
URL_HOME=https://vip.hackrobot.cn/
URL_TRACK=http://127.0.0.1:4011/health
# 不探公网 /vip-track.js(易被 SSL/CDN 抖成 000);只看本地文件是否在
JS_FILE=/opt/vip-track/vip-track.js
NEED_DOWN=2
NEED_UP=2
if [ -f "$NTFY_ENV" ]; then
# shellcheck disable=SC1090
set -a
. "$NTFY_ENV"
set +a
fi
NTFY_BASE=${NTFY_BASE_URL:-https://ntfy.nomadro.cn}
NTFY_TOPIC=${NTFY_TOPIC:-vip-hackrobot}
NTFY_TOKEN=${NTFY_TOKEN:-}
http_code() {
# Only %{http_code}; on total failure curl still prints 000 and exits non-zero — do NOT echo again.
code=$(curl -sk -o /dev/null -w '%{http_code}' --connect-timeout 5 --max-time 12 "$1" 2>/dev/null || true)
case "$code" in
[1-5][0-9][0-9]) printf '%s' "$code" ;;
*) printf '000' ;;
esac
}
notify() {
title="$1"
msg="$2"
tags="$3"
pri="$4"
ev="$5"
ascii_title=$(printf '%s' "$title" | tr -cd 'A-Za-z0-9 _\[\]VIPWeb.-' | cut -c1-60)
[ -n "$ascii_title" ] || ascii_title="WebVIP"
hdr=(-H "Title: $ascii_title" -H "Tags: $tags" -H "Priority: $pri")
if [ -n "$NTFY_TOKEN" ]; then
hdr+=(-H "Authorization: Bearer $NTFY_TOKEN")
fi
curl -sS -o /dev/null --max-time 5 "${hdr[@]}" -d "${title}: ${msg}" "${NTFY_BASE}/${NTFY_TOPIC}" || true
curl -sS -o /dev/null --max-time 3 -X POST http://127.0.0.1:4011/ \
-H 'content-type: application/json' \
-d "{\"events\":[{\"name\":\"${ev}\",\"page\":\"/\",\"label\":\"health\"}],\"client\":\"healthcheck\"}" || true
}
code_home=$(http_code "$URL_HOME")
code_track=$(http_code "$URL_TRACK")
if [ -f "$JS_FILE" ] && [ -s "$JS_FILE" ]; then
code_js=file_ok
else
code_js=missing
fi
# Primary = homepage. Sidecar/track secondary; js 只看本地文件,避免公网误报。
ok=1
if [ "$code_home" != "200" ]; then
ok=0
elif [ "$code_track" != "200" ] && [ "$code_js" != "file_ok" ]; then
ok=0
fi
prev=$(cat "$STATE_FILE" 2>/dev/null || echo up)
# streak format: kind count e.g. fail 2 / ok 1
read -r streak_kind streak_n <"$STREAK_FILE" 2>/dev/null || true
streak_kind=${streak_kind:-ok}
streak_n=${streak_n:-0}
if [ "$ok" = 1 ]; then
if [ "$streak_kind" = "ok" ]; then
streak_n=$((streak_n + 1))
else
streak_kind=ok
streak_n=1
fi
else
if [ "$streak_kind" = "fail" ]; then
streak_n=$((streak_n + 1))
else
streak_kind=fail
streak_n=1
fi
fi
echo "$streak_kind $streak_n" >"$STREAK_FILE"
now=$prev
if [ "$prev" != "down" ] && [ "$streak_kind" = "fail" ] && [ "$streak_n" -ge "$NEED_DOWN" ]; then
now=down
elif [ "$prev" = "down" ] && [ "$streak_kind" = "ok" ] && [ "$streak_n" -ge "$NEED_UP" ]; then
now=up
fi
echo "$now" >"$STATE_FILE"
detail="home=$code_home js=$code_js track=$code_track streak=${streak_kind}:${streak_n} · vip.hackrobot.cn"
if [ "$now" = "down" ] && [ "$prev" != "down" ]; then
notify "[WebVIP] 站点异常" "$detail" "rotating_light,skull" "5" "health_down"
elif [ "$now" = "up" ] && [ "$prev" = "down" ]; then
notify "[WebVIP] 站点恢复" "$detail" "white_check_mark,tada" "3" "health_up"
fi
exit 0