feat: simple sensor panels with file-based data source (#7)

* add simulation mode for easier development

* improved sensor file watcher, poc cpu & mem usage

Trigger file read by rename event

* feat: system information sensor tool

Gather various sensor values with the sysinfo crate:
https://github.com/GuillaumeGomez/sysinfo
Values can be written to a sensor source file with the `--out` cmd line
option for the `asterctl` tool.

* ci: build sysinfo tool and include in build artifact

* feat: support integerDigits, decimalDigits sensor value format options

* docs: update documentation

Closes #6
This commit is contained in:
Markus Zehnder
2025-08-22 13:41:31 +02:00
committed by GitHub
parent 3f174251d6
commit e85d616da7
32 changed files with 3803 additions and 3933 deletions
+73
View File
@@ -0,0 +1,73 @@
#!/bin/bash
# Simple proof-of-concept Linux shell script to periodically write the CPU usage into a sensor text file.
#
# A single sensor `cpu_percent` is written to the sensor file containing the overall cpu usage in percent.
# CPU usage is calculated every second.
#
# CPU usage logic from:
# https://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux
#
set -e
set -o pipefail
REFRESH=1
TEMP_DIR="${TMPDIR:-/tmp}"
TMP_SENSOR_FILE="${TEMP_DIR}/cpu.txt.tmp"
SENSOR_FILE="${TEMP_DIR}/sensors/cpu.txt"
#=============================================================
usage() {
cat << EOF
Simple PoC script to periodically write the CPU usage into a sensor text file.
Usage:
$0 [-r REFRESH] [-s SENSOR_FILE] [-t TEMP_DIR]
-r REFRESH refresh in seconds. Default: $REFRESH
-s SENSOR_FILE output sensor file. Default: $SENSOR_FILE
-t TEMP_DIR temporary directory. Default: $TEMP_DIR
EOF
exit 1
}
#=============================================================
#------------------------------------------------------------------------------
# Start of script
#------------------------------------------------------------------------------
# check command line arguments
while getopts "r:s:t:h" opt; do
case ${opt} in
"r")
REFRESH="$OPTARG"
;;
"s")
SENSOR_FILE="$OPTARG"
;;
"t")
TMP_SENSOR_FILE="$OPTARG/cpu.txt.tmp"
;;
h )
usage
;;
: )
echo "Option: -$OPTARG requires an argument" 1>&2
usage
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
esac
done
mkdir -p "$(dirname "$SENSOR_FILE")"
while :
do
USAGE=$({ head -n1 /proc/stat;sleep "$REFRESH";head -n1 /proc/stat; } | awk '/^cpu /{u=$2-u;s=$4-s;i=$5-i;w=$6-w}END{print "cpu_percent:"int(0.5+100*(u+s+w)/(u+s+i+w))}')
echo "$USAGE" > "$TMP_SENSOR_FILE"
mv "$TMP_SENSOR_FILE" "$SENSOR_FILE"
done
+84
View File
@@ -0,0 +1,84 @@
#!/bin/bash
# Simple proof-of-concept Linux shell script to periodically write the memory usage into a sensor text file.
#
# The following sensor values are written to the sensor file.
# - memory_total in bytes
# - memory_free in bytes
# - memory_available in bytes
# - memory_usage in percent
#
# Memory usage is calculated every five seconds.
set -e
set -o pipefail
REFRESH=5
TEMP_DIR="${TMPDIR:-/tmp}"
TMP_SENSOR_FILE="${TEMP_DIR}/mem.txt.tmp"
SENSOR_FILE="${TEMP_DIR}/sensors/mem.txt"
#=============================================================
usage() {
cat << EOF
Simple PoC script to periodically write the memory usage into a sensor text file.
Usage:
$0 [-r REFRESH] [-s SENSOR_FILE] [-t TEMP_DIR]
-r REFRESH refresh in seconds. Default: $REFRESH
-s SENSOR_FILE output sensor file. Default: $SENSOR_FILE
-t TEMP_DIR temporary directory. Default: $TEMP_DIR
EOF
exit 1
}
#=============================================================
#------------------------------------------------------------------------------
# Start of script
#------------------------------------------------------------------------------
# check command line arguments
while getopts "r:s:t:h" opt; do
case ${opt} in
"r")
REFRESH="$OPTARG"
;;
"s")
SENSOR_FILE="$OPTARG"
;;
"t")
TMP_SENSOR_FILE="$OPTARG/mem.txt.tmp"
;;
h )
usage
;;
: )
echo "Option: -$OPTARG requires an argument" 1>&2
usage
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
esac
done
mkdir -p "$(dirname "$SENSOR_FILE")"
while :
do
MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2}')
MEM_FREE=$(grep MemFree /proc/meminfo | awk '{print $2}')
MEM_AVAILABLE=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
printf "memory_total:%d\nmemory_free:%d\nmemory_available:%d\nmemory_usage:%d\n" \
"$MEM_TOTAL" \
"$MEM_FREE" \
"$MEM_AVAILABLE" \
$(((MEM_TOTAL - MEM_AVAILABLE) * 100 / MEM_TOTAL)) > "$TMP_SENSOR_FILE"
mv "$TMP_SENSOR_FILE" "$SENSOR_FILE"
sleep "$REFRESH"
done