#!/bin/bash
#
# Univention system stats
#
# SPDX-FileCopyrightText: 2010-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

date=$(date)
log="/var/log/univention/system-stats.log"

declare -a cmds=(
	"$(command -v df) -lhT"
	"$(command -v ps) auxf"
	"$(command -v top) -b -n2 -c -w512"
	"$(command -v free)"
	"$(command -v uptime)"
	"$(command -v sensors)"
	"$(command -v smbstatus)"
)

# print usage
function usage () {
	echo "usage: ${0##*/} [-h]"
	echo "This script logs some systems stats (df, free, ...) in \"$log\"."

	exit 0
}

# get options
while getopts ':h:' OPTION ; do
	case "$OPTION" in
		h)   usage ;;
		*)   usage ;;
	esac
done

[ -e "$log" ] ||
	: > "$log"
chmod 0640 "$log"
chgrp adm "$log"

# redirect output
exec 1>>"$log"
exec 2>/dev/null

echo "--- system stats for $date"
echo

for cmd in "${cmds[@]}"; do
	tool=${cmd/ */}
	if [ -x "$tool" ]; then
		echo "-> $cmd"
		$cmd
		echo
	fi
done

echo "--- end system stats for $date"

exit 0
