A simple uptime monitor script that pings your website periodically to see if it's up and running. If the site breaks then it sends an alert to Discord.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const axios = require('axios');
const sites = [
'https://google.com',
'https://this-site-doesnt-exist.zyx',
];
const sendWarning = async (siteUrl) => {
const content = `⚠ '${siteUrl}' is not responding ⚠`
console.warn(content);
return lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${process.env.CHANNEL}`, content,
});
}
// Send a GET request up to 3 times for a specific site
const checkSite = async (site) => {
for (let checks = 0; checks < 3; checks++) {
const passed = await axios
.get(site, {timeout: 8e3}) // 8 secs
.then(() => true)
.catch(() => false);
if (passed) return true;
}
return false;
};
// Test all sites and post warnings
await Promise.all(
(await Promise.all(sites.map(checkSite)))
.map((passed, i) => [passed, sites[i]])
.filter(([passed, _]) => !passed)
.map(([_, site]) => sendWarning(site))
);