summaryrefslogtreecommitdiffstats
path: root/core/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update
blob: 0ddb6159c42b583a276ba03e3fb7cdd06b4e4a74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/ash

[ -r "/etc/system-uuid" ] || exit 0

. /opt/openslx/config
[ -z "$SLX_REMOTE_LOG" ] && exit 0

UUID=$(cat "/etc/system-uuid")

[ -z "$UUID" ] && exit 1

USED=0
Name=

# detect if we need '--no-legend' or not...
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")
	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
	fi
done

# Also report usage of /tmp and swap
TMP="$( slx-tools fs_path_space /tmp )"
TMP_FREE="${TMP% *}"
TMP_SIZE="${TMP#* }"
SWAP_SIZE=$( awk '{if ($1 == "SwapTotal:") { print $2; exit; }}' /proc/meminfo )
SWAP_FREE=$( awk '{if ($1 == "SwapFree:") { print $2; exit; }}' /proc/meminfo )
MEM_SIZE=$( awk '{if ($1 == "MemTotal:") { print $2; exit; }}' /proc/meminfo )
MEM_FREE=$( awk '{if ($1 == "MemAvailable:") { print $2; exit; }}' /proc/meminfo )
if [ -z "$MEM_FREE" ]; then
	MEM_FREE=$( awk 'BEGIN{n=0}{if ($1 == "MemFree:" || $1 == "Buffers:" || $1 == "Cached:") n += $2}END{print n}' /proc/meminfo )
fi
if [ -n "$SLX_EXAM" ]; then
	# This isn't an actual runmode, but in case exam mode is active on a client you definitely want
	# to know about it, more than other runmodes actually
	SLX_RUNMODE_MODULE="exams"
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

# Warn user if tmp or swap usage is high; system might crash soon

# ..but not if flag to shut up is present
[ -e "/run/openslx/no-ram-warning" ] && exit 0

WARN=
if [ "$SWAP_FREE" -gt 0 ] && [ "$SWAP_FREE" -lt 500000 ]; then # less than 500MB swap
	WARN="$WARN
Der Arbeitsspeicher des Computers ist fast voll.
The computer is running out of RAM."
fi
if [ -n "$TMP_FREE" ] && [ "$TMP_FREE" -lt 500000 ]; then
	WARN="$WARN
Es verbleibt wenig temporärer Speicher für die Arbeitsdaten der laufenden VM.
Little temporary storage is left for the current VM."
fi

if [ -n "$WARN" ]; then
	WARN="$WARN

Bitte sichern Sie Ihre Arbeit und starten Sie den PC neu.
Please save your work and reboot this machine.

Sie können einen bwLehrpool-Admin bitten, eine größere ID-44-Partition einzurichten.
You could ask a bwLehrpool administrator to create a larger ID-44 partition."
	idle-daemon --send "warn $WARN"
fi

exit 0