Adds hosts support for grafana

This commit is contained in:
Simone Mainardi 2017-06-09 13:11:33 +02:00
parent 5d9e90e17b
commit 1c579b5643
2 changed files with 88 additions and 26 deletions

View file

@ -24,29 +24,86 @@ else
-- tprint(_GRAFANA)
local target = _GRAFANA["payload"]["target"]
--[[
example targets:
interface_eth0
host_192.168.2.0
host_192.168.2.0_interface_eth0
--]]
if target == nil then
target = ""
end
local res = {}
local host_info
local target_interfaces = {}
-- PARSE the host part of the target
-- the host part is preceeded by prefix host_
if isEmptyString(target) or string.starts("host_", target) or string.starts(target, "host_") then
local addr = string.match(target or '', "_(.-)_") -- assumes address is between the first two underscores
if isEmptyString(addr) then
addr = string.match(target or '', "_(.-)$") -- assumes address is between the first underscore and the end of string
end
host_info = hostkey2hostinfo(addr)
-- tprint(host_info)
local a, b = string.find(target, addr)
target = string.sub(target, b + 2 --[[ +2 removes the optional uderscore preceding interface_ --]])
-- tprint({target=target})
end
-- PARSE the interface part of the target
-- the interface part is preceeded by prefix interface_
if isEmptyString(target) or string.starts("interface_", target) or string.starts(target, "interface_") then
local ifnames = interface.getIfNames()
for _, n in pairs(ifnames) do
local tb = "interface_"..n.."_bytes"
local tp = "interface_"..n.."_packets"
local tbt = "interface_"..n.."_bytestotal"
local tpt = "interface_"..n.."_packetstotal"
local tap = "interface_"..n.."_allprotocols"
local t = "interface_"..n
for _, t in pairs({tb, tp, tbt, tpt, tap}) do
if isEmptyString(target) or string.starts(t, target) or string.starts(target, t) then
res[#res +1] = t
end
if isEmptyString(target) or string.starts(t, target) or string.starts(target, t) then
target_interfaces[n] = 1
end
end
end
print(json.encode(res, nil))
local matches = {}
for n,_ in pairs(target_interfaces) do
interface.select(n)
matches[n] = {}
if host_info ~= nil then
local matching_hosts = interface.findHost(hostinfo2hostkey(host_info))
for addr, label in pairs(matching_hosts) do
matches[n][addr] = label
end
end
end
local rsp_targets = {}
local metrics = {"bytes", "bytestotal", "allprotocols"}
if host_info == nil then -- packets only for interfaces
metrics[#metrics + 1] = "packets"
metrics[#metrics + 1] = "packetstotal"
end
for _, metric in pairs(metrics) do
for n, hosts in pairs(matches) do
local interface_series = "interface_"..n.."_"..metric
for addr, label in pairs(hosts) do
local host_series = "host_"..addr
rsp_targets[#rsp_targets + 1] = host_series.."_"..interface_series
end
if host_info == nil then
rsp_targets[#rsp_targets + 1] = interface_series
end
end
end
-- tprint({matches=matches, rsp_targets=rsp_targets})
print(json.encode(rsp_targets, nil))
end