Solved

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

Posted on 2009-07-04
8
828 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:totalimpact
  • 3
  • 3
8 Comments
 
LVL 14

Expert Comment

by:mikelfritz
ID: 24842390
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
 

Author Comment

by:totalimpact
ID: 24843806
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
 
LVL 14

Expert Comment

by:mikelfritz
ID: 24844840
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:totalimpact
ID: 24853385
How about this - does it look better:
http://pastebin.ca/1495067
0
 
LVL 14

Accepted Solution

by:
mikelfritz earned 250 total points
ID: 24859879
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
 

Author Closing Comment

by:totalimpact
ID: 31599876
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with a backup script in Linux 4 75
Changing $PATH variable - OS X 2 66
linux pull logs and export FTP 4 60
batch file problem 5 22
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now