-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (71 loc) · 2.47 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
/*
* Copyright (c) 2017, Anthony DeDominic <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
'use strict';
module.exports = function(input, options) {
if (!options) options = {};
if (!input) return;
var perfs = '';
// long perfdata
input.split('\n').forEach((line) => {
perfs += line.split('|')[1] || '';
});
input = perfs.trim();
if (input === '') return;
// 'label'=value[UOM];[warn];[crit];[min];[max]
// splits by label & values
var perfdatas = input.split(/\s+(?=(?:[^']*['][^']*['])*[^']*$)/);
var err = null;
var perf;
if (!options.flatten) perf = {};
else perf = [];
perfdatas.find(perfdata => {
perfdata = perfdata.split('=');
var label = perfdata[0];
if (!perfdata[1]) return err = 'invalid perfdata, no values';
var values = perfdata[1].split(';');
if (!values[0]) return err = 'invalid perfdata, no primary value';
var value_oum = values[0].match(/(\d+(?:\.\d+)?)\s*(\D+)?/);
if (!value_oum) return err = 'primary value is not a number';
if (options.flatten) {
perf.push({
label,
oum: value_oum[2],
value: +value_oum[1],
warn: +values[1],
crit: +values[2],
min: +values[3],
max: +values[4],
});
}
else {
perf[label] = {
oum: value_oum[2],
value: +value_oum[1],
warn: +values[1],
crit: +values[2],
min: +values[3],
max: +values[4],
};
}
return undefined;
});
if (err) {
if (options.throwErr)
throw Error(err);
return;
}
return perf;
};