Thursday, June 06, 2013

motd

Ubuntu Dynamic MOTD

I'm just finishing off moving all my web sites and related things from one VPS host to another. The new host had customized the message of the day on the Ubuntu 12.04 LTS image which I had installed. It had helpful links to support and what not, things that are only actually helpful the first time I logged in.

As I went through the process of removing all this I had to learn about the update-motd system, since I quickly found that the solution wasn't a simple:

[sourcecode language="bash"]$ sudo echo "" > /etc/motd[/sourcecode]

So I cooked up a bash script to spit out some information about the system every time I log into the server. The output looks like this:

motd

Create or edit update-motd's 00-header file.

[sourcecode language="bash"]$ sudo vi /etc/update-motd.d/00-header[/sourcecode]

And here's the source of the bash script:

[sourcecode language="bash"]#!/bin/sh

[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
# Fall back to using the very slow lsb_release utility
DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi

UPTIME=`uptime | awk '{if ($4 == "day," || $4 == "days,") print $3, $4, $5; else print $3}' | awk -F: '{print $1, "hrs", $2, "mins"}' | sed 's/,//g'`

LOADAVG=`uptime | awk '{if ($4 == "day," || $4 == "days,") print $10, $11, $12; else print $8, $9, $10}'`

PROCCOUNT=`ps -l | wc -l`
PROCCOUNT=`expr $PROCCOUNT - 4`
IP=$(/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')

echo -e "
==============: System Info :===================
Hostname: `hostname`
Address: $IP
Distro: $DISTRIB_DESCRIPTION
Kernel: `uname -r`
Uptime: $UPTIME
Load Avgs: $LOADAVG
Processes: $PROCCOUNT
==============: Memory Info :===================
Total: `cat /proc/meminfo | grep MemTotal | awk {'print $2'}` kB
Free: `cat /proc/meminfo | grep MemFree | awk {'print $2'}` kB
Lowest: `cat /proc/meminfo | grep LowFree | awk {'print $2'}` kB
===============: Disk Info :===================="
df -h[/sourcecode]