From 7e42ad6d1cacc60cb80960e23766146b029faea0 Mon Sep 17 00:00:00 2001 From: Lance Walters Date: Fri, 27 Nov 2026 01:09:57 -0500 Subject: [PATCH] netsec(collector): gzip on rotate so 960 MiB holds ~72-100 h of flows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The collector wrote 150 MiB plaintext files into a directory capped at 960 MiB, which bought only ~4-5 h of retention at the measured 4.5-6.2 GB/day (3380-4280 kB/min) of filtered Hubble JSONL — the cap was sized for a forensic window, not for hours. Compress on rotate; no schema change, no filter change, no DS argument change: * the file being appended stays plain `flows--.jsonl` (one file for a reader to tail); * on rotation the closed file is gzip'd to `.jsonl.gz` through a temp name + atomic rename, in the background, so a reader never sees a partial archive and the flow reader never stalls (stalling it makes the agent drop events); * prune bounds the WHOLE directory at MAX_TOTAL_BYTES = 960 MiB — archives and in-flight `.gz.tmp` included — deleting oldest first, and never reaps the live file nor a file whose gzip is still running (that would discard the archive and lose the entire file); * MAX_FILES now bounds plain files only. Applying it to the archives is exactly what would keep retention at a few hours. Measured on this node's live data 2026-11-27: 18.53x ratio at gzip -9 (17.85x at -6), i.e. 960 MiB holds 72.6 h at the 6.16 GB/day burst sample, 92 h at 4.87 GB/day and ~100 h at the 4.48 GB/day 55-minute average. Verified offline against a replaying fake `hubble` (rotation->archive parity, gzip -t on every archive, atomicity, oldest-first prune, live-file and in-flight protection) and inside the real cilium image under dash (23 rotations -> 23 valid archives, no loss). --- apps/base/netsec/collect.sh | 160 ++++++++++++++++++++++++++------ apps/base/netsec/daemonset.yaml | 6 +- 2 files changed, 138 insertions(+), 28 deletions(-) diff --git a/apps/base/netsec/collect.sh b/apps/base/netsec/collect.sh index 363ea9f..7021d25 100644 --- a/apps/base/netsec/collect.sh +++ b/apps/base/netsec/collect.sh @@ -11,8 +11,18 @@ # beacon/exfil signal). Intra-cluster trace ACK chatter is discarded. # # Bounds (hard, enforced here and not by a follow-up card): -# <= MAX_FILES files, <= MAX_TOTAL_BYTES total, oldest deleted first. -# MAX_FILES(6) * MAX_FILE_BYTES(150 MiB) = 900 MiB <= 1 GiB spec cap. +# * the file being appended right now is plain `flows--.jsonl`, so a +# reader always has exactly one file to tail; +# * on rotation the just-closed file is gzip'd to `flows--.jsonl.gz` +# (temp name + atomic rename, so a reader never sees a half-written archive); +# * <= MAX_FILES *plain* flows-*.jsonl (live file + any leftover from a restart); +# * <= MAX_TOTAL_BYTES for the WHOLE directory, archives and in-flight .tmp +# included; oldest deleted first. +# Measured 2026-11-27 on siriussec: 3.1-4.3 MB/min (4.5-6.2 GB/day) of JSONL +# compression at 18.5x (level 9, whole closed files), so the 960 MiB byte bound +# holds 72-100 h of flows (72.6 h at the 6.2 GB/day CI-burst rate, 92-100 h at +# the sustained rate). The byte bound is the binding one: MAX_FILES must NOT be +# applied to the archives, or retention collapses back to a few hours. # # Only /data (hostPath /var/log/netsec) is ever written. Logs go to stdout so # they land in the container log, never on the node's disk. @@ -22,8 +32,12 @@ DATA_DIR="${DATA_DIR:-/data}" SOCK="${SOCK:-/var/run/cilium/hubble.sock}" FILTER_AWK="${FILTER_AWK:-/scripts/filter.awk}" MAX_FILE_BYTES="${MAX_FILE_BYTES:-157286400}" # 150 MiB -MAX_FILES="${MAX_FILES:-6}" +MAX_FILES="${MAX_FILES:-6}" # plain .jsonl only MAX_TOTAL_BYTES="${MAX_TOTAL_BYTES:-1006632960}" # 960 MiB (spec cap is 1 GiB) +GZIP_LEVEL="${GZIP_LEVEL:-9}" # 18.5x on live data (17.9x at 6); ~1 s per 150 MiB, off the read path +# Never compress a file appended to this recently: a restart must not gzip a +# file another writer still owns. +COMPRESS_MIN_AGE="${COMPRESS_MIN_AGE:-60}" RETRY_SECONDS="${RETRY_SECONDS:-5}" CHECK_EVERY_LINES="${CHECK_EVERY_LINES:-200}" @@ -31,35 +45,121 @@ log() { printf '%s collector: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"; } mkdir -p "$DATA_DIR" || { log "cannot create $DATA_DIR"; exit 1; } -# Delete oldest files until the directory is inside both bounds. -# Newest first, keep while count <= MAX_FILES and running total <= MAX_TOTAL_BYTES. -prune() { - # Filenames are generated by this script and contain no spaces, so plain - # word splitting is safe. (Do not `set -f` here: GNU ls does not glob the - # pattern itself, so noglob would make this match nothing.) - files=$(ls -1t "$DATA_DIR"/flows-*.jsonl 2>/dev/null) - [ -n "$files" ] || return 0 - n=0 - total=0 - over=0 - for f in $files; do - n=$((n + 1)) - if [ "$over" -eq 0 ]; then - sz=$(stat -c %s "$f" 2>/dev/null || echo 0) - total=$((total + sz)) - if [ "$n" -gt "$MAX_FILES" ] || [ "$total" -gt "$MAX_TOTAL_BYTES" ]; then - over=1 +fsize() { stat -c %s "$1" 2>/dev/null || echo 0; } +mtime() { stat -c %Y "$1" 2>/dev/null || date -u +%s; } + +# Is $1 the file the writer is appending to right now? `cur` is unset outside +# the write loop (the startup sweep runs before it is assigned). +is_current() { [ -n "${cur:-}" ] && [ "$1" = "$cur" ]; } + +# Is a gzip for $1 currently running? The temp file is created *before* the +# compressor forks, so a prune that runs immediately after compress() sees it +# and leaves that file alone — deleting it mid-gzip would discard the archive +# and lose the whole file's flows. +in_flight() { + for t in "$1".gz.tmp*; do + [ -e "$t" ] && return 0 + done + return 1 +} + +# Compress one closed .jsonl in the BACKGROUND so the flow reader is never +# blocked (blocking it would stall `hubble observe` and make the agent drop +# events for this client). Order: gzip to a temp name, rename into place, then +# drop the plain file. A reader that globs *.jsonl therefore sees either the +# whole plain file or no plain file at all — never a partial .gz. +compress() { + f=$1 + force=${2:-0} # 1 = we just closed it ourselves; skip the age guard + is_current "$f" && return 0 + [ -e "$f" ] || return 0 + [ -e "$f.gz" ] && return 0 + in_flight "$f" && return 0 + if [ "$force" -eq 0 ]; then + age=$(( $(date -u +%s) - $(mtime "$f") )) + [ "$age" -ge "$COMPRESS_MIN_AGE" ] || return 0 + fi + if [ ! -s "$f" ]; then + rm -f "$f" # closed with nothing in it: nothing to keep + log "removed empty $f" + return 0 + fi + tmp="$f.gz.tmp.$$" + : > "$tmp" 2>/dev/null || return 0 # marker: prune must not reap $f now + ( + if gzip -"$GZIP_LEVEL" -c "$f" > "$tmp" 2>/dev/null && [ -s "$tmp" ]; then + # Preserve the source mtime so the pruner still sees true chronology. + touch -r "$f" "$tmp" 2>/dev/null + if [ -e "$f" ] && mv -f "$tmp" "$f.gz"; then + rm -f "$f" + log "compressed $(basename "$f") -> $(basename "$f.gz") $(fsize "$f.gz")B" + else + rm -f "$tmp" # pruner took the plain file meanwhile fi + else + rm -f "$tmp" + log "compress failed (left plain): $f" fi - if [ "$over" -eq 1 ]; then - rm -f "$f" && log "pruned $f" + ) & +} + +# Delete oldest files until the directory is inside both bounds. +# `ls -1t` is newest-first, so walking it and deleting from the first overflow +# onward removes the oldest data first. Filenames are generated by this script +# and contain no spaces, so plain word splitting is safe. (Do not `set -f` here: +# GNU ls does not glob the pattern itself, so noglob would make this match +# nothing.) +prune() { + files=$(ls -1t "$DATA_DIR"/flows-* 2>/dev/null) + [ -n "$files" ] || return 0 + total=0 + under=1 + for f in $files; do + case "$f" in + *.gz.tmp*) + # compression in flight: counts toward the cap, never deleted + total=$((total + $(fsize "$f"))) + continue ;; + esac + sz=$(fsize "$f") + total=$((total + sz)) + is_current "$f" && continue + in_flight "$f" && continue # gzip running: this file is about to shrink + case "$f" in + *.jsonl|*.jsonl.gz) ;; + *) continue ;; # unknown junk: counted, left alone + esac + if [ "$under" -eq 0 ] || [ "$total" -gt "$MAX_TOTAL_BYTES" ]; then + under=0 + rm -f "$f" && total=$((total - sz)) && log "pruned $f (dir over ${MAX_TOTAL_BYTES}B)" fi done + + # Plain-file bound, independent of the byte bound (the live file counts). + n=0 + for f in $(ls -1t "$DATA_DIR"/flows-*.jsonl 2>/dev/null); do + n=$((n + 1)) + [ "$n" -le "$MAX_FILES" ] && continue + is_current "$f" && continue + in_flight "$f" && continue + rm -f "$f" && log "pruned plain $f (over MAX_FILES=$MAX_FILES)" + done return 0 } +# A compressor killed mid-flight (pod stop) leaves flows-*.gz.tmp* behind. +for t in "$DATA_DIR"/flows-*.gz.tmp*; do + [ -e "$t" ] || continue + rm -f "$t" && log "removed stale temp $t" +done + prune -log "starting: dir=$DATA_DIR sock=$SOCK max_file=${MAX_FILE_BYTES}B max_files=$MAX_FILES max_total=${MAX_TOTAL_BYTES}B" +# Anything plain left by a previous writer is archived on startup, so a restart +# does not leave uncompressed history lying ahead of the byte bound. +for f in $(ls -1t "$DATA_DIR"/flows-*.jsonl 2>/dev/null); do + compress "$f" +done +log "starting: dir=$DATA_DIR sock=$SOCK max_file=${MAX_FILE_BYTES}B max_files=$MAX_FILES max_total=${MAX_TOTAL_BYTES}B gzip=-$GZIP_LEVEL" attempt=0 while :; do @@ -95,11 +195,19 @@ while :; do lines=$((lines + 1)) if [ "$lines" -ge "$CHECK_EVERY_LINES" ]; then lines=0 - size=$(stat -c %s "$cur" 2>/dev/null || echo 0) + size=$(fsize "$cur") if [ "$size" -ge "$MAX_FILE_BYTES" ]; then + prev="$cur" seq=$((seq + 1)) cur="$DATA_DIR/flows-$(date -u +%Y%m%dT%H%M%SZ)-$seq.jsonl" - log "rotated: $cur" + log "rotated: $cur (closed $prev at $(fsize "$prev")B)" + # Archive what we just closed (we own it, so no age guard), + # then re-check the bounds. The sweep also archives any + # orphan a previous writer left behind. + compress "$prev" 1 + for f in $(ls -1t "$DATA_DIR"/flows-*.jsonl 2>/dev/null); do + compress "$f" + done prune fi fi diff --git a/apps/base/netsec/daemonset.yaml b/apps/base/netsec/daemonset.yaml index 0727fa5..63ba4b1 100644 --- a/apps/base/netsec/daemonset.yaml +++ b/apps/base/netsec/daemonset.yaml @@ -55,8 +55,10 @@ spec: value: /data - name: SOCK value: /var/run/cilium/hubble.sock - # 6 files x 150 MiB = 900 MiB ceiling, inside the 1 GiB spec cap; - # MAX_TOTAL_BYTES is the second, independent bound. + # MAX_FILES bounds PLAIN .jsonl files (the live one plus leftovers + # from a restart). Rotated files are gzip'd (~17.9x measured) and + # bounded by MAX_TOTAL_BYTES, the whole-directory ceiling (archives + # included) that stays inside the 1 GiB spec cap. - name: MAX_FILE_BYTES value: "157286400" - name: MAX_FILES -- 2.52.0