basic server check script - help

Hello Fellow bash enthusiasts...

I am stuck on this local script and could use some fresh eyes on this code.

#!/bin/bash
MAIL_ME="jj@uberdork.com"
MAIL_SUBJECT="Norveld_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date)
echo $(ServerTZ=PST8PDT date) - Web Server Check = $(curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-) >> /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test
if [ $? -eq 0 ] ; then
 exit 0
 else
 if [ $? -eq 1 ] ; then
 echo "Norveld_Server_Check - FAILED" on $SERVER_DATE | mail $MAIL_ME -s $MAIL_SUBJECT
 exit 1
 fi
 fi
done
#EOF

PING IS NOT AN OPTION'
Server when it's borked will ping,
but be unresponsive, so I ask the Webserver what software it's using with

curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-

which returns "Microsoft-IIS/7.5" and works even if the server is unresponsive through usual means.

This runs under my cron every minute.
There's only 1 problem, server has been up/down for awhile and I NEVER get an email.

telnet xxx.xxx.xxx.xxxx 80

is an option, but I have zero experience with how to handle the response from the server. or

wget xxx.xxx.xxx.xxx/index.html

Whatever method I use to check for responsiveness, I have to get an email as the desired email address.

local: GNU bash, version 4.1.10(1)-release (i586-suse-linux-gnu)
Remote: Windows Server 2008 R2 (64bit) with IIS.

Thanks.

Happy New Year!

A very happy new year to you too!! :b:

Well, it's obvious why you never got E-mail 'cause you are actually checking the exit status of echo command which is giving 0 and the first if block gets executed.

Plus I do not understand why you used extra done and fi. Anyways, below is the code I wouls used for the same purpose (I have not tested it though ;)). See if this works for you:

#!/bin/bash
MAIL_ME="jj@uberdork.com"
MAIL_SUBJECT="Norveld_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date)

# store this in a variable
temp=$(curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-)
echo $(ServerTZ=PST8PDT date) - Web Server Check = $temp >> /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test

if [ $temp='Microsoft-IIS/7.5' ] ; then
 exit 0
else
 echo "Norveld_Server_Check - FAILED" on $SERVER_DATE | mail $MAIL_ME -s $MAIL_SUBJECT
 exit 1
fi
1 Like

admin_xor;

"echo always returns zero" is the rule I was looking for, when I couldn't even think of it!
I have implemented your suggested code, but /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test had an apache entry. A typo probably prevented the email from being sent.

The extra "fi" was probably a bad hack at some ! logic snippet I had since removed... I dunno, I tend to "hack now, debug during-run" (complete with "set -x" statement.)

Since this post, I reviewed Chapter 6 of "Linux Shell Script Programming" by Todd Meadors - Copyright 2003) where it illustrated a clue...

dir1=`pwd`

on Page 215

I will change some detail in your code and watch the inbox!

Thanks for the code and have a Prosperous New Year.

Edit:
Changed temp=$(curl -Is http://xxx.xxx.xx.xx | \grep -E '^Server' | cut -c9-) for 7 passes and no email. (I used the wrong IP)...

Thu Dec 29 14:32:01 EST 2011 - Web Server Check = Apache
Thu Dec 29 14:33:02 EST 2011 - Web Server Check = Apache
Thu Dec 29 14:34:01 EST 2011 - Web Server Check = Apache
Thu Dec 29 14:35:01 EST 2011 - Web Server Check = Apache
Thu Dec 29 14:36:01 EST 2011 - Web Server Check = Apache

subscribed with interest...

The correction idea in post #2 contains a basic scripting error:

There are no spaces either side of the equals sign. This is not a syntax error BUT the "if" will always return "true" regardless of the value of $temp. Also get into he habit of putting string variables in quotes.
Try:

if [ "$temp" = 'Microsoft-IIS/7.5' ] ; then

However, this will still only work if the value of $temp is the exact matching string. The sample posted does not give this fact away.

2 Likes

Well I tested the script neither before nor after the modifications!! :wink: It's just the long time C programming and shell scripting which drove me to the conclusion!!

I haven't really tested what apache (or probably IIS since you are using Windows 2008 R2) web server returning to the curl query. I just read your post and and modified the your script just enough to give you an idea!!! :smiley:

Enjoy the new year!!

Thanks methyl and admin_xor:

As usual "YMMV"... :slight_smile:

anyway, does this make any difference?

+ temp=$'Microsoft-IIS/7.5\r'

the \r is what stands out...

JJ

Edit:

I also tried

...
if [ "$temp" = "Microsoft-IIS/7.5\r" ] ; then
..

---------- Post updated at 05:27 PM ---------- Previous update was at 04:33 PM ----------

I get variations on this using "set -x"...

echo Thu Dec 29 12:52:01 PST 2011 - Web Server Check = $'Microsoft-IIS/7.5\r'
+ [[ Microsoft-IIS/7.5^M = \\\M\\\i\\\c\\\r\\\o\\\s\\\o\\\f\\\t\\\-\\\I\\\I\\\S\\\/\\\7\\\.\\\5\\\\\r ]]

but I am having limited success with this code ...

if [ $temp = "Microsoft-IIS/7.5\r" ] ; then
 echo "Norveld_Server_Check - FAILED" on $LOCAL_DATE | mail $MAIL_ME -s $MAIL_SUBJECT
 exit 0
fi

The if logic is "backwards"?
This doesn't fire an email when run manually, but does via cron.

I might have to export some stuff. :slight_smile:

Some days, I'm sure I don't know diddly about my chosen occupation.

Thanks.

Edit:
It's completely NORMAL for cron to send email so I suppressed those with

> /dev/null 2>&1

Thanks for "listening".
Sometimes we just have to reach out and ask.

---------- Post updated at 10:15 PM ---------- Previous update was at 05:27 PM ----------

another method, maybe?

#!/bin/bash
# http://bashscripts.org/forum/viewtopic.php?f=16&t=1522
# set -x
MAIL_ME="jj"
MAIL_SUBJECT="Norveld_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date +%c)
LOCAL_DATE=$(date +%c)

# store this in a variable
IIS=$(curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-21)
echo $(TZ=PST8PDT date) - Web Server Check = "$IIS" # >> /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test

if [ "$IIS" = "Microsoft-IIS" ] ; then
exit 0
else
 echo "Norveld_Server_Check - FAILED" on "$LOCAL_DATE" | mail "$MAIL_ME" -s "$MAIL_SUBJECT"
 exit 1
fi

the rubber-hits-the-road at

curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-21

which leaves off the "/7.5" of the string "Microsoft-IIS/7.5"
NOW I can compare "$IIS" to "Microsoft-IIS"...?

fingers are crossed.

Thanks.

---------- Post updated 12-30-11 at 01:46 PM ---------- Previous update was 12-29-11 at 10:15 PM ----------

I tested my script manually 103 times yesterday and finally settled on the current script.

HNY - 1d

Hmm. The Microsoft output contains a carriage-return character which messes up your string comparison (carriage-return = ctrl/M = ^M = \r in some programs but not I'm afraid in raw Shell). The "\r" is just a display but not the actual character.

To get rid of the carriage-return, insert this "tr" in your pipeline at the appropriate place (after "curl"). The "tr" program does recognise "\r" as meaning carriage-return.

          | tr -d '\r' |

Yes the space either side of the equals sign does make an important difference. As does the double quotes round the string variable. The syntax has to be exact for the "if" statement to work properly.

Thanks methyl:

the "cut -c9-21" seems to do the trick for now. :slight_smile:

I also suspect that the /7 may have b0rked up the output.