-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
68 lines (51 loc) · 2.08 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
'use strict';
const EventEmitter = require('events').EventEmitter
, statEmitter = module.exports = new EventEmitter()
, optionsparser = require('./optionsparser')
, stats = require('./lib/stats');
var on = false;
statEmitter.start = function(options, cb) {
if(!isPlatformSupported()) {
if(cb && typeof(cb) === 'function') return cb('Platform currently unsupported');
else return;
}
let frequency, memusedThreshold = 0, cpuloadThreshold = 0, diskusedThreshold = 0, diskfilesystems, mounts;
try {
frequency = optionsparser.getFrequency(options);
if(options && frequency.mode === 'onalert') {
memusedThreshold = optionsparser.getMemoryUsedAlertThreshold(options.memoryalert);
cpuloadThreshold = optionsparser.getCpuLoadAlertThreshold(options.cpualert);
diskusedThreshold = optionsparser.getDiskUsedAlertThreshold(options.diskalert);
}
if(options) {
diskfilesystems = optionsparser.getDiskFilesystems(options.diskalert);
mounts = optionsparser.getDiskMounts(options.diskalert);
}
//console.log(frequency, memusedThreshold, cpuloadThreshold, diskusedThreshold, diskfilesystems, mounts);
}
catch(err) {
if(cb && typeof(cb) === 'function') return cb(err);
else return;
}
on = true;
var check = setInterval(function() {
if(!on) {
clearInterval(check);
return;
}
stats.memory(statEmitter, { threshold: memusedThreshold });
stats.cpu(statEmitter, { threshold: cpuloadThreshold });
stats.disk(statEmitter, { threshold: diskusedThreshold, diskfilesystems: diskfilesystems, mounts: mounts });
if(frequency.mode === 'once') {
clearInterval(check);
}
}, frequency.interval);
if(cb && typeof(cb) === 'function') return cb(null);
else return;
}
statEmitter.stop = function() {
on = false;
}
function isPlatformSupported() {
return process.env.platform !== 'other';
}