-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.lua
78 lines (71 loc) · 2.91 KB
/
init.lua
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
-- Copyright 2015 Boundary, Inc.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
local framework = require('framework')
local Plugin = framework.Plugin
local MeterDataSource = framework.MeterDataSource
local urldecode = framework.string.urldecode
local ipack = framework.util.ipack
local trim = framework.string.trim
local params = framework.params
params.items = params.items or {}
local metric_mapping = {
['system.disk.reads.total'] = 'DISK_READS_TOTAL',
['system.disk.reads'] = 'DISK_READS',
['system.disk.read_bytes.total'] = 'DISK_READ_BYTES_TOTAL',
['system.disk.read_bytes'] = 'DISK_READ_BYTES',
['system.disk.writes.total'] = 'DISK_WRITES_TOTAL',
['system.disk.writes'] = 'DISK_WRITES',
['system.disk.write_bytes.total'] = 'DISK_WRITE_BYTES_TOTAL',
['system.disk.write_bytes'] = 'DISK_WRITE_BYTES',
['system.disk.ios'] = 'DISK_IOS',
['system.fs.use_percent'] = 'DISKUSE_SUMMARY',
['system.fs.use_percent.total'] = 'DISKUSE_SUMMARY'
}
local ds = MeterDataSource:new()
function ds:onFetch(socket)
socket:write(self:queryMetricCommand({match = "(system.disk.*|system.fs.use_percent)"}))
end
local function matchItem(item, dir, dev)
item.dir = item.dir and string.lower(trim(item.dir))
item.device = item.device and string.lower(trim(item.device))
dir = dir and string.lower(trim(dir))
dev = dev and string.lower(trim(dev))
return (item.dir == dir and item.device == dev) or (item.dir and (not item.device or item.device == "") and item.dir == dir) or ((not item.dir or item.dir == "") and item.device and item.device == dev)
end
local plugin = Plugin:new(params, ds)
function plugin:onParseValues(data)
local result = {}
for _, v in ipairs(data) do
local metric, rest = string.match(v.metric, '([^|]+)|?(.*)')
local boundary_metric = metric_mapping[metric]
if boundary_metric then
if string.find(metric, 'total') then
result[boundary_metric] = { value = v.value, timestamp = v.timestamp }
elseif rest then
local dir, dev = string.match(rest, '^dir=(.+)&dev=(.+)')
dir = urldecode(dir)
dev = urldecode(dev)
for _, item in ipairs(params.items) do
if matchItem(item, dir, dev) then
local source = self.source .. '.' .. (item.diskname or dir .. '.' .. dev)
ipack(result, boundary_metric, v.value, v.timestamp, source)
break
end
end
end
end
end
return result
end
plugin:run()