--
-- (C) 2014-15 - ntop.org
--
-- This file contains the description of all functions
-- used to trigger host alerts
local verbose = false
-- 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
-- ############################################
-- Runtime preference
function prefsInputFieldPrefs(label, comment, prekey, key, default_value, _input_type, showEnabled, disableAutocomplete, allowURLs)
if(string.ends(prekey, ".")) then
k = prekey..key
else
k = prekey.."."..key
end
if(_GET[key] ~= nil) then
v_s = _GET[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) then
io.write("Hello\n")
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 ascynchronously notify the runtime ntopng instance for changes
notifyNtopng(key)
end
else
v_s = ntop.getPref(k)
value = v_s
if((v_s==nil) or (v_s=="")) then
ntop.setPref(k, tostring(default_value))
value = default_value
notifyNtopng(key)
end
end
if ((showEnabled == nil) or (showEnabled == true)) then
showEnabled = "table-row"
else
showEnabled = "none"
end
local input_type = "text"
if _input_type ~= nil then input_type = _input_type end
print('
'..label..'
'..comment..'
')
print [[
]]
print [[
]]
end
function toggleTableButton(label, comment, on_label, on_value, on_color , off_label, off_value, off_color, submit_field, redis_key, disabled)
if(_GET[submit_field] ~= nil) then
ntop.setPref(redis_key, _GET[submit_field])
value = _GET[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')
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(_GET[submit_field] ~= nil) then
if ( (value == nil) or (value ~= _GET[submit_field])) then
ntop.setPref(redis_key, _GET[submit_field])
value = _GET[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
function multipleTableButtonPrefs(label, comment, array_labels, array_values, default_value, selected_color,
submit_field, redis_key, disabled, elementToSwitch, showElementArray,
javascriptAfterSwitch, showElement)
if(_GET[submit_field] ~= nil) then
ntop.setPref(redis_key, _GET[submit_field])
value = _GET[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
type_button = "btn-"..selected_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(_GET[submit_field] ~= nil) then
ntop.setCache(redis_key, _GET[submit_field])
value = _GET[submit_field]
notifyNtopng(submit_field, _GET[submit_field])
else
value = ntop.getCache(redis_key)
end
if value == "" or value == nil then
value = "normal"
end
if(value == "trace") then color = "default"
elseif(value == "debug") then color = "success"
elseif(value == "info") then color = "info"
elseif(value == "normal") then color = "primary"
elseif(value == "warning") then color = "warning"
elseif(value == "error") then color = "danger"
else color = "default"
end
if(label ~= "") then print('