summaryrefslogtreecommitdiffstats
path: root/remote/modules/idleaction/data
diff options
context:
space:
mode:
authorSimon Rettberg2013-12-22 00:41:31 +0100
committerSimon Rettberg2013-12-22 00:41:31 +0100
commit678ccfcec84fe188f394ed0ea71801aac13d0f66 (patch)
tree14f644bc95cbee7aca878839cefda17f7bb20b75 /remote/modules/idleaction/data
parentMerge branch 'master' of simonslx:openslx-ng/tm-scripts (diff)
downloadtm-scripts-678ccfcec84fe188f394ed0ea71801aac13d0f66.tar.gz
tm-scripts-678ccfcec84fe188f394ed0ea71801aac13d0f66.tar.xz
tm-scripts-678ccfcec84fe188f394ed0ea71801aac13d0f66.zip
[idleaction] New module (WORK IN PROGESS)
idleaction is supposed to check if the machine is in use and trigger certain actions if not It can terminate sessions that have been idle for a certain amount of time, and it can shutdown the computer if no user logged in for some time. In the future it will also be able to forcefully shut down the computer at a given time of day.
Diffstat (limited to 'remote/modules/idleaction/data')
-rw-r--r--remote/modules/idleaction/data/etc/cron.d/openslx-idleaction6
-rwxr-xr-xremote/modules/idleaction/data/opt/openslx/scripts/idleaction-cron_script97
2 files changed, 103 insertions, 0 deletions
diff --git a/remote/modules/idleaction/data/etc/cron.d/openslx-idleaction b/remote/modules/idleaction/data/etc/cron.d/openslx-idleaction
new file mode 100644
index 00000000..66dbcd5c
--- /dev/null
+++ b/remote/modules/idleaction/data/etc/cron.d/openslx-idleaction
@@ -0,0 +1,6 @@
+# Trigger scripts that checks idle status of machine and triggers actions
+
+SHELL=/bin/ash
+
+*/5 * * * * root /opt/openslx/scripts/idleaction-cron_script
+
diff --git a/remote/modules/idleaction/data/opt/openslx/scripts/idleaction-cron_script b/remote/modules/idleaction/data/opt/openslx/scripts/idleaction-cron_script
new file mode 100755
index 00000000..c59d0f3b
--- /dev/null
+++ b/remote/modules/idleaction/data/opt/openslx/scripts/idleaction-cron_script
@@ -0,0 +1,97 @@
+#!/bin/ash
+
+. /opt/openslx/config || exit 1
+
+# If existent, no session is open. Will contain timestamp of last activity.
+# If not existent, at least one user is logged in
+IDLEHINT="/dev/shm/idlehint"
+NOW=$(date +%s)
+
+#
+# 1) Check for idle timeout
+#
+if [ -n "${SLX_LOGOUT_TIMEOUT}" ]; then
+ # Logout timeout is set, see which users we should kick
+ IS_IDLE=yes
+ # get all sessions
+ SESSIONS=$(loginctl | awk '{print $1}')
+ if [ -n "$SESSIONS" ]; then
+ TMP=$(/dev/shm/idlecheck.tmp)
+ # Iterate over sessions
+ for ses in $SESSIONS; do
+ # Get information
+ loginctl show-session "$ses" > "$TMP"
+ NAME=$(grep '^Name=' "$TMP" | cut -c 6-)
+ [ -z "$NAME" ] && continue # No name - should not happen
+ export DISPLAY=$(grep '^Display=' "$TMP" | cut -c 9-)
+ # X11
+ if [ -n "$DISPLAY" ]; then
+ # Seems to be x11
+ USRHOME=$(getent passwd "$NAME" | awk -F ':' '{print $6}')
+ export XAUTHORITY="$USRHOME/.Xauthority"
+ # Now that we have DISPLAY and XAUTHORITY set, xprintidle should work
+ IDLE=$(xprintidle)
+ if [ -n "$IDLE" ]; then
+ IDLE=$(( $IDLE / 1000 ))
+ if [ "$IDLE" -lt "$SLX_LOGOUT_TIMEOUT" ]; then
+ IS_IDLE=no
+ else
+ loginctl terminate-session "$ses"
+ fi
+ fi
+ continue # Done with this session, skip normal tty/ssh checks
+ fi
+ # end X11
+ # other sessions
+ IDLE=$(grep '^IdleSinceHint=' "$TMP" | cut -c 15-)
+ if [ "${#IDLE}" -lt 7 ]; then # wah wah waaaah
+ IS_IDLE=no
+ continue
+ fi
+ # divide by 1000000 by chopping of last 6 chars - number might be too large for $(( ))
+ IDLE=$(echo "$IDLE" | cut -c "-$(( ${#IDLE} - 6 ))")
+ [ "$IDLE" -gt "$NOW" ] && IDLE="$NOW"
+ IDLE=$(( $NOW - $IDLE ))
+ if [ "$IDLE" -lt "$SLX_LOGOUT_TIMEOUT" ]; then
+ IS_IDLE=no
+ else
+ loginctl terminate-session "$ses"
+ fi
+ # end other sessions
+ done
+ rm -f -- "$TMP"
+ if [ "$IS_IDLE" = "yes" ]; then
+ [ ! -e "$IDLEHINT" ] && echo "$NOW" > "$IDLEHINT"
+ else
+ rm -f -- "$IDLEHINT"
+ fi
+ fi
+else
+ # No logout timeout is set, take shortcut for shutdown timeout (if set)
+ if [ -n "$SLX_SHUTDOWN_TIMEOUT" ]; then
+ SESSIONS=$(loginctl | wc -l)
+ if [ "$SESSIONS" = "0" ]; then
+ [ ! -e "$IDLEHINT" ] && echo "$NOW" > "$IDLEHINT"
+ else
+ rm -f -- "$IDLEHINT"
+ fi
+ fi
+fi
+
+#
+# 2) Check for no-session-shutdown timeout
+#
+if [ -n "$SLX_SHUTDOWN_TIMEOUT" ] && [ -e "$IDLEHINT" ]; then
+ IDLE=$(cat "$IDLEHINT")
+ [ "$IDLE" -gt "$NOW" ] && IDLE="$NOW"
+ IDLE=$(( $NOW - $IDLE ))
+ if [ "$IDLE" -gt "$SLX_SHUTDOWN_TIMEOUT" ]; then
+ poweroff -nf # TODO: Do proper shutdown once it works reliably
+ fi
+fi
+
+#
+# 3) Check for hard scheduled shutdown
+#
+# TODO
+