3
\$\begingroup\$

I'm new to coding and made a small Node script that checks when a website is down because of high traffic. It's a website that sells tickets and I don't want to press F5 every 10 seconds to check if the website works again.

Because I send a ping so often I sometimes receive something back from the website. But when I check it again a couple of seconds later I get a request timeout again.

So I made a counter to track how stable the website is. If I receive a request timeout subtract from the counter.

It works, but I was wondering if there is a better way or solution to do this. Because I'm still new I don't have much experience with other solutions to this sort of problem.

This is my code:

const Monitor = require('ping-monitor');
const sendEmail = require('./Email.js')

const myMonitor = new Monitor({
  website: 'https://www.pathe.nl/',
  title: 'Pathe',
  interval: 0.1 // minutes, so 0.1 is 6 seconds
});

let counterUp = 0;

myMonitor.on('up', function (res, state) {
  counterUp++
  console.log('counter is ' + counterUp)
  console.log('Yay!! ' + res.website + ' is up.');

  // send email when script made sure the website is really up and stable. Otherwise we receive an email when the website isn't fully up and running. Because we send a ping every 6 seconds.
  if (counterUp == 20) {
    sendEmail(myMonitor.title)
    console.log('sending email')
  }
});


myMonitor.on('down', function (res) {
  counterUp--
  console.log('counter is ' + counterUp)
  console.log('Oh Snap!! ' + res.website + ' is down! ' + res.statusMessage);
});


myMonitor.on('stop', function (website) {
  console.log(website + ' monitor has stopped.');
});


myMonitor.on('error', function (error) {
  console.log(error);
});
\$\endgroup\$
-1
\$\begingroup\$

In reality, people just use third-party services for that. For instance, uptime website monitoring can be done StatusCake. If you own the target site, you install infrastructure tools like New Relic. Cloud services these days also provide uptime data and tools out of the box.

The only reason I would write my own uptime monitor is if you're doing this for a small project or doing it for fun.

Now the problem with the uptime monitor you wrote is that it contributes to the site traffic. To the website, you're just another client trying to connect to the site. Together with your traffic are other sources of traffic, like legitimate users, web crawlers, malicious site scanners, DDOS bots, etc.

The reason you get the Request Timeout (HTTP 504) is because the site is already overloaded. It can be worse though. The site may choose to ignore you, block you temporarily, or even permanently. If you've seen a captcha appear when doing a Google search, that's one example.

So, what I recommend is to:

  • Reduce the rate you ping. Every 5, 15, or 60 minutes is reasonable. You're probably not going to need it every 6 seconds.
  • Hit a static asset on the domain instead, like the favicon. Hitting actual pages will cause 504s since it requires logic and probably database calls to generate the page. Just make sure it's not cached or served from a CDN.
    • This only tells you if the static server/caching proxy is up, not the actual server with the logic.
  • If the website has an API for uptime or a status page that you can scrape, use that instead. Those are usually served by a separate server so they can still tell you the status even if the main site is down.
  • If you own the website, monitor the infrastructure instead.
\$\endgroup\$
  • \$\begingroup\$ Thank you for your feedback! I made this project just for fun and to learn. So I'm going to implement your suggestions. \$\endgroup\$ – Daniel May 9 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.