An Easy Way to Monitor a Website's Health

An Easy Way to Monitor a Website's Health

ยท

3 min read

This article is part of the series on making a wiki, but in fact, it can be applied to any website or service.

The problem

Recently the docker daemon running the wiki I host got into a bad state. My wiki ended up returning an HTTP error 403 for 3 days until I get poked by users.

Facepalm

To avoid such a situation in the future, I decided to look for an automated health check.

The solution

I found an easy and quick solution using the website healthchecks.io.

They even have a free tier for hobbyists with up to 20 jobs.

I created an account and a job there. Got my ping URL!

After I wrote a script healthcheck.sh that I ended up running on the server hosting my wiki:

#!/bin/bash

status=$(curl -o /dev/null -Isw '%{http_code}\n' https://gameofroles.wiki/view/Accueil)

if [ "$status" -eq "200" ]; then
    curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXXXX
    echo "Wiki up"
else
    curl --retry 3 https://hc-ping.com/XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXXXX/$status
    echo "Wiki down: $status"
fi

In short, I retrieve first the HTTP return status of the homepage. If it is 200, then all is good. Else I ping my job with the failing return status.

Then this script is executed daily with a cron defined like that:

0 0 * * * /wiki/healthcheck.sh

From now on, if my website is down, I will receive an automated email from healthchecks.io notifying me about the situation!

Awesome

Note that in the case my server is fully down, which means both my wiki and my health check are not going to be able to run, the website healthchecks.io will automatically throw me a notification email too after a day and 1 hour. Because they expect a regular daily ping. The period and grace time before considering a job is failing are configurable.

image.png


Photo by Hush Naidoo on Unsplash

Did you find this article valuable?

Support Sonny Alves Dias by becoming a sponsor. Any amount is appreciated!

ย