-- -- (C) 2014-19 - ntop.org -- local dirs = ntop.getDirs() package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path local alerts_api = require "alerts_api" local format_utils = require "format_utils" local user_scripts = require "user_scripts" local flow_callbacks_utils = {} -- ############################################## local function print_callbacks_config_tbody_simple_view(descr) print[[]] print[[]] print[[]] print(i18n("flow_callbacks.callback")) print[[]] print[[]] print(i18n("flow_callbacks.callback_config")) print[[]] print[[]] print(i18n("flow_callbacks.callback_function_duration_simple_view")) print[[]] print[[]] print[[
]] print('\n') local has_modules = false for _, user_script in pairsByKeys(descr.modules, asc) do if true --[[user_script.gui]] then if not has_modules then has_modules = true end local title local description if(user_script.gui) then title = i18n(user_script.gui.i18n_title) or user_script.gui.i18n_title description = i18n(user_script.gui.i18n_description) or user_script.gui.i18n_description else title = user_script.key description = "" end print("".. title .."
") print(""..description..".\n") print("") if(user_script.gui and user_script.gui.input_builder) then print(user_script.gui.input_builder(user_script)) else print('') end print("") if user_script.benchmark and table.len(user_script.benchmark) > 0 then local worst_case_benchmark local max_duration local num_calls local avg_speed local fmt for mod_fn, mod_benchmark in pairsByKeys(user_script.benchmark, asc) do -- Just show the maximum (worst-case) duration among all available functions if not max_duration or max_duration < mod_benchmark["tot_elapsed"] then max_duration = mod_benchmark["tot_elapsed"] num_calls = mod_benchmark["tot_num_calls"] avg_speed = mod_benchmark["avg_speed"] end end if num_calls > 1 then fmt = i18n("flow_callbacks.callback_function_duration_fmt_long", {num_calls = format_utils.formatValue(num_calls), time = format_utils.secondsToTime(max_duration), speed = format_utils.formatValue(round(avg_speed, 0))}) else fmt = i18n("flow_callbacks.callback_function_duration_fmt_short", {time = format_utils.secondsToTime(max_duration)}) end print(string.format("%s", fmt)) else print("") end print("") end end if not has_modules then print(""..i18n("flow_callbacks.no_callbacks_defined")..".") end print[[]] return has_modules end -- ################################# local function print_callbacks_config_tbody_expert_view(descr) print[[]] print[[]] print[[]] print[[]] print(i18n("flow_callbacks.callback_latest_run")) print[[]] print[[]] print[[]] print[[]] print(i18n("flow_callbacks.callback")) print[[]] print[[]] print(i18n("flow_callbacks.callback_config")) print[[]] print[[]] print(i18n("flow_callbacks.callback_function")) print[[]] print[[]] print(i18n("flow_callbacks.callback_function_duration")) print[[]] print[[]] print(i18n("flow_callbacks.callback_function_num_flows")) print[[]] print[[]] print(i18n("flow_callbacks.callback_function_throughput")) print[[]] print[[]] print[[]] print('\n') local has_modules = false for _, user_script in pairsByKeys(descr.modules, asc) do if true --[[user_script.gui]] then if not has_modules then has_modules = true end local rowspan = 1 if user_script.benchmark and table.len(user_script.benchmark) > 0 then rowspan = table.len(user_script.benchmark) end local title local description if(user_script.gui) then title = i18n(user_script.gui.i18n_title) or user_script.gui.i18n_title description = i18n(user_script.gui.i18n_description) or user_script.gui.i18n_description else title = user_script.key description = "" end print("".. title .."
") print(""..description..".\n") print("") if(user_script.gui and user_script.gui.input_builder) then print(user_script.gui.input_builder(user_script)) else print('') end print("") print("") local num = 1 if user_script.benchmark and table.len(user_script.benchmark) > 0 then for mod_fn, mod_benchmark in pairsByKeys(user_script.benchmark, asc) do local avg_fps = mod_benchmark["tot_num_calls"] / mod_benchmark["tot_elapsed"] if avg_fps ~= avg_fps or not avg_fps or avg_fps < 0.01 then avg_fps = "< 0.01" else avg_fps = format_utils.formatValue(format_utils.round(avg_fps, 0)) end local trtd, slash_tdtr = "", "" if num == 1 then trtd = '' end print(string.format("%s %s %s%s%s %s
%s", trtd, mod_fn, format_utils.secondsToTime(mod_benchmark["tot_elapsed"]), format_utils.formatValue(mod_benchmark["tot_num_calls"]), avg_fps, i18n("flow_callbacks.callback_elapsed_time_avg"), slash_tdtr)) num = num + 1 end else print("") end end end if not has_modules then print(""..i18n("flow_callbacks.no_callbacks_defined")..".") end print[[]] return has_modules end -- ################################# function flow_callbacks_utils.print_callbacks_config() local show_advanced_prefs = false if _POST and _POST["show_advanced_prefs"] and _POST["show_advanced_prefs"] == "true" then show_advanced_prefs = true end local ifid = interface.getId() local descr = user_scripts.load(user_scripts.script_types.flow, ifid, "flow", nil, true --[[ also return disabled ]]) print [[
]] if table.len(_POST) > 0 then for mod_key, user_script in pairs(descr.modules) do local pref_key = "enabled_" .. mod_key local val = _POST[pref_key] if(val ~= nil) then if(val == "on") then user_scripts.enableModule(ifid, "flow", mod_key) user_script.enabled = true else user_scripts.disableModule(ifid, "flow", mod_key) user_script.enabled = false end end end end local has_modules if show_advanced_prefs then has_modules = print_callbacks_config_tbody_expert_view(descr) else has_modules = print_callbacks_config_tbody_simple_view(descr) end print[[
]] if has_modules then print[[]] print[[]] end print[[
]] print[[
]] local cls_on = "btn btn-sm" local cls_off = cls_on if show_advanced_prefs then cls_on = cls_on..' btn-primary active' cls_off = cls_off..' btn-default' else cls_on = cls_on..' btn-default' cls_off = cls_off..' btn-primary active' end print('') print('') print[[
]] print("
" .. i18n("flow_callbacks.notes") .. ":
") end return flow_callbacks_utils