File: iface_ports_list.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 (105 lines) | stat: -rw-r--r-- 2,378 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
--
-- (C) 2013-22 - ntop.org
--

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

require "lua_utils"
local format_utils = require "format_utils"

sendHTTPContentTypeHeader('text/html')

local host_info = url2hostinfo(_GET)
local host_key = hostinfo2hostkey(host_info)

if isEmptyString(host_key) then
   host_info = nil
   host_key = nil
end

local function fill_ports_array(field_key, flows_stats)
   local ports_array = {}

   for _, value in ipairs(flows_stats) do
      local p = value[field_key..".port"]
      if(ports_array[p] == nil) then ports_array[p] = 0 end
      ports_array[p] = ports_array[p] + value["bytes"]
   end

   return ports_array
end

local flows_stats = interface.getFlowsInfo(host_key) or {}
if flows_stats["flows"] then
   flows_stats = flows_stats["flows"]
end

local client_ports = fill_ports_array("cli", flows_stats)
local server_ports = fill_ports_array("srv", flows_stats)
local ports

if(_GET["clisrv"] == "server") then
   ports = server_ports
else
   ports = client_ports
end

local _ports = { }
local tot = 0

for k, v in pairs(ports) do
   _ports[k] = v
   tot = tot + v
end

local threshold_percent = 5
local threshold = (tot * threshold_percent) / 100

print "[ "

local min_num = 4
local num = 0
local accumulate = 0
for key, value in pairsByValues(_ports, rev) do

   if value < threshold then
      break
   end

   if(num > 0) then
      print ",\n"
   end

   print("\t { \"label\": \"" .. key .."\", \"value\": ".. value ..", \"url\": \""..ntop.getHttpPrefix().."/lua/flows_stats.lua?port="..key)
   if host_key then
      print("&host="..host_key)
   end

   print("\" }")

   accumulate = accumulate + value
   num = num + 1
end

-- In case there is some leftover do print it as "Other"
if accumulate < tot then
   local other_label = i18n("other")
   local url = hostinfo2detailsurl(host_info, {page = "flows"})

   if(num > 0) then
      print (",\n")
   else
      if table.len(_ports) > 0 then
	 other_label = i18n("num_different_ports", {num = format_utils.formatValue(table.len(_ports)), threshold = threshold_percent})
      end
   end

   print("\t { \"label\": \""..other_label.."\", \"value\": ".. (tot - accumulate) ..", \"url\": \""..url.."\"}")
end

if tot == 0 then
   print("\t { \"label\": \""..i18n("no_ports").."\", \"value\": 0 }")
end

print "\n]"