Added health checker.

This commit is contained in:
BurnyLlama 2022-05-28 16:10:37 +02:00
parent 35e27546b7
commit dffd4b0507
3 changed files with 53 additions and 6 deletions

View File

@ -1,9 +1,52 @@
import getLogger from "../../libs/logger.js" import getLogger from "../../libs/logger.js"
import settings from "../../libs/settings.js" import settings from "../../libs/settings.js"
import https from "https"
const log = getLogger("Health Checker", settings.checkers["health-checker"].color) const log = getLogger("Health Checker", settings.checkers["health-checker"].color)
export function start() { function checkService(serviceDomain) {
log(`Checking service '${serviceDomain}'`)
return new Promise(
(resolve, reject) => {
const req = https.request({
hostname: serviceDomain,
port: 443,
path: "/",
method: "GET"
}, res => {
if (res.statusCode === 200) resolve(`Service '${serviceDomain}' seems to be running!`)
res.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`))
})
req.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`))
req.end()
}
)
}
export function start(sendMessage) {
log("Starting up the health checker...") log("Starting up the health checker...")
log("Services to watch:", settings.checkers["health-checker"].services)
function checkServices(serviceDomains) {
for (const serviceDomain of serviceDomains) {
checkService(serviceDomain).then(
status => log(status)
).catch(
status => {
log(status)
sendMessage(status)
}
)
}
}
checkServices(settings.checkers["health-checker"].services)
setInterval(
() => checkServices(settings.checkers["health-checker"].services),
(settings.checkers["health-checker"].interval ?? 300) * 1000
)
return
} }

View File

@ -12,7 +12,7 @@ const currentIp = await getIp()
const cachedOldIp = fs.readFileSync("./ip.cache").toString() const cachedOldIp = fs.readFileSync("./ip.cache").toString()
let oldIp = cachedOldIp ? oldIp : currentIp let oldIp = cachedOldIp ? oldIp : currentIp
async function checkIp() { async function checkIp(sendMessage) {
log("Checking for IP changes...") log("Checking for IP changes...")
const nowIp = await getIp() const nowIp = await getIp()
if (nowIp !== oldIp) { if (nowIp !== oldIp) {
@ -27,7 +27,11 @@ async function checkIp() {
export async function start(sendMessage) { export async function start(sendMessage) {
log("Starting up the IP watcher...") log("Starting up the IP watcher...")
checkIp() checkIp(sendMessage)
setInterval(checkIp, (settings.checkers["ip-changed"].interval ?? 3600) * 1000) setInterval(
() => checkIp(sendMessage),
(settings.checkers["ip-changed"].interval ?? 3600) * 1000
)
return return
} }

View File

@ -13,7 +13,7 @@ export async function start() {
password: settings.notifiers.xmpp.sender.pass password: settings.notifiers.xmpp.sender.pass
}) })
xmpp.on("error", err => console.error(err)) xmpp.on("error", err => console.error(err))
xmpp.on("offline", () => console.log("OFFLINE!")) xmpp.on("offline", () => console.log("OFFLINE!"))
async function sendMessage(msg) { async function sendMessage(msg) {