LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Make shell script to monitor a web service. (https://www.linuxquestions.org/questions/linux-server-73/make-shell-script-to-monitor-a-web-service-4175582342/)

kkrrss 06-15-2016 11:19 AM

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.

TB0ne 06-15-2016 01:21 PM

Quote:

Originally Posted by kkrrss (Post 5561284)
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) ))
...will give you the duration between two dates.

thesnow 06-15-2016 01:39 PM

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


Habitual 06-15-2016 02:35 PM

And what about false-positives?

I highly suggest an outside "ping service".

kkrrss 06-16-2016 01:35 AM

Thanks all.. I will check & update the status soon.

TenTenths 06-16-2016 02:15 AM

Quote:

Originally Posted by Habitual (Post 5561428)
And what about false-positives?

I highly suggest an outside "ping service".

I've had good experiences with www.nodeping.com

kkrrss 06-17-2016 10:22 AM

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.

Habitual 06-17-2016 10:28 AM

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.

kkrrss 06-17-2016 10:35 AM

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

Habitual 06-17-2016 10:45 AM

Please use code tags around script and other output. It preserves formatting.
[code]Script output...[/code]

kkrrss 06-17-2016 10:53 AM

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


schneidz 06-17-2016 11:10 AM

Quote:

Originally Posted by kkrrss (Post 5562507)
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.

convert the dates to seconds then re-convert the output:
Code:

[schneidz@hyper tmp]$ date +%s -d 'Jun 17 02:00'
1466143200
[schneidz@hyper tmp]$ date -d '@1466143200'
Fri Jun 17 02:00:00 EDT 2016


kkrrss 06-17-2016 11:21 AM

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.

Habitual 06-17-2016 11:25 AM

schneidz:
Please write my script.

schneidz 06-17-2016 01:38 PM

Quote:

Originally Posted by kkrrss (Post 5562541)
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.

Code:

[schneidz@hyper tmp]$ stat fibo.c
  File: ‘fibo.c’
  Size: 266              Blocks: 8          IO Block: 4096  regular file
Device: 802h/2050d        Inode: 2361767    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/schneidz)  Gid: ( 1000/schneidz)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-17 10:45:46.631982635 -0400
Modify: 2016-06-17 10:44:45.687330019 -0400
Change: 2016-06-17 10:44:45.687330019 -0400
 Birth: -



All times are GMT -5. The time now is 08:42 PM.