Shell script to monitor url status | Linux

Shell Scripting

While working on production system we came to know that we need to monitor the status of soa-infra url to make sure the application is up and running.

We had many soa-infra applications so it was very hectic to check and monitor all of them one by one. So we finally created a simple shell script which checks all the soa-infra urls listed in MANAGED_SERVER_URLS and tries to connect 3times if it fails it will send a mail to the id specified in the script.
It basically logsin to the soa-infra url for a status 200 code, if the status code comes clean i.e 200 it will ignore/pass it and if status comes something else then it will asume the application is down with some issues and it will send a mail to the admin with the url which failed to access.

For better usage we added this script to cronjob and ran it each 2hrs to check the status and report.

SCRIPT(CheckSoaInfra.sh):
————————————————————————————————–

#!/bin/bash
### Monitor soa-infra URL to determine if its up. It depended on HTTP status code of 200 to ensure that's is OK.
### After 3 failed check it will send a mail notification
######To be modified######
WORKDIR="/tmp/"
OUTFILE="wget_op.txt"
SERVEROUTPUT="/tmp/serveroutput.txt"

##########
###LOGIN###
WLS_USER="weblogic"
WLS_PASSWD="weblogic*123"
###URLS###
MANAGE_SERVER_URLS=(
"http://fumes1.techpaste.com:6160/soa-infra"
"http://fumes2.techpaste.com:6186/soa-infra"
)

###NOTIFICATIONS###
EMAIL="admin(at)techpaste.com "
######End to be modified######

### STATUS CODE ###
HTTP_OK="200 OK|202 Accepted"
### Binaries ###
WGET=/usr/bin/wget
MAIL="mutt"

###Change DIR###
cd $WORKDIR

###Test all URLs listed ###

for (( x=0; x < ${#MANAGE_SERVER_URLS[@]};x++))
do
SERVER_URL=${MANAGE_SERVER_URLS[$x]}
PROBLEM_FILE_NAME="/tmp/server_problem_$x.txt"
echo "$SERVER_URL"
$WGET --timeout=60 --tries=3 --no-proxy --http-user=$WLS_USER --http-password=$WLS_PASSWD $SERVER_URL --output-document=$SERVEROUTPUT 2> $OUTFILE
egrep "$HTTP_OK" $OUTFILE
if [ "$?" -ne "1" ]; then #OK found 200 OK
echo "STATUS IS OK"
if [ -f $PROBLEM_FILE_NAME ]; then #remove file if problem fixed
rm $PROBLEM_FILE_NAME
fi
else #failure
echo "STATUS IS NOT"
HTTPSTATUS=`grep "HTTP" $OUTFILE`
if [ -f $PROBLEM_FILE_NAME ]; then #existing problem
cat $OUTFILE >> $PROBLEM_FILE_NAME
mutt -s "$SERVER_URL NOT AVAILABLE $HTTPSTATUS" $EMAIL -a $OUTFILE -a $SERVEROUTPUT < /dev/null
else
mutt -s "$SERVER_URL NOT AVAILABLE $HTTPSTATUS" $EMAIL -a $OUTFILE -a $SERVEROUTPUT < /dev/null
cp $OUTFILE $PROBLEM_FILE_NAME
fi
fi
done

————————————————————————————————–

SAMPLE O/P:
————————————————————————————————–

http://fumes1.techpaste.com:6160/soa-infra
STATUS IS NOT UP
http://fumes2.techpaste.com:6186/soa-infra
HTTP request sent, awaiting response... 200 OK
STATUS IS OK

————————————————————————————————–

 

In case of any ┬ęCopyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply