Spaces:
Sleeping
Sleeping
File size: 6,261 Bytes
e4bf523 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | '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);
// });
|