added additional windows support (cpu, cpuCache, cpuCurrentspeed, mem, networkInterfaces, docker)

This commit is contained in:
Sebastian Hildebrandt
2017-06-16 14:27:21 +02:00
parent 87a153136c
commit 92e58b57b8
9 changed files with 425 additions and 311 deletions
+49 -18
View File
@@ -56,10 +56,12 @@ function cpuBrandManufacturer(res) {
return res;
}
function getValue(lines, property) {
function getValue(lines, property, separator) {
separator = separator || ':';
property = property.toLowerCase();
for (let i = 0; i < lines.length; i++) {
if (lines[i].toLowerCase().startsWith(property)) {
const parts = lines[i].split(':');
const parts = lines[i].split(separator);
if (parts.length > 1) {
return parts[1].trim();
}
@@ -155,15 +157,36 @@ function getCpu() {
})
}
if (_windows) {
exec("wmic cpu get name", function (error, stdout) {
exec("wmic cpu get name, description, revision, l2cachesize, l3cachesize, manufacturer, currentclockspeed, maxclockspeed /value", function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
let line = (lines && lines[0]) ? lines[0] : '';
result.brand = line.split('@')[0].trim();
result.speed = line.split('@')[1].trim();
result.speed = parseFloat(result.speed.replace(/GHz+/g, ""));
let lines = stdout.split('\r\n');
let name = getValue(lines, 'name', '=') || '';
result.brand = name.split('@')[0].trim();
result.speed = name.split('@')[1].trim();
result.speed = parseFloat(result.speed.replace(/GHz+/g, "").trim()).toFixed(2);
_cpu_speed = result.speed;
result = cpuBrandManufacturer(result);
result.revision = getValue(lines, 'revision', '=');
result.cache.l2 = getValue(lines, 'l2cachesize', '=');
result.cache.l3 = getValue(lines, 'l3cachesize', '=');
if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2) * 1024}
if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3) * 1024}
result.vendor = getValue(lines, 'manufacturer', '=');
result.speedmax = Math.round(parseFloat(getValue(lines, 'maxclockspeed', '=').replace(/,/g, '.')) / 10.0) / 100;
result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''
let description = getValue(lines, 'description', '=').split(' ');
for (let i = 0; i < description.length; i++) {
if (description[i].toLowerCase().startsWith('family') && (i+1) < description.length && description[i+1]) {
result.family = description[i+1]
}
if (description[i].toLowerCase().startsWith('model') && (i+1) < description.length && description[i+1]) {
result.model = description[i+1]
}
if (description[i].toLowerCase().startsWith('stepping') && (i+1) < description.length && description[i+1]) {
result.stepping = description[i+1]
}
}
}
resolve(result);
})
@@ -209,9 +232,9 @@ function getCpuCurrentSpeedSync() {
}
avgFreq = avgFreq / cpus.length;
return {
min: parseFloat((minFreq / 1000).toFixed(2)),
max: parseFloat((maxFreq / 1000).toFixed(2)),
avg: parseFloat((avgFreq / 1000).toFixed(2))
min: parseFloat(((minFreq + 1) / 1000).toFixed(2)),
max: parseFloat(((maxFreq + 1) / 1000).toFixed(2)),
avg: parseFloat(((avgFreq + 1) / 1000).toFixed(2))
}
} else {
return {
@@ -333,9 +356,9 @@ function cpuTemperature(callback) {
if (result.cores.length) {
result.main = sum / result.cores.length;
}
if (callback) { callback(result) }
resolve(result);
}
if (callback) { callback(result) }
resolve(result);
});
}
});
@@ -397,11 +420,6 @@ function cpuCache(callback) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
let result = {};
if (_linux) {
@@ -452,6 +470,19 @@ function cpuCache(callback) {
resolve(result);
});
}
if (_windows) {
exec("wmic cpu get l2cachesize, l3cachesize /value", function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n');
result.l2 = getValue(lines, 'l2cachesize', '=');
result.l3 = getValue(lines, 'l3cachesize', '=');
if (result.l2) { result.l2 = parseInt(result.l2) * 1024}
if (result.l3) { result.l3 = parseInt(result.l3) * 1024}
}
if (callback) { callback(result) }
resolve(result);
})
}
});
});
}
-21
View File
@@ -51,12 +51,6 @@ function dockerContainers(all, callback) {
let result = [];
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
if (!_docker_socket) {
_docker_socket = new DockerSocket();
}
@@ -226,11 +220,6 @@ function dockerContainerStats(containerID, callback) {
};
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
if (containerID) {
if (!_docker_socket) {
@@ -294,11 +283,6 @@ function dockerContainerProcesses(containerID, callback) {
let result = [];
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
if (containerID) {
if (!_docker_socket) {
@@ -368,11 +352,6 @@ exports.dockerContainerProcesses = dockerContainerProcesses;
function dockerAll(callback) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
dockerContainers(true).then(result => {
if (result && Object.prototype.toString.call(result) === '[object Array]' && result.length > 0) {
let l = result.length;
+5 -3
View File
@@ -1,13 +1,15 @@
'use strict';
const net = require('net');
const isWin = require('os').type() === 'Windows_NT';
const socketPath = isWin ? '//./pipe/docker_engine' : '/var/run/docker.sock';
class DockerSocket {
listContainers(all, callback) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let socket = net.createConnection({path: socketPath});
let alldata = '';
socket.on("connect", () => {
@@ -38,7 +40,7 @@ class DockerSocket {
id = id || '';
if (id) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let socket = net.createConnection({path: socketPath});
let alldata = '';
socket.on("connect", () => {
@@ -72,7 +74,7 @@ class DockerSocket {
id = id || '';
if (id) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let socket = net.createConnection({path: socketPath});
let alldata = '';
socket.on("connect", () => {
+34 -7
View File
@@ -24,6 +24,20 @@ const _darwin = (_platform === 'Darwin');
const _windows = (_platform === 'Windows_NT');
const NOT_SUPPORTED = 'not supported';
function getValue(lines, property, separator) {
separator = separator || ':';
property = property.toLowerCase();
for (let i = 0; i < lines.length; i++) {
if (lines[i].toLowerCase().startsWith(property)) {
const parts = lines[i].split(separator);
if (parts.length > 1) {
return parts[1].trim();
}
}
}
return '';
}
function graphics(callback) {
function parseLinesDarwin(lines) {
@@ -252,13 +266,6 @@ function graphics(callback) {
// function starts here
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) {
callback(NOT_SUPPORTED)
}
reject(error);
}
let result = {
controllers: [],
displays: []
@@ -304,6 +311,26 @@ function graphics(callback) {
})
})
}
if (_windows) {
// https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/03/use-powershell-to-discover-multi-monitor-information/
exec("wmic path win32_VideoController get AdapterCompatibility, AdapterDACType, name, PNPDeviceID, CurrentVerticalResolution, CurrentHorizontalResolution, CurrentNumberOfColors, AdapterRAM, CurrentBitsPerPixel, CurrentRefreshRate, MinRefreshRate, MaxRefreshRate /value", function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n');
result.controllers.push({});
result.displays.push({});
result.controllers[0].model = getValue(lines, 'name', '=');
result.controllers[0].vendor = getValue(lines, 'AdapterCompatibility', '=');
result.controllers[0].bus = getValue(lines, 'PNPDeviceID', '=').startsWith('PCI') ? 'PCI' : '';
result.controllers[0].vram = getValue(lines, 'AdapterRAM', '=');
result.displays[0].resolutionx = getValue(lines, 'CurrentHorizontalResolution', '=');
result.displays[0].resolutiony = getValue(lines, 'CurrentVerticalResolution', '=');
result.displays[0].depth = getValue(lines, 'CurrentBitsPerPixel', '=');
}
resolve(result);
})
}
});
});
}
+1
View File
@@ -82,6 +82,7 @@
// --------------------------------
//
// version date comment
// 3.20.0 2017-06-16 extend windows support (cpu, cpuCache, cpuCurrentspeed, mem, networkInterfaces, docker)
// 3.19.0 2017-06-12 OSX temperature now an optional dependency
// 3.18.0 2017-05-27 extended `cpu` info (vendor, family, model, stepping, revision, cache, speedmin/max)
// 3.17.3 2017-04-29 minor fix (blockDevices data array, Windows)
+22
View File
@@ -150,6 +150,28 @@ module.exports = function (callback) {
});
});
}
if (_windows) {
let swaptotal = 0;
let swapused = 0;
exec("wmic pagefile get AllocatedBaseSize, CurrentUsage", function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
lines.forEach(function (line) {
if (line !== '') {
line = line.trim().split(/\s\s+/);
swaptotal = swaptotal + parseInt(line[0]);
swapused = swapused + parseInt(line[1]);
}
});
}
result.swaptotal = swaptotal * 1024 * 1024;
result.swapused = swapused * 1024 * 1024;
result.swapfree = result.swaptotal - result.swapused;
if (callback) { callback(result) }
resolve(result);
});
}
});
});
};