Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // index.js
  5. // ----------------------------------------------------------------------------------
  6. // Description: System Information - library
  7. // for Node.js
  8. // Copyright: (c) 2014 - 2020
  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. // Dependencies
  21. // ----------------------------------------------------------------------------------
  22. const lib_version = require('../package.json').version;
  23. const util = require('./util');
  24. const system = require('./system');
  25. const osInfo = require('./osinfo');
  26. const cpu = require('./cpu');
  27. const memory = require('./memory');
  28. const battery = require('./battery');
  29. const graphics = require('./graphics');
  30. const filesystem = require('./filesystem');
  31. const network = require('./network');
  32. const wifi = require('./wifi');
  33. const processes = require('./processes');
  34. const users = require('./users');
  35. const internet = require('./internet');
  36. const docker = require('./docker');
  37. const vbox = require('./virtualbox');
  38. let _platform = process.platform;
  39. const _windows = (_platform === 'win32');
  40. const _freebsd = (_platform === 'freebsd');
  41. const _openbsd = (_platform === 'openbsd');
  42. const _netbsd = (_platform === 'netbsd');
  43. const _sunos = (_platform === 'sunos');
  44. // ----------------------------------------------------------------------------------
  45. // init
  46. // ----------------------------------------------------------------------------------
  47. if (_windows) {
  48. util.getCodepage();
  49. }
  50. // ----------------------------------------------------------------------------------
  51. // General
  52. // ----------------------------------------------------------------------------------
  53. function version() {
  54. return lib_version;
  55. }
  56. // ----------------------------------------------------------------------------------
  57. // Get static and dynamic data (all)
  58. // ----------------------------------------------------------------------------------
  59. // --------------------------
  60. // get static data - they should not change until restarted
  61. function getStaticData(callback) {
  62. return new Promise((resolve) => {
  63. process.nextTick(() => {
  64. let data = {};
  65. data.version = version();
  66. Promise.all([
  67. system.system(),
  68. system.bios(),
  69. system.baseboard(),
  70. system.chassis(),
  71. osInfo.osInfo(),
  72. osInfo.uuid(),
  73. osInfo.versions(),
  74. cpu.cpu(),
  75. cpu.cpuFlags(),
  76. graphics.graphics(),
  77. network.networkInterfaces(),
  78. memory.memLayout(),
  79. filesystem.diskLayout()
  80. ]).then(res => {
  81. data.system = res[0];
  82. data.bios = res[1];
  83. data.baseboard = res[2];
  84. data.chassis = res[3];
  85. data.os = res[4];
  86. data.uuid = res[5];
  87. data.versions = res[6];
  88. data.cpu = res[7];
  89. data.cpu.flags = res[8];
  90. data.graphics = res[9];
  91. data.net = res[10];
  92. data.memLayout = res[11];
  93. data.diskLayout = res[12];
  94. if (callback) { callback(data); }
  95. resolve(data);
  96. });
  97. });
  98. });
  99. }
  100. // --------------------------
  101. // get all dynamic data - e.g. for monitoring agents
  102. // may take some seconds to get all data
  103. // --------------------------
  104. // 2 additional parameters needed
  105. // - srv: comma separated list of services to monitor e.g. "mysql, apache, postgresql"
  106. // - iface: define network interface for which you like to monitor network speed e.g. "eth0"
  107. function getDynamicData(srv, iface, callback) {
  108. if (util.isFunction(iface)) {
  109. callback = iface;
  110. iface = '';
  111. }
  112. if (util.isFunction(srv)) {
  113. callback = srv;
  114. srv = '';
  115. }
  116. return new Promise((resolve) => {
  117. process.nextTick(() => {
  118. iface = iface || network.getDefaultNetworkInterface();
  119. srv = srv || '';
  120. // use closure to track ƒ completion
  121. let functionProcessed = (function () {
  122. let totalFunctions = 15;
  123. if (_windows) totalFunctions = 11;
  124. if (_freebsd || _openbsd || _netbsd) totalFunctions = 11;
  125. if (_sunos) totalFunctions = 6;
  126. return function () {
  127. if (--totalFunctions === 0) {
  128. if (callback) {
  129. callback(data);
  130. }
  131. resolve(data);
  132. }
  133. };
  134. })();
  135. // var totalFunctions = 14;
  136. // function functionProcessed() {
  137. // if (--totalFunctions === 0) {
  138. // if (callback) { callback(data) }
  139. // resolve(data);
  140. // }
  141. // }
  142. let data = {};
  143. // get time
  144. data.time = osInfo.time();
  145. /**
  146. * @namespace
  147. * @property {Object} versions
  148. * @property {string} versions.node
  149. * @property {string} versions.v8
  150. */
  151. data.node = process.versions.node;
  152. data.v8 = process.versions.v8;
  153. cpu.cpuCurrentspeed().then(res => {
  154. data.cpuCurrentspeed = res;
  155. functionProcessed();
  156. });
  157. users.users().then(res => {
  158. data.users = res;
  159. functionProcessed();
  160. });
  161. if (!_windows) {
  162. processes.processes().then(res => {
  163. data.processes = res;
  164. functionProcessed();
  165. });
  166. }
  167. cpu.currentLoad().then(res => {
  168. data.currentLoad = res;
  169. functionProcessed();
  170. });
  171. if (!_sunos) {
  172. cpu.cpuTemperature().then(res => {
  173. data.temp = res;
  174. functionProcessed();
  175. });
  176. }
  177. if (!_openbsd && !_freebsd && !_netbsd && !_sunos) {
  178. network.networkStats(iface).then(res => {
  179. data.networkStats = res;
  180. functionProcessed();
  181. });
  182. }
  183. if (!_sunos) {
  184. network.networkConnections().then(res => {
  185. data.networkConnections = res;
  186. functionProcessed();
  187. });
  188. }
  189. memory.mem().then(res => {
  190. data.mem = res;
  191. functionProcessed();
  192. });
  193. if (!_sunos) {
  194. battery().then(res => {
  195. data.battery = res;
  196. functionProcessed();
  197. });
  198. }
  199. if (!_windows && !_sunos) {
  200. processes.services(srv).then(res => {
  201. data.services = res;
  202. functionProcessed();
  203. });
  204. }
  205. if (!_sunos) {
  206. filesystem.fsSize().then(res => {
  207. data.fsSize = res;
  208. functionProcessed();
  209. });
  210. }
  211. if (!_windows && !_openbsd && !_freebsd && !_netbsd && !_sunos) {
  212. filesystem.fsStats().then(res => {
  213. data.fsStats = res;
  214. functionProcessed();
  215. });
  216. }
  217. if (!_windows && !_openbsd && !_freebsd && !_netbsd && !_sunos) {
  218. filesystem.disksIO().then(res => {
  219. data.disksIO = res;
  220. functionProcessed();
  221. });
  222. }
  223. if (!_openbsd && !_freebsd && !_netbsd && !_sunos) {
  224. wifi.wifiNetworks().then(res => {
  225. data.wifiNetworks = res;
  226. functionProcessed();
  227. });
  228. }
  229. internet.inetLatency().then(res => {
  230. data.inetLatency = res;
  231. functionProcessed();
  232. });
  233. });
  234. });
  235. }
  236. // --------------------------
  237. // get all data at once
  238. // --------------------------
  239. // 2 additional parameters needed
  240. // - srv: comma separated list of services to monitor e.g. "mysql, apache, postgresql"
  241. // - iface: define network interface for which you like to monitor network speed e.g. "eth0"
  242. function getAllData(srv, iface, callback) {
  243. return new Promise((resolve) => {
  244. process.nextTick(() => {
  245. let data = {};
  246. if (iface && util.isFunction(iface) && !callback) {
  247. callback = iface;
  248. iface = '';
  249. }
  250. if (srv && util.isFunction(srv) && !iface && !callback) {
  251. callback = srv;
  252. srv = '';
  253. iface = '';
  254. }
  255. getStaticData().then(res => {
  256. data = res;
  257. getDynamicData(srv, iface).then(res => {
  258. for (let key in res) {
  259. if ({}.hasOwnProperty.call(res, key)) {
  260. data[key] = res[key];
  261. }
  262. }
  263. if (callback) { callback(data); }
  264. resolve(data);
  265. });
  266. });
  267. });
  268. });
  269. }
  270. function get(valueObject, callback) {
  271. return new Promise((resolve) => {
  272. process.nextTick(() => {
  273. const allPromises = Object.keys(valueObject)
  274. .filter(func => ({}.hasOwnProperty.call(exports, func)))
  275. .map(func => {
  276. const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')'));
  277. const funcWithoutParams = func.split('(')[0];
  278. if (params) {
  279. return exports[funcWithoutParams](params)
  280. } else {
  281. return exports[funcWithoutParams]('')
  282. }
  283. });
  284. Promise.all(allPromises).then(data => {
  285. const result = {};
  286. let i = 0;
  287. for (let key in valueObject) {
  288. if ({}.hasOwnProperty.call(valueObject, key) && {}.hasOwnProperty.call(exports, key) && data.length > i) {
  289. if (valueObject[key] === '*' || valueObject[key] === 'all') {
  290. result[key] = data[i];
  291. } else {
  292. const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
  293. if (data[i]) {
  294. if (Array.isArray(data[i])) {
  295. // result is in an array, go through all elements of array and pick only the right ones
  296. const partialArray = [];
  297. data[i].forEach(element => {
  298. const partialRes = {};
  299. keys.forEach(k => {
  300. if ({}.hasOwnProperty.call(element, k)) {
  301. partialRes[k] = element[k];
  302. }
  303. });
  304. partialArray.push(partialRes);
  305. });
  306. result[key] = partialArray;
  307. } else {
  308. const partialRes = {};
  309. keys.forEach(k => {
  310. if ({}.hasOwnProperty.call(data[i], k)) {
  311. partialRes[k] = data[i][k];
  312. }
  313. });
  314. result[key] = partialRes;
  315. }
  316. } else {
  317. result[key] = {};
  318. }
  319. }
  320. i++;
  321. }
  322. }
  323. if (callback) { callback(result); }
  324. resolve(result);
  325. });
  326. });
  327. });
  328. }
  329. function observe(valueObject, interval, callback) {
  330. let _data = null;
  331. const result = setInterval(() => {
  332. get(valueObject).then(data => {
  333. if (JSON.stringify(_data) !== JSON.stringify(data)) {
  334. _data = Object.assign({}, data);
  335. callback(data);
  336. }
  337. });
  338. }, interval);
  339. return result;
  340. }
  341. // ----------------------------------------------------------------------------------
  342. // export all libs
  343. // ----------------------------------------------------------------------------------
  344. exports.version = version;
  345. exports.system = system.system;
  346. exports.bios = system.bios;
  347. exports.baseboard = system.baseboard;
  348. exports.chassis = system.chassis;
  349. exports.time = osInfo.time;
  350. exports.osInfo = osInfo.osInfo;
  351. exports.versions = osInfo.versions;
  352. exports.shell = osInfo.shell;
  353. exports.uuid = osInfo.uuid;
  354. exports.cpu = cpu.cpu;
  355. exports.cpuFlags = cpu.cpuFlags;
  356. exports.cpuCache = cpu.cpuCache;
  357. exports.cpuCurrentspeed = cpu.cpuCurrentspeed;
  358. exports.cpuTemperature = cpu.cpuTemperature;
  359. exports.currentLoad = cpu.currentLoad;
  360. exports.fullLoad = cpu.fullLoad;
  361. exports.mem = memory.mem;
  362. exports.memLayout = memory.memLayout;
  363. exports.battery = battery;
  364. exports.graphics = graphics.graphics;
  365. exports.fsSize = filesystem.fsSize;
  366. exports.fsOpenFiles = filesystem.fsOpenFiles;
  367. exports.blockDevices = filesystem.blockDevices;
  368. exports.fsStats = filesystem.fsStats;
  369. exports.disksIO = filesystem.disksIO;
  370. exports.diskLayout = filesystem.diskLayout;
  371. exports.networkInterfaceDefault = network.networkInterfaceDefault;
  372. exports.networkGatewayDefault = network.networkGatewayDefault;
  373. exports.networkInterfaces = network.networkInterfaces;
  374. exports.networkStats = network.networkStats;
  375. exports.networkConnections = network.networkConnections;
  376. exports.wifiNetworks = wifi.wifiNetworks;
  377. exports.services = processes.services;
  378. exports.processes = processes.processes;
  379. exports.processLoad = processes.processLoad;
  380. exports.users = users.users;
  381. exports.inetChecksite = internet.inetChecksite;
  382. exports.inetLatency = internet.inetLatency;
  383. exports.dockerInfo = docker.dockerInfo;
  384. exports.dockerContainers = docker.dockerContainers;
  385. exports.dockerContainerStats = docker.dockerContainerStats;
  386. exports.dockerContainerProcesses = docker.dockerContainerProcesses;
  387. exports.dockerAll = docker.dockerAll;
  388. exports.vboxInfo = vbox.vboxInfo;
  389. exports.getStaticData = getStaticData;
  390. exports.getDynamicData = getDynamicData;
  391. exports.getAllData = getAllData;
  392. exports.get = get;
  393. exports.observe = observe;