-- -- (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(''..label..'

'..comment..'') local style = {} style["text-align"] = "right" style["margin-bottom"] = "0.5em" print [[
]] 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[[
]] end function prefsInformativeField(label, comment, showEnabled, extra) local extra = extra or {} extra["style"] = extra["style"] or {} extra["style"]["display"] = "none" prefsInputFieldPrefs(label, comment, "", "", "", nil, showEnabled, nil, nil, extra) end function toggleTableButton(label, comment, on_label, on_value, on_color , off_label, off_value, off_color, submit_field, redis_key, disabled) if(_POST[submit_field] ~= nil) then ntop.setPref(redis_key, _POST[submit_field]) value = _POST[submit_field] notifyNtopng(submit_field) else value = ntop.getPref(redis_key) end if (disabled == true) then disabled = 'disabled = ""' else disabled = "" end -- Read it anyway to if(value == off_value) then rev_value = on_value on_active = "btn-default" off_active = "btn-"..off_color.." active" else rev_value = off_value on_active = "btn-"..on_color.." active" off_active = "btn-default" end if(label ~= "") then print(''..label..'

'..comment..'\n') end print('

\n
') print('') print('
\n') print('\n') print('\n') print('
\n') if(label ~= "") then print('') end return(value) end function toggleTableButtonPrefs(label, comment, on_label, on_value, on_color , off_label, off_value, off_color, submit_field, redis_key, default_value, disabled, elementToSwitch, hideOn, showElement) value = ntop.getPref(redis_key) if(_POST[submit_field] ~= nil) then if ( (value == nil) or (value ~= _POST[submit_field])) then ntop.setPref(redis_key, _POST[submit_field]) value = _POST[submit_field] notifyNtopng(submit_field) end else if ((value == nil) or (value == "")) then if (default_value ~= nil) then value = default_value else value = off_value end ntop.setPref(redis_key, value) notifyNtopng(submit_field) end end if (disabled == true) then disabled = 'disabled = ""' else disabled = "" end -- Read it anyway to if(value == off_value) then on_active = "btn-default" off_active = "btn-"..off_color.." active" else value = on_value on_active = "btn-"..on_color.." active" off_active = "btn-default" end local objRow = "" if ((showElement ~= nil) and (showElement == false)) then objRow = " style=\"display:none\"" else objRow = " style=\"display:table-row\"" end if(label ~= "") then print(''..label..'

'..comment..'\n') end print('

') print('') print('
\n') print('\n') if(label ~= "") then print('') end print('\n') print('\n') return(value) end local function get_pref_redis_key(options) return "ntopng.prefs." .. ternary(options.pref ~= nil, options.pref, options.field) end function prefsToggleButton(params) defaults = { to_switch = {}, -- a list of elements to be switched on or off on_text = "On", -- The text when the button is on on_value = "1", -- The value when the button is on on_class = "success", -- The css class when the button is on off_text = "Off", -- The text when the button is off off_value = "0", -- The value when the button is off off_class = "danger", -- The css class when the button is off reverse_switch = false -- If true, elements are hidden when the item is enabled } local options = table.merge(defaults, params) local redis_key = get_pref_redis_key(options) return toggleTableButtonPrefs(subpage_active.entries[options.field].title, subpage_active.entries[options.field].description .. (subpage_active.entries[options.field].content or ""), options.on_text, options.on_value, options.on_class, options.off_text, options.off_value, options.off_class, options.field, redis_key, options.default, options.disabled, options.to_switch, options.reverse_switch, not options.hidden) end function multipleTableButtonPrefs(label, comment, array_labels, array_values, default_value, selected_color, submit_field, redis_key, disabled, elementToSwitch, showElementArray, javascriptAfterSwitch, showElement) if(_POST[submit_field] ~= nil) then ntop.setPref(redis_key, _POST[submit_field]) value = _POST[submit_field] notifyNtopng(submit_field) else value = ntop.getPref(redis_key) if(value == "") then if(default_value ~= nil) then ntop.setPref(redis_key, default_value) value = default_value end end end if (disabled == true) then disabled = 'disabled = ""' else disabled = "" end local objRow = "" if ((showElement ~= nil) and (showElement == false)) then objRow = " style=\"display:none\"" else objRow = " style=\"display:table-row\"" end if(value ~= nil) then if(label ~= "") then print(''..label..'

'..comment..'\n') end print('

') for nameCount = 1, #array_labels do local type_button = "btn-default" if(value == array_values[nameCount]) then local color if type(selected_color) == "table" then color = selected_color[nameCount] else color = selected_color end type_button = "btn-"..color.." active" end print('\n') end print('
\n') print('\n') print('\n') if(label ~= "") then print('') end end return(value) end function loggingSelector(label, comment, submit_field, redis_key) prefs = ntop.getPrefs() if prefs.has_cmdl_trace_lvl then return end if(_POST[submit_field] ~= nil) then ntop.setCache(redis_key, _POST[submit_field]) value = _POST[submit_field] notifyNtopng(submit_field, _POST[submit_field]) else value = ntop.getCache(redis_key) end if value == "" or value == nil then value = "normal" end local logging_values = {"trace", "debug", "info", "normal", "warning", "error"} local color_map = {"default", "success", "info", "primary", "warning", "danger"} local logging_keys = {} local color = "default" for i,v in ipairs(logging_values) do logging_keys[i] = firstToUpper(v) end multipleTableButtonPrefs("Log level", "Choose the runtime logging level.", logging_keys, logging_values, value, color_map, submit_field, redis_key) return(value) end