-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
276 lines (218 loc) · 8.49 KB
/
index.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';
const fs = require('fs');
const os = require('os');
const promisify = require('util').promisify;
const CpuUsage = require('./lib/CpuUsage');
const CpuUsageSma = require('./lib/CpuUsageSma');
const readFileAsync = promisify(fs.readFile);
const MB_MULTIPLIER = 1024;
const MEMINFO_PATH = '/proc/meminfo';
/**
* Row example: MemTotal: 8068528 kB
* @param {string} row
* @returns {number} size in kB
*/
function getSizeFromMeminfoRow(row) {
return +row.split(':')[1].slice(0, -3).trim();
}
class SystemHealthMonitor {
static get STATUS_STOPPED() {
return 1;
}
static get STATUS_STARTED() {
return 2;
}
constructor({checkIntervalMsec, mem, cpu}) {
if (!Number.isSafeInteger(checkIntervalMsec) || checkIntervalMsec < 1) {
throw new Error('field "checkIntervalMsec" is required must be an integer and more than 1');
}
this._checkIntervalMsec = checkIntervalMsec;
this._isMemOverloaded = undefined;
this._isCpuOverloaded = undefined;
this._cpuUsageCalculator = undefined;
this._initMemChecks(mem);
this._initCpuChecks(cpu);
this._status = SystemHealthMonitor.STATUS_STOPPED;
this._memTotal = -1;
this._memFree = -1;
this._cpuCount = -1;
this._cpuUsage = 100;
this._isOverload = true;
this._healthScheduler = undefined;
this._determineHealthIndicators = this._determineHealthIndicators.bind(this);
}
async start() {
if (this._status === SystemHealthMonitor.STATUS_STARTED) {
throw new Error('SystemHealthMonitor service is already started');
}
await this._determineHealthIndicators();
this._healthScheduler = setInterval(this._determineHealthIndicators, this._checkIntervalMsec);
this._status = SystemHealthMonitor.STATUS_STARTED;
}
stop() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
clearInterval(this._healthScheduler);
this._status = SystemHealthMonitor.STATUS_STOPPED;
}
getMemTotal() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
return this._memTotal;
}
getMemFree() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
return this._memFree;
}
getCpuCount() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
return this._cpuCount;
}
getCpuUsage() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
return this._cpuUsage;
}
isOverloaded() {
if (this._status === SystemHealthMonitor.STATUS_STOPPED) {
throw new Error('SystemHealthMonitor service is not started');
}
return this._isOverload;
}
/* istanbul ignore next */
async _readMeminfoFile() {
return await readFileAsync(MEMINFO_PATH, 'utf8');
}
/**
* For /proc/meminfo doc see: https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
* @returns {Promise.<void>}
*/
async _getMemInfo() {
let memFree = 0;
let indicatorsCounter = 5;
let memInfo = undefined;
try {
memInfo = await this._readMeminfoFile();
} catch (err) {
this._memTotal = -1;
this._memFree = -1;
return;
}
for (const line of memInfo.split('\n')) {
if (line.includes('MemTotal:')) {
this._memTotal = Math.floor(getSizeFromMeminfoRow(line) / MB_MULTIPLIER);
indicatorsCounter--;
continue;
}
if (/(^MemFree|^Buffers|^Cached|^SReclaimable):/.test(line)) {
memFree += parseInt(getSizeFromMeminfoRow(line));
indicatorsCounter--;
continue;
}
if (indicatorsCounter === 0) {
break;
}
}
this._memFree = Math.ceil(memFree / MB_MULTIPLIER);
}
/* istanbul ignore next */
_getCpuInfo() {
try {
return os.cpus();
} catch (err) {
return undefined;
}
}
/* istanbul ignore next */
_getCpuCount() {
try {
return this._cpusList.length;
} catch (err) {
return -1;
}
}
async _determineHealthIndicators() {
await this._getMemInfo();
const memOverload = this._isMemOverloaded(this._memFree, this._memTotal);
this._cpusList = this._getCpuInfo();
if (this._cpusList === undefined) {
this._cpuCount = -1;
this._cpuUsage = 100;
} else {
this._cpuCount = this._getCpuCount();
this._cpuUsage = this._cpuUsageCalculator.calculate(this._cpusList);
}
const cpuOverload = this._isCpuOverloaded(this._cpuUsage);
this._isOverload = memOverload || cpuOverload;
}
_isMemOverloadedByIncorrectData(free, total) {
return free < 0 || total < 0;
}
_isMemOverloadedByFixedThreshold(minFree, free, total) {
return this._isMemOverloadedByIncorrectData(free, total) || free < minFree;
}
_isMemOverloadedByRateThreshold(highWatermark, free, total) {
return this._isMemOverloadedByIncorrectData(free, total) || (total - free) / total > highWatermark;
}
_isCpuOverloadByRateThreshold(highWatermark, cpuUsage) {
return highWatermark * 100 < cpuUsage;
}
_initMemChecks(mem) {
if (typeof mem !== 'object') {
throw new Error('field "mem" is required and must be an object');
}
if (mem.thresholdType === 'fixed') {
if (mem.minFree === undefined || !Number.isSafeInteger(mem.minFree) || mem.minFree <= 0) {
throw new Error('"mem.minFree" field is required for threshold = fixed and must be more then 0');
}
this._isMemOverloaded = this._isMemOverloadedByFixedThreshold.bind(this, mem.minFree);
} else if (mem.thresholdType === 'rate') {
if (mem.highWatermark === undefined || !Number.isFinite(mem.highWatermark) ||
mem.highWatermark <= 0 || mem.highWatermark >= 1) {
throw new Error(
'"mem.highWatermark" field is required for threshold = rate and must be in range (0;1)'
);
}
this._isMemOverloaded = this._isMemOverloadedByRateThreshold.bind(this, mem.highWatermark);
} else if (mem.thresholdType === 'none') {
this._isMemOverloaded = this._isMemOverloadedByIncorrectData.bind(this);
} else {
throw new Error('"mem.thresholdType" is not set or has invalid type');
}
}
_initCpuChecks(cpu) {
if (typeof cpu !== 'object') {
throw new Error('field "cpu" is required and must be an object');
}
if (cpu.calculationAlgo === 'sma') {
if (!Number.isSafeInteger(cpu.periodPoints) || cpu.periodPoints < 1) {
throw new Error('"cpu.periodPoints" field is required for SMA algorithm and must be more than 0');
}
this._cpuUsageCalculator = new CpuUsageSma(cpu.periodPoints);
} else if (cpu.calculationAlgo === 'last_value') {
this._cpuUsageCalculator = new CpuUsage();
} else {
throw new Error('"cpu.calculationAlgo" is not set or has invalid type');
}
if (cpu.thresholdType === 'rate') {
if (!cpu.highWatermark || !Number.isFinite(cpu.highWatermark) || cpu.highWatermark > 1) {
throw new Error(
'"cpu.highWatermark" field is required for threshold = rate and must be in range (0,1]'
);
}
this._isCpuOverloaded = this._isCpuOverloadByRateThreshold.bind(this, cpu.highWatermark);
} else if (cpu.thresholdType === 'none') {
this._isCpuOverloaded = () => false;
} else {
throw new Error('"cpu.thresholdType" is not set or has invalid type');
}
}
}
module.exports = SystemHealthMonitor;