From 51340cff7dee8bda3c5ac4a92195ba6bd432f898 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 24 Apr 2020 20:13:51 +0200 Subject: [hardware-stats] Tweak hooks for immediate usage updates; add %CPU stat The hooks that immediately call the usage update script have been modified to only fire for :0 X11 sessions, and also trigger for Xreset, which usually means a session ended. Also added a %CPU stat that gets reported every 5 minutes. --- .../data/etc/X11/Xreset.d/update-usage-stats | 4 +++ .../openslx/pam/hooks/session-open.d/usage_stats | 6 ++-- .../opt/openslx/scripts/cron-system_usage_update | 36 +++++++++++++++++++--- .../system-check/hooks.d/50-hardware-report | 4 +-- 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 core/modules/hardware-stats/data/etc/X11/Xreset.d/update-usage-stats (limited to 'core/modules/hardware-stats') diff --git a/core/modules/hardware-stats/data/etc/X11/Xreset.d/update-usage-stats b/core/modules/hardware-stats/data/etc/X11/Xreset.d/update-usage-stats new file mode 100644 index 00000000..03ab852e --- /dev/null +++ b/core/modules/hardware-stats/data/etc/X11/Xreset.d/update-usage-stats @@ -0,0 +1,4 @@ +#!/bin/ash - sourced + +[ "$DISPLAY" = ":0" ] && /opt/openslx/scripts/cron-system_usage_update & + diff --git a/core/modules/hardware-stats/data/opt/openslx/pam/hooks/session-open.d/usage_stats b/core/modules/hardware-stats/data/opt/openslx/pam/hooks/session-open.d/usage_stats index e2154aa7..0c287f04 100644 --- a/core/modules/hardware-stats/data/opt/openslx/pam/hooks/session-open.d/usage_stats +++ b/core/modules/hardware-stats/data/opt/openslx/pam/hooks/session-open.d/usage_stats @@ -1,9 +1,7 @@ #!/bin/ash - sourced -if [ "x$PAM_SERVICE" != "xsu" -a "x$PAM_SERVICE" != "xsudo" ]; then - sleep 3 +if [ "$DISPLAY" = ":0" ] || [ "$PAM_TTY" = ":0" ]; then + sleep 4 /opt/openslx/scripts/cron-system_usage_update fi & -true - diff --git a/core/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update b/core/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update index 0ddb6159..6e0b7a9e 100755 --- a/core/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update +++ b/core/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update @@ -1,5 +1,9 @@ #!/bin/ash +touch /run/openslx || exit 1 +full=false +[ "$1" = "--full" ] && full=true + [ -r "/etc/system-uuid" ] || exit 0 . /opt/openslx/config @@ -16,14 +20,16 @@ Name= LEGEND= loginctl --help 2>&1 | grep -q -- '--no-legend' && LEGEND="--no-legend" for SESSION in $(loginctl $LEGEND | awk '{print $1}'); do - unset Display Remote State - eval $(loginctl -p Display -p Remote -p State -p Class -p Name show-session "$SESSION") + unset Display Remote State Class + eval "$(loginctl -p Display -p Remote -p State -p Class -p Name show-session "$SESSION")" if [ "$Display" = ":0" ] && [ "$Remote" = "no" ] && [ "$State" = "active" -o "$State" = "online" ] && [ "$Class" = "user" ]; then USED=1 # We only consider sessions on the primary display, which should always be the case - break + break # keeps $Name set! fi + unset Name done + # Also report usage of /tmp and swap TMP="$( slx-tools fs_path_space /tmp )" TMP_FREE="${TMP% *}" @@ -40,12 +46,34 @@ if [ -n "$SLX_EXAM" ]; then # to know about it, more than other runmodes actually SLX_RUNMODE_MODULE="exams" fi +# CPU temp and load +CPU_LOAD= +if $full; then + current="$( awk '$1 == "cpu" {print ($5+$6) " " ($2+$3+$4+$5+$6+$7+$8)}' /proc/stat )" + c_count=${current#* } + write=false + if [ -s "/run/openslx/usage.update" ]; then + old="$( cat "/run/openslx/usage.state" )" + l_count="${old#* }" + # sloppy way to prevent rapid updates; counter speed varies depending on HZ and core count + if [ "$(( c_count - l_count ))" -gt 12000 ]; then + write=true + CPU_LOAD="$( echo "$current $old" | awk '{ print (100 - ($1 - $3)/($2 - $4) * 100) }' )" + fi + else + write=true + fi + if $write; then + echo "$current" > "/run/openslx/usage.state" + fi +fi curl --retry 3 --retry-connrefused -m 6 -s --data-urlencode "type=~runstate" --data-urlencode "uuid=$UUID" --data-urlencode "used=$USED" \ --data-urlencode "user=$Name" --data-urlencode "tmpsize=$TMP_SIZE" --data-urlencode "tmpfree=$TMP_FREE" \ --data-urlencode "swapsize=$SWAP_SIZE" --data-urlencode "swapfree=$SWAP_FREE" \ --data-urlencode "memsize=$MEM_SIZE" --data-urlencode "memfree=$MEM_FREE" \ - --data-urlencode "runmode=$SLX_RUNMODE_MODULE" "$SLX_REMOTE_LOG" > /dev/null 2>&1 + --data-urlencode "runmode=$SLX_RUNMODE_MODULE" --data-urlencode "cpuload=$CPU_LOAD" \ + "$SLX_REMOTE_LOG" > /dev/null 2>&1 # Warn user if tmp or swap usage is high; system might crash soon diff --git a/core/modules/hardware-stats/data/opt/openslx/system-check/hooks.d/50-hardware-report b/core/modules/hardware-stats/data/opt/openslx/system-check/hooks.d/50-hardware-report index a17e9ead..31b0b26f 100755 --- a/core/modules/hardware-stats/data/opt/openslx/system-check/hooks.d/50-hardware-report +++ b/core/modules/hardware-stats/data/opt/openslx/system-check/hooks.d/50-hardware-report @@ -70,13 +70,13 @@ report_hardware_info() { SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/openslx/sbin:/opt/openslx/bin - ${START}-59/5 * * * * root sleep ${DELAY}; /opt/openslx/scripts/cron-system_usage_update + ${START}-59/5 * * * * root sleep ${DELAY}; /opt/openslx/scripts/cron-system_usage_update --full EOF # TODO remove this hack one day: Sometimes, aufs doesn't update the mtime of dirs # when creating files, so cron would not rescan the cron directory. touch "/etc/cron.d" # Trigger right now so resource usage gets updated - /opt/openslx/scripts/cron-system_usage_update + /opt/openslx/scripts/cron-system_usage_update --full return 0 } -- cgit v1.2.3-55-g7522