-1

I use Ubuntu 16.04 with Nginx and a few WordPress sites. Sometimes I don't visit a site for a long time (>=1 month) and it might be that the site is down.

I'm looking for a small utility that will email my Gmail account, if one of my Nginx-WordPress sites is down (without mentioning a reason).

Approaches considered so far

1. Creating a tool from scratch

  1. Creating the whole non-default configuration for my SMTP server.
  2. Adding anc configuring DNS recors at the hosting providers DNS management tool.
  3. Adding a weekly cron task with curl -l -L on each domain and save it's output into a file.
  4. Adding a weekly cron task of say one hour later, to check each file and email myself if the status code isn't 200.

This might seem simple, but is actually quite complex (though not necessarily complicated), and it also might be a bit fragile. A dedicated, communal, maintained utility might be better for me.

2. Third party tools

I don't want to use some grandiose, third-party network-monitoring service like Nagios, Icinga, Zabbix, Shinken, etc, and they all seem an overkill per this particular cause.

3. Postfix add-on

I've already installed Postfix with the internet-site configuration so that tool might utilize Postfix. I just use the Postfix defaults, some default conf I could add on top of internet-site, maybe without adding and configuring DNS records.

A utility which is an interactive program to re-configure Postfix might ease my pain; I wouldn't have to fill my Ubuntu-Nginx-WordPress-Environment installation-script with much SMTP configuration data. Maybe I'll just have to set some DNS records after that, and that's it. Anything that would ease the process this way or another is also an option for me.

4. Handling the spam filter

Even if Gmail would mistakenly move my first email (or the first series of email) to spam, I could put it into a whitelist.


My question

Is there a utility I could use to have this behavior?

  • 1
    You could write some shell script using wget or curl (or some other HTTP client command line program) and emailing you when it fails (using some command line SMTP client program, e.g. mail). You would then run periodically (e.g. twice a day) that script from your crontab. You don't need to mess the DNS – Basile Starynkevitch Feb 14 '18 at 6:06
  • Where will this email monitoring tool run? On a server separate from your web servers right? Do you know how to send mail using postfix from the command line? as Basile suggested, have you considered creating a simple script that will check if your servers are up and send you a mail using postfix if not? If you need help using postfix to send mail from the command line/a script that would be a more direct question. – Jesse_b Feb 14 '18 at 12:41
  • I know of no way to send email from postfix itself (I know some add-on utility is needed for that). Also, I don't know a smooth way to check if my site is down or not, or at least, how to "fish" the http status code correctly from curl, @Jesse_b. An answer could help. – user9303970 Feb 14 '18 at 12:57
  • 2
    @user9303970: Please modify your question to ask either about how to send mail with postfix or how to check if your website is up. Or use the site/google's search function and I'm sure you will find something. – Jesse_b Feb 14 '18 at 14:52
9

Best bet is to use a service like uptime robot. Free tier will cover less than 50 sites, pro plan is quite cheap. It'll do a simple ping check or even HTTP status code check

The upshot of this is that you're not adding an additional point of failure (that you can control). You've no longer got to maintain and update a monitoring service

4
+200

You can use something like this;

> cat email_warning.sh 
#!/bin/bash

#
# email_warning.sh 
#

UP=true
FROM=you@valid.spf
TO=you@gmail.com
MAX=5
URL="https://$(hostname)/"

function doMail {
    S="$1 $(date)"
    F=$(echo $S | perl -p0e 's/[\s\t ]+/_/g;s/^(.{122}).*$/$1/g')
    echo -e "Subject:$S\n\n$S" | sendmail -F $F -f $FROM $TO
}

function doUP {
    curl -s -m $MAX "$URL" > /dev/null
    R=$?;
    if [ "$R" == "0" -a "$UP" == false ] ; then
        UP=true
        doMail "recovered $URL"
    elif [ "$R" != "0" -a "$UP" == true ] ; then 
        UP=false
        doMail "over $MAX s for $URL"
    fi
}

while [ "" == "" ] ; do
    ping -c 2 8.8.8.8 >/dev/null 2>&1
    if [ "$?" == "0" ] ; then
        doUP
    fi
    sleep 60
done
  • just run it in tmux/screen/nohup (or use cron and echo $UP > /dev/shm/email_warning.tmp etc)
  • the script can be tuned to monitor a local service or a remote one if you also want emails about network and server issues.
  • It's also likely desirable to check the service response for other failure modes (like a non-functioning database).
  • You don't need your own functioning SMTP server (though sendmail is installed with postfix) for this to work as long as you are not on a home network
  • The doMail function is designed to be optimal for Gmail to forward to a phone without data.
  • Gmail spam filter will not be an issue if you have a valid SPF in your DNS or just add the email address to your contacts and add a not spam filter.
2

Try a hack of the script I built below. My example contains 3 well known hosts and 2 non-existent hosts.

pingtest.sh

HOSTLIST='www.google.com www.oracle.com www.facebook.com www.sdfafdsfdsf.com www.uieyafbmndhfjsbxcvn.com'

BAD_HOSTLIST=
for HOST in $HOSTLIST ; do
        echo $HOST
        ping -c 3 -w 5 $HOST || BAD_HOSTLIST="$BAD_HOSTLIST $HOST"
        echo =======================
done

if [ -n "$BAD_HOSTLIST" ] ; then
        echo ___________________________________________
        echo The following hosts failed the ping test:-
        echo $BAD_HOSTLIST | xargs -n 1
        echo $BAD_HOSTLIST | mailx -s 'Hosts that failed the ping test' MyName@gmail.com
fi

If you need to quickly set up a mail forwarder, try this short & easy article on using ssmtp: https://www.cyberciti.biz/tips/linux-use-gmail-as-a-smarthost.html

A quick look at the output of the script is below:

# ./pingtest.sh
www.google.com
PING www.google.com (216.58.200.100) 56(84) bytes of data.
64 bytes from www.google.com (216.58.200.100): icmp_seq=1 ttl=55 time=29.8 ms
64 bytes from www.google.com (216.58.200.100): icmp_seq=2 ttl=55 time=28.4 ms
64 bytes from www.google.com (216.58.200.100): icmp_seq=3 ttl=55 time=30.3 ms

--- www.google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 28.483/29.577/30.365/0.810 ms
=======================
www.oracle.com
PING e870.dscx.akamaiedge.net (104.116.147.64) 56(84) bytes of data.
64 bytes from www.oracle.com (104.116.147.64): icmp_seq=1 ttl=56 time=29.5 ms
64 bytes from www.oracle.com (104.116.147.64): icmp_seq=2 ttl=56 time=28.3 ms
64 bytes from www.oracle.com (104.116.147.64): icmp_seq=3 ttl=56 time=27.8 ms

--- e870.dscx.akamaiedge.net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 27.842/28.570/29.503/0.706 ms

=======================
www.facebook.com
PING star-mini.c10r.facebook.com (157.240.8.35) 56(84) bytes of data.
64 bytes from www.facebook.com (157.240.8.35): icmp_seq=1 ttl=53 time=27.8 ms
64 bytes from www.facebook.com (157.240.8.35): icmp_seq=2 ttl=53 time=79.4 ms
64 bytes from www.facebook.com (157.240.8.35): icmp_seq=3 ttl=53 time=79.0 ms

--- star-mini.c10r.facebook.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 27.887/62.130/79.467/24.215 ms
=======================
www.sdfafdsfdsf.com
ping: www.sdfafdsfdsf.com: Name or service not known
=======================
www.uieyafbmndhfjsbxcvn.com
ping: www.uieyafbmndhfjsbxcvn.com: Name or service not known
=======================
___________________________________________
The following hosts failed the ping test:-
www.sdfafdsfdsf.com
www.uieyafbmndhfjsbxcvn.com
  • I'm not sure that ping is a good monitoring method for a website; the OP even mentions curl... – Jeff Schaller Feb 21 '18 at 2:20
  • True, for a website curl's better. It wouldn't be too hard to adapt the above structure to use curl though. – Paul T Feb 21 '18 at 2:34
  • Hi, what's the xargs -n 1? – user9303970 Feb 21 '18 at 9:35
  • Hi user9* - xargs -n 1 will take the arguments provided and output them each on a separate line. Paul T. – Paul T Feb 23 '18 at 0:18
1

If you are not married to email, you can use Pushbullet, which will send you a push notification to your phone using curl. You need to install it on your mobile device.

Then simple have a small bash script which curls your website and if it does not respond, then you send a push notification straight to your phone like this:

curl -u $auth_token: https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="$MSG"

where $auth_token is your Pushbullet auth token after you register.

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.