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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

totalimpactAuthor Commented:
How about this - does it look better:
http://pastebin.ca/1495067
0
mikelfritzCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.