1

Need improvement on a script which continuously tests website.

It's currently been used the following script, but it is giving a large amount of failing emails, while website is still up:

#!/bin/bash
while true; do
    date > wsdown.txt ;
    cp /dev/null pingop.txt ;
    ping -i 1 -c 1 -W 1 website.com > pingop.txt ;
    sleep 1 ;
    if grep -q "64 bytes" pingop.txt ; then
        :
    else
        mutt -s "Website Down!" bruno.bvaraujo@gmail.com < wsdown.txt ;
        sleep 10 ;
    fi
done

Thinking now or in somehow improving this script or using another way.

  • 7
    The first thing that comes to mind is that you're testing a website with ping; perhaps ICMP (ping) is occasionally being blocked or dropped? Consider testing instead against the web server port? – Jeff Schaller Sep 22 at 12:50
  • 2
    You might have better results using httping. It initiates an actual HTTP(S) connection to the server and verifies that the response is successful (2xx). vanheusden.com/httping – smitelli Sep 23 at 0:54
  • 1
    Is this the only website you'll ever monitor? It may be worth exploring a more general purpose alerting system, like Check_MK or similar. – Criggie Sep 24 at 9:16
  • What is Check_MK? An application, is it in Linux Standard Base? – Vasconcelos1914 Sep 24 at 11:57
13

You don't need ; at the end of each line, this is not C.

You don't need:

cp /dev/null pingop.txt

because the very next line in the script

ping -i 1 -c 1 -W 1 google.com > pingop.txt

will overwrite contents of pingop.txt anyway. And if we're here, you don't even need to save output of ping to the file if you're not going to send it or process it later, just do:

if ping -i 1 -c 1 -W 1 website.com >/dev/null 2>&1
then
    sleep 1
else
    mutt -s "Website Down!" bruno.bvaraujo@gmail.com < wsdown.txt
    sleep 10

To answer your question about false alarms - ping might not be the best way for testing if website is up. Some websites just do not respond to ICMP requests, for example:

$ ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.

--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

However, http://httpbin.org is up. If you're using website.com in your example you most probably access it with HTTP/HTTPS and in that case consider using curl -Is:

$ curl -Is "httpbin.org" >/dev/null  2>&1
$ echo $?
0
$ curl -Is "non-existing-domain-lalalala.com" >/dev/null  2>&1
$ echo $?
6

OP asked about speed difference between ping and curl in the comments. There is no big difference if you're testing website that responds to ping:

$ time curl -Is google.com >/dev/null  2>&1
real    0m0.068s
user    0m0.002s
sys     0m0.001s
$ time ping -i 1 -c 1 -W 1 google.com
PING google.com (216.58.215.110) 56(84) bytes of data.
64 bytes from waw02s17-in-f14.1e100.net (216.58.215.110): icmp_seq=1 ttl=54 time=8.06 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.068/8.068/8.068/0.000 ms
real    0m0.061s
user    0m0.000s
sys     0m0.000s

But when testing website that does not respond to ping then curl is not only more reliable but also faster than ping with -W that you use now:

$ time ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.

--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms


real    0m1.020s
user    0m0.000s
sys     0m0.000s
$ time curl -Is httpbin.org  >/dev/null  2>&1

real    0m0.256s
user    0m0.003s
sys     0m0.000s
  • 1
    I would get log from curl/wget transfert and parse for 20x http's answer versus 404/50x. Latter answer can be send as warning mail. – Archemar Sep 22 at 12:59
  • 1
    Good idea. I wonder however how should HTTP/1.1 301 Moved Permanently be treated? – Arkadiusz Drabczyk Sep 22 at 13:01
  • 1
    For example curl -Is shellhacks.com – Arkadiusz Drabczyk Sep 22 at 13:02
  • 1
    do we speak about Schrodinger sites ? neither up, nor down ? you can't tell unless you log to server. – Archemar Sep 22 at 13:05
  • 1
    https://www.shellhacks.com is up. – Arkadiusz Drabczyk Sep 22 at 13:06
3

there is not much to improve for a ping version

#!/bin/bash
while true; do
    date > wsdown.txt  
    ping -i 1 -c 1 -W 1 website.com > pingop.txt # '>' will overwrite file
    sleep 1 ;
    if ! grep -q "64 bytes" pingop.txt ; then ## negate test
        mutt -s "Website Down!" bruno.bvaraujo@gmail.com < wsdown.txt 
        sleep 10 
    fi
done

note that

  • you din't need ; to "close" command
  • in case website.com goes down, you are likely to get a lot of spam
  • ping(icmp) and http:// are two different protocols.
0

You could increase the ping command timing, it would increase the time response, but it would maybe also reduce losses, as follows:

From ping -i 1 -c 1 -W 1 website.com > pingop.txt ;

To ping -i 2 -c 1 -W 4 website.com > pingop.txt ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.