Make shell script to monitor a web service.
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. #!/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 Since i'm new for the shell scripting and appreciate your support. |
Quote:
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:
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] |
And what about false-positives?
I highly suggest an outside "ping service". |
Thanks all.. I will check & update the status soon.
|
Quote:
|
Hi Again,
can anyone let me know how I can subtract the file created time from current time and get the output result in bash script. Eg : I created file name "mytestfile.txt" at Jun 17 02:00 assume current time is 03:00, I want to take out put of 03:00 - 02:00 = 01.00 hour.. in HH:MM:SS format. Please help.. Thank You. |
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. |
Hi Habitul, Here is my updated one. pls advice..
#!/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" 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 |
Please use code tags around script and other output. It preserves formatting.
[code]Script output...[/code] |
This is how it is.. I want subtract the $MEMFILE created date/time from the current date / time
Code:
|
Quote:
Code:
[schneidz@hyper tmp]$ date +%s -d 'Jun 17 02:00' |
Hi schneidz,
Kindly explain please.. I want to take the created date & time of $MEMFILE and subtract it from current date & time. and get the total down time. Please explain How can include it in my bash script. |
schneidz:
Please write my script. |
Quote:
Code:
[schneidz@hyper tmp]$ stat fibo.c |
All times are GMT -5. The time now is 04:24 PM. |