summary refs log tree commit diff
path: root/slowcord
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-29 16:17:53 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-29 16:58:49 +1000
commit9cf9fcaa6696daeeca98610f39a45b8836a2f6f8 (patch)
treeda838a833f1ae4621a298fb282acfc86fa08d313 /slowcord
parentAdded nginx configs (diff)
downloadserver-9cf9fcaa6696daeeca98610f39a45b8836a2f6f8.tar.xz
/-/monitorz endpoint for system perf mon
Diffstat (limited to 'slowcord')
-rw-r--r--slowcord/status/src/index.ts31
1 files changed, 28 insertions, 3 deletions
diff --git a/slowcord/status/src/index.ts b/slowcord/status/src/index.ts
index 11298699..82a15d1c 100644
--- a/slowcord/status/src/index.ts
+++ b/slowcord/status/src/index.ts
@@ -2,6 +2,7 @@ import "dotenv/config";
 import https from "https";
 import Fosscord from "fosscord-gopnik";
 import mysql from "mysql2";
+import fetch from "node-fetch";
 
 const dbConn = mysql.createConnection(process.env.DATABASE as string);
 const executePromise = (sql: string, args: any[]) => new Promise((resolve, reject) => dbConn.execute(sql, args, (err, res) => { if (err) reject(err); else resolve(res); }));
@@ -31,11 +32,11 @@ client.on("ready", () => {
 	console.log(`Ready on gateway as ${client.user!.tag}`);
 });
 
-client.on("error", (error) => {
+client.on("error", (error: any) => {
 	console.log(`Gateway error`, error);
 });
 
-client.on("warn", (msg) => {
+client.on("warn", (msg: any) => {
 	console.log(`Gateway warning:`, msg);
 });
 
@@ -43,13 +44,22 @@ const savePerf = async (time: number, name: string, error?: string | Error) => {
 	if (error && typeof error != "string") error = error.message;
 	try {
 		await executePromise("INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)", [time ?? 0, name, new Date(), error ?? null]);
-		await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]);
+		// await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]);
 	}
 	catch (e) {
 		console.error(e);
 	}
 };
 
+const saveSystemUsage = async (load: number, procUptime: number, sysUptime: number, ram: number) => {
+	try {
+		await executePromise("INSERT INTO monitor (time, cpu, procUp, sysUp, ram) VALUES (?, ?, ?, ?, ?)", [new Date(), load, procUptime, sysUptime, ram]);
+	}
+	catch (e) {
+		console.error(e);
+	}
+}
+
 const makeTimedRequest = (path: string, body?: object): Promise<number> => new Promise((resolve, reject) => {
 	const opts = {
 		hostname: new URL(path).hostname,
@@ -104,6 +114,13 @@ const measureApi = async (name: string, path: string, body?: object) => {
 	await savePerf(time, name, error);
 };
 
+interface monitorzSchema {
+	load: number[];
+	procUptime: number;
+	sysUptime: number;
+	memPercent: number;
+}
+
 const app = async () => {
 	await new Promise((resolve) => dbConn.connect(resolve));
 	console.log("Connected to db");
@@ -117,6 +134,14 @@ const app = async () => {
 		await measureApi("login", `${instance.app}/login`);
 		// await gatewayMeasure("websocketPing");
 
+		const res = await fetch(`${instance.api}/-/monitorz`, {
+			headers: {
+				Authorization: process.env.INSTANCE_TOKEN as string,
+			}
+		});
+		const json = await res.json() as monitorzSchema;
+		await saveSystemUsage(json.load[2], json.procUptime, json.sysUptime, json.memPercent);
+
 		setTimeout(doMeasurements, parseInt(process.env.MEASURE_INTERVAL as string));
 	};