Nico Martin
Posted on April 4, 2023
TLDR: the whole source code for this project is on GitHub: https://github.com/nico-martin/uptime-slackbot
For several years I have been using UptimeRobot, a service that monitors the availability of my site and sends me a message in a Slack channel when the site is down. Until the end of 2022, this worked great with my free plan. However, after an adjustment to the pricing structure, Slack integration is now only available on the "Solo." Plan.
Of course I understand that the operation of the platform has to be financed and yes, $84 per year is quite reasonable. But for my private projects this is not really worth it.
Moreover, it should be quite easy to build such a monitoring system yourself, right?
So what do I need exactely. I need a service that requests a Website every five minutes. If the request has a status code of 300 or higher, its concidered as "down". If lower than 300 it is "up".
"Down" should send me a notification. Furthermore it should recheck every 2 seconds and notify me again when the site is back up. So far so good.
Let's take the individual parts apart.
- I need a reliable service that executes the requests and attempts retries when a request fails.
- I need a database that tracks the attempts.
- I need to set up a messaging system through slack.
So far so good. I was able to answer the first fundamental question very quickly. No, I do not want to be responsible for hosting an application myself, for which high availability is absolutely necessary. So it had to be a cloud provider. And a quick comparison of the developer experience immediately led me to Firebase.
With Cloud Functions I have an easy way to execute certain tasks in time intervals and with the Firestore Database I have a highly available database to store the status of my requests.
Initialize Firebase
The setup in Firebase is pretty simple.
Communicationis handled by firebase-tools
, which is best installed globally right away:
npm install -g firebase-tools
After that you can go to your project folder and install some dependencies:
npm install firebase-functions@latest firebase-admin@latest --save
The initial setup can then be completed with the following steps:
- Run
npx firebase login
, this allows you to login via the browser -
npx firebase init firestore
this will initialize your project. I guess you will need to create a new project if you haven't created the project already. -
npx firebase init functions
now you can set up the cloud functions for the same project before. You will also be asked if you want to use TypeScript or JavaScript. In this example, I choose TypeScript. - As the last step, all dependencies will be installed for you together with the whole folder structure.
- Additionally I have installed prettier for automatic code formatting in the
functions
folder:npm install prettier eslint-config-prettier --save-dev
. To make the setup work exactly the same as in my example I recommend to compare your eslint and typescript configuration with mine.
And with that we are ready to start with the implementation. More about this in the second part.
Posted on April 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.