File: active.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 (270 lines) | stat: -rw-r--r-- 7,900 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
--
-- (C) 2013-22 - ntop.org
--

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

require "lua_utils"
require "flow_utils"
local format_utils = require("format_utils")
local json = require "dkjson"
local rest_utils = require("rest_utils")

--
-- Read list of active hosts
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"ifid": "1"}' http://localhost:3000/lua/rest/v1/get/host/active.lua
--
-- NOTE: in case of invalid login, no error is returned but redirected to login
--

local rc = rest_utils.consts.success.ok
local res = {}

local ifid = _GET["ifid"]

-- Pagination:
local all = _GET["all"]
local currentPage = _GET["currentPage"]
local perPage     = _GET["perPage"]
local sortColumn  = _GET["sortColumn"] -- ip, name, since, last, alerts, country, vlan, num_flows, traffic, thpt
local sortOrder   = _GET["sortOrder"]

-- Filters
local mode        = _GET["mode"] -- all local remote broadcast_domain filtered blacklisted dhcp
local ipversion   = _GET["version"]
local protocol    = _GET["protocol"]
local traffic_type = _GET["traffic_type"]
local asn          = _GET["asn"]
local vlan         = _GET["vlan"]
local network      = _GET["network"]
local cidr         = _GET["network_cidr"]
local pool         = _GET["pool"]
local country      = _GET["country"]
local os_          = tonumber(_GET["os"])
local mac          = _GET["mac"]
local top_hidden   = ternary(_GET["top_hidden"] == "1", true, nil)

if isEmptyString(ifid) then
   rc = rest_utils.consts.err.invalid_interface
   rest_utils.answer(rc)
   return
end

interface.select(ifid)

if not isEmptyString(_GET["sortColumn"]) then
   -- Backward compatibility
   _GET["sortColumn"] = "column_" .. _GET["sortColumn"]
   sortColumn  = _GET["sortColumn"]
end

if(currentPage == nil) then
   currentPage = 1
else
   currentPage = tonumber(currentPage)
end

if(perPage == nil) then
   perPage = getDefaultTableSize()
else
   perPage = tonumber(perPage)
   tablePreferences("rows_number",perPage)
end

local traffic_type_filter

if traffic_type == "one_way" then
   traffic_type_filter = 1 -- ntop_typedefs.h TrafficType traffic_type_one_way
elseif traffic_type == "bidirectional" then
   traffic_type_filter = 2 -- ntop_typedefs.h TrafficType traffic_type_bidirectional
end

if isEmptyString(mode) then
   mode = "all"
end

interface.select(ifname)

local to_skip = (currentPage-1) * perPage

if(sortOrder == "desc") then sOrder = false else sOrder = true end

local filtered_hosts = false
local blacklisted = false
local anomalous = false
local dhcp_hosts = false

local hosts_retrv_function = interface.getHostsInfo
if mode == "local" then
   hosts_retrv_function = interface.getLocalHostsInfo
elseif mode == "remote" then
   hosts_retrv_function = interface.getRemoteHostsInfo
elseif mode == "broadcast_domain" then
   hosts_retrv_function = interface.getBroadcastDomainHostsInfo
elseif mode == "filtered" then
   filtered_hosts = true
elseif mode == "blacklisted" then
   blacklisted_hosts = true
elseif mode == "dhcp" then
   dhcp_hosts = true
end



local hosts_stats = hosts_retrv_function(false, sortColumn, perPage, to_skip, sOrder,
                          country, os_, tonumber(vlan), tonumber(asn),
                          tonumber(network), mac,
                          tonumber(pool), tonumber(ipversion),
                          tonumber(protocol), traffic_type_filter,
                          filtered_hosts, blacklisted_hosts, top_hidden, anomalous, dhcp_hosts, cidr)

if hosts_stats == nil then
   rest_utils.answer(rest_utils.consts.err.not_found)
   return
end

hosts_stats = hosts_stats["hosts"]

if hosts_stats == nil then
   rest_utils.answer(rest_utils.consts.err.internal_error)
   return
end

if all ~= nil then
   perPage = 0
   currentPage = 0
end

function get_host_name(h)
   if h["name"] == nil then
      if h["ip"] ~= nil then
         h["name"] = ip2label(h["ip"])
      else
         h["name"] = h["mac"]
      end
   end
   return(h["name"])
end

