-
Notifications
You must be signed in to change notification settings - Fork 8
/
optionsparser.js
94 lines (80 loc) · 2.94 KB
/
optionsparser.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
'use strict';
const util = require('util');
// frequency: '2s'
exports.getFrequency = function(options) {
let f = {};
if(!options || !options.frequency) {
f.mode = 'once';
f.interval = 2000; // default 2s
return f;
}
f.mode = options.frequency;
if(options.frequency === 'once') {
f.interval = 0;
return f;
}
else if(options.frequency === 'onalert') {
f.interval = 2000; // check for alert condition every 2 seconds
return f;
}
else if(options.frequency.length !== 2 || isNaN(options.frequency[0])) {
throw "Invalid frequency. Try something like 'once', 'onalert', '2m' or '1h'";
}
f.mode = 'time';
let n = parseInt(options.frequency[0]);
let s = options.frequency[1];
switch(s) {
case 's': f.interval = n * 1000; break;
case 'm': f.interval = n * 1000 * 60; break;
case 'h': f.interval = n * 1000 * 60 * 60; break;
default: f.interval = 2000;
}
return f;
}
// memory: used: '>80%'
exports.getMemoryUsedAlertThreshold = function(memoptions) {
if(!memoptions || !memoptions.used) return 50; // default 50%
let u = memoptions.used;
if(u.length < 3 || !u.includes('>') || !u.includes('%')) {
throw "Invalid 'used' memory option. Try something like used: '>80%'";
}
return parseInt(u.split('>')[1].split('%')[0]);
}
// cpu: load: '>80%'
exports.getCpuLoadAlertThreshold = function(cpuoptions) {
if(!cpuoptions || !cpuoptions.load) return 10; // default 50%
let u = cpuoptions.load;
if(u.length < 3 || !u.includes('>') || !u.includes('%')) {
throw "Invalid 'load' cpu option. Try something like used: '>80%'";
}
return parseInt(u.split('>')[1].split('%')[0]);
}
// disk: filesystem/filesystems: '/dev/disk01'
// disk: filesystem/filesystems: ['/dev/disk01', '/dev/disk02']
exports.getDiskFilesystems = function(diskoptions) {
let filesystems = [];
if(!diskoptions || (!diskoptions.filesystem && !diskoptions.filesystems)) return null;
let u = diskoptions.filesystem || diskoptions.filesystems;
if(util.isArray(u)) filesystems = u;
else filesystems.push(u);
return filesystems;
}
// disk: mount/mounts: '/'
// disk: mount/mounts: ['/', '/home/sv']
exports.getDiskMounts = function(diskoptions) {
let mounts = [];
if(!diskoptions || (!diskoptions.mount && !diskoptions.mounts)) return null;
let u = diskoptions.mount || diskoptions.mounts;
if(util.isArray(u)) mounts = u;
else mounts.push(u);
return mounts;
}
// disk: used: '>80%'
exports.getDiskUsedAlertThreshold = function(diskoptions) {
if(!diskoptions || !diskoptions.used) return 50; // default 50%
let u = diskoptions.used;
if(u.length < 3 || !u.includes('>') || !u.includes('%')) {
throw "Invalid 'used' disk option. Try something like used: '>80%'";
}
return parseInt(u.split('>')[1].split('%')[0]);
}