[SOLVED] Make shell script to monitor a web service.
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I'm writing shell script to monitor one of my web service and script is running by cronjob every one hour. here is it looks like.
Code:
#!/bin/bash
WEBSERVER=http://www.mywebsite.com
SEND_ID="frommail@example.com"
TO_ID="tomail@example.com"
curl -s --head $WEBSERVER | grep "200 OK" > /dev/null
if [ $? -ne 0 ];then
echo " web server is down! $(date)" | mail -s "subject-Web server status changed!" -r $SEND_ID $TO_ID
fi
Can anyone let me know how can I trigger an email once the service is up and it should be contained the total downtime of the web service.
Eg : http://www.mywebsite.com is back at Wed Jun 15 01:40:30 UTC 2016. Total downtime was 19 minutes and 42 seconds b Since i'm new for the shell scripting and appreciate your support.
Please put your code into CODE tags when posting...makes it much easier to read.
You don't say how many websites you want to check, or how often, but if it's just one, and this is on your internal network, I'd just store the server name/date into a variable. Then, loop through the check routine again, sleeping each time...once the server comes back up, you then have a beginning of the outage, as well as the time it came back up...simple math will give you the duration.
So:
Instead of just sending the email as you are above, store the $WEBSERVER and $(date) (you can still send it, if you want, of course)
Sleep for some period (up to you), and loop back to start..DON'T Overwrite the variables when doing this!
Once you get the 200/ok, then grab a second $(date)
Compare the two dates to get outage duration
THEN send the email.
This:
Code:
echo $(( ($(date --date="<start of outage variable here>" +%s) - $(date --date="<end of outage variable here>" +%s) )/(60*60*24) ))
Checking once an hour won't be able to provide an accurate determination of the time the service was down. If you check at 12:00 and it's up, but then if it goes down at 12:01 or 12:59 your next iteration won't be able to tell. At best you would know +/- 1 hour how long it was down. You could get better information by checking more frequently.
As far as notifications, I've used lock or status files to do something like this in the past.
Generally,
Code:
if [your down condition is true]
then
if [lock-file does not exist]
then
send an email that service is down
touch lock-file
else
# Don't send an email, or do something else
exit
fi
else
if [lock-file does exist]
then
send an email that service is back up (you can calculate down-time by subtracting lock-file modified datetime from current datetime)
rm -f lock-file
fi
Say what?
Your script doesn't include any redirection to a "file".
I think you want "auditing" of the success and failures of this busted script,
on an hour-by-hour basis?
Use a service, save your sanity.
Uptimerobot.com and others are free and spam free.
Features may be limited but for basic/free ping, they'll do.
echo "Web Service is down at $(date)" | mail -s "Web service is down!" -r $SEND_ID $TO_ID
echo "Web server is down and email has sent..."
touch $MEMFILE
echo "$MEMFILE created at $(date)..."
exit
else
curl -s --head $WEBSERVER | grep "200 OK" > /dev/null
if [ $? -ne 0 ] && [ -f $MEMFILE ];then
echo "Web service is still down.. hold email notfications untill the service is up...."
exit
if [ $? -eq 0 ] && [ -e $MEMFILE ];then
echo "web server is up"
echo "Web Service is back up at $(date)" | mail -s "Web service is up!" -r $SEND_ID $TO_ID
echo "Email has sent.."
rm -rf $MEMFILE
echo "$MEMFILE deleted at $(date)..."
else
echo "web service is up & running.. nothing to perform.."
This is how it is.. I want subtract the $MEMFILE created date/time from the current date / time
Code:
#!/bin/bash
WEBSERVER=http://www.mydomain.com
SEND_ID="sender@example.com"
TO_ID="mymail@example.com"
MEMFILE=lock-file
curl -s --head $WEBSERVER | grep "200 OK" > /dev/null
if [ $? -ne 0 ] && [ ! -f $MEMFILE ];then
echo "Web Service is down at $(date)" | mail -s "Web service is down!" -r $SEND_ID $TO_ID
echo "Web server is down and email has sent..."
touch $MEMFILE
echo "$MEMFILE created at $(date)..."
exit
else
curl -s --head $WEBSERVER | grep "200 OK" > /dev/null
if [ $? -ne 0 ] && [ -f $MEMFILE ];then
echo "Web service is still down.. hold email notfications untill the service is up...."
exit
else
curl -s --head $WEBSERVER | grep "200 OK" > /dev/null
if [ $? -eq 0 ] && [ -e $MEMFILE ];then
echo "web server is up"
#Calculating the Total down time..
echo "Web Service is back up at $(date)" | mail -s "Web service is up!" -r $SEND_ID $TO_ID
echo "Email has sent.."
rm -rf $MEMFILE
echo "$MEMFILE deleted at $(date)..."
else
echo "web service is up & running.. nothing to perform.."
fi
fi
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.