Skip to content

Instantly share code, notes, and snippets.

@ProBackup-nl
Last active April 2, 2024 08:54
Show Gist options
  • Select an option

  • Save ProBackup-nl/6064d0fded8412308c8592487460e830 to your computer and use it in GitHub Desktop.

Select an option

Save ProBackup-nl/6064d0fded8412308c8592487460e830 to your computer and use it in GitHub Desktop.
Dynamic system status MOTD for Arch Linux
#!/bin/bash
# Preferred installation method
# nano /etc/profile.d/motd.sh;chmod +x /etc/profile.d/motd.sh
## <paste this>
#
# Alternative installation method
# nano /usr/bin/motd.sh;chmod +x /usr/bin/motd.sh
## <paste this>
# patch.sh or # patch -d/ --dry-run -p0 -i ~/patch.txt
# don't: # echo "session optional pam_exec.so stdout /usr/bin/motd.sh" >> /etc/pam.d/system-login
# that results in loads of logged "conversation failed" messages
# Color variables
W="\033[00;37m"
B="\033[01;36m"
R="\033[01;34m"
X="\033[01;37m"
A="\033[01;32m"
# Time of day
HOUR=$(date +"%H")
if [ $HOUR -lt 12 -a $HOUR -ge 0 ]
then TIME="morning"
elif [ $HOUR -lt 17 -a $HOUR -ge 12 ]
then TIME="afternoon"
else TIME="evening"
fi
# Collect info
KERNEL=`uname -r`
CPU=`/usr/bin/awk -F '[ :][ :]+' '/^model name/ { print $2; exit; }' /proc/cpuinfo`
if [ -z "$CPU" ]
then CPU=`lscpu | grep 'Model name' | cut -f 2 -d ':' | awk '{$1=$1}1'`
fi
if [ -r /sys/class/thermal/thermal_zone0/temp ]
then TEMP=`head -n 1 /sys/class/thermal/thermal_zone0/temp | xargs -I{} awk "BEGIN {printf \"%d\n\", {}/1000}"`
elif [ -r /sys/class/hwmon/hwmon1/temp1_input ]
then TEMP=`head -n 1 /sys/class/hwmon/hwmon1/temp1_input | xargs -I{} awk "BEGIN {printf \"%d\n\", {}/1000}"`
elif [ -r /sys/class/hwmon/hwmon2/temp2_input ]
then TEMP=`head -n 1 /sys/class/hwmon/hwmon1/temp2_input | xargs -I{} awk "BEGIN {printf \"%d\n\", {}/1000}"`
fi
LOAD1=`cat /proc/loadavg | awk {'print $1'}`
LOAD5=`cat /proc/loadavg | awk {'print $2'}`
LOAD15=`cat /proc/loadavg | awk {'print $3'}`
USERS=`users | wc -w`
MEMORY1=`free -t -m | grep "Mem" | awk '{print $3" MB";}'`
MEMORY2=`free -t -m | grep "Mem" | awk '{print $2" MB";}'`
#MEMPERCENT=`free | awk '/Mem/{printf("%.2f% (Used) "), $3/$2*100}'`
SWAP=`free -m | tail -n 1 | awk '{print $3}'`
PSA=`ps -Afl | wc -l`
# this script is intended to run as PAM, when running on interactive shells PAM_USER is empty
if [ -n "$PAM_USER" ]
then S_USER="$PAM_USER"
elif [ -n "$USER" ]
then S_USER="$USER"
fi
[ -n "$S_USER" ] && PSU=`ps U $S_USER h | wc -l`
SPACE=`df -h --output=pcent / | sed 1d | awk '{print $1}'`
intSpace=${SPACE%\%}
PACMAN=`pacman -Qu | wc -l`
LAST_FSU=`tac /var/log/pacman.log | grep -m1 -F "[PACMAN] starting full system upgrade" | cut -d "[" -f2 | cut -d "]" -f1`
days_since=$((($(date +%s)-$(date --date="$LAST_FSU" +%s))/86400))
echo -e "
$A. $X
$A/#\ $X _ $A _ _
$A/###\ $X __ _ _ __ ___| |__ $A| (_)_ __ _ ___ __
$A/#####\ $X / _' | '__/ __| '_ \ $A| | | '_ \| | | \ \/ /
$A/##.-.##\ $X | (_| | | | (__| | | |$A| | | | | | |_| |> <
$A/##( )##\ $X \__,_|_| \___|_| |_|$A|_|_|_| |_|\__,_/_/\_\\
$A/#.-- --.#\ $X
$A/' '\ $B
"
echo -e " $W Good $TIME $S_USER, welcome to $X${HOSTNAME}"
echo -e " $R KERNEL $W= $KERNEL "
echo -en " $R CPU $W= $CPU @ "
if [ $TEMP -ge 75 ] # at 80 the RPi CPU becomes throttled
then echo -e "$B$TEMP\xc2\xb0C $W"
else echo -e "$TEMP\xc2\xb0C "
fi
if [ $LOAD1 != "0.00" -a $LOAD5 != "0.00" -a $LOAD15 != "0.00" ]
then echo -e " $R LOAD $W= $LOAD1 1 min, $LOAD5 5 min, $LOAD15 15 min "
fi
if [ $USERS -ge 2 ]
then echo -e " $R USERS $W= Currently $USERS users logged on "
fi
echo -e " $R Memory Used $W= $MEMORY1 / $MEMORY2 "
if [ $SWAP -ge 1 ]
then echo -e " $R Swap Used $W= $SWAP MB "
fi
echo -e " $R Processes $W= You are running $PSU of $PSA processes "
echo -e " $R System Uptime $W= `uptime --pretty | cut -c 4-` "
if [ $intSpace -ge 70 ]
then echo -e " $R Disk Space Used $W= $B$SPACE $W "
elif [ $intSpace -ge 50 -a $intSpace -lt 70 ]
then echo -e " $R Disk Space Used $W= $SPACE "
fi
if [ $PACMAN -ge 1 ]
then echo -e " $R Pacman -Syu $W= $B$PACMAN$W packages can be updated "
fi
if [ $days_since -ge 8 ]
then echo -e " It has been $days_since day$([ $days_since -ne 1 ] && echo s) since your last ${B}pacman -Syu$W"
fi
systemctl --failed --quiet >/dev/null
if [ $? -ne 0 ]
then systemctl --failed
fi
echo ""
patch -d/ -p0 << 'EOF'
+++ /etc/profile 2021-02-13 00:18:13.441450462 +0100
@@ -41,6 +41,7 @@
test -r /etc/bash.bashrc
then
. /etc/bash.bashrc
+ test -r /usr/bin/motd.sh && . /usr/bin/motd.sh
fi
# Termcap is outdated, old, and crusty, kill it.
EOF
+++ /etc/profile 2021-02-13 00:18:13.441450462 +0100
@@ -41,6 +41,7 @@
test -r /etc/bash.bashrc
then
. /etc/bash.bashrc
+ test -r /usr/bin/motd.sh && . /usr/bin/motd.sh
fi
# Termcap is outdated, old, and crusty, kill it.
@ProBackup-nl

ProBackup-nl commented Feb 4, 2021

Copy link
Copy Markdown
Author

Less clutter because it hides normal values and cyan colours values that need attention. Here is an example of normal output with system update reminder:
Screenshot_19-02-21_19_17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment