--
-- (C) 2013 - ntop.org
--
-- Ntop lua class example
function printTable(table,key)
if (key ~= nil) then print(""..key..":
") end
for k, v in pairs(table) do
if (type(v) == "table") then
printTable(table[k],k)
else
if (type(v) == "boolean") then
if (v) then v = "true" else v = "false" end
end
print("- "..k .." = "..v.."
")
end
end
print("
")
end
-- Set package.path information to be able to require lua module
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
if ( (dirs.scriptdir ~= nil) and (dirs.scriptdir ~= "")) then package.path = dirs.scriptdir .. "/lua/modules/?.lua;" .. package.path end
require "lua_utils"
-- Here you can choose the type of your HTTP message {'text/html','application/json',...}. There are two main function that you can use:
-- function sendHTTPHeaderIfName(mime, ifname, maxage)
-- function sendHTTPHeader(mime)
-- For more information please read the scripts/lua/modules/lua_utils.lua file.
sendHTTPHeader('text/html; charset=iso-8859-1')
ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
print('ntopng API Lua example')
print('')
print('Examples of ntop lua class
')
print('This class provides a set of general functions used to interact with ntopng configuration. In the following page we provide an example of which information can you get by this class.
For more information, please read the source code of this file and the doxygen of API Lua.
')
print('
Global variables
')
print('There are a few global variables that are defined by default when ntopng is running. The following list show the main variables:
')
print('')
print('- ifname = ' .. ifname)
print('
')
print('\n')
print('
General information
')
print('The ntopng lua class provide a few methods to get information about the ntopng running instance.
')
print('ntopng information
')
print('ntop.getInfo()
')
print('')
for key, value in pairs(ntop.getInfo()) do
print("- ".. key.." = "..value.."
")
end
print('
')
print('ntopng directory
')
print('ntop.getDirs()
')
print('')
for key, value in pairs(ntop.getDirs()) do
print("- ".. key.." = "..value.."
")
end
print('
')
print('ntopng uptime
')
print('ntop.getUptime()
')
print('')
print("- uptime = "..ntop.getUptime().."
")
print('
')
print('ntopng get time msec
')
print('ntop.gettimemsec()
')
print('')
print("- time = "..ntop.gettimemsec().."
")
print('
')
print('ntopng check trace mode
')
print('ntop.verboseTrace()
')
print('')
if (ntop.verboseTrace()) then
print("- mode = MAX_TRACE_LEVEL
")
else
print(" - mode != MAX_TRACE_LEVEL
")
end
print('
')
print('ntopng check if the system is windows
')
print('ntop.isWindows()
')
print('')
if (ntop.isWindows()) then
print("- true
")
else
print(" - false
")
end
print('
')
print('ntopng dumpFile
')
print('Dumps the specified file onto the returned web page. Usually it is used to create simple server-side page includes. In this case we have dump the file containing the header html.
')
print('
Preference values
')
-- print('The ntopng lua class provide a few methods to get information about the redis cache.
')
print('ntop.getPrefs()
')
printTable(ntop.getPrefs())
print('
Redis
')
print('The ntopng lua class provide a few methods to get information about the redis cache.
')
print('ntopng set and get (key,value) in redis cache
')
print('key_name = "ntopng.prefs."..ifname..".name"\ntest_name = "redis_cache_set_name"\nkey_speed = "ntopng.prefs."..ifname..".speed"\ntest_speed = "123456"\n\nntop.setCache(key_name,test_name)\nntop.setCache(key_speed,test_speed)\n\nntop.getCache(key_name)\nntop.getCache(key_speed)
')
key_name = 'ntopng.prefs.'..ifname..'.name'
test_name = "redis_cache_set_name"
key_speed = 'ntopng.prefs.'..ifname..'.speed'
test_speed = "123456"
ntop.setCache(key_name,test_name)
ntop.setCache(key_speed,test_speed)
print('Output:
')
-- print("- Set: ".. key_name .." = ".. test_name .."
")
print(" - ".. key_name .." = ".. ntop.getCache(key_name) .."
")
-- print(" - Set: ".. key_speed .." = ".. test_speed .."
")
print(" - ".. key_speed .." = ".. ntop.getCache(key_speed) .."
")
print('
')
print('ntopng delete (key,value) from redis cache
')
print('ntop.delCache(key_name)
')
ntop.delCache(key_name)
print('Output:
')
-- print("- Set: ".. key_name .." = ".. test_name .."
")
print(" - ".. key_name .." = ".. ntop.getCache(key_name) .."
")
-- print(" - Set: ".. key_speed .." = ".. test_speed .."
")
print(" - ".. key_speed .." = ".. ntop.getCache(key_speed) .."
")
print('
')
print('ntopng set and get hash in redis cache
')
print('ntop.setHashCache(\"ntopng.alternate_names\", \"127.0.0.1\", \"test_name\")\n\nntop.getHashCache(\"ntopng.alternate_names\", \"127.0.0.1\")
')
ntop.setHashCache("ntopng.alternate_names", "127.0.0.1", "test_name")
print('Output:
')
print("- ntopng.alternate_names = "..ntop.getHashCache("ntopng.alternate_names", "127.0.0.1") .."
")
print('
')
print('
Address resolution
')
print('The ntopng lua class provide a few methods to get information about the address resolution.
')
print('ntopng resolve the IP address and get host name
')
print('ntop.resolveAddress("127.0.0.1")
')
print('')
print("- 127.0.0.1 = "..ntop.resolveAddress("127.0.0.1").."
")
print('
')
print('
Logging
')
-- print('The ntopng lua class provide a few methods to get information about the address resolution.
')
print('Syslog
')
print('ntop.syslog(true,"Sample syslog LOG_ERROR message by ntopng lua API.")\nntop.syslog(false,"Sample syslog LOG_INFO message by ntopng lua API.")
')
ntop.syslog(true,"Sample syslog LOG_ERROR message by ntopng lua API.")
ntop.syslog(false,"Sample syslog LOG_INFO message by ntopng lua API.")
print('')
print("- Check the output: tail -f /var/log/system.log .
By default, syslog configuration does not show in the system.log file the messages with Level set to info.
")
print('
')
print('
Users
')
print('List of current ntop users
')
print('ntop.getUsers()
')
printTable(ntop.getUsers())
print('
Create new ntop user
')
print('ntop.addUser("test","test_full_name","test_pw")
')
ntop.addUser("test","test_full_name","test_pw")
print('List of current ntop users
')
printTable(ntop.getUsers())
print('
Reset user password
')
print('ntop.resetUserPassword("test","test_pw","new_pw")
')
ntop.resetUserPassword("test","test_pw","new_pw")
print('
Delete user
')
print('ntop.deleteUser("test")
')
ntop.deleteUser("test")
print('List of current ntop users
')
printTable(ntop.getUsers())
print('
TDB
')
print('')
print('- getMembersCache')
print('
- delHashCache')
print('
- getHashKeysCache')
print('
- delHashCache')
print('
- setPopCache')
print('
- dumpDailyStats')
print('
- getHostId')
print('
- getIdToHost')
print('
- ZMQ')
print('
')
dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")