File: ntop.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (227 lines) | stat: -rw-r--r-- 7,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
--
-- (C) 2013 - ntop.org
--

-- Ntop lua class example

function printTable(table,key)
  if (key ~= nil) then print(""..key..":<ul>") 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("<li>"..k .." = "..v.."<br>")
    end
  end
  print("</ul>")
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"

local page_utils = require("page_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.
sendHTTPContentTypeHeader('text/html')


page_utils.print_header()

dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")

print('<html><head><title>ntopng API Lua example</title></head>')
print('<body>')
print('<h1>Examples of ntop lua class</h1>')
print('<p>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.<br><b>For more information, please read the source code of this file and the doxygen of API Lua.</b></p>')


print('<br><h2>Global variables</h2>')
print('<p>There are a few global variables that are defined by default when ntopng is running. The following list show the main variables:</p>')
print('<ul>')
print('<li>ifname = ' .. ifname)

print('</ul>')
print('</body></html>\n')


print('<br><h2>General information</h2>')
print('<p>The ntopng lua class provide a few methods to get information about the ntopng running instance.</p>')


print('<h4>ntopng information</h4>')
print('<pre><code>ntop.getInfo()</code></pre>')
print('<ul>')
for key, value in pairs(ntop.getInfo()) do
   print("<li>".. key.." = "..value.."<br>")
end
print('</ul>')

print('<h4>ntopng directory</h4>')
print('<pre><code>ntop.getDirs()</code></pre>')
print('<ul>')
for key, value in pairs(ntop.getDirs()) do
   print("<li>".. key.." = "..value.."<br>")
end
print('</ul>')

print('<h4>ntopng uptime</h4>')
print('<pre><code>ntop.getUptime()</code></pre>')
print('<ul>')
print("<li>uptime = "..ntop.getUptime().."<br>")
print('</ul>')

print('<h4>ntopng get time msec</h4>')
print('<pre><code>ntop.gettimemsec()</code></pre>')
print('<ul>')
print("<li>time = "..ntop.gettimemsec().."<br>")
print('</ul>')

print('<h4>ntopng check trace mode</h4>')
print('<pre><code>ntop.verboseTrace()</code></pre>')
print('<ul>')
if (ntop.verboseTrace()) then
  print("<li>mode = MAX_TRACE_LEVEL<br>")
else
  print("<li>mode != MAX_TRACE_LEVEL<br>")
end
print('</ul>')

print('<h4>ntopng check if the system is windows</h4>')
print('<pre><code>ntop.isWindows()</code></pre>')
print('<ul>')
if (ntop.isWindows()) then
  print("<li>true<br>")
else
  print("<li>false<br>")
end
print('</ul>')

print('<h4>ntopng dumpFile</h4>')
print('<p>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.</p>')


print('<br><h2>Preference values</h2>')
-- print('<p>The ntopng lua class provide a few methods to get information about the redis cache.</p>')
print('<pre><code>ntop.getPrefs()</code></pre>')
printTable(ntop.getPrefs())



print('<br><h2>Redis</h2>')
print('<p>The ntopng lua class provide a few methods to get information about the redis cache.</p>')

print('<h4>ntopng set and get (key,value) in redis cache</h4>')
print('<pre><code>key_name = "ntopng.prefs.ifid_"..tostring(interface.name2id(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)</code></pre>')
key_name = 'ntopng.prefs.ifid_'..tostring(interface.name2id(ifname))..'.name'
test_name = "redis_cache_set_name"
key_speed = 'ntopng.prefs.ifid_'..tostring(interface.name2id(ifname))..'.speed'
test_speed = "123456"

ntop.setCache(key_name,test_name)
ntop.setCache(key_speed,test_speed)

print('<p>Output:<ul>')
-- print("<li> Set: ".. key_name .." = ".. test_name .."<br>")
print("<li>".. key_name .." = ".. ntop.getCache(key_name) .."<br>")
-- print("<li> Set: ".. key_speed .." = ".. test_speed .."<br>")
print("<li>".. key_speed .." = ".. ntop.getCache(key_speed) .."<br>")
print('</ul></p>')

print('<h4>ntopng delete (key,value) from redis cache</h4>')
print('<pre><code>ntop.delCache(key_name)</code></pre>')
ntop.delCache(key_name)

print('<p>Output:<ul>')
-- print("<li> Set: ".. key_name .." = ".. test_name .."<br>")
print("<li>".. key_name .." = ".. ntop.getCache(key_name) .."<br>")
-- print("<li> Set: ".. key_speed .." = ".. test_speed .."<br>")
print("<li>".. key_speed .." = ".. ntop.getCache(key_speed) .."<br>")
print('</ul></p>')



print('<h4>ntopng set and get hash in redis cache</h4>')
print('<pre><code>ntop.setHashCache(\"ntopng.alternate_names\", \"127.0.0.1\", \"test_name\")\n\nntop.getHashCache(\"ntopng.alternate_names\", \"127.0.0.1\")</code></pre>')

ntop.setHashCache("ntopng.alternate_names", "127.0.0.1", "test_name")

print('<p>Output:<ul>')
print("<li>ntopng.alternate_names = "..ntop.getHashCache("ntopng.alternate_names", "127.0.0.1") .."<br>")
print('</ul></p>')


print('<br><h2>Address resolution</h2>')
print('<p>The ntopng lua class provide a few methods to get information about the address resolution.</p>')

print('<h4>ntopng resolve the IP address and get host name</h4>')
print('<pre><code>ntop.resolveName("127.0.0.1")</code></pre>')

print('<ul>')
print("<li>127.0.0.1 = "..ntop.resolveName("127.0.0.1").."<br>")
print('</ul>')

print('<br><h2>Logging</h2>')
-- print('<p>The ntopng lua class provide a few methods to get information about the address resolution.</p>')

print('<h4>Syslog</h4>')
print('<pre><code>ntop.syslog(true,"Sample syslog LOG_ERROR message by ntopng lua API.")\nntop.syslog(false,"Sample syslog LOG_INFO message by ntopng lua API.")</code></pre>')

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('<ul>')
print("<li>Check the output: tail -f /var/log/system.log .<br>By default, syslog configuration does not show in the system.log file the messages with Level set to info.<br>")
print('</ul>')

print('<br><h2>Users</h2>')
print('<h4>List of current ntop users</h4>')
print('<pre><code>ntop.getUsers()</code></pre>')
printTable(ntop.getUsers())

print('<br><h4>Create new ntop user</h4>')
print('<pre><code>ntop.addUser("test","test_full_name","test_pw")</code></pre>')
ntop.addUser("test","test_full_name","test_pw")

print('<h4>List of current ntop users</h4>')
printTable(ntop.getUsers())

print('<br><h4>Reset user password</h4>')
print('<pre><code>ntop.resetUserPassword("test","test_pw","new_pw")</code></pre>')
ntop.resetUserPassword("test","test_pw","new_pw")

print('<br><h4>Delete user</h4>')
print('<pre><code>ntop.deleteUser("test")</code></pre>')
ntop.deleteUser("test")

print('<h4>List of current ntop users</h4>')
printTable(ntop.getUsers())



print('<br><h4>TDB</h4>')
print('<p><ul>')
print('<li>getMembersCache')
print('<li>delHashCache')
print('<li>getHashKeysCache')
print('<li>delHashCache')
print('<li>setPopCache')
print('<li>dumpDailyStats')
print('<li>getHostId')
print('<li>getIdToHost')
print('<li>ZMQ')
print('</ul></p>')

dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")