Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fdd918875 | |||
|
|
7e42ad6d1c |
2 changed files with 135 additions and 25 deletions
|
|
@ -11,8 +11,18 @@
|
||||||
# beacon/exfil signal). Intra-cluster trace ACK chatter is discarded.
|
# beacon/exfil signal). Intra-cluster trace ACK chatter is discarded.
|
||||||
#
|
#
|
||||||
# Bounds (hard, enforced here and not by a follow-up card):
|
# Bounds (hard, enforced here and not by a follow-up card):
|
||||||
# <= MAX_FILES files, <= MAX_TOTAL_BYTES total, oldest deleted first.
|
# * the file being appended right now is plain `flows-<ts>-<n>.jsonl`, so a
|
||||||
# MAX_FILES(6) * MAX_FILE_BYTES(150 MiB) = 900 MiB <= 1 GiB spec cap.
|
# reader always has exactly one file to tail;
|
||||||
|
# * on rotation the just-closed file is gzip'd to `flows-<ts>-<n>.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
|
# 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.
|
# 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}"
|
SOCK="${SOCK:-/var/run/cilium/hubble.sock}"
|
||||||
FILTER_AWK="${FILTER_AWK:-/scripts/filter.awk}"
|
FILTER_AWK="${FILTER_AWK:-/scripts/filter.awk}"
|
||||||
MAX_FILE_BYTES="${MAX_FILE_BYTES:-157286400}" # 150 MiB
|
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)
|
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}"
|
RETRY_SECONDS="${RETRY_SECONDS:-5}"
|
||||||
CHECK_EVERY_LINES="${CHECK_EVERY_LINES:-200}"
|
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; }
|
mkdir -p "$DATA_DIR" || { log "cannot create $DATA_DIR"; exit 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
|
||||||
|
) &
|
||||||
|
}
|
||||||
|
|
||||||
# Delete oldest files until the directory is inside both bounds.
|
# Delete oldest files until the directory is inside both bounds.
|
||||||
# Newest first, keep while count <= MAX_FILES and running total <= MAX_TOTAL_BYTES.
|
# `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() {
|
prune() {
|
||||||
# Filenames are generated by this script and contain no spaces, so plain
|
files=$(ls -1t "$DATA_DIR"/flows-* 2>/dev/null)
|
||||||
# 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 "$files" ] || return 0
|
||||||
n=0
|
|
||||||
total=0
|
total=0
|
||||||
over=0
|
under=1
|
||||||
for f in $files; do
|
for f in $files; do
|
||||||
n=$((n + 1))
|
case "$f" in
|
||||||
if [ "$over" -eq 0 ]; then
|
*.gz.tmp*)
|
||||||
sz=$(stat -c %s "$f" 2>/dev/null || echo 0)
|
# compression in flight: counts toward the cap, never deleted
|
||||||
|
total=$((total + $(fsize "$f")))
|
||||||
|
continue ;;
|
||||||
|
esac
|
||||||
|
sz=$(fsize "$f")
|
||||||
total=$((total + sz))
|
total=$((total + sz))
|
||||||
if [ "$n" -gt "$MAX_FILES" ] || [ "$total" -gt "$MAX_TOTAL_BYTES" ]; then
|
is_current "$f" && continue
|
||||||
over=1
|
in_flight "$f" && continue # gzip running: this file is about to shrink
|
||||||
fi
|
case "$f" in
|
||||||
fi
|
*.jsonl|*.jsonl.gz) ;;
|
||||||
if [ "$over" -eq 1 ]; then
|
*) continue ;; # unknown junk: counted, left alone
|
||||||
rm -f "$f" && log "pruned $f"
|
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
|
fi
|
||||||
done
|
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
|
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
|
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
|
attempt=0
|
||||||
while :; do
|
while :; do
|
||||||
|
|
@ -95,11 +195,19 @@ while :; do
|
||||||
lines=$((lines + 1))
|
lines=$((lines + 1))
|
||||||
if [ "$lines" -ge "$CHECK_EVERY_LINES" ]; then
|
if [ "$lines" -ge "$CHECK_EVERY_LINES" ]; then
|
||||||
lines=0
|
lines=0
|
||||||
size=$(stat -c %s "$cur" 2>/dev/null || echo 0)
|
size=$(fsize "$cur")
|
||||||
if [ "$size" -ge "$MAX_FILE_BYTES" ]; then
|
if [ "$size" -ge "$MAX_FILE_BYTES" ]; then
|
||||||
|
prev="$cur"
|
||||||
seq=$((seq + 1))
|
seq=$((seq + 1))
|
||||||
cur="$DATA_DIR/flows-$(date -u +%Y%m%dT%H%M%SZ)-$seq.jsonl"
|
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
|
prune
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,10 @@ spec:
|
||||||
value: /data
|
value: /data
|
||||||
- name: SOCK
|
- name: SOCK
|
||||||
value: /var/run/cilium/hubble.sock
|
value: /var/run/cilium/hubble.sock
|
||||||
# 6 files x 150 MiB = 900 MiB ceiling, inside the 1 GiB spec cap;
|
# MAX_FILES bounds PLAIN .jsonl files (the live one plus leftovers
|
||||||
# MAX_TOTAL_BYTES is the second, independent bound.
|
# 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
|
- name: MAX_FILE_BYTES
|
||||||
value: "157286400"
|
value: "157286400"
|
||||||
- name: MAX_FILES
|
- name: MAX_FILES
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue