Monitoring Internet connection at Home using Healthchecks.io, Termux and CouchDB

For last few days we have been running a dog cam to watch Max. whenever we are away. Max is getting older, faster than I think. So even though he is very comfortable staying alone at home 1, we like to keep an eye on him for our sanity. The camera is nothing fancy, we switch it on (Power On) when we leave house and Switch it off (Power off) when we come back. We can watch him when we are away on a phone app2. But the biggest challenge has been tracking internet connection. The connection goes off even when it shouldn't. And sometimes it doesn't come back automatically unless we raise a ticket. So now along with keeping an eye on Max, I also need to keep an eye on internet.

He sleeps most of the day

He sleeps most of the day

The initial idea was to run a script that connects to google, if it can't then it would send me a SMS. It's simple and easy. But I also wanted a report on how long, how often etc. And also a better way to reach me than just SMS. So then came the idea of using Healthchecks.io. Healthchecks provides an instant alert when a cron job fails. Make a simple HTTP request to the ping URL at regular (which you decide) interval. When the Ping URL is not pinged on time, Healthchecks.io will send an alert through various channels. Healthchecks is an Free and Open Source Software. But I use the hosted version, which allows up to 10 free checks.

Now I have a script which just sends a curl/wget request to healthchecks.io every 15 minutes. If it cant ping (internet is down), Healthcheck sends me an alert (email and Pushover) message. Healthcheck.io can also send a webhook post on the url with the same details. This is where CouchDB comes into act. I created a CouchDB database called internet. And made the healthcheck.io to post JSON to that DB. On healthcheck.io I gave to post URL as

url: https://WhateverisMyChouchDB.com/internet_status

post data

{"_id":"$NOW","name":"$NAME","status":"$STATUS"}

- Where $NOW - Current UTC time in ISO8601 format. Example: "2019-02-24T15:36:15+00:00"
- $STATUS - Check's current status ("up" or "down")
- $NAME - Name of the check

These variables will be replace at the run time. You also need to set headers, so you can authenticate to CouchDB and post

Content-Type : application/json
Authorization : Basic xxxxxx

where xxxxx is base64 of username:password. It's a simple basic access authentication. that CouchDB supports.

Now there will be a data entry in my couchdb when the link goes down and comes back. I also get notifications on my phone. Data entries in the CouchDB is simple

//when it went down
{
  "_id": "2019-02-24T14:15:02+00:00",
  "_rev": "1-d55eb8fc749df761468454ae50443743",
  "name": "internet@home",
  "status": "down"
}
//when it came back again
{
  "_id": "2019-02-24T14:55:14+00:00",
  "_rev": "1-f74c13d1d1bec08b2ea559d61c46fdb0",
  "name": "internet@home",
  "status": "up"
}

So it would be very easy to do some reporting end of the year or every week etc. I can also make an live graph if I want.

Setup

Finally I had to have a machine, running all the time to do the pings. Initially I thought of using a Pizero but then settled for an old Android phone running Termux.

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically - additional packages are available using the APT package manager.

So I enabled the cron on it3. Setup the cron to do a curl to the heathchecks.io ping URL every 15 minutes. This is the phone we usually leave at home connected4 to wifi. So since its always there doing nothing it can do this. I also think it's safer to leave it connected to internet than a pi. I also plan to run other things on it to get most of its cpu/power usage. May be I will write a dedicated blog on how I use Termux. Until then happy monitoring.

  1. He mostly sleeps
  2. I will write about this if anyone else wants do somewhat similar thing
  3. If you are not interested in working in command line interface. Try the nock nock beta android app. It can do pings to remote servers at scheduled intervals. Has a great UI.
  4. I use it to watch videos at home