We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

wget schedule sending mail if response time > 5 secs

Medium Priority
812 Views
Last Modified: 2013-11-15
I'm using a script called Responser which sends email when any of the my sites are down/slow. I just wanted to check if it's sending the mail only if the sites are really down or slow(as I checked it accessing as soon I got the error mails and sites was fine). I thought of using wget as it shows the speed and response time and I want to schedule it to compare it with my responser script whether both sends the mail at the same time saying error.

I've postfix installed and I could send mails using mail command to any e-mail id from the terminal. I want the output of wget http://www.mysite.com to be mailed with the condition if response time is greater than 5 seconds. Then I can schedule it to run at every 5 mins. I hope someone could help me.

Thanks!
Comment
Watch Question

Hi!

I had a little problem finding a site with more than 1 second ping response time, but try this script:
#!/bin/sh

pcmd="ping -c 2 -W 29 -i 30 -w 59 -n yoursite.com"

ptime=$($pcmd | grep 'rtt' | awk -F'/' '{ print $6 }')
echo "Ping MAX response time:" $ptime
psec=$(echo $ptime | awk -F'.' '{ print $1 }')
echo "Response time in whole ms:" $psec

if [ "$psec" -gt 5000 ]; then echo "Over 5 sec"
	#  Put your mail commandline here
fi

Open in new window


It sends two pings (because the first pings sometimes are lost), waits 30 seconds between the pings and makes no attempts to lookup symbolic names for host addresses.

Regards, Tobias
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Hmmm, double pasted the script

#!/bin/bash

# make sure it ends within 5m (300s)
trap exit_script(1) SIGALRM
alarm(290)

recipient='admin@misite.com' #the mail address to send notifications
page='http://www.mysite.com/' #the page to measure
threshold=5 #the number of seconds above which a mail is sent

content=$(mktemp content.XXXXXX)

function 
if [[ $duration -gt 5 ]]; then
        cat $content|mail -s 'Retrieving $page took $duration' $recipient
fi
exit_script(0)
     fi
        exit $1
}

duration=$(/usr/bin/time -f '%e' 2>&1 wget -q $page -O $content)
if [[ $duration -gt 5 ]]; then
        cat $content|mail -s 'Retrieving $page took $duration' $recipient
fi
exit_script(0)

Open in new window


The script will send the data wget retreived as the content of the message
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Tobias, Hi de2Zotjes, the scripts seems great!

@Tobias: I'll give a try on my VM and get back here

@de2Zotjes: Don't know why I'm getting the following error when I try to execute the script(test1.sh is the script name)

./test1.sh: line 4: syntax error near unexpected token `('
./test1.sh: line 4: `trap exit_script(1) SIGALRM'

Author

Commented:
@Tobias: Awesome!, it works. I'm just wondering if it's possible to edit the script for multiple websites
Sure! Instead of the hardcoded sitename in line 8, use $1

ptime=$(time (wget $1 >$outfile 2>&1) 2>&1)

Open in new window

Then call the script and type the site as an argument:

./yourscript.sh http://xpro.se

Open in new window

Regards, Tobias
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
It's my sole responsibility for  assigning points to experts legally. Tobias's answer is perfectly matched and I hope he would never mind if I wont' assign full points as he would keep on gaining sharing his knowledge. Thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.