Solved

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

Posted on 2009-07-04
8
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

630 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