'use strict' /** * Copyright (c) 2017~2019, OBCon Inc. * All rights reserved. */ /** * @file * @copyright 2017~2019, OBCon Inc. * @author gye hyun james kim [pnuskgh@gmail.com] */ let os = require('os'); let osu = require('node-os-utils') //--- yum -y install sysstat //--- yum -y install nmon class Usage { constructor() { } usageServer(callback) { let usage = { cpu: {}, memory: {}, storage: {}, network: {}, process: {} } usage.cpu = this._getUsageCpu(); usage.memory = this._getUsageMemory(); usage.network = this._getUsageNetwork(); usage.process = this._getUsageProcess(); this._getUsageStorage(function(usageStorage) { usage.storage = usageStorage; callback(usage); }); } //--- usageServer : CPU _getUsageCpu() { let usage = { count: 0, //--- CPU 갯수 model: '', //--- CPU 모델 rate: { //--- Type별 cpu 사용률(%) user: 0.0, //--- 사용자 사용률 nice: 0.0, sys: 0.0, //--- 시스템 사용률 idle: 0.0, //--- 여유률 irq: 0.0 } }; let data = os.cpus(); usage.count = data.length; if (0 < usage.count) { usage.model = data[0].model; usage.speed = data[0].speed; let total = 0; for (let idx = 0; idx < data.length; idx++) { let item = data[idx]; //--- type : user, nice, sys, idle, irq for (let type in item.times) { if (idx == 0) { usage.rate[type] = item.times[type]; } else { usage.rate[type] = usage.rate[type] + item.times[type]; } total = total + item.times[type]; } } for (let type in data[0].times) { usage.rate[type] = this._round(usage.rate[type] / total * 100, 2); } } return usage; } //--- usageServer : Memory _getUsageMemory() { let usage = { free: 0.0, //--- 여유 메모리(MB) used: 0.0, //--- 사용 메모리(MB) total: 0.0, //--- 전체 메모리(MB) rate: { free: 0.0, used: 0.0 } }; // usageServer.memory['used'] = process.memoryUsage(); usage['free'] = this._round(os.freemem() / 1024 / 1024, 2); usage['used'] = this._round((os.totalmem() - os.freemem()) / 1024 / 1024, 2); usage['total'] = this._round(os.totalmem() / 1024 / 1024, 2); usage.rate = {}; usage.rate['free'] = this._round(usage['free'] / usage['total'] * 100, 2); usage.rate['used'] = this._round(usage['used'] / usage['total'] * 100, 2); return usage; } //--- usageServer : Storage _getUsageStorage(callback) { if (this._isLinux()) { osu.drive.info() .then(function(info) { let usage = { free: parseFloat(info.freeGb), used: parseFloat(info.usedGb), total: this._round(parseFloat(info.freeGb) + parseFloat(info.usedGb), 2), rate: { free: this._round(parseFloat(info.freePercentage), 2), used: this._round(parseFloat(info.usedPercentage), 2) } }; callback(usage); }.bind(this)); } else { callback({ free: 0.0, used: 0.0, total: 0.0, rate: { free: 0.00, used: 0.00 } }); } } _getUsageNetwork() { let usage = { }; osu.netstat.stats() .then(info => { if (info != 'not supported') { console.log(info) } }); osu.netstat.inOut() .then(info => { if (info != 'not supported') { console.log(info) } }); return usage; } _getUsageProcess() { let usage = { }; return usage; } _getUsageDatabase() { let database = 'obcondb'; let query = []; //--- Table별로 사용 용량 계산 query = []; query.push(' SELECT TABLE_SCHEMA "DB Name", ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) "MB"'); query.push(' FROM information_schema.TABLES'); query.push(' GROUP BY TABLE_SCHEMA'); console.log(query.join(' ')); //--- Table별로 사용 용량 계산 query = []; query.push('SELECT TABLE_NAME, TABLE_ROWS, round((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) "MB"'); query.push(' FROM information_schema.TABLES'); query.push(' WHERE table_schema = "' + database + '"'); query.push(' ORDER BY (DATA_LENGTH + INDEX_LENGTH) DESC'); console.log(query.join(' ')); } usageService() { return {}; } usageSite() { return {}; } _isLinux() { return (process.env.HOME) ? true:false; } _round(value, pos) { if (pos == 2) { return Math.round(value * 100) / 100; } else { return Math.round(value); } } } module.exports = Usage; // console.log('Start Usage.js'); // let usage = new Usage(); // usage.usageServer(function(usage) { // console.log(usage); // });