Create your own website uptime monitor with NodeJS and Bugsnag, the ultimate step-by-step

As a maintainer of a website, you will probably want to know when the website is down, before any user. Some tools like Pingdom or Uptime Robot can help you with that, but their free plans only include few features with limited usage.

You can create your own modular service that can do the same job and get a notification whenever one of your websites goes down, 💯 % free.
All you need is :
* A Raspberry Pi (or any other chip or computer that can run 24/7)
* Beginner knowledge of NodeJS
* A Bugsnag account and API credentials (click here for the guide)
* Some coffee ☕️

I’d really appreciate if you name the project “Shuri”, just like I did. You’ll know why at the end of this article.

Step 1: Create the project

On Linux or Mac OS, open your console and reproduce these steps to create the project.

Then, install the dependencies :

Step 2: Create index.js

Create the index.js in the project folder, with the code above.

VSCode is used here as code editor, but you can use Atom, Sublime Text, NotePad, Vim, or whatever you want as well.

index.js is the main file of the project. It’s in charge of fetching the websites recursively, every fetchInterval milliseconds. Every time it finds an error, it calls the handle method from the Bugsnag module. You can add other modules later and call them from here.

Step 3: Create data/urls.json

Create the data folder, and urls.json file inside it. Then, add an array with a list of all the website urls you want to monitor.

Step 4: Create data/accept.json

Create a file under the data folder you created in the previous step, then create the accept.json file below it.

Sometimes, a website you’re fetching is just in maintenance mode, or on another state and you don’t want to do any log just because the returned status code isn’t 200 as expected.

In accept.json, you can create a list of websites for which you can accept some specific status codes, for example 503 for maintenance mode.
Leave the json empty with { } if you don’t want to add any acceptable status code. Status code 200 will be used by default for the urls in data/urls.json.

Step 5: Create modules/bugsnag.js

Now create the modules folder, then add a js file named bugsnag.js .

In our context, you can think of a module like a class with a handle function. The benefit of this approach is that whenever you want to add new functionalities, you can separate them instead of having a bunch of functionalities in one same file. Thus, each functionality follows the same structure while having different internal structures.

Basically, this module just sends the detailed error every time the service is unable to fetch a url correctly. Bugsnag will send you an email for new errors only. So, if a website is down for 45mins, you won’t get an email notification every minute. Don’t worry about that 😏.

If you want to create a new module, just create another js file in the modules folder, require it from index.js and call its handle static method.

Step 6: Create credentials.json

Create the credentials.json file in the root folder of the project.

Once you create this file, get your bugsnag api key and insert it between the empty quotes.

Step 7: PM2 configuration

PM2 is a Process Manager for Node.JS. In our context, we need it to make sure our service runs with zero downtime (if the service which checks if a website is down, goes down, it’s going to be very problematic 😬).

It’s very simple to install. You’ll just need to run this command:

Once pm2 is installed, you’ll have to add a configuration file for pm2 :

Conclusion

You can test your code simply by running this command:

Bravo! It works! 🙈

Since the service is basically a recursive function, it won’t stop by its own. To make it stop, just do a ctrl + C in the same console.

You can run it with pm2 as a background process with the command pm2 start ecosystem.config.json , then stop it with the command pm2 stop ecosystem.config.json .

It’s recommended to use pm2 to run it on your raspberry, so you’re sure it never stops running.

Bonus

In case you were wondering why I named it “Shuri”…

I’m a big Marvel fan, I name almost all my projects as MCU characters or kingdoms. And in the Black Panther movie, Shuri is the princess ; a badass black tech girl who was protecting the kingdom of Wakanda. She was using her technical skills to do her job and used to warn T’challa (also known as Black Panther) anytime there was an issue or a threat he had to fix, so he would know in advance what to do.

Naming the project like that, is a tribute to all women in STEM, all over the world. You rock girls!
Everything men can do… You know the rest.

IT Consultant, Tech Writer