Website monitoring using headless Raspberry Pi

As a webmaster, there is nothing more embarrassing than a client calling you to tell you that their site is down, when you’re the one that should be monitoring it can calling them instead. I had this happen to me once and that was enough for me to formulate a solution. I had a raspeberry pi sitting in my desk draw doing nothing for almost a year and I thought, this might be a good time to pull it out and put it to work. After some quick google searches I found this great solution online – https://github.com/fzaninotto/uptime. This Uptime node app allowed you the ability to monitor multiple sites and receive email alerts when status changes.

First matter of business was to get the RPI up and running. This was a unique challenge because I didn’t have an extra keyboard, and monitor to configure it once the OS was installed. So essentially I need a solution that allowed me boot the PI and auto-configure it. After downloading and burning Rasbian Jessie Lite, I was able to enable ssh and auto-configure the wireless card automatically. To enable ssh on boot, create a file “ssh” on the boot partition on the sd card. When the pi starts and see’s this file it will automatically enable ssh access to it. For the wifi dongle (yes I went with wifi instead of ethernet cable) I was able to have it automatically get an IP address from DHCP by creating a file called “wpa_supplicant.conf” with the wifi configuration information in there (note the below configuration assumes no password on the wifi AP).

network={
    ssid="my_wifi_name"
    scan_ssid=1
    id_str="home_wifi_1"
    proto=RSN
    key_mgmt=NONE
}

Here’s a quick breakdown on using wpa_subplicant.conf and longer man page

Once the PI has booted it will connect to the wifi network “my_wifi_name” and ask for an IP address. And with the ssh file installed on the boot partition, ssh is enabled you can use you favorite ssh tool (putty) or just the ssh command in the terminal (macOS) to access the box. Remember the default username for Rasbian is “pi” and the password “raspberry”. It be wise to run “sudo raspi-config” once logged in and configure your local, hostname and change your password.

The second thing was to install the necessary tools you would need to get the server up; you can get ’em all via apt-get.

sudo apt-get install git
sudo apt-get install nodejs
sudo npm cache clean -f
sudo npm install -g n
sudo n 6.0
git clone git://github.com/fzaninotto/uptime.git
cd uptime
npm install
npm remove nodemailer
npm install nodemailer@3.1.5
node app

You will notice in the list of commands npm install -g n and n 6.0, I discovered that uptime works well with node 6, so this line installs the node helper utility n and then uses n to upgrade nodejs to version 6. Also the uptime email plugin works well with nodemailer version 3.1.5, hence the command to remove what’s installed and install that specific version.

If you intend to use the emailer plugin then you will have to fix a bug in the emailer code.

nano ./uptime/plugins/email/index.js

on line 60 change “var mailer = nodemailer.createTransport(config.method, config.transport);” to “var mailer = nodemailer.createTransport(config.transport);”. This change is to facilitate the new syntax for the createTransport command. I got the clue from this post: http://stackoverflow.com/questions/33792811/node-js-nodemailer-smtptransport-causes-typeerror-this-mailcomposer-setmessageo.

var mailer = nodemailer.createTransport(config.transport);
//var mailer = nodemailer.createTransport(config.method, config.transport);

Lastly I had to make the uptime start whenever the Pi restarts. To do this edit the the rc.local file

sudo nano /etc/rc.local

And add the lines right be fore the exit 0

#run uptime monitor
cd /home/pi/uptime/; node app &

Now you’re good to go with your uptime server, all you need to do is follow the instructions on Fzaninotto’s git wiki and setup your website monitoring.

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.