summaryrefslogtreecommitdiffstats
path: root/remote/modules
diff options
context:
space:
mode:
authorSimon Rettberg2015-12-04 13:11:09 +0100
committerSimon Rettberg2015-12-04 13:11:09 +0100
commit60c39caafca9a8cea1ee59ff13c492366099802b (patch)
tree73be65cd1301244fb2fc1d92bd579b07578b7023 /remote/modules
parent[pam] Add .d directory support to pam_script_ses_open/close (diff)
downloadtm-scripts-60c39caafca9a8cea1ee59ff13c492366099802b.tar.gz
tm-scripts-60c39caafca9a8cea1ee59ff13c492366099802b.tar.xz
tm-scripts-60c39caafca9a8cea1ee59ff13c492366099802b.zip
[hardware-stats] New module
Diffstat (limited to 'remote/modules')
-rw-r--r--remote/modules/hardware-stats/data/etc/cron.d/usage_stats7
-rw-r--r--remote/modules/hardware-stats/data/etc/systemd/system/hardware-stats.service11
l---------remote/modules/hardware-stats/data/etc/systemd/system/multi-user.target.wants/hardware-stats.service1
-rwxr-xr-xremote/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update22
l---------remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_close.d/usage_stats1
-rw-r--r--remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_open.d/usage_stats8
-rwxr-xr-xremote/modules/hardware-stats/data/opt/openslx/scripts/systemd-hardware_stats153
-rw-r--r--remote/modules/hardware-stats/module.build17
-rw-r--r--remote/modules/hardware-stats/module.conf6
-rw-r--r--remote/modules/hardware-stats/module.conf.ubuntu12
10 files changed, 238 insertions, 0 deletions
diff --git a/remote/modules/hardware-stats/data/etc/cron.d/usage_stats b/remote/modules/hardware-stats/data/etc/cron.d/usage_stats
new file mode 100644
index 00000000..282ee63e
--- /dev/null
+++ b/remote/modules/hardware-stats/data/etc/cron.d/usage_stats
@@ -0,0 +1,7 @@
+# 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
+
+*/5 * * * * root /opt/openslx/scripts/cron-system_usage_update
+
diff --git a/remote/modules/hardware-stats/data/etc/systemd/system/hardware-stats.service b/remote/modules/hardware-stats/data/etc/systemd/system/hardware-stats.service
new file mode 100644
index 00000000..c28e75a5
--- /dev/null
+++ b/remote/modules/hardware-stats/data/etc/systemd/system/hardware-stats.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Gather statistics about this machine and send to boot server
+DefaultDependencies=no
+After=tmp.target multi-user.target
+Wants=tmp.target
+
+[Service]
+Type=oneshot
+ExecStart=/opt/openslx/scripts/systemd-hardware_stats
+RemainAfterExit=yes
+
diff --git a/remote/modules/hardware-stats/data/etc/systemd/system/multi-user.target.wants/hardware-stats.service b/remote/modules/hardware-stats/data/etc/systemd/system/multi-user.target.wants/hardware-stats.service
new file mode 120000
index 00000000..e463586d
--- /dev/null
+++ b/remote/modules/hardware-stats/data/etc/systemd/system/multi-user.target.wants/hardware-stats.service
@@ -0,0 +1 @@
+../hardware-stats.service \ No newline at end of file
diff --git a/remote/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update b/remote/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update
new file mode 100755
index 00000000..889409d9
--- /dev/null
+++ b/remote/modules/hardware-stats/data/opt/openslx/scripts/cron-system_usage_update
@@ -0,0 +1,22 @@
+#!/bin/ash
+
+UUID=$(cat /run/system-uuid)
+
+[ -z "$UUID" ] && exit 1
+
+USED=0
+
+for SESSION in $(loginctl | awk '{print $1}'); do
+ unset Display Remote State
+ eval $(loginctl -p Display -p Remote -p State show-session "$SESSION")
+ if [ -n "$Display" ] && [ "$Remote" = "no" ] && [ "$State" = "active" ]; then
+ USED=1
+ break;
+ fi
+done
+
+# TODO UUID
+
+curl --data-urlencode "type=~runstate" --data-urlencode "uuid=$UUID" --data-urlencode "used=$USED" \
+ "$SLX_REMOTE_LOG"
+
diff --git a/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_close.d/usage_stats b/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_close.d/usage_stats
new file mode 120000
index 00000000..129ca2f8
--- /dev/null
+++ b/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_close.d/usage_stats
@@ -0,0 +1 @@
+../pam_script_ses_open.d/usage_stats \ No newline at end of file
diff --git a/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_open.d/usage_stats b/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_open.d/usage_stats
new file mode 100644
index 00000000..7c15ee1b
--- /dev/null
+++ b/remote/modules/hardware-stats/data/opt/openslx/scripts/pam_script_ses_open.d/usage_stats
@@ -0,0 +1,8 @@
+
+(
+ sleep 3
+ /opt/openslx/scripts/cron-system_usage_update
+) &
+
+true
+
diff --git a/remote/modules/hardware-stats/data/opt/openslx/scripts/systemd-hardware_stats b/remote/modules/hardware-stats/data/opt/openslx/scripts/systemd-hardware_stats
new file mode 100755
index 00000000..97c74901
--- /dev/null
+++ b/remote/modules/hardware-stats/data/opt/openslx/scripts/systemd-hardware_stats
@@ -0,0 +1,153 @@
+#!/bin/bash
+# Use bash since ash can only do math on 32bit numbers, which is not sufficient for ID44 calculations
+
+# This script gathers information about the hardware and configuration and reports it back to the server
+
+. /opt/openslx/config
+
+if [ -z "$SLX_REMOTE_LOG" ]; then
+ echo "No remote log url given, will not report"
+ exit 1
+fi
+
+# 1) Get MAC Address used for booting
+eval $(grep -Eo BOOTIF=\\S+ /proc/cmdline)
+if [ "${#BOOTIF}" -ne "20" ]; then
+ echo "Getting MAC from /proc/cmdline failed, using 'ip a'..."
+ BOOTIF=01-$(ip a | grep -A 1 ': br0' | grep -o 'ether ..:..:..:..:..:..' | cut -d' ' -f2 | sed s/:/-/g)
+ if [ "${#BOOTIF}" -ne "20" ]; then
+ echo "FAIL FAIL FAIL"
+ BOOTIF="99-88-77-66-55-44-33"
+ fi
+fi
+MAC=${BOOTIF:3}
+
+which dmidecode || sleep 5
+
+# 2) Get machine UUID, with fallback to MAC address if it fails for some reason
+UUID=$(dmidecode -s system-uuid)
+if [ "${#UUID}" -ne "36" ]; then
+ echo "Determined UUID (${UUID}) has not expected length of 36, falling back to MAC..."
+ UUID="000000000000000-$BOOTIF"
+fi
+echo "$UUID" > /run/system-uuid
+
+# 3) Uptime in seconds
+UPTIME=$(grep -o -E '^[0-9]+' /proc/uptime)
+
+# 4) Number of real CPU cores
+CPUCORES=$(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -u | wc -l)
+# Fallback 1...
+if [ -z "$CPUCORES" ] || [ "$CPUCORES" = "0" ]; then
+ for c in $(dmidecode -t 4 | grep 'Core Enabled' | awk -F ':' '{print $2}'); do
+ CPUCORES=$(( $CPUCORES + $c ))
+ done
+fi
+# Fallback 2...
+if [ -z "$CPUCORES" ] || [ "$CPUCORES" = "0" ]; then
+ CPUCORES=$(grep -E -e '^core id\s*:' -e '^physical\s*:' /proc/cpuinfo | xargs -l2 echo | sort -u | wc -l)
+fi
+
+# 5) CPU model name
+CPUMODEL=$(grep -m1 '^model name\s*:' /proc/cpuinfo | sed 's/^model name\s*:\s*//')
+
+# 6) RAM
+RAM=$(grep '^MemTotal:' /proc/meminfo | awk '{print $2}')
+RAM=$(( $RAM / 1024 ))
+if [ -z "$RAM" ] || [ "$RAM" -lt 500 ]; then
+ # Fallback to dmidecode
+ RAM=0
+ for c in $(dmidecode -t 17 | grep -o 'Size:.*MB$' | awk '{print $2}'); do
+ RAM=$(( $RAM + $c ))
+ done
+fi
+
+# 7) 64bit virtualization support
+VT="UNSUPPORTED"
+VIRTTYPE=$(grep -m1 '^flags\s*:' /proc/cpuinfo | grep -wo -e svm -e vmx)
+[ -n "$VIRTTYPE" ] && modprobe msr
+
+if [ "$VIRTTYPE" = "vmx" ]; then # intel
+ BIT1=$(rdmsr --bitfield 0:0 0x3a 2>/dev/null || echo "fail")
+ BIT2=$(rdmsr --bitfield 2:2 0x3a 2>/dev/null || echo "fail")
+ if [ "$BIT1" = "fail" -o "$BIT2" = "fail" ]; then
+ VT="UNKNOWN"
+ elif [ "$BIT1" = "0" -o "$BIT2" = "1" ]; then
+ VT="ENABLED"
+ else
+ VT="DISABLED"
+ fi
+elif [ "$VIRTTYPE" = "svm" ]; then # amd
+ BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || echo "fail")
+ if [ "$BIT" = "fail" ]; then
+ VT="UNKNOWN"
+ elif [ "$BIT" = "0" ]; then
+ VT="ENABLED"
+ else
+ VT="DISABLED"
+ fi
+fi
+
+# 8) ID44 partition size
+ID44=0
+for c in $(fdisk -l | grep -E '[0-9]+\s+44\s+' | awk '{print $1}'); do
+ val=$(blockdev --getsize64 "$c")
+ [ -z "$val" ] && continue
+ [ "$val" -gt "$ID44" ] && ID44=$val
+done
+ID44=$(( $ID44 / 1058576 )) # we'd rather underreport
+
+# 9) check smart values
+BADSECTORS=0
+if which smartctl; then
+ FILE=$(mktemp)
+ [ -z "$FILE" ] && FILE="/tmp/smartctl.$$.$RANDOM.$RANDOM"
+ for dev in /dev/sd?; do
+ smartctl -H -A -f "brief" "$dev" > "$FILE" || continue
+ # parse
+ OVERALL=$(grep -o "test result: .*$" "$FILE" | cut -c 14-)
+ [ "x$OVERALL" = "xPASSED" ] && OVERALL=""
+ REALLOC=$(grep "^ *5 " "$FILE" | awk '{print $8}')
+ SPINRETRY_VAL=$(grep "^ *10 " "$FILE" | awk '{print $4}')
+ SPINRETRY_THR=$(grep "^ *10 " "$FILE" | awk '{print $6}')
+ [ -n "$OVERALL" ] && BADSECTORS=$(( $BADSECTORS + 100 ))
+ if [ -n "$REALLOC" ] && [ "$REALLOC" -gt "0" ]; then
+ BADSECTORS=$(( $BADSECTORS + $REALLOC ))
+ fi
+ if [ -n "$SPINRETRY_VAL" ] && [ "$SPINRETRY_VAL" -le "$SPINRETRY_THR" ]; then
+ BADSECTORS=$(( $BADSECTORS + 100 ))
+ fi
+ done
+ rm -f -- "$FILE"
+fi
+
+# n) Dump raw data to a file
+DATAFILE=$(mktemp)
+[ -z "$DATAFILE" ] && DATAFILE="/root/power-stats.$$"
+cat > "$DATAFILE" <<EOF
+############################### CPU #####################################
+Sockets: $(grep '^physical id' /proc/cpuinfo | sort -u | wc -l)
+Real cores: $CPUCORES
+Virtual cores: $(grep '^processor' /proc/cpuinfo | sort -u | wc -l)
+######################## Partition tables ###############################
+EOF
+fdisk -l >> "$DATAFILE"
+cat >> "$DATAFILE" <<EOF
+############################ PCI ID #####################################
+EOF
+lspci -n -m >> "$DATAFILE"
+cat >> "$DATAFILE" <<EOF
+########################## dmidecode ####################################
+EOF
+dmidecode >> "$DATAFILE"
+
+# Fire away
+curl --data-urlencode "type=~poweron" --data-urlencode "uuid=$UUID" --data-urlencode "macaddr=$MAC" \
+ --data-urlencode "uptime=$UPTIME" --data-urlencode "realcores=$CPUCORES" --data-urlencode "mbram=$RAM" \
+ --data-urlencode "kvmstate=$VT" --data-urlencode "cpumodel=$CPUMODEL" --data-urlencode "id44mb=$ID44" \
+ --data-urlencode "badsectors=$BADSECTORS" \
+ --data-urlencode "data@$DATAFILE" "$SLX_REMOTE_LOG"
+rm -f -- "$DATAFILE"
+
+exit 0
+
diff --git a/remote/modules/hardware-stats/module.build b/remote/modules/hardware-stats/module.build
new file mode 100644
index 00000000..f9b81669
--- /dev/null
+++ b/remote/modules/hardware-stats/module.build
@@ -0,0 +1,17 @@
+fetch_source() {
+ :
+}
+
+build() {
+ COPYLIST="list_dpkg_output"
+ rm -f -- "$COPYLIST"
+
+
+ list_packet_files >> "$COPYLIST"
+ tarcopy "$(cat "${COPYLIST}" | sort -u)" "${MODULE_BUILD_DIR}"
+}
+
+post_copy() {
+ :
+}
+
diff --git a/remote/modules/hardware-stats/module.conf b/remote/modules/hardware-stats/module.conf
new file mode 100644
index 00000000..5850b267
--- /dev/null
+++ b/remote/modules/hardware-stats/module.conf
@@ -0,0 +1,6 @@
+REQUIRED_BINARIES="
+ rdmsr
+ dmidecode
+ blockdev
+"
+
diff --git a/remote/modules/hardware-stats/module.conf.ubuntu b/remote/modules/hardware-stats/module.conf.ubuntu
new file mode 100644
index 00000000..2037d13c
--- /dev/null
+++ b/remote/modules/hardware-stats/module.conf.ubuntu
@@ -0,0 +1,12 @@
+REQUIRED_INSTALLED_PACKAGES="
+ msr-tools
+ dmidecode
+ util-linux
+"
+
+REQUIRED_CONTENT_PACKAGES="
+ msr-tools
+ dmidecode
+ util-linux
+"
+