#!/bin/sh

# /etc/update-motd.d/50-sysinfo
# 2025-08-16
# by Gernot Walzl

# Inspired by
# http://manpages.ubuntu.com/manpages/bionic/man1/landscape-sysinfo.1.html

DATE=$(date '+%a %F %T %Z')
SYSLOAD=$(awk '{ print $1 }' /proc/loadavg)
DISK_USAGE=$(df / | awk '/\// { print $5 }')
DISK_SPACE=$(df -h / | awk '/\// { print $2 }')
MEM_USAGE=$(free | awk '/Mem:/ { printf "%3.1f%%", 100*$3/$2 }')
SWAP_USAGE=$(free | awk '/Swap:/ { if ($2) printf "%3.1f%%", 100*$3/$2 }')
PROCESSES=$(ps ax --no-headers | wc -l)
USERS=$(users | wc -w)
UPTIME=$(uptime | awk '{ sub(/,/, " hours"); print $3, $4 }')
INETDEV=$(ip route list default | awk '/default/ { print $5 }')
if [ -n "$INETDEV" ]; then
  IPADDR=$(ip -4 address show dev "$INETDEV" \
    | awk '/inet/ { split($2, addr, "/"); print addr[1] }')
fi
SYS_THERM_0='/sys/class/thermal/thermal_zone0'
if [ -d "$SYS_THERM_0" ]; then
  THERM_TYPE=$(cat "$SYS_THERM_0/type")
  THERM_TEMP=$(awk '{ printf "%3.1f°C", $1/1000 }' "$SYS_THERM_0/temp")
fi

printf '\n'
printf 'System information as of %s\n' "$DATE"
printf '\n'
printf 'System load:    %-18s ' "$SYSLOAD"
printf 'Processes:      %s\n' "$PROCESSES"
printf 'Usage of /:     %-18s ' "$DISK_USAGE of $DISK_SPACE"
printf 'Users:          %s\n' "$USERS"
printf 'Memory usage:   %-18s ' "$MEM_USAGE"
printf 'System uptime:  %s\n' "$UPTIME"
printf 'Swap usage:     %-18s ' "$SWAP_USAGE"
if [ -n "$INETDEV" ]; then
  printf 'IP of %-9s %s\n' "$INETDEV:" "$IPADDR"
else
  printf '\n'
fi
if [ -n "$THERM_TEMP" ]; then
  printf 'Temp of %-7s %s\n' "$THERM_TYPE:" "$THERM_TEMP"
fi
printf '\n'
