add website tracking
This commit is contained in:
@ -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();
|
||||
|
@ -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
64
src/website/website.ts
Normal 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())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
47
src/website/websiteManager.ts
Normal file
47
src/website/websiteManager.ts
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user