-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibperfdata.lua
More file actions
419 lines (378 loc) · 11.4 KB
/
libperfdata.lua
File metadata and controls
419 lines (378 loc) · 11.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
-- Copyright Benoit Dolez < bdolez @ ant-computing.com >
-- Copyright Zenetys < bdolez @ zenetys.com >
-- Initial author: Benoit Dolez
-- Licence: MIT
-- nagios-plugins utils functions to respect :
-- https://nagios-plugins.org/doc/guidelines.html
-- (extract from guidelines)
--
-- performance data:
-- format: 'label'=value[UOM];[warn];[crit];[min];[max][<space>...]
-- 1. space separated list of label/value pairs
-- 2. label can contain any characters except the equals sign or
-- single quote (')
-- 3. the single quotes for the label are optional. Required if spaces
-- are in the label
-- 4. label length is arbitrary, but ideally the first 19 characters are
-- unique (due to a limitation in RRD). Be aware of a limitation in
-- the amount of data that NRPE returns to Nagios
-- 5. to specify a quote character, use two single quotes
-- 6. warn, crit, min or max may be null (for example, if the threshold is
-- not defined or min and max do not apply). Trailing unfilled semicolons
-- can be dropped
-- 7. min and max are not required if UOM=%
-- 8. value, min and max in class [-0-9.]. Must all be the same UOM. value
-- may be a literal "U" instead, this would indicate that the actual value
-- couldn't be determined
-- 9. warn and crit are in the range format (see the Section called Threshold
-- and Ranges). Must be the same UOM
-- 10. UOM (unit of measurement) is one of:
-- 1. no unit specified - assume a number (int or float) of things (eg,
-- users, processes, load averages)
-- 2. s - seconds (also us, ms)
-- 3. % - percentage
-- 4. B - bytes (also KB, MB, TB)
-- 5. c - a continous counter (such as bytes transmitted on an interface)
--
-- It is up to third party programs to convert the Nagios Plugins performance
-- data into graphs.
local LP = {
STATE_OK = 0,
STATE_WARNING = 1,
STATE_CRITICAL = 2,
STATE_UNKNOWN = 3,
STATE_DEPENDENT = 4,
state = {
[0] = "OK",
[1] = "WARNING",
[2] = "CRITICAL",
[3] = "UNKNOWN",
[4] = "DEPENDENT",
},
}
local STATE_ORDER = { [0] = 0, [1] = 2, [2] = 4, [3] = 3, [4] = 1 }
local STATE_LIST = {
LP.STATE_CRITICAL,
LP.STATE_UNKNOWN,
LP.STATE_DEPENDENT,
LP.STATE_WARNING,
LP.STATE_OK,
}
local UOM_PATTERN = "([0-9.]+)([s%%ckmgtpKMGTPBbi]*)"
local UOM_TMULT = {
[""] = 1,
["c"] = 1,
["k"] = 1000,
["K"] = 1000,
["M"] = 1000*1000,
["G"] = 1000*1000*1000,
["T"] = 1000*1000*1000*1000,
["P"] = 1000*1000*1000*1000*1000,
["ki"] = 1024,
["Bi"] = 1,
["Ki"] = 1024,
["Mi"] = 1024*1024,
["Gi"] = 1024*1024*1024,
["Ti"] = 1024*1024*1024*1024,
["Pi"] = 1024*1024*1024*1024*1024,
["b"] = 1,
["B"] = 1,
["kB"] = 1024,
["KB"] = 1024,
["MB"] = 1024*1024,
["GB"] = 1024*1024*1024,
["TB"] = 1024*1024*1024*1024,
["PB"] = 1024*1024*1024*1024*1024,
["%"] = 1,
["s"] = 1,
["ms"] = 0.001,
["us"] = 0.000001,
}
local UOM_UNIT = {
[""] = "",
["B"] = "B",
["Bps"] = "Bps",
["Bi"] = "Bi",
["auto"] = "",
["si"] = "",
["iec"] = "B",
["iec-i"] = "Bi",
["o"] = "o",
["b"] = "b",
["be"] = "be",
}
local UOM_RMULT = { [0] = "", "k", "M", "G", "T", "P" }
local UOM_BASE = {
["iec"] = 1024,
["iec-i"] = 1024,
["B"] = 1024,
["Bi"] = 1024,
["Bps"] = 1024,
["o"] = 1024,
["be"] = 1024,
}
-- "1234.56K" => 1234560
-- "1.1.2" => nil
-- 123456.789 => 123456.789
-- "1.2K03" => nil
-- "1.2Ki" => nil
-- coreutils command numfmt
-- https://www.gnu.org/software/coreutils/manual/coreutils.html#numfmt-invocation
function LP.numfmt(n, to, from, base, max, to_raw)
if (n == nil) then
return nil
end
if (base == nil) then
base = 1
end
if (from == nil or from == "auto") then
if (type(n) ~= "number" and type(n) ~= "string") then
return nil -- error
elseif (type(n) == "string") then
-- check for number
local t = tonumber(n)
if (t ~= nil) then
n = t
else
local t, uom, percent = LP.numbase(n)
if (t and uom and percent and max) then
-- transform percent to number
n = (t*uom*max/100)
elseif (t and uom) then
n = (t*uom)
else
return nil -- error
end
end
-- else type(n) == number
end
end
if (to == nil or to == "none") then
return n / base;
end
local function fp(v) -- format-precision
if (v-math.floor(v) == 0) then return "%.f" else return "%.2f" end
end
if (to == "%") then
return string.format(fp(n) .. "%%", n)
elseif (to == "c") then
return string.format("%dc", n)
elseif (to == "s") then
if (n > 1 or to_raw) then
return string.format("%.3fs", n)
elseif (n > 0.001) then
return string.format("%.fms", n*1000)
else
return string.format("%.fus", n*1000*1000)
end
elseif (to == "hms") then -- hour/minuts/seconds
-- FIXME
elseif (UOM_UNIT[to] ~= nil) then
local base = (UOM_BASE[to] or 1000)
local u = 0 ; while not to_raw and n > base do n=n/base ; u=u+1 ; end
return string.format(fp(n)..UOM_RMULT[u]..UOM_UNIT[to], n)
end
return string.format(fp(n), n)..to
end
function LP.numbase(n)
if (type(n) == nil) then
return nil;
elseif (type(n) == "number") then
return 1;
elseif (type(n) == "string") then
-- match UOM
local a = { string.find(n, UOM_PATTERN) }
-- all the string need to match
if (a[1] ~= 1 or a[2] ~= string.len(n)) then
return nil -- error
end
-- convert and check for number and UOM
return tonumber(a[3]), UOM_TMULT[a[4]], (a[4] == '%')
else
return nil
end
end
-- check_range takes a value and a range string, returning successfully if an
-- alert should be raised based on the range. Range values are inclusive.
-- Values may be integers or floats.
--
-- threshold and ranges :
-- format: [@]start:end
-- 1. start ≤ end
-- 2. start and ":" is not required if start=0
-- 3. if range is of format "start:" and end is not specified, assume end is
-- infinity
-- 4. to specify negative infinity, use "~"
-- 5. alert is raised if metric is outside start and end range(inclusive of
-- endpoints)
-- 6. if range starts with "@", then alert if inside this range(inclusive of
-- endpoints)
--
-- 5 1 # yes
-- 6 :1 # yes
-- 4 1:2 # yes
-- 4 @1:4 # yes
-- 5 @~:5 # yes
-- -1 @~:3 # yes
-- 4 @1 # no
-- 8 @:1 # no
-- 4 1: # no
-- 3 1:~ # no
-- 5 1:8 # no
-- 3 1:3 # no
-- 6 @1:5 # no
-- -6 @5: # no
function LP.check_range(v, range, max)
if (v == nil or range == nil) then
return nil -- error
end
local a = { string.find(range, "(@?)([^:]*)(:?)([^:]*)") }
if (a[1] ~= 1 or a[2] ~= string.len(range)) then
return nil -- error
end
-- compute value and range
local v = LP.numfmt(v)
local max = LP.numfmt(max)
local st, en
if (a[5] == ":") then
st = ((a[4] == "") and 0 or LP.numfmt(a[4], nil, nil, nil, max))
en = LP.numfmt(a[6], nil, nil, nil, max)
else
st = 0
en = LP.numfmt(a[4], nil, nil, nil, max)
end
if (st ~= nil and en ~= nil and en < st) then
return nil -- error
end
-- DEBUG
-- print(string.format("%s%s in (%s,%s)",
-- (a[3] == "@" and "inc " or ""), v, st or "~", en or "~"))
-- check value into range
if (a[3] == "@") then
return ((st == nil or st <= v) and (en == nil or v <= en))
else
return ((st ~= nil and v < st) or (en ~= nil and en < v))
end
end
function LP.compute_perfdata(perfdata, initial_state)
local state = initial_state or LP.STATE_OK
-- compute status from perfdata array
for _, p in pairs(perfdata) do
if (p.value == nil) then
if (p.null_state == nil) then
p.state = LP.STATE_UNKNOWN
else
p.state = p.null_state
end
elseif (p.critical and LP.check_range(p.value, p.critical, p.max)) then
p.state = LP.STATE_CRITICAL
elseif (p.warning and LP.check_range(p.value, p.warning, p.max)) then
p.state = LP.STATE_WARNING
else
p.state = LP.STATE_OK
end
if (STATE_ORDER[p.state] > STATE_ORDER[state]) then
state = p.state
end
end
return state
end
function LP.format_perfdata(perfdata, opts)
-- compat arg #2 as opts.raw
if type(opts) ~= 'table' then opts = { raw = opts } end
-- build perfdata from computed array
local s = ""
for i, p in ipairs(perfdata) do
if opts.limit and i > opts.limit then break end
local v = LP.numfmt(p.value, p.uom, nil, nil, nil, opts.raw)
local max = LP.numfmt(p.max)
local n, b = LP.numbase(v)
s = s .. "'" .. p.name .. "'=" .. (v or "U") .. ";"
s = s .. (LP.numfmt(p.warning, nil, nil, b, max, opts.raw) or "") .. ";"
s = s .. (LP.numfmt(p.critical, nil, nil, b, max, opts.raw) or "") .. ";"
s = s .. (LP.numfmt(p.min, nil, nil, b, nil, opts.raw) or "") .. ";"
s = s .. (LP.numfmt(p.max, nil, nil, b, nil, opts.raw) or "") .. " "
end
return s
end
function LP.format_output(perfdata, opts)
if not opts then opts = {} end
-- build auto output from computed array
local s = ""
local msg = {}
for k,v in pairs(STATE_LIST) do
msg[v] = {}
end
for i, p in ipairs(perfdata) do
if opts.limit and i > opts.limit then
if i > 1 then
opts.append = ', ...'..(opts.append and ' '..opts.append or '')
end
break
end
local pm = {
v = LP.numfmt(p.value, p.uom),
max = LP.numfmt(p.max),
name = p.label or p.name,
extra = '',
error = '',
}
pm.value = (pm.v or 'nil')
if (p.max and p.uom ~= '%') then
if p.value then pm.value = LP.numfmt(p.value*100/p.max, '%')..' = '..pm.value end
pm.extra = "/" .. LP.numfmt(p.max, p.uom)
end
if p.extra then pm.extra = pm.extra..p.extra end
if (p.state ~= LP.STATE_OK) then
pm.error = '**'
end
local m
if p.foutput then m = p.foutput(p, pm)
else m = ('%s%s: %s%s%s'):format(pm.error, pm.name, pm.value, pm.extra, pm.error) end
if (p.state == nil) then
table.insert(msg[LP.STATE_UNKNOWN], m)
else
table.insert(msg[p.state], m)
end
end
local output = {}
for k,v in pairs(STATE_LIST) do
if (#msg[v] > 0) then
table.insert(output, table.concat(msg[v], ", "))
end
end
return table.concat(output, ", ")..(opts.append or '');
end
return LP
--[[
perfdata = {
{
name = "test1",
value = 12,
warning = "12",
critical = "16",
uom = "",
min = 0,
max = 100,
},
{
name = "test2",
value = 8000,
warning = "23%",
critical = "79.999%",
uom = "B",
min = 0,
max = "10000",
},
{
name = "test3",
value = 18,
warning = "10",
critical = "15",
uom = ""
},
}
state = LP.compute_perfdata(perfdata)
print(LP.format_perfdata(perfdata))
print(LP.format_output(perfdata))
--]]