Tim's Blog

Information, Technology, Security, and other stuff.

Monitoring Websites with Nagios

Published 2015-06-08

I used to run this site on a very cheap web host. It seemed perfect, until a month or so after setting it up my site would periodically crash from a lack of memory. I figured I was getting was I was paying for ($1/month hosting isn't really designed for long-term use), and decided to move back to AWS, and then finally settled on DigitalOcean as the cheapest option I could find ($5/month).

Nevertheless, I became distrusting of VPS's (once bitten, twice shy), and this distrust was a big motivator into building a monitoring solution at home with Nagios.

Trying to find a guide on monitoring a website with Nagios wasn't straightforward though, so I'm just going to post here what I got working. I also monitor Google and my employer's website, just so I can discern between an individual host failure or problem with my ISP.

So first off, I created a new file called /usr/local/nagios/etc/objects/websites.cfg. Then, with a text editor, I defined the following hosts:

define host{
        use                     linux-server
        host_name               Cogito
        alias                   Cogito Group Website
        address                 175.107.155.58
        }

define host{
        use                     linux-server
        host_name               timothy-quinn
        alias                   Personal Blog
        address                 45.55.198.107
        }

define host{
        use                     linux-server
        host_name               Google
        alias                   Google Australia
        address                 216.58.220.131
        }

You can see I've put the IP addresses in for the sites, so there's no dependency on DNS (I've got separate checks for my DNS server). The easiest way to get the IP address is either by pinging the site (ping google.com.au) or by performing a name server lookup (nslookup google.com.au).

I've also set them to use the linux-server template. This could be incorrect, but it works for me as the check_http commands are defined for that template.

Then, further down in the same websites.cfg file, we have the service definitions:

define service{
        use                     generic-service
        host_name               Cogito,timothy-quinn,Google
        service_description     Check Content
        check_command           check_http! -w 5 -c 10 --ssl
        }

define service{
        use                     generic-service
        host_name               Cogito,timothy-quinn
        service_description     Check SSL
        check_command           check_http! -C 30,14
        }

The first just checks that the site responds in a timely manner. The second checks that the SSL certificate is valid, and also will give me a warning at 30 days (and critical at 14 days) that it will expire. I've excluded Google from the SSL check, as I don't care about their certificates.

Now it's just a case of editing /usr/local/nagios/etc/nagios.cfg and adding in the new configuration file we created:

# Definitions for monitoring Websites
cfg_file=/usr/local/nagios/etc/objects/websites.cfg

It's totally up to you where you put the host and service definitions. You can integrate them with one of the other files too if you like.

Oh and don't forget to restart nagios when you're done, to see the changes reflected:

service nagios restart

Here's what mine looks like once the checks have been performed:

Nagios Website Checks