domotz-remote-pawn
Version:
Domotz Agent
183 lines (144 loc) • 5.67 kB
JavaScript
/**
* This file is part of Domotz Agent.
* Copyright (C) 2020 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Andrea Azzara <a.azzara@domotz.com> on 28/02/20.
*/
var q = require('q');
const os = require('os');
const process = require('process');
const DIAGNOSTIC_SAMPLING_INTERVAL = 300;
function diagnostic(resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var cpu = 0;
var mem = 0;
var actualDiscoveryCounter = 0;
var discoveryIterationCounter = 0;
var missedDiscoveryCounter = 0;
function incActualDiscoveryCounter() {
actualDiscoveryCounter++;
}
function incDiscoveryIterationCounter() {
discoveryIterationCounter++;
}
function getDiscoveryIterationCounter() {
return discoveryIterationCounter;
}
function getActualDiscoveryCounter() {
return actualDiscoveryCounter;
}
function incMissedDiscoveryCounter() {
missedDiscoveryCounter++;
}
function resetMissedDiscoveryCounter() {
missedDiscoveryCounter = 0;
}
function getMissedDiscoveryCounter() {
return missedDiscoveryCounter;
}
//Create function to get CPU information
function cpuAverage() {
//Initialise sum of idle and time of cores and fetch CPU info
var totalIdle = 0,
totalTick = 0;
var cpus = os.cpus();
//Loop through CPU cores
for (var i = 0, len = cpus.length; i < len; i++) {
//Select CPU core
var cpu = cpus[i];
//Total up the time in the cores tick
for (var type in cpu.times) {
totalTick += cpu.times[type];
}
//Total up the idle time of the core
totalIdle += cpu.times.idle;
}
//Return the average Idle and Tick times
return { idle: totalIdle / cpus.length, total: totalTick / cpus.length };
}
function computeCPUMemStats() {
var deferred = q.defer();
//Grab first CPU Measure
var startMeasure = cpuAverage();
//Set delay for second Measure
setTimeout(function () {
try {
//Grab second Measure
var endMeasure = cpuAverage();
//Calculate the difference in idle and total time between the measures
var idleDifference = endMeasure.idle - startMeasure.idle;
var totalDifference = endMeasure.total - startMeasure.total;
myConsole.verbose('startMeasure idle %d ', startMeasure.idle);
myConsole.verbose('startMeasure total %d ', startMeasure.total);
myConsole.verbose('endMeasure idle %d ', endMeasure.idle);
myConsole.verbose('endMeasure total %d ', endMeasure.total);
myConsole.verbose('idleDifference %d ', idleDifference);
myConsole.verbose('totalDifference %d ', totalDifference);
myConsole.verbose('ratio ', idleDifference / totalDifference);
//Calculate the average percentage CPU usage
var percentageCPU = 100 - Math.round((100 * idleDifference) / totalDifference);
//Output result to console
myConsole.debug(percentageCPU + '% CPU Usage.');
const used = Math.ceil(process.memoryUsage().heapUsed / (1024 * 1024));
myConsole.debug(used + ' MEM Usage (Mb).');
deferred.resolve({ cpu: percentageCPU, mem: used });
cpu = percentageCPU;
mem = used;
} catch (err) {
deferred.reject(err);
}
}, DIAGNOSTIC_SAMPLING_INTERVAL);
return deferred.promise;
}
var getCPU = function () {
return cpu;
};
var getMem = function () {
return mem;
};
var getUptime = function () {
return Math.floor(process.uptime());
};
function getLoadAvg() {
// The load average is a UNIX-only concept, on Windows it will always return [0, 0, 0]
try {
return os.loadavg().map(function (e) {
return e.toFixed(2);
});
} catch (e) {
return '';
}
}
function getNumberOfCPUs() {
return os.cpus().length;
}
return {
incActualDiscoveryCounter: incActualDiscoveryCounter,
incDiscoveryIterationCounter: incDiscoveryIterationCounter,
getDiscoveryIterationCounter: getDiscoveryIterationCounter,
getActualDiscoveryCounter: getActualDiscoveryCounter,
incMissedDiscoveryCounter: incMissedDiscoveryCounter,
getMissedDiscoveryCounter: getMissedDiscoveryCounter,
resetMissedDiscoveryCounter: resetMissedDiscoveryCounter,
computeCPUMemStats: computeCPUMemStats,
getCPU: getCPU,
getMem: getMem,
getUptime: getUptime,
getLoadAvg: getLoadAvg,
getNumberOfCPUs: getNumberOfCPUs,
};
}
module.exports.diagnostic = diagnostic;