-- -- (C) 2018 - ntop.org -- local format_utils = require("format_utils") local custom_column_utils = {} local dirs = ntop.getDirs() local custom_column_pref_key = "ntopng.prefs.custom_column" -- IMPORTANT: keep it in sync with sortField (ntop_typedefs.h) -- AND host_search_walker:NetworkInterface.cpp -- AND NetworkInterface::getFlows() custom_column_utils.available_custom_columns = { -- KEY LABEL Host::lua()-label formatting { "traffic_sent", i18n("flows_page.total_bytes_sent"), "bytes.sent", bytesToSize, "right" }, { "traffic_rcvd", i18n("flows_page.total_bytes_rcvd"), "bytes.rcvd", bytesToSize, "right" }, { "traffic_unknown", i18n("flows_page.total_bytes_unknown"), "bytes.ndpi.unknown", bytesToSize, "right" }, { "num_flows_as_client", i18n("flows_page.flows_as_client"), "active_flows.as_client", format_utils.formatValue, "center" }, { "num_flows_as_server", i18n("flows_page.flows_as_server"), "active_flows.as_server", format_utils.formatValue, "center" }, { "total_num_anomalous_flows_as_client", i18n("total_outgoing_anomalous_flows"), "anomalous_flows.as_client", format_utils.formatValue, "center" }, { "total_num_anomalous_flows_as_server", i18n("total_incoming_anomalous_flows"), "anomalous_flows.as_server", format_utils.formatValue, "center" }, { "total_num_unreachable_flows_as_client", i18n("total_outgoing_unreachable_flows"), "unreachable_flows.as_client", format_utils.formatValue, "center" }, { "total_num_unreachable_flows_as_server", i18n("total_incoming_unreachable_flows"), "unreachable_flows.as_server", format_utils.formatValue, "center" }, { "alerts", i18n("show_alerts.engaged_alerts"), "num_alerts", format_utils.formatValue, "center" }, { "total_alerts", i18n("alerts_dashboard.total_alerts"), "total_alerts", format_utils.formatValue, "center" }, } local available_custom_columns = custom_column_utils.available_custom_columns -- ########################################### function custom_column_utils.hostStatsToColumnValue(host_stats, column, formatted) for _, c in ipairs(available_custom_columns) do if c[1] == column then local k = c[3] if formatted then local val = host_stats[c[3]] if val ~= nil and val > 0 then return c[4](val) else return "0" end else return host_stats[c[3]] end end end end -- ########################################### function custom_column_utils.label2criteriakey(what) what = what:gsub("^column_", "") local id for id, _ in ipairs(available_custom_columns) do local c = available_custom_columns[id][1] local fnctn = available_custom_columns[id][4] if(what == c) then return what, fnctn end end return what, format_utils.formatValue end -- ########################################### function custom_column_utils.getCustomColumnName() local cc = ntop.getPref(custom_column_pref_key) local res = "" for _, lg in ipairs(custom_column_utils.available_custom_columns) do local key = lg[1] local label = lg[2] local align = lg[5] if cc == key then return label, key, align end end return custom_column_utils.available_custom_columns[1][2], custom_column_utils.available_custom_columns[1][1], custom_column_utils.available_custom_columns[1][5] end -- ########################################### local function setCustomColumnName(custom_column_name) local res for _, lg in ipairs(custom_column_utils.available_custom_columns) do local key = lg[1] local label = lg[2] if custom_column_name == key then ntop.setPref(custom_column_pref_key, custom_column_name) return true end end return false end -- ########################################### function custom_column_utils.updateCustomColumn() local _, current_column = custom_column_utils.getCustomColumnName() if not isEmptyString(_GET["custom_column"]) and (_GET["custom_column"] ~= current_column) then if setCustomColumnName(_GET["custom_column"]) then local custom_column_key, custom_column_format = custom_column_utils.label2criteriakey(_GET["custom_column"]) tablePreferences("sort_hosts", "column_"..custom_column_key) tablePreferences("sort_order_hosts", "desc") end end end -- ########################################### function custom_column_utils.isCustomColumn(column) local c = column:gsub("^column_", "") for _, lg in ipairs(custom_column_utils.available_custom_columns) do if c == lg[1] then return true end end return false end -- ########################################### function custom_column_utils.printCustomColumnDropdown(base_url, page_params) local custom_column = custom_column_utils.getCustomColumnName() local custom_column_params = table.clone(page_params) custom_column_params["custom_column"] = nil print[[\ \
]] end -- ########################################### return custom_column_utils