qs-stats: single writer, fix @STAT pipe race
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
fb0a724d7e
commit
a7976205a8
1 changed files with 23 additions and 18 deletions
|
|
@ -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
|
||||
${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
|
||||
if [ "$($C/cat "$h/name" 2>/dev/null)" = coretemp ]; then
|
||||
[ "$($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
|
||||
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 ))"
|
||||
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
|
||||
$C/sleep 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
'')
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue