Raspberry Pi Home Server – Check that a website is always online

Warning : This article has been automatically translated by Bing Translate


Danger : This article has been automatically translated by Bing Translate


Caveat : This article has been automatically translated by Bing Translate

With the change of host of second blog of the House, I did not have that compliments at home ! Errors 500 and 504 at rehearsals, regular absences. Brief, need to take things in hand !

Check If Website Is Up or Down

The support of the host asked me only “What a mistake ? What time exactly ?” (as if course it could find him even in the logs on basis of a date and a URL but good …). “C'est à cause de WordPress monsieur !”

So I decided to set up a monitoring, testing if the site meets and this is not the case, What is the error.

The first step is to install sendmail and exim4 to be able to send the alert in case of unavailability of the site which will be monitored.

  1. Update of the system
    apt-get update && apt-get upgrade -y
  2. Install exim4 and exim4-config
     apt - get install exim4-config exim4
  3. Launch the configuration of exim4 with the following command :
    dpkg - reconfigure exim4-config

    To help you, This is my setup :
    1 2 3 4 5 6 7 8 9 10 11.
    .

  4. Edit the file with your Gmail information
    # password file used when the local exim is authenticating to a remote
    # host as a client.
    #
    # see exim4_passwd_client(5) For more documentation
    #
    # Example:
    ### target.mail.Server.example:login:password gmail - smtp.l.google.com:votre.Compte@gmail.com:votre.MOT.de.passe *. google.com:votre.Compte@gmail.com:votre.MOT.de.passe smtp.gmail.com:votre.Compte@gmail.com:votre.MOT.de.passe
  5. Control rights on the file for security
    12
  6. Update the configuration
     Update - exim4.conf
  7. Restart the service
     exim4 service restart
  8. Force the sending of the mails waiting
     exim4 - qff
  9. You are now ready for your first test. Create a file with the following content :
    to : votre.Compte@gmail.com from : The name of your subject machine : This is a test your first shipment of mail

    For sending, nothing simpler :

    CAT test | sendmail t

Phase 2 : monitor site ! For this, It will look for the home page and analyzing the result.

  1. Install wget
    apt - get install wget
  2. Create the script that will monitor your site. I called it my home /root/check_website.sh
     #!/bin/sh WEBSITE ="www.my.website.fr"
    TIMEOUT = 40 RETRIES = 1 WAIT_RETRIES = 5 RESULT ="/tmp/website.result"
    
    TOSEND = 0 MAIL ="to : my.email@gmail.com/* <! [CDATA [*/! function () {try {var t = "currentScript" in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n =2)i=parseInt(a.substr(n,2),16)^r,e =String.fromCharCode(i);e=document.createTextNode(e),c.parentnode.replacechild(e,c)}}}catch(u){}}();/*]] > */"
    MAIL ="$Mailnfrom : Webchecker"
    MAIL ="$Mailnsubject : Website $WEBSITEn"
    
    /usr/bin/wget "$WEBSITE" --timeout=$TIMEOUT --append-output=/var/log/check_website.log --tries=$RETRIES --output-document=$RESULT --waitretry=$WAIT_RETRIES
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
            # Put here the list of messages that you want to monitor
    	WORDS="500 Error;Internal Server Error;504 Gateway Time-out"
    	export IFS=";"
    	for WORD in $WORDS
    	do
    		TST=`grep $WORD $RESULT`
    		RSLT=$?
    		if [ $RSLT -eq 0 ]; then
    			MAIL="$MAILn- Test of the word '$WORD' : OK"
    			TOSEND=1
    		else
    			MAIL="$MAILn- Test of the word '$WORD': Failed"
    		fi
    	done
    else
    	TOSEND=1
    	MAIL="$MAILnTimeout (${TIMEOUT}s) of the wget command"
    fi
    MAIL="$MAIL  n  Overclocking : "`date`
    if [ $Tosend -eq 1 ]; then
    	echo $MAIL | sendmail t fi
  3. Add the call to the script in the crontab file. For this, Edit the file with the command
     crontab-e

    At the end of the file add the line :

    */X * * * * /root/check_website.sh

    Replace X by the frequency in minutes of the control. Me I put 1 to control every minutes (Yes, I repeat, the host is really sucks)

Sources :

  • http://bernaerts.dyndns.org/Linux/75-Debian/278-Debian-sendmail-Gmail-account
  • http://ServerFault.com/questions/282215/script-to-automatically-test-if-a-Web-site-is-available
Like this article ? Bear with me on patreon!

You may also like...