add website tracking
All checks were successful
deploy / deploy (push) Successful in 22s
Publish Docker Image / docker (push) Successful in 46s

This commit is contained in:
Lee
2024-01-04 00:57:21 +00:00
parent 358bb6272c
commit ff11240334
7 changed files with 195 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import Influx from "./influx/influx";
import ServerManager from "./server/serverManager";
import WebsiteManager from "./website/websiteManager";
/**
* The influx database instance.
@ -7,3 +8,4 @@ import ServerManager from "./server/serverManager";
export const influx = new Influx();
new ServerManager();
new WebsiteManager();

View File

@ -41,7 +41,7 @@ export default class ServerManager {
// ping all servers in parallel
await Promise.all(this.servers.map((server) => server.pingServer()));
logger.info("Finished pinging servers");
logger.info("Finished pinging servers!");
}
/**

64
src/website/website.ts Normal file
View File

@ -0,0 +1,64 @@
import { Point } from "@influxdata/influxdb-client";
import axios from "axios";
import { influx } from "..";
import { logger } from "../utils/logger";
import Config from "../../data/config.json";
type WebsiteOptions = {
name: string;
url: string;
};
export default class Website {
/**
* The name of the website.
*/
private name: string;
/**
* The url of the website.
*/
private url: string;
constructor({ name, url }: WebsiteOptions) {
this.name = name;
this.url = url;
}
/**
* Pings a website and gets the response.
*
* @returns the response
*/
public async pingWebsite(): Promise<void> {
try {
const before = Date.now();
const response = await axios.get(this.url, {
validateStatus: () => true, // Don't throw a error on non-200 status codes
timeout: Config.pinger.timeout,
});
if (response.status === 500) {
throw new Error("Server returned 500 status code");
}
const responseTime = Date.now() - before;
influx.writePoint(
new Point("websiteStatus")
.tag("name", this.name)
.booleanField("online", true)
.intField("responseTime", responseTime)
.timestamp(Date.now())
);
} catch (err) {
logger.error(`Failed to ping ${this.name}:`, err);
influx.writePoint(
new Point("websiteStatus")
.tag("name", this.name)
.booleanField("online", false)
.timestamp(Date.now())
);
}
}
}

View File

@ -0,0 +1,47 @@
import cron from "node-cron";
import { logger } from "../utils/logger";
import Config from "../../data/config.json";
import Websites from "../../data/websites.json";
import Website from "./website";
export default class WebsiteManager {
private websites: Website[] = [];
constructor() {
logger.info("Loading websites...");
for (const configWebsite of Websites) {
const website = new Website({
name: configWebsite.name,
url: configWebsite.url,
});
this.websites.push(website);
}
logger.info(`Loaded ${this.websites.length} websites!`);
cron.schedule(Config.pinger.pingCron, () => {
this.pingWebsites();
});
}
/**
* Ping all websites to update their status.
*/
private async pingWebsites(): Promise<void> {
logger.info(`Pinging websites ${this.websites.length}`);
// ping all websites in parallel
await Promise.all(this.websites.map((website) => website.pingWebsite()));
logger.info("Finished pinging websites!");
}
/**
* Returns the websites.
*
* @returns the websites
*/
public getWebsites(): Website[] {
return this.websites;
}
}