Shell Script to show the status of various linux daemons, interfaces and disk space in motd.

I am wondering if there are any pre-built shell scripts that show the status of common services, interfaces and disk space? I kind of have a start, but I was hoping there would be something freely available with nice formatting and all that.

I found this, and want to setup something similar on my Centos 5.3 server:
https://help.ubuntu.com/9.04/serverguide/C/update-motd.html

I downloaded the source, and I think I have it all ported to my platform, the only thing is my scripting skills are still very basic, i am choking on some areas. I wish it was better formatted, because theres still a couple of other items I  might add, and I am not sure if it will fit on one screen.

Specific status items I am looking for:
iptables
mysql
sendmail
webmin
samba
apache
sshd
df -h (show only the percentage free on /)
eth0 (UP? or DOWN?)
eht1 (UP? or DOWN?)
wlan0 (UP? or DOWN?)

Below is my rough draft
#!/bin/sh
#####Script to show status of daemons, interfaces and free disk space:
#########Apache status:
if [ -e /var/run/httpd.pid ]
then 
	echo "Webserver      	Online" > /etc/test.motd
else
	echo "Webserver      	Offline" >> /etc/test.motd
fi
#########MySQL status:
if [ -e /var/run/mysqld/mysqld.pid ]
then 
	echo "MySQL Database 	Online" >> /etc/test.motd
else
	echo "MySQL Database 	Offline" >> /etc/test.motd
fi
#########Sendmail status:
if [ -e /var/run/sendmail.pid ]
then 
	echo "Mailserver 	Online" >> /etc/test.motd
else
	echo "Mailserver 	Offline" >> /etc/test.motd
fi
#########Fail2ban status:
if [ -e /var/run/fail2ban/fail2ban.pid ]
then 
	echo "Fail2ban 	Online" >> /etc/test.motd
else
	echo "Fail2ban	Offline" >> /etc/test.motd
fi
#########SSHd status:
SSHSTAT=`/etc/init.d/sshd status |grep 'is running'`
if [ "$SSHSTAT" = "is running" ]
then 
	echo "SSH		 	Online" >> /etc/test.motd
else
	echo "SSH			Offline" >> /etc/test.motd
fi
#########Webmin status:
WEBMINSTAT=`/etc/init.d/webmin status |grep 'is running'`
if [ "$WEBMINSTAT" = "is running" ]
then 
	echo "Webmin		 	Online" >> /etc/test.motd
else
	echo "Webmin			Offline" >> /etc/test.motd
fi
##########Zaptel Status:
if [ -e /proc/zaptel/ ]
then 
	echo "Zaptel Trunks 	Online" >> /etc/test.motd
else
	echo "Zaptel Trunks Offline" >> /etc/test.motd
fi
#########Iptables status: 
FIREWALLSTAT=`/etc/init.d/iptables status`
if [ "$FIREWALLSTAT" = "Firewall is stopped." ]
then 
	echo "Firewall 	Offline" >> /etc/test.motd
else
	echo "Firewall 	Online" >> /etc/test.motd
fi
#########Eth0 status:
ETH0STAT=`ifconfig eth0 |grep UP | awk '{print $1}'`
if [ "$ETH0STAT" = UP ]
then 
	echo "LAN ETH0 	Online" >> /etc/test.motd
else
	echo "LAN ETH0  	Offline" >> /etc/test.motd
fi
#########Eth1 status:
ETH1STAT="ifconfig eth1 |grep UP | awk '{print $1}'"
if [ "$ETH1STAT" = UP ]
then 
	echo "LAN ETH1 	Online" >> /etc/test.motd
else
	echo "LAN ETH1  	Offline" >> /etc/test.motd
fi
#########Wlan0 status:
WLAN0STAT="ifconfig wlan0 |grep UP | awk '{print $1}'"
if [ "$WLAN0STAT" = UP ]
then 
	echo "Wireless 	Online" >> /etc/test.motd
else
	echo "Wireless  	Offline" >> /etc/test.motd
fi
#########Show percent used on root file system:
EXCLUDE_PARTITIONS="/auto/ripper|/dev/hda1|/dev/sda1|none|/dev/md0|/dev/md1|tmpfs|cdrom|Filesystem"
ROOTUSED=`df -H | grep -vE "^$EXCLUDE_PARTITIONS" | awk '{print $5 " " }' | sed 's/%//'`
if [ $ROOTUSED -gt 80 ]
then 
	echo "DANGER Disk space used: [31;47$ROOTUSED%" >> /etc/test.motd
else 
	echo "Disk space used: $ROOTUSED%" >> /etc/test.motd
fi
#########How many users are logged on:
echo "`who | wc -l` users are logged in" >> /etc/test.motd

Open in new window

totalimpactAsked:
Who is Participating?
 
mikelfritzConnect With a Mentor Commented:
A little.  The ansi escape codes are getting goofed up...

So it looks like at this point you are using 13 lines of the page.  One easy way to scrunch it up would be to make all of your echo lines the same length and include trailing spaces and a seperator to seperate columns and then in every other section use "echo -n" to supress the newline.  So the echo looks like:
 echo -n "Webserver Online     -     "
 echo "MySQL Database Online"
 echo -n "Mailserver  Offline     -     "
 echo "Fail2ban               Offline"

That should give you something like:
Webserver Online     -     MySQL Database Online
Mailserver  Offline     -     Fail2ban               Offline

It might take a little tweaking to get it to look right.  The ansi escape codes could be put in after you get the layout right.

You could get a little more fancy by maybe making more columns for certain sections where the echo's are small enough.

0
 
mikelfritzCommented:
Your rough draft has a bunch of control  codes globbed in with it so it's difficult to read.  What happens if you run it and that cat /etc/test.motd

Does it take more than one screen and does it show what you want?

I don't see the update-motd package for centos but it would be easy to just have yours run through cron.
0
 
totalimpactAuthor Commented:
At the moment it does not fill more than 1 screen, but there are a couple of more items I wish to add before its done, at that point it will fill more than one screen, and I would like to double up the output into 2 columns - but I am not sure on how to build columns.

update-motd is so far only built for debian based systems - so yes, most likely I will just use cron, or .bashrc to load it.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
mikelfritzCommented:
Can you repost your script.  Maybe just attach it so the paste does'nt mess it up.  I'm sure we could whip up an awk script to clean up the output for display.

0
 
totalimpactAuthor Commented:
How about this - does it look better:
http://pastebin.ca/1495067
0
 
totalimpactAuthor Commented:
The final answer was quite satisfactory, but I was still looking for someone to provide part of my original request for a ready to go status script.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.