sysiddr5 1.0.1-beta-3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sysiddr5 might be problematic. Click here for more details.

package/lib/index.js ADDED
@@ -0,0 +1,504 @@
1
+ 'use strict';
2
+ // @ts-check
3
+ // ==================================================================================
4
+ // index.js
5
+ // ----------------------------------------------------------------------------------
6
+ // Description: System Information - library
7
+ // for Node.js
8
+ // Copyright: (c) 2014 - 2023
9
+ // Author: Sebastian Hildebrandt
10
+ // ----------------------------------------------------------------------------------
11
+ // Contributors: Guillaume Legrain (https://github.com/glegrain)
12
+ // Riccardo Novaglia (https://github.com/richy24)
13
+ // Quentin Busuttil (https://github.com/Buzut)
14
+ // Lapsio (https://github.com/lapsio)
15
+ // csy (https://github.com/csy1983)
16
+ // ----------------------------------------------------------------------------------
17
+ // License: MIT
18
+ // ==================================================================================
19
+
20
+ // ----------------------------------------------------------------------------------
21
+ // Dependencies
22
+ // ----------------------------------------------------------------------------------
23
+
24
+ const lib_version = require('../package.json').version;
25
+ const util = require('./util');
26
+ const system = require('./system');
27
+ const osInfo = require('./osinfo');
28
+ const cpu = require('./cpu');
29
+ const memory = require('./memory');
30
+ const battery = require('./battery');
31
+ const graphics = require('./graphics');
32
+ const filesystem = require('./filesystem');
33
+ const network = require('./network');
34
+ const wifi = require('./wifi');
35
+ const processes = require('./processes');
36
+ const users = require('./users');
37
+ const internet = require('./internet');
38
+ const docker = require('./docker');
39
+ const vbox = require('./virtualbox');
40
+ const printer = require('./printer');
41
+ const usb = require('./usb');
42
+ const audio = require('./audio');
43
+ const bluetooth = require('./bluetooth');
44
+
45
+ let _platform = process.platform;
46
+ const _windows = (_platform === 'win32');
47
+ const _freebsd = (_platform === 'freebsd');
48
+ const _openbsd = (_platform === 'openbsd');
49
+ const _netbsd = (_platform === 'netbsd');
50
+ const _sunos = (_platform === 'sunos');
51
+
52
+ // ----------------------------------------------------------------------------------
53
+ // init
54
+ // ----------------------------------------------------------------------------------
55
+
56
+ if (_windows) {
57
+ util.getCodepage();
58
+ }
59
+
60
+ // ----------------------------------------------------------------------------------
61
+ // General
62
+ // ----------------------------------------------------------------------------------
63
+
64
+ function version() {
65
+ return lib_version;
66
+ }
67
+
68
+ // ----------------------------------------------------------------------------------
69
+ // Get static and dynamic data (all)
70
+ // ----------------------------------------------------------------------------------
71
+
72
+ // --------------------------
73
+ // get static data - they should not change until restarted
74
+
75
+ function getStaticData(callback) {
76
+
77
+ return new Promise((resolve) => {
78
+ process.nextTick(() => {
79
+
80
+ let data = {};
81
+
82
+ data.version = version();
83
+
84
+ Promise.all([
85
+ system.system(),
86
+ system.bios(),
87
+ system.baseboard(),
88
+ system.chassis(),
89
+ osInfo.osInfo(),
90
+ osInfo.uuid(),
91
+ osInfo.versions(),
92
+ cpu.cpu(),
93
+ cpu.cpuFlags(),
94
+ graphics.graphics(),
95
+ network.networkInterfaces(),
96
+ memory.memLayout(),
97
+ filesystem.diskLayout()
98
+ ]).then((res) => {
99
+ data.system = res[0];
100
+ data.bios = res[1];
101
+ data.baseboard = res[2];
102
+ data.chassis = res[3];
103
+ data.os = res[4];
104
+ data.uuid = res[5];
105
+ data.versions = res[6];
106
+ data.cpu = res[7];
107
+ data.cpu.flags = res[8];
108
+ data.graphics = res[9];
109
+ data.net = res[10];
110
+ data.memLayout = res[11];
111
+ data.diskLayout = res[12];
112
+ if (callback) { callback(data); }
113
+ resolve(data);
114
+ });
115
+ });
116
+ });
117
+ }
118
+
119
+
120
+ // --------------------------
121
+ // get all dynamic data - e.g. for monitoring agents
122
+ // may take some seconds to get all data
123
+ // --------------------------
124
+ // 2 additional parameters needed
125
+ // - srv: comma separated list of services to monitor e.g. "mysql, apache, postgresql"
126
+ // - iface: define network interface for which you like to monitor network speed e.g. "eth0"
127
+
128
+ function getDynamicData(srv, iface, callback) {
129
+
130
+ if (util.isFunction(iface)) {
131
+ callback = iface;
132
+ iface = '';
133
+ }
134
+ if (util.isFunction(srv)) {
135
+ callback = srv;
136
+ srv = '';
137
+ }
138
+
139
+ return new Promise((resolve) => {
140
+ process.nextTick(() => {
141
+
142
+ iface = iface || network.getDefaultNetworkInterface();
143
+ srv = srv || '';
144
+
145
+ // use closure to track ƒ completion
146
+ let functionProcessed = (function () {
147
+ let totalFunctions = 15;
148
+ if (_windows) { totalFunctions = 13; }
149
+ if (_freebsd || _openbsd || _netbsd) { totalFunctions = 11; }
150
+ if (_sunos) { totalFunctions = 6; }
151
+
152
+ return function () {
153
+ if (--totalFunctions === 0) {
154
+ if (callback) {
155
+ callback(data);
156
+ }
157
+ resolve(data);
158
+ }
159
+ };
160
+ })();
161
+
162
+ let data = {};
163
+
164
+ // get time
165
+ data.time = osInfo.time();
166
+
167
+ /**
168
+ * @namespace
169
+ * @property {Object} versions
170
+ * @property {string} versions.node
171
+ * @property {string} versions.v8
172
+ */
173
+ data.node = process.versions.node;
174
+ data.v8 = process.versions.v8;
175
+
176
+ cpu.cpuCurrentSpeed().then((res) => {
177
+ data.cpuCurrentSpeed = res;
178
+ functionProcessed();
179
+ });
180
+
181
+ users.users().then((res) => {
182
+ data.users = res;
183
+ functionProcessed();
184
+ });
185
+
186
+ processes.processes().then((res) => {
187
+ data.processes = res;
188
+ functionProcessed();
189
+ });
190
+
191
+ cpu.currentLoad().then((res) => {
192
+ data.currentLoad = res;
193
+ functionProcessed();
194
+ });
195
+
196
+ if (!_sunos) {
197
+ cpu.cpuTemperature().then((res) => {
198
+ data.temp = res;
199
+ functionProcessed();
200
+ });
201
+ }
202
+
203
+ if (!_openbsd && !_freebsd && !_netbsd && !_sunos) {
204
+ network.networkStats(iface).then((res) => {
205
+ data.networkStats = res;
206
+ functionProcessed();
207
+ });
208
+ }
209
+
210
+ if (!_sunos) {
211
+ network.networkConnections().then((res) => {
212
+ data.networkConnections = res;
213
+ functionProcessed();
214
+ });
215
+ }
216
+
217
+ memory.mem().then((res) => {
218
+ data.mem = res;
219
+ functionProcessed();
220
+ });
221
+
222
+ if (!_sunos) {
223
+ battery().then((res) => {
224
+ data.battery = res;
225
+ functionProcessed();
226
+ });
227
+ }
228
+
229
+ if (!_sunos) {
230
+ processes.services(srv).then((res) => {
231
+ data.services = res;
232
+ functionProcessed();
233
+ });
234
+ }
235
+
236
+ if (!_sunos) {
237
+ filesystem.fsSize().then((res) => {
238
+ data.fsSize = res;
239
+ functionProcessed();
240
+ });
241
+ }
242
+
243
+ if (!_windows && !_openbsd && !_freebsd && !_netbsd && !_sunos) {
244
+ filesystem.fsStats().then((res) => {
245
+ data.fsStats = res;
246
+ functionProcessed();
247
+ });
248
+ }
249
+
250
+ if (!_windows && !_openbsd && !_freebsd && !_netbsd && !_sunos) {
251
+ filesystem.disksIO().then((res) => {
252
+ data.disksIO = res;
253
+ functionProcessed();
254
+ });
255
+ }
256
+
257
+ if (!_openbsd && !_freebsd && !_netbsd && !_sunos) {
258
+ wifi.wifiNetworks().then((res) => {
259
+ data.wifiNetworks = res;
260
+ functionProcessed();
261
+ });
262
+ }
263
+
264
+ internet.inetLatency().then((res) => {
265
+ data.inetLatency = res;
266
+ functionProcessed();
267
+ });
268
+ });
269
+ });
270
+ }
271
+
272
+ // --------------------------
273
+ // get all data at once
274
+ // --------------------------
275
+ // 2 additional parameters needed
276
+ // - srv: comma separated list of services to monitor e.g. "mysql, apache, postgresql"
277
+ // - iface: define network interface for which you like to monitor network speed e.g. "eth0"
278
+
279
+ function getAllData(srv, iface, callback) {
280
+
281
+ return new Promise((resolve) => {
282
+ process.nextTick(() => {
283
+ let data = {};
284
+
285
+ if (iface && util.isFunction(iface) && !callback) {
286
+ callback = iface;
287
+ iface = '';
288
+ }
289
+
290
+ if (srv && util.isFunction(srv) && !iface && !callback) {
291
+ callback = srv;
292
+ srv = '';
293
+ iface = '';
294
+ }
295
+
296
+ getStaticData().then((res) => {
297
+ data = res;
298
+ getDynamicData(srv, iface).then((res) => {
299
+ for (let key in res) {
300
+ if ({}.hasOwnProperty.call(res, key)) {
301
+ data[key] = res[key];
302
+ }
303
+ }
304
+ if (callback) { callback(data); }
305
+ resolve(data);
306
+ });
307
+ });
308
+ });
309
+ });
310
+ }
311
+
312
+ function get(valueObject, callback) {
313
+ return new Promise((resolve) => {
314
+ process.nextTick(() => {
315
+ const allPromises = Object.keys(valueObject)
316
+ .filter(func => ({}.hasOwnProperty.call(exports, func)))
317
+ .map(func => {
318
+ const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')'));
319
+ let funcWithoutParams = func.indexOf(')') >= 0 ? func.split(')')[1].trim() : func;
320
+ funcWithoutParams = func.indexOf('|') >= 0 ? func.split('|')[0].trim() : funcWithoutParams;
321
+ if (params) {
322
+ return exports[funcWithoutParams](params);
323
+ } else {
324
+ return exports[funcWithoutParams]('');
325
+ }
326
+ });
327
+
328
+ Promise.all(allPromises).then((data) => {
329
+ const result = {};
330
+ let i = 0;
331
+ for (let key in valueObject) {
332
+ if ({}.hasOwnProperty.call(valueObject, key) && {}.hasOwnProperty.call(exports, key) && data.length > i) {
333
+ if (valueObject[key] === '*' || valueObject[key] === 'all') {
334
+ result[key] = data[i];
335
+ } else {
336
+ let keys = valueObject[key];
337
+ let filter = '';
338
+ let filterParts = [];
339
+ // remove params
340
+ if (keys.indexOf(')') >= 0) {
341
+ keys = keys.split(')')[1].trim();
342
+ }
343
+ // extract filter and remove it from keys
344
+ if (keys.indexOf('|') >= 0) {
345
+ filter = keys.split('|')[1].trim();
346
+ filterParts = filter.split(':');
347
+
348
+ keys = keys.split('|')[0].trim();
349
+ }
350
+ keys = keys.replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
351
+ if (data[i]) {
352
+ if (Array.isArray(data[i])) {
353
+ // result is in an array, go through all elements of array and pick only the right ones
354
+ const partialArray = [];
355
+ data[i].forEach(element => {
356
+ let partialRes = {};
357
+ if (keys.length === 1 && (keys[0] === '*' || keys[0] === 'all')) {
358
+ partialRes = element;
359
+ } else {
360
+ keys.forEach(k => {
361
+ if ({}.hasOwnProperty.call(element, k)) {
362
+ partialRes[k] = element[k];
363
+ }
364
+ });
365
+ }
366
+ // if there is a filter, then just take those elements
367
+ if (filter && filterParts.length === 2) {
368
+ if ({}.hasOwnProperty.call(partialRes, filterParts[0].trim())) {
369
+ const val = partialRes[filterParts[0].trim()];
370
+ if (typeof val == 'number') {
371
+ if (val === parseFloat(filterParts[1].trim())) {
372
+ partialArray.push(partialRes);
373
+ }
374
+ } else if (typeof val == 'string') {
375
+ if (val.toLowerCase() === filterParts[1].trim().toLowerCase()) {
376
+ partialArray.push(partialRes);
377
+ }
378
+ }
379
+ }
380
+ } else {
381
+ partialArray.push(partialRes);
382
+ }
383
+
384
+ });
385
+ result[key] = partialArray;
386
+ } else {
387
+ const partialRes = {};
388
+ keys.forEach(k => {
389
+ if ({}.hasOwnProperty.call(data[i], k)) {
390
+ partialRes[k] = data[i][k];
391
+ }
392
+ });
393
+ result[key] = partialRes;
394
+ }
395
+ } else {
396
+ result[key] = {};
397
+ }
398
+ }
399
+ i++;
400
+ }
401
+ }
402
+ if (callback) { callback(result); }
403
+ resolve(result);
404
+ });
405
+ });
406
+ });
407
+ }
408
+
409
+ function observe(valueObject, interval, callback) {
410
+ let _data = null;
411
+
412
+ const result = setInterval(() => {
413
+ get(valueObject).then((data) => {
414
+ if (JSON.stringify(_data) !== JSON.stringify(data)) {
415
+ _data = Object.assign({}, data);
416
+ callback(data);
417
+ }
418
+ });
419
+ }, interval);
420
+ return result;
421
+ }
422
+
423
+ // ----------------------------------------------------------------------------------
424
+ // export all libs
425
+ // ----------------------------------------------------------------------------------
426
+
427
+ exports.version = version;
428
+ exports.system = system.system;
429
+ exports.bios = system.bios;
430
+ exports.baseboard = system.baseboard;
431
+ exports.chassis = system.chassis;
432
+
433
+ exports.time = osInfo.time;
434
+ exports.osInfo = osInfo.osInfo;
435
+ exports.versions = osInfo.versions;
436
+ exports.shell = osInfo.shell;
437
+ exports.uuid = osInfo.uuid;
438
+
439
+ exports.cpu = cpu.cpu;
440
+ exports.cpuFlags = cpu.cpuFlags;
441
+ exports.cpuCache = cpu.cpuCache;
442
+ exports.cpuCurrentSpeed = cpu.cpuCurrentSpeed;
443
+ exports.cpuTemperature = cpu.cpuTemperature;
444
+ exports.currentLoad = cpu.currentLoad;
445
+ exports.fullLoad = cpu.fullLoad;
446
+
447
+ exports.mem = memory.mem;
448
+ exports.memLayout = memory.memLayout;
449
+
450
+ exports.battery = battery;
451
+
452
+ exports.graphics = graphics.graphics;
453
+
454
+ exports.fsSize = filesystem.fsSize;
455
+ exports.fsOpenFiles = filesystem.fsOpenFiles;
456
+ exports.blockDevices = filesystem.blockDevices;
457
+ exports.fsStats = filesystem.fsStats;
458
+ exports.disksIO = filesystem.disksIO;
459
+ exports.diskLayout = filesystem.diskLayout;
460
+
461
+ exports.networkInterfaceDefault = network.networkInterfaceDefault;
462
+ exports.networkGatewayDefault = network.networkGatewayDefault;
463
+ exports.networkInterfaces = network.networkInterfaces;
464
+ exports.networkStats = network.networkStats;
465
+ exports.networkConnections = network.networkConnections;
466
+
467
+ exports.wifiNetworks = wifi.wifiNetworks;
468
+ exports.wifiInterfaces = wifi.wifiInterfaces;
469
+ exports.wifiConnections = wifi.wifiConnections;
470
+
471
+ exports.services = processes.services;
472
+ exports.processes = processes.processes;
473
+ exports.processLoad = processes.processLoad;
474
+
475
+ exports.users = users.users;
476
+
477
+ exports.inetChecksite = internet.inetChecksite;
478
+ exports.inetLatency = internet.inetLatency;
479
+
480
+ exports.dockerInfo = docker.dockerInfo;
481
+ exports.dockerImages = docker.dockerImages;
482
+ exports.dockerContainers = docker.dockerContainers;
483
+ exports.dockerContainerStats = docker.dockerContainerStats;
484
+ exports.dockerContainerProcesses = docker.dockerContainerProcesses;
485
+ exports.dockerVolumes = docker.dockerVolumes;
486
+ exports.dockerAll = docker.dockerAll;
487
+
488
+ exports.vboxInfo = vbox.vboxInfo;
489
+
490
+ exports.printer = printer.printer;
491
+
492
+ exports.usb = usb.usb;
493
+
494
+ exports.audio = audio.audio;
495
+ exports.bluetoothDevices = bluetooth.bluetoothDevices;
496
+
497
+ exports.getStaticData = getStaticData;
498
+ exports.getDynamicData = getDynamicData;
499
+ exports.getAllData = getAllData;
500
+ exports.get = get;
501
+ exports.observe = observe;
502
+
503
+ exports.powerShellStart = util.powerShellStart;
504
+ exports.powerShellRelease = util.powerShellRelease;