Fix grafana endpoint to use new timeseries api

Fixes #1874
This commit is contained in:
emanuele-f 2018-08-08 11:46:33 +02:00
parent 8b66faeb9d
commit ae27a4c240
2 changed files with 44 additions and 14 deletions

View file

@ -8,6 +8,7 @@ package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "graph_utils"
require "grafana_utils"
local ts_utils = require("ts_utils")
interface.select(ifname)
local ifnames = interface.getIfNames()
@ -25,7 +26,7 @@ else
-- override max_num_points in singlerrd2json
global_max_num_points = _POST["payload"]["maxDataPoints"]
if global_max_num_points > 600 then
global_max_num_points = 600 -- ensures 100% match between ntopng and grafana charts
global_max_num_points = 600
end
local res = {}
@ -49,18 +50,43 @@ else
local ifname = string.gsub(t["target"], "^(.-)_", "") -- lazy match to remove up to the first underscore
ifname = string.gsub(ifname, "_(.-)$", "") -- lazy match to remove up to the last underscore
local rrdfile = ""
if is_traffic then rrdfile = "bytes.rrd"
elseif is_packets then rrdfile = "packets.rrd"
elseif is_allprotos then rrdfile = "all"
elseif is_allcategories then rrdfile = "all_ndpi_categories" end
local schema = nil
local is_topk = false
if is_traffic then
if is_host then schema = "host:traffic" else schema = "iface:traffic" end
elseif is_packets then schema = "iface:packets"
elseif is_allprotos then
if is_host then schema = "host:ndpi" else schema = "iface:ndpi" end
is_topk = true
elseif is_allcategories then
if is_host then schema = "host:ndpi_categories" else schema = "iface:ndpi_categories" end
is_topk = true
else goto continue end
local datapoints = {}
local rr = rrd2json(getInterfaceId(ifname), addr, rrdfile, epoch_begin, epoch_end)
local options = {
max_num_points = global_max_num_points
}
local totalval = rr["totalval"]
rr = json.decode(rr["json"])
local rr
local tags = {ifid=getInterfaceId(ifname), host=addr}
if is_topk then
rr = ts_utils.queryTopk(schema, tags, epoch_begin, epoch_end, options)
else
rr = ts_utils.query(schema, tags, epoch_begin, epoch_end, options)
end
if not rr then
goto continue
end
local totalval = nil
if rr.statistics then
totalval = rr.statistics.total
end
local label = ifname
if is_host then label = addr..", "..label end
@ -69,7 +95,9 @@ else
if is_allprotos then
toSeries(rr, res, label)
elseif string.ends(t["target"], "traffic_total_bytes") or string.ends(t["target"], "traffic_total_packets") then
if totalval then
res[#res + 1] = {target="Total", datapoints={{totalval, 0 --[[ it's an integral, an instant is not meaningful here --]]}}}
end
else
toSeries(rr, res, label)
end
@ -77,6 +105,7 @@ else
-- tprint({target=target, is_traffic=is_traffic, is_packets=is_packets, entity_name=entity_name})
::continue::
end
--tprint(_POST["payload"])