Health Monitoring of Applications and Websites with the i3 Window Manager
For more than one year I have been running and developing molescrape (Github), a scraping platform that executes my scrapy spiders and monitors them. I do not only monitor the spider execution itself, but of course also the whole molescrape platform.
Recently, I found out how simple it is to create your own entries in the i3 status bar with i3blocks. Thus, I had the idea to create a block that displays the status of molescrape. Usually, I will be informed by mail if there are some outages, so it’s more a nice-to-have feature, but it might even become useful in case I do not see or receive a mail once.
I created the script for my own monitoring service, but since it’s only based on querying a public HTML page you can easily adjust it other health check services (like UptimeRobot).
The result will look like this:
Setup
In case you do not use i3blocks already please install it first. The
installation differs depending on your linux distribution, but generally
you should search for a package called i3blocks
.
To use i3blocks add it to your i3 configuration file in
$HOME/.config/i3/config
:
bar {
status_command i3blocks -c $HOME/.config/i3blocks.conf
}
You can change the configuration path of i3blocks to your liking if you do not
want to use $HOME/.config/i3blocks.conf
.
At this point this configuration file does not exist yet, but we will create
it soon.
Since we will need pango fonts, also change the font setting in the configuration file:
font pango:Fira Mono, Icons 8
Fetch Monitor Status from Website
First, we will create the script that checks the current health status and reports it back to i3blocks. Scripts for i3blocks are extremely simple. They only have to output the text that should be displayed in the status bar. i3blocks will execute them in defined intervals and update the output on the status bar. If you want to use colours, you can use Pango formatting (but in that case you have to setup a pango font in i3 as we did before).
My script is extremely simple. It will fetch the content of the public status website and then parse the HTML source code for the words “OK”, “Expired” and “Down”. To make sure that I do not fetch standard texts by accident, I require that these words must be surrounded by HTML tags. I.e., I actually search for “>OK<”, “>Expired<” and “>Down<”. This ensures that there must be a HTML tag immediately before the searched texts and immediately after it.
I count the occurrences of these words, sum them up and check if the number of OK equals the total number of status texts or not. If all systems are OK, I will output a green status text and otherwise a red one.
#!/usr/bin/env bash
content=$(wget https://pidar.eu/dashboard/tag/molescrape/core -q -O -)
num_ok=$(echo $content | awk -F">OK<" '{print NF-1}')
num_expired=$(echo $content | awk -F">Expired<" '{print NF-1}')
num_down=$(echo $content | awk -F">Down<" '{print NF-1}')
num_total=$(echo "$num_ok + $num_expired + $num_down" | bc)
if [ "$num_ok" == "$num_total" ]; then
echo '<span color="#00FF00">'$num_ok"/"$num_total'</span>'
else
echo '<span color="red">'$num_ok"/"$num_total'</span>'
fi
I saved this script file to $HOME/.i3blocks/pidar-molescrape
.
Adding the script to the i3blocks status bar
The only task left is to add the script to the status bar. For this, create
the configuration file $HOME/.config/i3blocks.conf
and add the following
block:
[pidar-molescrape]
command=$HOME/.i3blocks/pidar-molescrape
label=MOLE
interval=60
markup=pango
This will check the public status page every 60 seconds and display the current health status in my i3 status bar.
Even though this tutorial was for my own service PiDAR (which is mostly used by me) you can probably adjust it easily to any other service with a public dashboard or API like UptimeRobot (Public Status Pages) or Healthchecks.io (API, I think no public status pages).
I do not maintain a comments section. If you have any questions or comments regarding my posts, please do not hesitate to send me an e-mail to blog@stefan-koch.name.