113 lines
4.1 KiB
Shell
113 lines
4.1 KiB
Shell
|
|
#!/bin/sh
|
||
|
|
# hubble-flow-collector — long-lived, filtered, bounded capture of Hubble flows.
|
||
|
|
#
|
||
|
|
# Why a long-lived consumer: the Cilium agent's Hubble ring buffer holds ~4096
|
||
|
|
# events per type. At the measured ~150 flows/s that is ~28 s of history, so any
|
||
|
|
# polling design (cron + `hubble observe --last N`) loses almost everything
|
||
|
|
# between runs. This process stays attached to the flow API instead.
|
||
|
|
#
|
||
|
|
# What is written (see filter.awk): drop / policy-verdict / l7 events, plus
|
||
|
|
# trace events whose peer is outside the cluster CIDRs (to-world = the
|
||
|
|
# 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.
|
||
|
|
#
|
||
|
|
# 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.
|
||
|
|
set -u
|
||
|
|
|
||
|
|
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_TOTAL_BYTES="${MAX_TOTAL_BYTES:-1006632960}" # 960 MiB (spec cap is 1 GiB)
|
||
|
|
RETRY_SECONDS="${RETRY_SECONDS:-5}"
|
||
|
|
CHECK_EVERY_LINES="${CHECK_EVERY_LINES:-200}"
|
||
|
|
|
||
|
|
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
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
if [ "$over" -eq 1 ]; then
|
||
|
|
rm -f "$f" && log "pruned $f"
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
prune
|
||
|
|
log "starting: dir=$DATA_DIR sock=$SOCK max_file=${MAX_FILE_BYTES}B max_files=$MAX_FILES max_total=${MAX_TOTAL_BYTES}B"
|
||
|
|
|
||
|
|
attempt=0
|
||
|
|
while :; do
|
||
|
|
attempt=$((attempt + 1))
|
||
|
|
stamp=$(date -u +%Y%m%dT%H%M%SZ)
|
||
|
|
seq=$attempt
|
||
|
|
cur="$DATA_DIR/flows-$stamp-$seq.jsonl"
|
||
|
|
log "attach attempt=$attempt file=$cur"
|
||
|
|
|
||
|
|
# --type is repeatable; filtering server-side keeps the 148/s trace bulk
|
||
|
|
# (all subtypes) off the wire. We only ask for trace:to-network, which is
|
||
|
|
# the packet-level "left the node toward a peer" half of the to-world test.
|
||
|
|
# stderr is left unredirected on purpose: it lands in the container log.
|
||
|
|
hubble observe \
|
||
|
|
--server "unix://$SOCK" \
|
||
|
|
--follow \
|
||
|
|
--output json \
|
||
|
|
--type drop \
|
||
|
|
--type policy-verdict \
|
||
|
|
--type l7 \
|
||
|
|
--type trace:to-network \
|
||
|
|
| awk -f "$FILTER_AWK" \
|
||
|
|
| {
|
||
|
|
lines=0
|
||
|
|
while IFS= read -r line; do
|
||
|
|
# Only JSON flows reach the file; anything else is printed to the
|
||
|
|
# container log instead of corrupting the JSONL.
|
||
|
|
case "$line" in
|
||
|
|
"{"*) ;;
|
||
|
|
*) printf '%s\n' "$line"; continue ;;
|
||
|
|
esac
|
||
|
|
printf '%s\n' "$line" >> "$cur"
|
||
|
|
lines=$((lines + 1))
|
||
|
|
if [ "$lines" -ge "$CHECK_EVERY_LINES" ]; then
|
||
|
|
lines=0
|
||
|
|
size=$(stat -c %s "$cur" 2>/dev/null || echo 0)
|
||
|
|
if [ "$size" -ge "$MAX_FILE_BYTES" ]; then
|
||
|
|
seq=$((seq + 1))
|
||
|
|
cur="$DATA_DIR/flows-$(date -u +%Y%m%dT%H%M%SZ)-$seq.jsonl"
|
||
|
|
log "rotated: $cur"
|
||
|
|
prune
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
log "stream ended ($cur)"
|
||
|
|
}
|
||
|
|
|
||
|
|
prune
|
||
|
|
log "reattaching in ${RETRY_SECONDS}s"
|
||
|
|
sleep "$RETRY_SECONDS"
|
||
|
|
done
|