File: callback_utils.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 (276 lines) | stat: -rw-r--r-- 7,960 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
--
-- (C) 2017-22 - ntop.org
--

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

local os_utils = require "os_utils"

local callback_utils = {}

-- ########################################################

-- Iterates available interfaces, excluding PCAP interfaces.
-- Each valid interface is select-ed and passed to the callback.
-- NOTE: direction must only be used by second.lua
function callback_utils.foreachInterface(ifnames, condition, callback, update_direction_stats)
   for _,_ifname in pairs(ifnames) do
      if(ntop.isShutdown()) then return true end

      -- NOTE: "eth" will be overwritten here for emulated directions
      interface.select(_ifname)

      if update_direction_stats then
	 interface.updateDirectionStats()
      end

      local ifstats = interface.getStats()

      if condition == nil or condition(ifstats.id) then
	 if((ifstats.type ~= "pcap dump") and (ifstats.type ~= "unknown")) then
	    if callback(_ifname, ifstats, false) == false then
	       return false
	    end
	 end
      end
   end

   return true
end

-- ########################################################

-- An iterator on the C batched API
--
--    batched_function: the function to call
--    field: a string to get the value from a slot
--    function_params : parameters to pass to the function
--
local function getBatchedIterator(batched_function, field, function_params)
   local debug_enabled = false
   local loaded_elems = nil
   local nextSlot = 0
   local iterator_finished = false
   local first_iteration = true
   function_params = function_params or {}

   return function()
      if (loaded_elems == nil) or table.empty(loaded_elems) then
         if loaded_elems ~= nil then
            -- that was the first iteration
            first_iteration = false
         end

         if ((nextSlot == 0) or (nextSlot == nil)) and not first_iteration then
            iterator_finished = true
         end

         if iterator_finished then
	    return nil
	 end

         -- we need to load new slots from C
         if(debug_enabled) then
            io.write("getBatchedIterator["..field.."](curSlot=".. nextSlot ..")\n")
         end

         -- Assumption: nextSlot is always the first parameter
         local slot = batched_function(nextSlot, table.unpack(function_params))

         if slot == nil then
            iterator_finished = true
            return nil
         end

         nextSlot = slot.nextSlot
         loaded_elems = slot[field]

         if(debug_enabled) then
            io.write("getBatchedIterator["..field.."] nextSlot=".. nextSlot ..")\n")
         end

      end

      for key, value in pairs(loaded_elems) do
         loaded_elems[key] = nil -- pop
         return key, value
      end
   end
end

-- A batched iterator over the active flows
-- @param flows_filter A table containing flow filters matching those specified in Paginator.cpp
function callback_utils.getFlowsIterator(flows_filter)
   return getBatchedIterator(interface.getBatchedFlowsInfo, "flows",  flows_filter)
end

-- A batched iterator over the local hosts with timeseries
function callback_utils.getLocalHostsTsIterator(...)
   return getBatchedIterator(interface.getBatchedLocalHostsTs, "hosts", { ... })
end

-- A batched iterator over the local hosts
function callback_utils.getLocalHostsIterator(...)
   return getBatchedIterator(interface.getBatchedLocalHostsInfo, "hosts", { ... })
end

-- A batched iterator over the remote hosts
function callback_utils.getRemoteHostsIterator(...)
   return getBatchedIterator(interface.getBatchedRemoteHostsInfo, "hosts", { ... })
end

-- A batched iterator over the hosts (both local and remote)
function callback_utils.getHostsIterator(...)
   return getBatchedIterator(interface.getBatchedHostsInfo, "hosts", { ... })
end

-- A batched iterator over the l2 devices
function callback_utils.getDevicesIterator(...)
   return getBatchedIterator(interface.getBatchedMacsInfo, "macs", { ... })
end

-- ########################################################

-- Iterates each active flow on the ifname interface.
-- Each flow is passed to the callback with some more information.
function callback_utils.foreachFlow(ifname, deadline, callback, ...)
   interface.select(ifname)

   local iterator = callback_utils.getFlowsIterator({...})

   for flow_key, flow in iterator do

      if(ntop.isShutdown()) then return true end

      if ((deadline ~= nil) and (os.time() >= deadline)) then
	 -- Out of time
	 return false
      end

      if callback(flow_key, flow) == false then
	 return false
      end
   end

   return true
end

-- ########################################################

-- Iterates each active host on the ifname interface for RRD creation.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachLocalRRDHost(ifname, with_ts, with_one_way_traffic_hosts, callback)
   interface.select(ifname)

   local iterator

   if with_ts then
      iterator = callback_utils.getLocalHostsTsIterator(nil --[[ show_details --]], nil --[[ maxHits --]], nil --[[ anomalousOnly --]], with_one_way_traffic_hosts)
   else
      iterator = callback_utils.getLocalHostsIterator(false --[[ show_details --]], nil --[[ maxHits --]], nil --[[ anomalousOnly --]], with_one_way_traffic_hosts)
   end

   for hostname, host_ts in iterator do
      if(ntop.isShutdown()) then return true end
      if ntop.isDeadlineApproaching() then
	 -- Out of time
	 return false
      end

	 if callback(hostname, host_ts) == false then
	    return false
	 end
   end

   return true
end

-- ########################################################

-- Iterates each active host on the ifname interface.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachHost(ifname, callback)
   interface.select(ifname)

   local iterator = callback_utils.getHostsIterator(false --[[ no details ]])

   for hostname, hoststats in iterator do
      if(ntop.isShutdown()) then return true end

      if ntop.isDeadlineApproaching() then
	 -- Out of time
	 return false
      end

      if callback(hostname, hoststats) == false then
	 return false
      end
   end

   return true
end

-- ########################################################

-- Iterates each active host on the ifname interface.
-- Each host is passed to the callback with some more information.
function callback_utils.foreachLocalHost(ifname, callback)
   interface.select(ifname)

   local iterator = callback_utils.getLocalHostsIterator(false --[[ no details ]])

   for hostname, hoststats in iterator do
      if(ntop.isShutdown()) then return true end

      if ntop.isDeadlineApproaching() then
	 -- Out of time
	 return false
      end

      if callback(hostname, hoststats) == false then
	 return false
      end
   end

   return true
end

-- Iterates each device on the ifname interface.
-- Each device is passed to the callback with some more information.
function callback_utils.foreachDevice(ifname, callback)
   interface.select(ifname)

   local devices_stats = callback_utils.getDevicesIterator()

   for devicename, devicestats in devices_stats do
      if(ntop.isShutdown()) then return true end
      devicename = hostinfo2hostkey(devicestats) -- make devicename the combination of mac address and vlan

      if ntop.isDeadlineApproaching() then
         -- Out of time
         return false
      end

      if callback(devicename, devicestats) == false then
	 return false
      end
   end

   return true
end

-- ########################################################

function callback_utils.uploadTSdata()
   local ts_utils = require("ts_utils_core")
   local drivers = ts_utils.listActiveDrivers()
   ts_utils.setup()

   for _, driver in ipairs(drivers) do
      driver:export()
   end
end
-- ########################################################

return callback_utils