UNPKG

ethstats-cli

Version:
297 lines (251 loc) 9.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var Usage = /*#__PURE__*/ function () { function Usage(diContainer) { _classCallCheck(this, Usage); this.log = diContainer.logger; this.systemInfo = diContainer.systemInfo; this.errorHandler = diContainer.clientErrorHandler; this.server = diContainer.server; this.nodeProcessName = null; } _createClass(Usage, [{ key: "getCpuLoad", value: function getCpuLoad() { var _this = this; var result = 0; return this.systemInfo.currentLoad().then(function (data) { if (data) { result = data.currentload; } return result; }).catch(function (error) { _this.log.error(_this.errorHandler.resolve(error)); return result; }); } }, { key: "getMemLoad", value: function getMemLoad() { var _this2 = this; var result = { memTotal: 0, memUsed: 0 }; return this.systemInfo.mem().then(function (data) { if (data) { result.memTotal = data.total; result.memUsed = data.used; } return result; }).catch(function (error) { _this2.log.error(_this2.errorHandler.resolve(error)); return result; }); } }, { key: "getNetworkStats", value: function () { var _getNetworkStats = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { var _this3 = this; var result, iface; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: result = { rxSec: 0, txSec: 0 }; _context.next = 3; return this.systemInfo.networkInterfaceDefault(); case 3: iface = _context.sent; return _context.abrupt("return", this.systemInfo.networkStats(iface).then(function (data) { if (data) { result.rxSec = data[0].rx_sec; result.txSec = data[0].tx_sec; } return result; }).catch(function (error) { _this3.log.error(_this3.errorHandler.resolve(error)); return result; })); case 5: case "end": return _context.stop(); } } }, _callee, this); })); function getNetworkStats() { return _getNetworkStats.apply(this, arguments); } return getNetworkStats; }() }, { key: "getFileSystemStats", value: function getFileSystemStats() { var _this4 = this; var result = { rxSec: 0, wxSec: 0 }; return this.systemInfo.fsStats().then(function (data) { if (data) { result.rxSec = data.rx_sec; result.wxSec = data.wx_sec; } return result; }).catch(function (error) { _this4.log.error(_this4.errorHandler.resolve(error)); return result; }); } }, { key: "getDiskIO", value: function getDiskIO() { var _this5 = this; var result = { rIOSec: 0, wIOSec: 0 }; return this.systemInfo.disksIO().then(function (data) { if (data) { result.rIOSec = data.rIO_sec; result.wIOSec = data.wIO_sec; } return result; }).catch(function (error) { _this5.log.error(_this5.errorHandler.resolve(error)); return result; }); } }, { key: "getProcessLoad", value: function getProcessLoad(processName) { var _this6 = this; var result = { cpu: 0, mem: 0 }; return this.systemInfo.processLoad(processName).then(function (data) { if (data) { result.cpu = data.cpu; result.mem = data.mem; } return result; }).catch(function (error) { _this6.log.error(_this6.errorHandler.resolve(error)); return result; }); } }, { key: "setNodeProcessName", value: function setNodeProcessName(node) { if (node) { this.nodeProcessName = node.split('/')[0].toLowerCase(); } } }, { key: "getStats", value: function () { var _getStats = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2() { var result, memLoad, networkStats, fsStats, diskIO, nodeLoad, clientLoad; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: this.log.debug('Get usage'); _context2.next = 3; return this.getCpuLoad(); case 3: _context2.t0 = _context2.sent; result = { hostCpuLoad: _context2.t0, hostMemTotal: 0, hostMemUsed: 0, hostNetRxSec: 0, hostNetTxSec: 0, hostFsRxSec: 0, hostFsWxSec: 0, hostDiskRIOSec: 0, hostDiskWIOSec: 0, nodeCpuLoad: 0, nodeMemLoad: 0, clientCpuLoad: 0, clientMemLoad: 0 }; _context2.next = 7; return this.getMemLoad(); case 7: memLoad = _context2.sent; result.hostMemTotal = memLoad.memTotal; result.hostMemUsed = memLoad.memUsed; _context2.next = 12; return this.getNetworkStats(); case 12: networkStats = _context2.sent; result.hostNetRxSec = networkStats.rxSec < 0 ? 0 : networkStats.rxSec; result.hostNetTxSec = networkStats.txSec < 0 ? 0 : networkStats.txSec; _context2.next = 17; return this.getFileSystemStats(); case 17: fsStats = _context2.sent; result.hostFsRxSec = fsStats.rxSec < 0 ? 0 : fsStats.rxSec; result.hostFsWxSec = fsStats.wxSec < 0 ? 0 : fsStats.wxSec; _context2.next = 22; return this.getDiskIO(); case 22: diskIO = _context2.sent; result.hostDiskRIOSec = diskIO.rIOSec < 0 ? 0 : diskIO.rIOSec; result.hostDiskWIOSec = diskIO.wIOSec < 0 ? 0 : diskIO.wIOSec; if (!this.nodeProcessName) { _context2.next = 31; break; } _context2.next = 28; return this.getProcessLoad(this.nodeProcessName); case 28: nodeLoad = _context2.sent; result.nodeCpuLoad = nodeLoad.cpu; result.nodeMemLoad = nodeLoad.mem; case 31: _context2.next = 33; return this.getProcessLoad('ethstats-cli'); case 33: clientLoad = _context2.sent; result.clientCpuLoad = clientLoad.cpu; result.clientMemLoad = clientLoad.mem; this.server.send('usage', result); return _context2.abrupt("return", result); case 38: case "end": return _context2.stop(); } } }, _callee2, this); })); function getStats() { return _getStats.apply(this, arguments); } return getStats; }() }]); return Usage; }(); exports.default = Usage;