SSH login show load avg, free memory, free disk

For Linux computers, you can display a cool login message using a “message of the day” (MOTD) type framework.

This script can be put in ~/.bash_profile or on Ubuntu systems as a file in the /etc/update-motd.d/ directory.

The login message you’ll get will include:

Uptime.............: 25 days, 01h47m 
Memory.............: 822MB (Free) / 1021MB (Total) 
Load Averages......: 0.03, 0.08, 0.10 (1, 5, 15 min)
Running Processes..: 123 
Free Disk Space....: 8112M on /dev/sda1

setup

Paste inside ~/.bash_profile. Change the /dev/sda1 to be the partition name you’re interested in.

#!/bin/bash

let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm" "$days" "$hours" "$mins" `

# get the load averages
read one five fifteen rest < /proc/loadavg

#compute Available Free Memory
MemFreeMB=$(cat /proc/meminfo | grep MemFree | awk {'print int($2/1000)'})
MemBuffMB=$(cat /proc/meminfo | grep Buffers | awk {'print int($2/1000)'})
MemCachMB=$(cat /proc/meminfo | grep ^Cached | awk {'print int($2/1000)'})
AvailMemMB=$(($MemFreeMB + $MemBuffMB + $MemCachMB))

echo "
Uptime.............: ${UPTIME}
Memory.............: "$AvailMemMB"MB (Available) / `cat /proc/meminfo | grep MemTotal | awk {'print int($2/1000)'}`MB (Total)
Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
Running Processes..: `ps ax | wc -l | tr -d " "`
Free Disk Space....: `df -P -BM | grep -E -m 1 '^/dev/sda1 ' | awk '{print $4 }' | awk -F '.' '{ print $1 }'` on /dev/sda1
"

Notes

reference

Tags:

Categories:

Updated:

Leave a Comment