Solved

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

Posted on 2009-07-04
8
825 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

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

Accepted Solution

by:
mikelfritz earned 250 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

15 Experts available now in Live!

Get 1:1 Help Now