local vals = {}
local num = 0
for key, value in pairs(hosts_stats) do
   num = num + 1
   postfix = string.format("0.%04u", num)

   if(isEmptyString(sortColumn)) then
      vals[key] = key
   elseif(sortColumn == "column_name") then
      hosts_stats[key]["name"] = get_host_name(hosts_stats[key])
      vals[hosts_stats[key]["name"]..postfix] = key
   elseif(sortColumn == "column_since") then
      vals[hosts_stats[key]["seen.first"]+postfix] = key
   elseif(sortColumn == "column_alerts") then
      vals[hosts_stats[key]["num_alerts"]+postfix] = key
   elseif(sortColumn == "column_last") then
      vals[hosts_stats[key]["seen.last"]+postfix] = key
   elseif(sortColumn == "column_country") then
      vals[hosts_stats[key]["country"]..postfix] = key
   elseif(sortColumn == "column_vlan") then
      vals[hosts_stats[key]["vlan"]..postfix] = key
   elseif(sortColumn == "column_num_flows") then
      local t = hosts_stats[key]["active_flows.as_client"]+hosts_stats[key]["active_flows.as_server"]
      vals[t+postfix] = key
   elseif(sortColumn == "column_num_dropped_flows") then
      local t = hosts_stats[key]["flows.dropped"] or 0
      vals[t+postfix] = key
   elseif(sortColumn == "column_traffic") then
      vals[hosts_stats[key]["bytes.sent"]+hosts_stats[key]["bytes.rcvd"]+postfix] = key
   elseif(sortColumn == "column_thpt") then
      vals[hosts_stats[key]["throughput_bps"]+postfix] = key
   elseif(sortColumn == "column_queries") then
      vals[hosts_stats[key]["queries.rcvd"]+postfix] = key
   elseif(sortColumn == "column_ip") then
      vals[hosts_stats[key]["ipkey"]+postfix] = key
   else
      vals[key] = key
   end
end

if sortOrder == "asc" then
   funct = asc
else
   funct = rev
end

local data = {}

for _key, _value in pairsByKeys(vals, funct) do
   local record = {}
   local key = vals[_key]
   local value = hosts_stats[key]
   local symkey = hostinfo2jqueryid(hosts_stats[key])

   record["key"] = symkey
   record["first_seen"] = value["seen.first"]
   record["last_seen"] = value["seen.last"]
   record["vlan"] = value["vlan"]
   record["ip"] = stripVlan(key) 
   record["os"] = value["os"]
   record["num_alerts"] = value["num_alerts"]

   local host = interface.getHostInfo(hosts_stats[key].ip, hosts_stats[key].vlan)
   if host ~= nil then
      record["country"] = host["country"]
      record["is_blacklisted"] = host["is_blacklisted"]
   end

   local name = value["name"]
   if isEmptyString(name) then
      local hinfo = hostkey2hostinfo(key)
      name = hostinfo2label(hinfo)
   end
   if isEmptyString(name) then
      name = key
   end
   if value["ip"] ~= nil then
      local label = hostinfo2label(value)
      if label ~= value["ip"] and name ~= label then
         name = name .. " ["..label.."]"
      end
   end

   record["name"] = name

   record["thpt"] = {}
   record["thpt"]["pps"] = value["throughput_pps"]
   record["thpt"]["bps"] = value["throughput_bps"]*8

   record["bytes"] = {}
   record["bytes"]["total"] = (value["bytes.sent"]+value["bytes.rcvd"])
   record["bytes"]["sent"]  = value["bytes.sent"]
   record["bytes"]["recvd"] = value["bytes.rcvd"]

   record["is_localhost"] = value["localhost"]
   record["is_multicast"] = value["is_multicast"]
   record["is_broadcast"] = value["is_broadcast"]
   record["is_broadcast_domain"] = value["broadcast_domain_host"]

   record["num_flows"] = {}
   record["num_flows"]["total"]     = (value["active_flows.as_client"] + value["active_flows.as_server"])
   record["num_flows"]["as_client"] = (value["active_flows.as_client"])
   record["num_flows"]["as_server"] = (value["active_flows.as_server"])

   data[#data + 1] = record
end -- for

res = {
   perPage = perPage,
   currentPage = currentPage,
   totalRows = total,
   data = data,
   sort = {
      {
         sortColumn,
         sortOrder
      }
   },
}

rest_utils.answer(rc, res)