fix: qubes statistics table

- CPU sum converted to average;
- Table is redrawed on terminal resize;
- Screen is saved before being drawn to;
- Works without sensors package; and
- Queries all relevant sensors.
This commit is contained in:
Ben Grande 2025-01-27 15:56:32 +01:00
parent 8a6cdd5096
commit ebe96406fb
No known key found for this signature in database
GPG Key ID: 00C64E14F51F9E56

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# SPDX-FileCopyrightText: 2018 Chris Laprise <https://github.com/tasket> # SPDX-FileCopyrightText: 2018 Chris Laprise <https://github.com/tasket>
# SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com> # SPDX-FileCopyrightText: 2023 - 2025 Benjamin Grande M. S. <ben.grande.b@gmail.com>
# #
# SPDX-License-Identifier: GPL-3.0-only # SPDX-License-Identifier: GPL-3.0-only
@ -9,57 +9,79 @@
## Credits: https://github.com/tasket/Qubes-scripts ## Credits: https://github.com/tasket/Qubes-scripts
set -eu set -eu
if ! command -v sensors >/dev/null; then
printf '%s\n' "Program not installed: sensors" >&2
exit 1
fi
if ! command -v xentop >/dev/null; then if ! command -v xentop >/dev/null; then
printf '%s\n' "Program not installed: xentop" >&2 printf '%s\n' "Program not installed: xentop" >&2
exit 1 exit 1
fi fi
sensors="1"
if ! command -v sensors >/dev/null; then
sensors="0"
fi
sensors_stats () { redraw(){
# shellcheck disable=SC2312 stty size </dev/null 2>&1 >&2 | read -r LINES COLUMNS
sensors | grep -E -e "^(${sensor_lines})" tput ed home
} }
delay=5 do_tui(){
sortcol=3 stty -echo -icanon
sensor_lines='Package id 0|fan1|CPU' tput smcup civis home
}
undo_tui(){
stty echo icanon
tput cnorm rmcup
}
trap 'undo_tui' HUP INT QUIT ABRT TERM EXIT
trap 'redraw' WINCH
do_tui
get_header(){
cpusum=0
memsum=0
table=""
if test "${sensors}" != "0"; then
sensors | grep -E -e "^(fan[1-9]+|[C|G]PU|temp[1-9]+):" |
grep -v -e "0 RPM" -e "+0\.0.C" |
tr -s "\t" " " | tr "\n" "\t" | sed "s/\s\+$/\n/"
fi
printf '%s%-40s %-6s %6s %8s%s\n' "${bold}" 'Qube' 'State' 'CPU(%)' \
'MEM(MiB)' "${nobold}"
}
table=""
delay=1
sortcol=1
newline=' newline='
' '
sumline="--------------------------------------------------------" bold="$(tput smso)"
header="\033[2K\nVM NAME STATE CPU(%) MEM(MB)" nobold="$(tput rmso)"
index=0
trap "tput reset" HUP INT QUIT ABRT TERM EXIT get_header
tput reset
sensors_stats
printf '%b\n' "${header}"
cpusum=0; memsum=0; table="${sumline}"
# shellcheck disable=SC2016,SC2312 # shellcheck disable=SC2016,SC2312
stdbuf -oL xentop -b -f -d "${delay}" | \ xentop -b -f -d "${delay}" | \
stdbuf -oL awk '{printf ("%-32s %5s %5d %7d\n", $1,$2,$4,$5/1000) }' | \ stdbuf -oL awk '{printf ("%-40s %-6s %6d %8d\n", $1,$2,$4,$5/1000) }' | \
( (
read -r ln read -r _
while true; do while true; do
if read -r -t 0.1 ln; then if read -r -t 0.1 line; then
table="${table}${newline}${ln}" table="${table:+${table}${newline}}${line}"
read -r _ _ cpu mem <<<"${ln}" read -r _ _ cpu mem <<<"${line}"
index=$((index+1))
cpusum=$((cpusum+cpu)) cpusum=$((cpusum+cpu))
memsum=$((memsum+mem)) memsum=$((memsum+mem))
cpuavg=$((cpusum/index))
else else
# End of list, print totals, make new page. index=0
sort -k "${sortcol}" -n -r <<<"${table}" sort -k "${sortcol}" -n <<<"${table}"
printf '%-32s %5s %5d %7d\033[J\033[H' " " " " "${cpusum}" \ printf '%s%-s %-34s %-6s %6d %8d%s' "${bold}" "Total" "" "" \
"${memsum}" "${cpuavg}" "${memsum}" "${nobold}"
read -r ln tput ed home
cpusum=0; memsum=0 read -r _
table="${sumline}" get_header
#clear
#sensors |grep -E "^(${sensor_lines})"
sensors_stats
printf '%b\n' "\033[2K${header}\n"
fi fi
done done
) )