diff --git a/hosts/FredOS-Mediaserver.nix b/hosts/FredOS-Mediaserver.nix index 3b8f2dd..678aa2b 100644 --- a/hosts/FredOS-Mediaserver.nix +++ b/hosts/FredOS-Mediaserver.nix @@ -31,28 +31,33 @@ # one @STAT line per tick (hottest coretemp across both sockets, WAN # byte rates from eno1). Everything rides one SSH connection. (pkgs.writeShellScriptBin "qs-stats" '' + # Single writer: the loop re-emits top's lines itself and injects a + # @STAT line at each frame header. top writing the pipe directly in + # parallel raced the injected lines (pipe writes aren't line-atomic) + # and spliced @STAT mid-frame, where the client parser never saw it. C=${pkgs.coreutils}/bin export LC_ALL=C - ${pkgs.procps}/bin/top -b -d 2 -w 512 & - trap 'kill $! 2>/dev/null' EXIT prev_rx="" - while :; do - t=0 - for h in /sys/class/hwmon/*; do - if [ "$($C/cat "$h/name" 2>/dev/null)" = coretemp ]; then - for f in "$h"/temp*_input; do - v=$($C/cat "$f" 2>/dev/null || echo 0) - [ "$v" -gt "$t" ] && t=$v + ${pkgs.procps}/bin/top -b -d 2 -w 512 | while IFS= read -r line; do + printf '%s\n' "$line" + case $line in + "top - "*) + t=0 + for h in /sys/class/hwmon/*; do + [ "$($C/cat "$h/name" 2>/dev/null)" = coretemp ] || continue + for f in "$h"/temp*_input; do + v=$($C/cat "$f" 2>/dev/null || echo 0) + [ "$v" -gt "$t" ] && t=$v + done done - fi - done - read -r rx tx < <(${pkgs.gawk}/bin/awk '$1 == "eno1:" {print $2, $10}' /proc/net/dev) - if [ -n "$prev_rx" ]; then - echo "@STAT temp=$((t / 1000)) rxbps=$(( (rx - prev_rx) / 2 )) txbps=$(( (tx - prev_tx) / 2 ))" - fi - prev_rx=$rx - prev_tx=$tx - $C/sleep 2 + read -r rx tx < <(${pkgs.gawk}/bin/awk '$1 == "eno1:" {print $2, $10}' /proc/net/dev) + if [ -n "$prev_rx" ]; then + printf '@STAT temp=%s rxbps=%s txbps=%s\n' "$((t / 1000))" "$(( (rx - prev_rx) / 2 ))" "$(( (tx - prev_tx) / 2 ))" + fi + prev_rx=$rx + prev_tx=$tx + ;; + esac done '') ];