Reputation: 2922
I am looking for a way to calculate like Windows does, the % of activity of a disk, not the space used. I'm looking to be able to send alerts from my application when the activity is getting too high for prolonged periods of time, indicating the server is overloaded. I am unable to find any plugin that can do this, only plugins that tell disk space used.
--Edit Thanks mihai for the answer. Here is the function I wrote to use that in case anyone else needs it.
This first version starts and stops the task each time.
function getDiskTimePercent() {
return new Promise(function (resolve, reject) {
exec('typeperf "\\LogicalDisk(C:)\\% Disk Time" -SC 1', function (error, stdout, stderr) {
if (error) {
reject(error);
} else {
const lines = stdout.split('\r\n');
const values = lines[2].split(",");
const result = Math.round(values.pop().replace(/"/g, ''), 0);
resolve(result);
}
});
});
}
This next version is more suitable for websites with constant timed updates. It leaves the process running for 50 samples then reboots the process. This prevents the process running forever if node crashes out.
let diskTrackerProcess;
let diskTrackerCurrent = 0;
function getDiskTimePercent() {
if (!diskTrackerProcess) {
diskTrackerProcess = exec('typeperf.exe "\\LogicalDisk(C:)\\% Disk Time" -SC 50');
diskTrackerProcess.stdout.on('data', function (data) {
const stdout = data.toString();
const lines = stdout.split('\r\n');
const values = lines[0].split(",");
const result = Math.round(values.pop().replace(/"/g, ''), 0);
if (!isNaN(result)) {
diskTrackerCurrent = result;
}
});
diskTrackerProcess.on('exit', function (code) {
diskTrackerProcess = null;
});
return diskTrackerCurrent;
} else {
return diskTrackerCurrent;
}
}
Upvotes: 1
Views: 880
Reputation: 38543
Node itself is not a good choice for this but you can always call external programs with it.
I would look into typeperf, a native Windows command line utility.
I was able to get the activity on my disk with:
typeperf "\\MyComputerName\LogicalDisk(C:)\% Disk Time"
Change MyComputerName
to the name of your machine and C:
to the disk you want to monitor.
Output:
"10/30/2018 22:25:42.132","0.000000"
"10/30/2018 22:25:43.139","0.000000"
"10/30/2018 22:25:44.211","0.000000"
"10/30/2018 22:25:45.220","0.000000"
"10/30/2018 22:25:46.225","0.000000"
"10/30/2018 22:25:47.232","0.081097"
"10/30/2018 22:25:48.237","0.000000"
"10/30/2018 22:25:49.251","0.197538"
"10/30/2018 22:25:50.321","60.390202"
"10/30/2018 22:25:51.333","97.860753"
"10/30/2018 22:25:52.337","11.451819"
"10/30/2018 22:25:53.351","34.524372"
The last number should display the disk activity. If it's greater then 0, it means there's something going on on your disk. The command seems to require admin rights or some elevated privilages, which may or may not be a problem in your case.
You can easily invoke this command from node (using child_process), and parse the output.
Upvotes: 1