Get Express Package CPU, RAM and Disk to display for my Discord Bot


#1

I’m trying to make a bot info command which will display the project’s cpu ram and disk space. How could I get this information?


#2

Hi @Fyrlex!

You can read these files:

/sys/fs/cgroup/memory/memory.soft_limit_in_bytes
/sys/fs/cgroup/memory/memory.stat
/sys/fs/cgroup/cpu/cpu.cfs_quota_us
/sys/fs/cgroup/cpu/cpu.cfs_period_us
/sys/fs/cgroup/cpu/cpuacct.usage

For the values you see in the Status pane, memory usage is the total_rss line from/sys/fs/cgroup/memory/memory.stat minus Node’s process.memoryUsage().rss. Disk space comes from the check() method from the NPM package diskusage.

The percentages we display in the status bar are calculations over sliding averages of historical data, and we tweak these a little periodically to provide better info. You’re unlikely to be able to achieve an exact match for those values at any given point.

Hope this helps!


#4

RAM Usage: process.memoryUsage().heapUsed / 1024 / 1024;

You can get most of the things from memoryUsage() method, it returns:

{
  rss: 1000,
  heapTotal: 1000,
  heapUsed: 1000,
  external: 1000
}

#5

Note that that only reports on the current process and heap usage, so it’s only a portion of the full memory usage within the project.


#6

What about for disk and cpu usage?


#7

Is there something else you’re looking for that wasn’t in my initial reply?


#8

I didn’t completely understand what you had written. I was looking for something like what @Bolt had posted.

What I do need is the maximum CPU (for max) and was wondering what the equation would be for it. Bolt stated above how to get the ram but it’s different for the CPU.

Currently I have this:

let cpu = Math.round(process.cpuUsage().system)
let cpupercent = Math.round((cpu * max) / 1000) / 10;

let ram = Math.round(process.memoryUsage().heapUsed / 1024 / 1024 * 10) / 10;
let rampercent = Math.round((ram / 512) * 1000) / 10;