Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions scripts/runewager_redeploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,14 @@ _health_check() {
http_code=$(curl -sS --max-time 3 -o /dev/null -w "%{http_code}" "$HEALTH_URL" 2>/dev/null) || true
if [[ "$http_code" == "200" ]]; then
ok "Bot healthy at ${HEALTH_URL} (${elapsed}s)"
# Print health payload — degrade gracefully if python3 absent
# Pretty-print health payload. Cascade: python3 → jq → raw.
local _payload; _payload=$(curl -sS --max-time 3 "$HEALTH_URL" 2>/dev/null)
if command -v python3 >/dev/null 2>&1; then
echo "$_payload" | python3 -m json.tool 2>/dev/null | head -20 | sed 's/^/ /' || echo "$_payload" | head -20 | sed 's/^/ /'
echo "$_payload" | python3 -m json.tool 2>/dev/null | head -20 | sed 's/^/ /' \
|| echo "$_payload" | head -20 | sed 's/^/ /'
Comment on lines +257 to +258

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The pretty-print fallback on JSON-tool failure probably never triggers because the pipeline exit status is dominated by sed.

Because this is in a pipeline, the exit code comes from the last command (sed), so the || echo "$_payload" ... fallback will almost never run even if python3 -m json.tool fails or outputs nothing. The same applies to the jq branch.

To make the fallback work on parse failure, either capture the json.tool output and branch explicitly:

if ! _formatted=$(printf '%s' "$_payload" | python3 -m json.tool 2>/dev/null); then
    printf '%s
' "$_payload" | head -20 | sed 's/^/    /'
else
    printf '%s
' "_formatted" | head -20 | sed 's/^/    /'
fi

or enable set -o pipefail so the pipeline fails when python3/jq fail, and keep the || form.

elif command -v jq >/dev/null 2>&1; then
echo "$_payload" | jq . 2>/dev/null | head -20 | sed 's/^/ /' \
|| echo "$_payload" | head -20 | sed 's/^/ /'
else
echo "$_payload" | head -20 | sed 's/^/ /'
fi
Expand Down
39 changes: 36 additions & 3 deletions scripts/rw_cpu_guard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ _load_env() {

# ─────────────────────────────────── TELEGRAM ALERT ──────────────────────────

# Escape all MarkdownV2 special characters so arbitrary text is safe to insert
# into a Telegram MarkdownV2 message. Must be called before building the text
# that is handed to sendMessage.
escape_md_v2() {
local s="$1"
s="${s//\\/\\\\}" # backslash — must come first
s="${s//_/\\_}"
s="${s//\*/\\*}"
s="${s//\[/\\[}"
s="${s//\]/\\]}"
s="${s//\(/\\(}"
s="${s//\)/\\)}"
s="${s//~/\\~}"
s="${s//\`/\\\`}"
s="${s//>/\\>}"
s="${s//#/\\#}"
s="${s//+/\\+}"
s="${s//-/\\-}"
s="${s//=/\\=}"
s="${s//|/\\|}"
s="${s//\{/\\{}"
s="${s//\}/\\}}"
s="${s//./\\.}"
s="${s//!/\\!}"
printf '%s' "$s"
}

tg_alert() {
local msg="$1"
[[ -z "$TG_BOT_TOKEN" || -z "$TG_ADMIN_IDS" ]] && return 0
Expand All @@ -232,6 +259,9 @@ tg_alert() {
(( elapsed < _ALERT_COOLDOWN )) && return 0
_LAST_ALERT_TS=$now

# Escape the full text (prefix + caller message) for MarkdownV2
local safe_text; safe_text=$(escape_md_v2 "🛡 [rw_cpu_guard] ${msg}")

IFS=',' read -ra _ids <<< "$TG_ADMIN_IDS"
local id
for id in "${_ids[@]}"; do
Expand All @@ -240,8 +270,8 @@ tg_alert() {
curl -sS --max-time 6 \
"https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${id}" \
--data-urlencode "text=🛡 [rw_cpu_guard] ${msg}" \
-d "parse_mode=Markdown" \
--data-urlencode "text=${safe_text}" \
-d "parse_mode=MarkdownV2" \
>/dev/null 2>&1 || true
done
}
Expand Down Expand Up @@ -650,7 +680,10 @@ _run_forensics() {
# ── 1-second CPU tracer (catches sub-8s spikes the guard daemon may miss) ─
echo "timestamp,pid,ppid,cpu%,mem%,cmd" > "$trace"
( while true; do
ps -eo pid,ppid,pcpu,pmem,cmd --sort=-pcpu --no-headers 2>/dev/null \
# Use 'args' (not 'cmd') so the full command path and every argument
# are captured. Fields $5..$NF are joined into one value so commands
# with spaces in their paths or arguments are never truncated.
ps -eo pid,ppid,pcpu,pmem,args --sort=-pcpu --no-headers 2>/dev/null \
| awk -v ts="$(date '+%F %T')" '$3+0 > 15 {
cmd = ""; for (i=5; i<=NF; i++) cmd = cmd (i>5 ? " " : "") $i;
gsub(/,/, ";", cmd);
Expand Down