Making system status reporting better

This commit is contained in:
Raven Scott 2023-04-17 03:17:15 +02:00
parent 4e69329501
commit fffef84119
2 changed files with 25 additions and 12 deletions

View File

@ -3,7 +3,9 @@ import fetch from 'node-fetch';
import { emptyResponses } from './assets/emptyMessages.js';
import { resetResponses, userResetMessages } from './assets/resetMessages.js';
import { errorMessages, busyResponses } from './assets/errorMessages.js';
import cpuStat from 'cpu-stat';
import os from 'os';
import {
Client,
GatewayIntentBits,
@ -181,18 +183,28 @@ async function generateResponse(conversation, message) {
// define a function that shows the system load percentage and updates the message
const showSystemLoad = async () => {
const systemLoad = os.loadavg()[0] / os.cpus().length * 100;
const freeMemory = os.freemem() / 1024 / 1024;
const totalMemory = os.totalmem() / 1024 / 1024;
const usedMemory = totalMemory - freeMemory;
const messageData = `Please wait, I am thinking...\nSystem Load: ${systemLoad.toFixed(2)}%\nMemory Usage: ${usedMemory.toFixed(2)} MB / ${totalMemory.toFixed(2)} MB`;
// if the message object doesn't exist, create it
if (!botMessage) {
botMessage = await message.channel.send(messageData);
} else {
botMessage.edit(messageData); // otherwise, update the message
}
cpuStat.usagePercent(function(err, percent, seconds) {
if (err) {
return console.log(err);
}
const systemLoad = percent //the percentage cpu usage over all cores
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
const usedMemory = totalMemory - freeMemory;
const messageData = `Please wait, I am thinking...\nSystem Load Average: ${systemLoad.toFixed(2)}%\nMemory Usage: ${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB`;
// if the message object doesn't exist, create it
if (!botMessage) {
(async () => {
botMessage = await message.channel.send(messageData);
})()
} else {
botMessage.edit(messageData); // otherwise, update the message
}
})
};
// call the function initially

View File

@ -13,6 +13,7 @@
"discord.js": "^14.9.0",
"dotenv": "^16.0.3",
"node-fetch": "^3.3.1",
"os": "^0.1.2"
"os": "^0.1.2",
"cpu-stat": "^2.0.1"
}
}