summaryrefslogtreecommitdiffstats
path: root/core/modules/hardware-stats/data/opt/openslx/scripts/systemd-hardware_report
blob: 464b9742199a1d266be94c43169719f35f026225 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/ash

. /opt/openslx/config

disable_remote_logging() {
	echo "Server doesn't seem to support hardware/usage stats - disabling logging"
	rm -f -- "/etc/cron.d/usage_stats"
}

# sends the hardware information of this machine generated by
# systemd-hardware_stats_gather in /run/hwreport and /run/hwinfo
report_hardware_info() {
	local uptime hwreport hwinfo uuid model subnet ret runmode jsonfile
	if [ -z "$SLX_REMOTE_LOG" ]; then
		disable_remote_logging
		return 1
	fi
	hwreport="/run/hwreport"
	if [ ! -s "$hwreport" ]; then
		echo "Missing hwreport file: $hwreport"
		# TODO send data without?
		return 1
	fi

	# Read generated data and current uptime and send it
	hwinfo="/run/hwinfo"
	if [ ! -s "$hwinfo" ]; then
		echo "Missing hwinfo file: $hwinfo"
		return 1
	fi
	. "$hwinfo"

	uuid=$(cat /etc/system-uuid)
	if [ -z "$uuid" ] || [ "${#uuid}" -ne "36" ]; then
		echo "No/malformed UUID, aborting" >&2
		exit 1
	fi
	runmode="$SLX_RUNMODE_MODULE"
	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
		runmode="exams"
	fi

	# Combine manufacturer and model name (for displaying purposes)
	model="$HW_MODEL"
	if [ "$HW_MANUF" != "Unknown" ]; then
	model="$model ($HW_MANUF)"
	fi
	# Get IP/subnet size
	local primary="${SLX_BRIDGE:-br0}"
	subnet="$( ip -o -f inet addr show "$primary" | awk '/scope global/ {print $4}' )"
	# Finally, new json-based reporting
	jsonfile="$( mktemp )"
	if ! python3 /opt/openslx/system-check/collect_hw_info_json.py -p > "$jsonfile"; then
		echo -n "" > "$jsonfile"
	fi
	# Bail out if a shutdown was triggered and submitted in the meantime
	[ -e /run/openslx/shutdown.mutex ] && exit 0
	# just assume the uuid/mac dumped are valid here (its checked often enough :))

	# got everything, get the last infos
	uptime=$(grep -oE '^[0-9]+' /proc/uptime)
	echo -n "Submitting to '$SLX_REMOTE_LOG' ... "
	curl --retry 4 --retry-connrefused --connect-timeout 3 --max-time 9 --retry-max-time 18 \
		--data-urlencode "type=~poweron" --data-urlencode "uuid=$uuid" --data-urlencode "macaddr=$HW_MAC" \
		--data-urlencode "uptime=$uptime" --data-urlencode "realcores=$HW_CORES" \
		--data-urlencode "vcores=$HW_THREADS" \
		--data-urlencode "sockets=$HW_SOCKETS" --data-urlencode "mbram=$HW_MBRAM" \
		--data-urlencode "kvmstate=$HW_KVM" --data-urlencode "cpumodel=$HW_CPUMODEL" \
		--data-urlencode "id44mb=$HW_ID44" \
		--data-urlencode "badsectors=$HW_BADSECTORS" --data-urlencode "systemmodel=$model" \
		--data-urlencode "subnet=$subnet" \
		--data-urlencode "runmode=$runmode" --data-urlencode "data@$hwreport" --data-urlencode "json@$jsonfile" \
		"$SLX_REMOTE_LOG" | grep -q "RESULT=0"
	local ret=$?
	if [ "$ret" -ne 0 ]; then
		echo "failed."
		disable_remote_logging
		return 1
	fi
	echo "succeeded."
	rm -f -- "$hwreport" "$jsonfile"
	START=$(( $RANDOM % 5 ))
	DELAY=$(( $RANDOM % 20 ))
	# Do not move/rename this file, we check its existence in other scripts
	cat > "/etc/cron.d/usage_stats" <<-EOF
		# Update usage statistics on server
		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 --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 --full
	return 0
}

report_hardware_info