-- -- (C) 2014-15 - ntop.org -- -- This file contains the description of all functions -- used to trigger host alerts local verbose = false local prefs = ntop.getPrefs() local info = ntop.getInfo() local menu_subpages = require "prefs_menu" show_advanced_prefs_key = "ntopng.prefs.show_advanced_prefs" -- ############################################ -- -- A menu is a list of menu entries -- A menu entry is composed by the following fields: -- - id: the subpage id -- - label: a label to be shown into the menu -- - entries: a list subpages (see belo) -- -- The following optional fields can be specified -- -- - advanced: if true, it should be only shown when advanced settings are enabled -- - pro_only: if true, it should be only shown in ntopng pro version -- - enterprise_only: if true, it should be only shown in ntopng enterprise version -- - disabled: if true, the entry is hidden -- -- A subpage is composed by the following fields: -- - title: the subpage title -- - description: the subpage description -- - hidden: (optional) if true, this subpage is hidden -- function isSubpageAvailable(subpage, show_advanced_prefs) if show_advanced_prefs == nil then show_advanced_prefs = toboolean(ntop.getPref(show_advanced_prefs_key)) end if (subpage.disabled) or ((subpage.advanced) and (not show_advanced_prefs)) or ((subpage.pro_only) and (not ntop.isPro())) or ((subpage.enterprise_only) and (not info["version.enterprise_edition"])) then return false end return true end local subpage_active = nil function prefsGetActiveSubpage(show_advanced_prefs, tab) for _, subpage in ipairs(menu_subpages) do if not isSubpageAvailable(subpage, show_advanced_prefs) then subpage.disabled = true if subpage.id == tab then -- will set to default tab = nil end elseif subpage.id == tab then subpage_active = subpage end end -- default subpage if isEmptyString(tab) then -- Pick the first available subpage for _, subpage in ipairs(menu_subpages) do if isSubpageAvailable(subpage, show_advanced_prefs) then subpage_active = subpage tab = subpage.id break end end end return subpage_active, tab end function printMenuSubpages(tab) for _, subpage in ipairs(menu_subpages) do if not subpage.disabled then print[[]] print(subpage.label) print[[]] end end end -- ############################################ -- notify ntopng upon preference changes function notifyNtopng(key) if key == nil then return end -- notify runtime ntopng configuration changes if string.starts(key, 'nagios') then if verbose then io.write('notifying ntopng upon nagios pref change\n') end ntop.reloadNagiosConfig() elseif string.starts(key, 'toggle_logging_level') then if verbose then io.write('notifying ntopng upon logging level pref change\n') end ntop.setLoggingLevel(value) end end -- ############################################ local options_script_loaded = false local options_ctr = 0 function prefsResolutionButtons(fmt, value, fixed_id) local ctrl_id if fixed_id ~= nil then ctrl_id = fixed_id else ctrl_id = "options_group_" .. options_ctr options_ctr = options_ctr + 1 end local res = makeResolutionButtons(FMT_TO_DATA_TIME, ctrl_id, fmt, value, {classes={"pull-right"}}) print(res.html) print("") return res.value end -- ############################################ -- Runtime preference function prefsInputFieldPrefs(label, comment, prekey, key, default_value, _input_type, showEnabled, disableAutocomplete, allowURLs, extra) extra = extra or {} if(string.ends(prekey, ".")) then k = prekey..key else k = prekey.."."..key end if(_POST[key] ~= nil) then v_s = _POST[key] v = tonumber(v_s) v_cache = ntop.getPref(k) value = v_cache if ((v_cache==nil) or (v ~= v_cache)) then if(v ~= nil and (v > 0) and (v <= 86400)) then ntop.setPref(k, tostring(v)) value = v elseif (v_s ~= nil) then if(allowURLs or (extra.pattern == getURLPattern())) then v_s = string.gsub(v_s, "ldaps:__", "ldaps://") v_s = string.gsub(v_s, "ldap:__", "ldap://") v_s = string.gsub(v_s, "http:__", "http://") v_s = string.gsub(v_s, "https:__", "https://") end ntop.setPref(k, v_s) value = v_s end -- least but not last we asynchronously notify the runtime ntopng instance for changes notifyNtopng(key) end else local v_s = nil if not isEmptyString(prekey) then v_s = ntop.getPref(k) end value = v_s if((v_s==nil) or (v_s=="") or (v_s=="nil")) then value = default_value if not isEmptyString(prekey) then ntop.setPref(k, tostring(default_value)) notifyNtopng(key) end end end if ((showEnabled == nil) or (showEnabled == true)) then showEnabled = "table-row" else showEnabled = "none" end local attributes = {} if extra.min ~= nil then if extra.tformat ~= nil then attributes["data-min"] = extra.min else attributes["min"] = extra.min end end if extra.max ~= nil then if extra.tformat ~= nil then attributes["data-max"] = extra.max else attributes["max"] = extra.max end end if extra.disabled == true then attributes["disabled"] = "disabled" end if extra.required == true then attributes["required"] = "" end if extra.pattern ~= nil then attributes["pattern"] = extra.pattern end if (_input_type == "number") then attributes["required"] = "required" end local input_type = "text" if _input_type ~= nil then input_type = _input_type end print('
'..comment..'
| ]] if extra.tformat ~= nil then value = prefsResolutionButtons(extra.tformat, value) end if extra.width == nil then if _input_type == "number" then style["width"] = "8em" else style["width"] = "20em" end style["margin-left"] = "auto" else style["width"] = "15em" end style["margin-left"] = "auto" style = table.merge(style, extra.style) attributes = table.merge(attributes, extra.attributes) print[[ | ||
'..comment..'
'..comment..'
'..comment..'