we want to check if URL is down or not. But sometimes, environment is down for maintenance for 3-4 hours and we dont want to keep sending emails during that time.
I have written a shell script for url check and running it every 30 mins using cronjob and incorporated below requirement. The actual requirements are:
1)check if url is up.If it is down, send an email.
2)cronjob will execute the script again. If Step 1 sent an email, then send an email again asking if the environment is under maintenance?
3)cronjob will execute the script again.If it is still down,dont do anything.
4) keep checking the url, if it is responding dont do anything.But it goes down again follow step 1-3 .
The script works. My request is,could you please review and suggest if there is nicer way to write the script since i'm learning shell script but dont know all the available options.
#!/bin/bash
#Checking urls from urls.txt
MAddr="[email protected]"
TIME=`date +%d-%m-%Y_%H.%M.%S`
SCRIPT_LOC=/user/inf/ete4/eteabp4/eid_scripts/jsing002
for url in `awk '{print $1}' $SCRIPT_LOC/urls.txt`
do
/usr/bin/wget -t 0 --spider --no-check-certificate $url > wget.output 2>&1
HTTPCode=`(/usr/bin/wget -t 0 --spider --no-check-certificate $url) 2>&1 | grep HTTP| tail -1|cut -c 41-43`
ENV=`(grep $url $SCRIPT_LOC/urls.txt | awk '{print $2}')`
echo $HTTPCode
E1=`/bin/grep -ise 'refused' -ise 'failed' wget.output`
if [ "$E1" != "" ] || [ $HTTPCode -ge 500 ]
then
status="DOWN"
echo "Step 1"
echo "${ENV}""_DOWN"
if [ -f "${ENV}""_DOWN" ];
then
echo "step 2"
echo "Please check if $ENV in Maintanance window.The check for $url has failed twice.Next The next failure email will be sent if preceding test was SUCCESSFUL" | /bin/mail -s "Is $ENV in Maintanance Window ?" $MAddr
mv "${ENV}""_DOWN" "${ENV}""_DOWN""_2"
echo "Step 3"
elif [ -f "${ENV}""_DOWN""_2" ];
then
echo "this is elif statement"
else
echo "E1 is empty. Site is down"
echo "Site is down. $url is not accessible" | /bin/mail -s "$ENV is $status" $MAddr
touch "${ENV}""_DOWN"
fi
else
if [ $HTTPCode -eq 200 ]
then
status="UP"
echo $status
rm "${ENV}""_DOWN""_2"
fi
fi
done
Content of urls.txt:
http://mer01bmrim:30270/rim/web E2E-RIMLITE4
http://mer01csmap:18001/console ABP_WL-E2E1
http://mer02sitap:18051/console ABP_WL-E2E2
http://mer03sitap:18101/console ABP_WL_E2E3