2026-09-30 11:46:00 +00:00
#!/bin/bash
#
# emergency-storeall.sh
#
2026-09-30 16:09:16 +00:00
# JVM heap-pressure watcher. Meant to be run frequently (e.g. every 10-15
# minutes) on its own cron schedule, independent of and much more often
# than nightly-storeall.sh.
2026-09-30 11:46:00 +00:00
#
2026-09-30 16:09:16 +00:00
# Cron-driven, same shape as nightly-storeall.sh: a single check-and-log
# per invocation, no internal sleep/loop - cron provides the cadence, and
# a missed or killed run just gets picked up again at the next tick.
2026-09-30 11:46:00 +00:00
#
2026-09-30 16:09:16 +00:00
# IMPORTANT: this script currently only WATCHES and LOGS heap pressure -
# it does NOT force storeall. That decision is still made manually for
# now (see the "GC death-spiral vs. a normal single Full GC" discussion
# this was parked on - a real historical example of a run bad enough to
# justify forcing it has never actually been found yet). Every run writes
# a MaintenanceLog entry with the current heap/GC numbers regardless of
# condition, so this quietly builds exactly the historical dataset that
# was missing last time - once there's real incident data to calibrate
# against, the forcing logic can be added on top of this without changing
# anything about how the numbers are gathered.
#
# Condition label is deliberately based on old-gen occupancy ALONE - the
# one signal already validated against real production GC logs (see
# sportrum-ansible's group_vars/production.yml for the 14-event history
# behind the 3072MB heap sizing). Full GC count/time are surfaced in the
# log purely as data, not fed into the condition label: a single Full GC
# at high occupancy is normal operation, not a warning sign by itself,
# and there isn't yet a validated way to tell a normal Full GC apart from
# the start of a death spiral just from GC frequency alone. Once the
# older incident logs are recovered, that can change.
#
# normal old-gen below HEAP_ELEVATED_THRESHOLD_PCT
# elevated old-gen at/above HEAP_ELEVATED_THRESHOLD_PCT, below CRITICAL
# critical old-gen at/above HEAP_CRITICAL_THRESHOLD_PCT
#
# Exits 0 on every completed check, whatever the condition - this script
# only observes. Exits 1 only if the heap check itself couldn't be
# performed (jstat missing, pid not found, etc.), so a broken monitor is
# still visible to cron/whatever watches this script's exit code.
2026-09-30 11:46:00 +00:00
set -u
cd " $( dirname " $0 " ) "
. ./upgrade-funs.sh
2026-09-30 16:09:16 +00:00
# ---- configuration - tune for your environment ----
# TODO: both still starting guesses, not yet validated against real
# jstat readings from production. HEAP_CRITICAL_THRESHOLD_PCT carries
# over the same 85% guess used before this script stopped forcing
# anything; HEAP_ELEVATED_THRESHOLD_PCT is new and even less validated -
# tune both once you've observed normal vs. tight old-gen occupancy on
# the actual server.
HEAP_ELEVATED_THRESHOLD_PCT = " ${ HEAP_ELEVATED_THRESHOLD_PCT :- 75 } "
HEAP_CRITICAL_THRESHOLD_PCT = " ${ HEAP_CRITICAL_THRESHOLD_PCT :- 85 } "
STATE_DIR = " ${ STATE_DIR :- $( dirname " $0 " ) /state } "
STATE_FILE = " $STATE_DIR /emergency-storeall.state "
mkdir -p " $STATE_DIR "
2026-09-30 11:46:00 +00:00
log( ) {
echo " $( date '+%Y-%m-%d %H:%M:%S' ) emergency-storeall: $* "
}
2026-09-30 16:09:16 +00:00
# Reads jstat -gcutil for the running Tomcat and prints five
# space-separated numbers: old-gen occupancy pct, full GC count (total
# since JVM start), full GC total time in seconds, young GC count, young
# GC total time in seconds. Prints nothing and returns 1 on any failure -
# every failure reason is logged so a silent/broken cron run is still
# diagnosable from its own log line.
#
# NOTE: same caveat as before this rewrite - untested against a real JVM
# in this session. Columns are located dynamically from jstat's own
# header rather than hardcoded by position, worth a dry run against
# production before trusting the numbers it produces.
read_heap_stats( ) {
local tomcat_pid out header data
local col_o col_fgc col_fgct col_ygc col_ygct
local o fgc fgct ygc ygct
2026-09-30 11:46:00 +00:00
tomcat_pid = $( sudo supervisorctl pid tomcat 2>/dev/null)
if [ -z " $tomcat_pid " ] ; then
log "could not resolve tomcat pid via supervisorctl, skipping heap check"
return 1
fi
2026-09-30 16:09:16 +00:00
2026-09-30 11:46:00 +00:00
out = $( sudo jstat -gcutil " $tomcat_pid " 2>/dev/null)
if [ -z " $out " ] ; then
log " jstat returned nothing for pid $tomcat_pid , skipping heap check "
return 1
fi
2026-09-30 16:09:16 +00:00
2026-09-30 11:46:00 +00:00
header = $( head -n 1 <<< " $out " )
data = $( tail -n 1 <<< " $out " )
2026-09-30 16:09:16 +00:00
col_o = $( awk '{for (i=1;i<=NF;i++) if ($i=="O") print i}' <<< " $header " )
col_fgc = $( awk '{for (i=1;i<=NF;i++) if ($i=="FGC") print i}' <<< " $header " )
col_fgct = $( awk '{for (i=1;i<=NF;i++) if ($i=="FGCT") print i}' <<< " $header " )
col_ygc = $( awk '{for (i=1;i<=NF;i++) if ($i=="YGC") print i}' <<< " $header " )
col_ygct = $( awk '{for (i=1;i<=NF;i++) if ($i=="YGCT") print i}' <<< " $header " )
if [ -z " $col_o " ] || [ -z " $col_fgc " ] || [ -z " $col_fgct " ] \
|| [ -z " $col_ygc " ] || [ -z " $col_ygct " ]
then
log "could not find all expected columns (O/FGC/FGCT/YGC/YGCT) in jstat output"
2026-09-30 11:46:00 +00:00
return 1
fi
2026-09-30 16:09:16 +00:00
o = $( awk -v c = " $col_o " '{print $c}' <<< " $data " | cut -d. -f1)
fgc = $( awk -v c = " $col_fgc " '{print $c}' <<< " $data " | cut -d. -f1)
fgct = $( awk -v c = " $col_fgct " '{print $c}' <<< " $data " )
ygc = $( awk -v c = " $col_ygc " '{print $c}' <<< " $data " | cut -d. -f1)
ygct = $( awk -v c = " $col_ygct " '{print $c}' <<< " $data " )
if [ -z " $o " ] || [ -z " $fgc " ] || [ -z " $fgct " ] || [ -z " $ygc " ] || [ -z " $ygct " ] ; then
log "could not parse one or more numbers out of jstat output"
2026-09-30 11:46:00 +00:00
return 1
fi
2026-09-30 16:09:16 +00:00
echo " $o $fgc $fgct $ygc $ygct "
2026-09-30 11:46:00 +00:00
}
# ---- main ----
2026-09-30 16:09:16 +00:00
stats = $( read_heap_stats) || exit 1
read -r old_gen_pct fgc_count fgc_time_s ygc_count ygc_time_s <<< " $stats "
# "Since last check" deltas for the Full GC numbers - purely informational
# (see the condition-label note above for why these don't drive the label
# itself yet), but this is the shape a future frequency-based signal would
# need, so it's captured now rather than only the point-in-time totals.
have_prev = false
prev_fgc_count = 0
prev_fgc_time_s = 0
if [ -f " $STATE_FILE " ] ; then
have_prev = true
read -r prev_fgc_count prev_fgc_time_s < " $STATE_FILE "
fi
prev_fgc_count = " ${ prev_fgc_count :- 0 } "
prev_fgc_time_s = " ${ prev_fgc_time_s :- 0 } "
echo " $fgc_count $fgc_time_s " > " $STATE_FILE "
if [ " $have_prev " = true ] ; then
new_fgc_count = $(( fgc_count - prev_fgc_count ))
# Negative means Tomcat restarted (counters reset) since the last check,
# not that GCs somehow went backwards - treat as "no new data" rather
# than a nonsense negative number.
[ " $new_fgc_count " -lt 0 ] && new_fgc_count = 0
new_fgc_time_s = $( awk -v a = " $fgc_time_s " -v b = " $prev_fgc_time_s " \
'BEGIN { d = a - b; if (d < 0) d = 0; printf "%.3f", d }' )
else
new_fgc_count = 0
new_fgc_time_s = "0.000"
log "no previous reading found (first run, or state file missing/reset) - since-last-check numbers start from this run"
2026-09-30 11:46:00 +00:00
fi
2026-09-30 16:09:16 +00:00
# ---- condition ----
if [ " $old_gen_pct " -ge " $HEAP_CRITICAL_THRESHOLD_PCT " ] ; then
condition = "critical"
severity = "error"
elif [ " $old_gen_pct " -ge " $HEAP_ELEVATED_THRESHOLD_PCT " ] ; then
condition = "elevated"
severity = "warning"
2026-09-30 11:46:00 +00:00
else
2026-09-30 16:09:16 +00:00
condition = "normal"
severity = "info"
2026-09-30 11:46:00 +00:00
fi
2026-09-30 16:09:16 +00:00
log " old-gen ${ old_gen_pct } % - condition: ${ condition } (+ ${ new_fgc_count } full GC(s) since last check, + ${ new_fgc_time_s } s; ${ fgc_count } full GC(s) total, ${ fgc_time_s } s total) "
maintenance_log " $severity " emergency_storeall nop " $condition " \
" Heap check: old-gen ${ old_gen_pct } %, condition ${ condition } . " \
" <oldGenPct> ${ old_gen_pct } </oldGenPct>
<fullGcCountSinceLastCheck>${ new_fgc_count } </fullGcCountSinceLastCheck>
<fullGcTimeSinceLastCheckSec>${ new_fgc_time_s } </fullGcTimeSinceLastCheckSec>
<fullGcCountTotal>${ fgc_count } </fullGcCountTotal>
<fullGcTimeTotalSec>${ fgc_time_s } </fullGcTimeTotalSec>
<youngGcCountTotal>${ ygc_count } </youngGcCountTotal>
<youngGcTimeTotalSec>${ ygc_time_s } </youngGcTimeTotalSec>"
exit 0