From 960bc2fad91c721a314e867b2af1c39512d8d961 Mon Sep 17 00:00:00 2001 From: Neon Date: Tue, 11 Mar 2025 10:49:47 -0400 Subject: [PATCH] feat: test alert cpu --- package.json | 5 +++-- src/index.ts | 6 +++--- src/modules/alertcpu.ts | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/modules/alertcpu.ts diff --git a/package.json b/package.json index 6219f5c..152bfa6 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "smee-client": "^2.0.3", "socket.io": "^4.8.1", "stegcloak": "^1.1.1", - "ws": "^8.18.0" + "ws": "^8.18.0", + "pidusage": "latest" }, "optionalDependencies": {}, "devDependencies": { @@ -44,4 +45,4 @@ "ts-node": "^10.9.2", "typescript": "^5.5.4" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 4eec517..f12e874 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ import * as utils from "./modules/index"; import { resetSpotifyCache } from "./modules/howWasYourDay"; import { PrivateDNS } from "./modules/nextdns"; import { attachDB } from "./modules/projectWaterydo"; - +import monitorMemCpu from "./modules/alertcpu" import { watchForWhenIUseHacktime } from "./modules/hacktime"; import { EncryptedJsonDb } from "./modules/encrypted-db"; @@ -111,8 +111,8 @@ app.start(process.env.PORT || 3000).then(async (d) => { text: `Starting Slack Bot :D`, }); init(app); - PrivateDNS(app, process.env.MY_NEXTDNS, `C07LT7XS28Z`); - PrivateDNS(app, process.env.HACKCLUB_NEXTDNS, `C07TWGJKK98`); + PrivateDNS(app, process.env.MY_NEXTDNS, `C07LT7XS28Z`); + monitorMemCpu() // grab spotify cache from db resetSpotifyCache(app); app.client.chat.postMessage({ diff --git a/src/modules/alertcpu.ts b/src/modules/alertcpu.ts new file mode 100644 index 0000000..6caa143 --- /dev/null +++ b/src/modules/alertcpu.ts @@ -0,0 +1,24 @@ +import { ModifiedApp } from "./slackapp"; +import pidusage from "pidusage" +const THRESHOLD_CPU = 80; // In percentage +const THRESHOLD_MEM = 700 * 1024 * 1024; // 700MB +export default function monitorMemCpu(app: ModifiedApp) { + setInterval(() => { + pidusage(process.pid, (err, stats) => { + if (stats.cpu > THRESHOLD_CPU) { + console.warn(`⚠️ High CPU: ${stats.cpu}%`); + app.client.chat.postMessage({ + channel: `C07LEEB50KD`, + text: `⚠️ High CPU: ${stats.cpu}%` + }) + } + if (stats.memory > THRESHOLD_MEM) { + console.warn(`⚠️ High Memory: ${(stats.memory / 1024 / 1024).toFixed(2)} MB`); + app.client.chat.postMessage({ + channel: `C07LEEB50KD`, + text: `⚠️ High Memory: ${(stats.memory / 1024 / 1024).toFixed(2)} MB` + }) + } + }); + }, 3000); +} \ No newline at end of file