July 26, 2011

Create a Linux Server Status MOTD

For those of us who manage multiple servers in multiple locations it can be beneficial to have a quick “system briefing” provided when we login to a Linux server via SSH. The Linux Message of the Day (MOTD) can be used for this.

I’ve created a simple script which will provide basic system information including the servers name, public IP, OS version, load averages, uptime etc.

To install the script:

1. Create a new text file named systemstats.sh:

# nano -w /usr/local/bin/systemstats.sh

2. Paste the following into the text file:

#!/bin/bash
#
# Server Status Script
# Version 0.1.3 m
# Updated: July 26th 2011 m

CPUTIME=$(ps -eo pcpu | awk ‘NR>1’ | awk ‘{tot=tot+$1} END {print tot}’)
CPUCORES=$(cat /proc/cpuinfo | grep -c processor)
UP=$(echo `uptime` | awk ‘{ print $3 ” ” $4 }’)
echo ”
System Status
Updated: `date`

– Server Name               = `hostname`
– Public IP                 = `dig +short myip.opendns.com @resolver1.opendns.com`
– OS Version                = `cat /etc/redhat-release`
– Load Averages             = `cat /proc/loadavg`
– System Uptime             = `echo $UP`
– Platform Data             = `uname -orpi`
– CPU Usage (average)       = `echo $CPUTIME / $CPUCORES | bc`%
– Memory free (real)        = `free -m | head -n 2 | tail -n 1 | awk {‘print $4’}` Mb
– Memory free (cache)       = `free -m | head -n 3 | tail -n 1 | awk {‘print $3’}` Mb
– Swap in use               = `free -m | tail -n 1 | awk {‘print $3’}` Mb
– Disk Space Used           = `df / | awk ‘{ a = $4 } END { print a }’`
” > /etc/motd

# End of script

3. Make the script executable:

# chmod +x /usr/local/bin/systemstats.sh

4. Edit your crontab and add a schedule to run the script every 5 minutes:

# nano -w /etc/crontab

At the bottom add:

# Status Script
*/5 * * * * root /usr/local/bin/systemstats.sh

5. Manually run the script and test there are no errors:

# /usr/local/bin/systemstats.sh

6. Login to your system using SSH and confirm that the new MOTD banner is shown.