mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-03-09 15:40:18 +00:00
Started work on hardware inventory support.
This commit is contained in:
parent
7927892855
commit
e684b5ce01
19 changed files with 1087 additions and 15 deletions
208
agents/modules_meshcmd/identifiers.js
Normal file
208
agents/modules_meshcmd/identifiers.js
Normal file
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
Copyright 2019 Intel Corporation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
function trimIdentifiers(val)
|
||||
{
|
||||
for(var v in val)
|
||||
{
|
||||
if (!val[v] || val[v] == 'None' || val[v] == '') { delete val[v]; }
|
||||
}
|
||||
}
|
||||
|
||||
function linux_identifiers()
|
||||
{
|
||||
var identifiers = {};
|
||||
var ret = {};
|
||||
var values = {};
|
||||
if (!require('fs').existsSync('/sys/class/dmi/id')) { throw ('this platform does not have DMI statistics'); }
|
||||
var entries = require('fs').readdirSync('/sys/class/dmi/id');
|
||||
for(var i in entries)
|
||||
{
|
||||
if (require('fs').statSync('/sys/class/dmi/id/' + entries[i]).isFile())
|
||||
{
|
||||
ret[entries[i]] = require('fs').readFileSync('/sys/class/dmi/id/' + entries[i]).toString().trim();
|
||||
|
||||
if (ret[entries[i]] == 'None') { delete ret[entries[i]];}
|
||||
}
|
||||
}
|
||||
identifiers['bios_date'] = ret['bios_date'];
|
||||
identifiers['bios_vendor'] = ret['bios_vendor'];
|
||||
identifiers['bios_version'] = ret['bios_version'];
|
||||
identifiers['board_name'] = ret['board_name'];
|
||||
identifiers['board_serial'] = ret['board_serial'];
|
||||
identifiers['board_vendor'] = ret['board_vendor'];
|
||||
identifiers['board_version'] = ret['board_version'];
|
||||
identifiers['product_uuid'] = ret['product_uuid'];
|
||||
|
||||
values.identifiers = identifiers;
|
||||
values.linux = ret;
|
||||
trimIdentifiers(values.identifiers);
|
||||
return (values);
|
||||
}
|
||||
|
||||
function windows_wmic_results(str)
|
||||
{
|
||||
var lines = str.trim().split('\r\n');
|
||||
var keys = lines[0].split(',');
|
||||
var i, key, keyval;
|
||||
var tokens;
|
||||
var result = [];
|
||||
|
||||
for (i = 1; i < lines.length; ++i)
|
||||
{
|
||||
var obj = {};
|
||||
tokens = lines[i].split(',');
|
||||
for (key = 0; key < keys.length; ++key)
|
||||
{
|
||||
if (tokens[key].trim())
|
||||
{
|
||||
obj[keys[key].trim()] = tokens[key].trim();
|
||||
}
|
||||
}
|
||||
result.push(obj);
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
||||
function windows_identifiers()
|
||||
{
|
||||
var ret = { windows: {}}; values = {}; var items; var i; var item;
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'bios', 'get', '/VALUE']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
var items = child.stdout.str.split('\r\r\n');
|
||||
for(i in items)
|
||||
{
|
||||
item = items[i].split('=');
|
||||
values[item[0]] = item[1];
|
||||
}
|
||||
|
||||
ret['identifiers'] = {};
|
||||
ret['identifiers']['bios_date'] = values['ReleaseDate'];
|
||||
ret['identifiers']['bios_vendor'] = values['Manufacturer'];
|
||||
ret['identifiers']['bios_version'] = values['SMBIOSBIOSVersion'];
|
||||
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'BASEBOARD', 'get', '/VALUE']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
var items = child.stdout.str.split('\r\r\n');
|
||||
for (i in items)
|
||||
{
|
||||
item = items[i].split('=');
|
||||
values[item[0]] = item[1];
|
||||
}
|
||||
ret['identifiers']['board_name'] = values['Product'];
|
||||
ret['identifiers']['board_serial'] = values['SerialNumber'];
|
||||
ret['identifiers']['board_vendor'] = values['Manufacturer'];
|
||||
ret['identifiers']['board_version'] = values['Version'];
|
||||
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'CSProduct', 'get', '/VALUE']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
var items = child.stdout.str.split('\r\r\n');
|
||||
for (i in items)
|
||||
{
|
||||
item = items[i].split('=');
|
||||
values[item[0]] = item[1];
|
||||
}
|
||||
ret['identifiers']['product_uuid'] = values['UUID'];
|
||||
trimIdentifiers(ret.identifiers);
|
||||
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'MEMORYCHIP', 'LIST', '/FORMAT:CSV']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
ret.windows.memory = windows_wmic_results(child.stdout.str);
|
||||
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'OS', 'GET', '/FORMAT:CSV']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
ret.windows.osinfo = windows_wmic_results(child.stdout.str)[0];
|
||||
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'PARTITION', 'LIST', '/FORMAT:CSV']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
ret.windows.partitions = windows_wmic_results(child.stdout.str);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
function macos_identifiers()
|
||||
{
|
||||
var ret = { identifiers: {} };
|
||||
var child;
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('ioreg -d2 -c IOPlatformExpertDevice | grep board-id | awk -F= \'{ split($2, res, "\\""); print res[2]; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
ret.identifiers.board_name = child.stdout.str.trim();
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('ioreg -d2 -c IOPlatformExpertDevice | grep IOPlatformSerialNumber | awk -F= \'{ split($2, res, "\\""); print res[2]; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
ret.identifiers.board_serial = child.stdout.str.trim();
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('ioreg -d2 -c IOPlatformExpertDevice | grep manufacturer | awk -F= \'{ split($2, res, "\\""); print res[2]; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
ret.identifiers.board_vendor = child.stdout.str.trim();
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('ioreg -d2 -c IOPlatformExpertDevice | grep version | awk -F= \'{ split($2, res, "\\""); print res[2]; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
ret.identifiers.board_version = child.stdout.str.trim();
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('ioreg -d2 -c IOPlatformExpertDevice | grep IOPlatformUUID | awk -F= \'{ split($2, res, "\\""); print res[2]; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
ret.identifiers.product_uuid = child.stdout.str.trim();
|
||||
|
||||
trimIdentifiers(ret.identifiers);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
switch(process.platform)
|
||||
{
|
||||
case 'linux':
|
||||
module.exports = { _ObjectID: 'identifiers', get: linux_identifiers };
|
||||
break;
|
||||
case 'win32':
|
||||
module.exports = { _ObjectID: 'identifiers', get: windows_identifiers };
|
||||
break;
|
||||
case 'darwin':
|
||||
module.exports = { _ObjectID: 'identifiers', get: macos_identifiers };
|
||||
break;
|
||||
default:
|
||||
module.exports = { get: function () { throw ('Unsupported Platform'); } };
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// bios_date = BIOS->ReleaseDate
|
||||
// bios_vendor = BIOS->Manufacturer
|
||||
// bios_version = BIOS->SMBIOSBIOSVersion
|
||||
// board_name = BASEBOARD->Product = ioreg/board-id
|
||||
// board_serial = BASEBOARD->SerialNumber = ioreg/serial-number | ioreg/IOPlatformSerialNumber
|
||||
// board_vendor = BASEBOARD->Manufacturer = ioreg/manufacturer
|
||||
// board_version = BASEBOARD->Version
|
||||
|
230
agents/modules_meshcmd/sysinfo.js
Normal file
230
agents/modules_meshcmd/sysinfo.js
Normal file
|
@ -0,0 +1,230 @@
|
|||
/*
|
||||
Copyright 2019 Intel Corporation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const PDH_FMT_LONG = 0x00000100;
|
||||
const PDH_FMT_DOUBLE = 0x00000200;
|
||||
|
||||
var promise = require('promise');
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
var GM = require('_GenericMarshal');
|
||||
GM.kernel32 = GM.CreateNativeProxy('kernel32.dll');
|
||||
GM.kernel32.CreateMethod('GlobalMemoryStatusEx');
|
||||
|
||||
GM.pdh = GM.CreateNativeProxy('pdh.dll');
|
||||
GM.pdh.CreateMethod('PdhAddEnglishCounterA');
|
||||
GM.pdh.CreateMethod('PdhCloseQuery');
|
||||
GM.pdh.CreateMethod('PdhCollectQueryData');
|
||||
GM.pdh.CreateMethod('PdhGetFormattedCounterValue');
|
||||
GM.pdh.CreateMethod('PdhGetFormattedCounterArrayA');
|
||||
GM.pdh.CreateMethod('PdhOpenQueryA');
|
||||
GM.pdh.CreateMethod('PdhRemoveCounter');
|
||||
}
|
||||
|
||||
function windows_cpuUtilization()
|
||||
{
|
||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
p.counter = GM.CreateVariable(16);
|
||||
p.cpu = GM.CreatePointer();
|
||||
p.cpuTotal = GM.CreatePointer();
|
||||
var err = 0;
|
||||
if ((err = GM.pdh.PdhOpenQueryA(0, 0, p.cpu).Val) != 0) { p._rej(err); return; }
|
||||
|
||||
// This gets the CPU Utilization for each proc
|
||||
if ((err = GM.pdh.PdhAddEnglishCounterA(p.cpu.Deref(), GM.CreateVariable('\\Processor(*)\\% Processor Time'), 0, p.cpuTotal).Val) != 0) { p._rej(err); return; }
|
||||
|
||||
if ((err = GM.pdh.PdhCollectQueryData(p.cpu.Deref()).Val != 0)) { p._rej(err); return; }
|
||||
p._timeout = setTimeout(function (po)
|
||||
{
|
||||
var u = { cpus: [] };
|
||||
var bufSize = GM.CreateVariable(4);
|
||||
var itemCount = GM.CreateVariable(4);
|
||||
var buffer, szName, item;
|
||||
var e;
|
||||
if ((e = GM.pdh.PdhCollectQueryData(po.cpu.Deref()).Val != 0)) { po._rej(e); return; }
|
||||
|
||||
if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, 0).Val) == -2147481646)
|
||||
{
|
||||
buffer = GM.CreateVariable(bufSize.toBuffer().readUInt32LE());
|
||||
}
|
||||
else
|
||||
{
|
||||
po._rej(e);
|
||||
return;
|
||||
}
|
||||
if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, buffer).Val) != 0) { po._rej(e); return; }
|
||||
for(var i=0;i<itemCount.toBuffer().readUInt32LE();++i)
|
||||
{
|
||||
item = buffer.Deref(i * 24, 24);
|
||||
szName = item.Deref(0, GM.PointerSize).Deref();
|
||||
if (szName.String == '_Total')
|
||||
{
|
||||
u.total = item.Deref(16, 8).toBuffer().readDoubleLE().toFixed(2);
|
||||
}
|
||||
else
|
||||
{
|
||||
u.cpus[parseInt(szName.String)] = item.Deref(16, 8).toBuffer().readDoubleLE().toFixed(2);
|
||||
}
|
||||
}
|
||||
|
||||
GM.pdh.PdhRemoveCounter(po.cpuTotal.Deref());
|
||||
GM.pdh.PdhCloseQuery(po.cpu.Deref());
|
||||
p._res(u);
|
||||
}, 100, p);
|
||||
|
||||
return (p);
|
||||
}
|
||||
function windows_memUtilization()
|
||||
{
|
||||
var info = GM.CreateVariable(64);
|
||||
info.Deref(0, 4).toBuffer().writeUInt32LE(64);
|
||||
GM.kernel32.GlobalMemoryStatusEx(info);
|
||||
|
||||
var ret =
|
||||
{
|
||||
MemTotal: require('bignum').fromBuffer(info.Deref(8, 8).toBuffer(), { endian: 'little' }),
|
||||
MemFree: require('bignum').fromBuffer(info.Deref(16, 8).toBuffer(), { endian: 'little' })
|
||||
};
|
||||
|
||||
ret.percentFree = ((ret.MemFree.div(require('bignum')('1048576')).toNumber() / ret.MemTotal.div(require('bignum')('1048576')).toNumber()) * 100).toFixed(2);
|
||||
ret.percentConsumed = ((ret.MemTotal.sub(ret.MemFree).div(require('bignum')('1048576')).toNumber() / ret.MemTotal.div(require('bignum')('1048576')).toNumber()) * 100).toFixed(2);
|
||||
ret.MemTotal = ret.MemTotal.toString();
|
||||
ret.MemFree = ret.MemFree.toString();
|
||||
return (ret);
|
||||
}
|
||||
|
||||
function linux_cpuUtilization()
|
||||
{
|
||||
var ret = { cpus: [] };
|
||||
var info = require('fs').readFileSync('/proc/stat');
|
||||
var lines = info.toString().split('\n');
|
||||
var columns;
|
||||
var x, y;
|
||||
var sum, idle, utilization;
|
||||
for (var i in lines)
|
||||
{
|
||||
columns = lines[i].split(' ');
|
||||
if (!columns[0].startsWith('cpu')) { break; }
|
||||
|
||||
x = 0, sum = 0;
|
||||
while (columns[++x] == '');
|
||||
for (y = x; y < columns.length; ++y) { sum += parseInt(columns[y]); }
|
||||
idle = parseInt(columns[3 + x]);
|
||||
utilization = (100 - ((idle / sum) * 100)).toFixed(2);
|
||||
if (!ret.total)
|
||||
{
|
||||
ret.total = utilization;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.cpus.push(utilization);
|
||||
}
|
||||
}
|
||||
|
||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
p._res(ret);
|
||||
return (p);
|
||||
}
|
||||
function linux_memUtilization()
|
||||
{
|
||||
var ret = {};
|
||||
|
||||
var info = require('fs').readFileSync('/proc/meminfo').toString().split('\n');
|
||||
var tokens;
|
||||
for(var i in info)
|
||||
{
|
||||
tokens = info[i].split(' ');
|
||||
switch(tokens[0])
|
||||
{
|
||||
case 'MemTotal:':
|
||||
ret.total = parseInt(tokens[tokens.length - 2]);
|
||||
break;
|
||||
case 'MemFree:':
|
||||
ret.free = parseInt(tokens[tokens.length - 2]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret.percentFree = ((ret.free / ret.total) * 100).toFixed(2);
|
||||
ret.percentConsumed = (((ret.total - ret.free) / ret.total) * 100).toFixed(2);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
function macos_cpuUtilization()
|
||||
{
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write('top -l 1 | grep -E "^CPU"\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.split('\n');
|
||||
if (lines[0].length > 0)
|
||||
{
|
||||
var usage = lines[0].split(':')[1];
|
||||
var bdown = usage.split(',');
|
||||
|
||||
var tot = parseFloat(bdown[0].split('%')[0].trim()) + parseFloat(bdown[1].split('%')[0].trim());
|
||||
ret._res({total: tot, cpus: []});
|
||||
}
|
||||
else
|
||||
{
|
||||
ret._rej('parse error');
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
function macos_memUtilization()
|
||||
{
|
||||
var mem = { };
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write('top -l 1 | grep -E "^Phys"\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.split('\n');
|
||||
if (lines[0].length > 0)
|
||||
{
|
||||
var usage = lines[0].split(':')[1];
|
||||
var bdown = usage.split(',');
|
||||
|
||||
mem.MemTotal = parseInt(bdown[0].trim().split(' ')[0]);
|
||||
mem.MemFree = parseInt(bdown[1].trim().split(' ')[0]);
|
||||
mem.percentFree = ((mem.MemFree / mem.MemTotal) * 100).toFixed(2);
|
||||
mem.percentConsumed = (((mem.MemTotal - mem.MemFree)/ mem.MemTotal) * 100).toFixed(2);
|
||||
return (mem);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw ('Parse Error');
|
||||
}
|
||||
}
|
||||
|
||||
switch(process.platform)
|
||||
{
|
||||
case 'linux':
|
||||
module.exports = { cpuUtilization: linux_cpuUtilization, memUtilization: linux_memUtilization };
|
||||
break;
|
||||
case 'win32':
|
||||
module.exports = { cpuUtilization: windows_cpuUtilization, memUtilization: windows_memUtilization };
|
||||
break;
|
||||
case 'darwin':
|
||||
module.exports = { cpuUtilization: macos_cpuUtilization, memUtilization: macos_memUtilization };
|
||||
break;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue