LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-15-2016, 11:19 AM   #1
kkrrss
Member
 
Registered: Jun 2016
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 06-15-2016, 01:21 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by kkrrss View Post
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.
 
Old 06-15-2016, 01:39 PM   #3
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
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
 
1 members found this post helpful.
Old 06-15-2016, 02:35 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
And what about false-positives?

I highly suggest an outside "ping service".
 
Old 06-16-2016, 01:35 AM   #5
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thanks all.. I will check & update the status soon.
 
Old 06-16-2016, 02:15 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,461

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
Quote:
Originally Posted by Habitual View Post
And what about false-positives?

I highly suggest an outside "ping service".
I've had good experiences with www.nodeping.com
 
Old 06-17-2016, 10:22 AM   #7
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by kkrrss; 06-17-2016 at 10:25 AM.
 
Old 06-17-2016, 10:28 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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.

Last edited by Habitual; 06-17-2016 at 10:33 AM.
 
Old 06-17-2016, 10:35 AM   #9
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 06-17-2016, 10:45 AM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Please use code tags around script and other output. It preserves formatting.
[code]Script output...[/code]
 
Old 06-17-2016, 10:53 AM   #11
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by kkrrss; 06-17-2016 at 10:55 AM.
 
Old 06-17-2016, 11:10 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by kkrrss View Post
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
 
Old 06-17-2016, 11:21 AM   #13
kkrrss
Member
 
Registered: Jun 2016
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 06-17-2016, 11:25 AM   #14
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
schneidz:
Please write my script.
 
Old 06-17-2016, 01:38 PM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by kkrrss View Post
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: -
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make this script to a service. deepcore Linux - Software 1 05-18-2014 10:14 AM
Web service test using Perl/Shell jack.sully Programming 9 03-13-2014 07:59 AM
Script for qmail service monitor. s3cr3t Linux - Newbie 3 06-05-2009 10:16 PM
Simple script to monitor POP3, SMTP service s3cr3t Solaris / OpenSolaris 1 05-29-2009 12:33 PM
Configuring Shell Script as service/daemon nkanthikiran Red Hat 2 11-07-2006 05:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration