File: host_top_peers_protocols.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 (77 lines) | stat: -rw-r--r-- 2,086 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
--
-- (C) 2014-15-15 - ntop.org
--

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

require "lua_utils"
local json = require("dkjson")

sendHTTPHeader('application/json')

interface.select(ifname)
local max_num_peers = 10
local host_info = url2hostinfo(_GET)
local flows     = getTopFlowPeers(hostinfo2hostkey(host_info), max_num_peers)

local tot = 0
local peers = {}
local peers_proto = {}
local ndpi = {}

for _, flow in ipairs(flows) do

   if(flow["cli.ip"] == _GET["host"]) then
      peer = hostinfo2hostkey(flow, "srv")
   else
      peer = hostinfo2hostkey(flow, "cli")
   end

   v = flow["bytes"]
   if(peers[peer] == nil) then peers[peer] = 0  end
   peers[peer] = peers[peer] + v

   if flow["proto.ndpi"] == nil then
      goto continue
   elseif ndpi[flow["proto.ndpi"]] == nil then
      ndpi[flow["proto.ndpi"]] = 0
   end

   ndpi[flow["proto.ndpi"]] = ndpi[flow["proto.ndpi"]] + v

   if(peers_proto[peer] == nil) then peers_proto[peer] = {}  end
   if(peers_proto[peer][flow["proto.ndpi"]] == nil) then peers_proto[peer][flow["proto.ndpi"]] = 0 end
   peers_proto[peer][flow["proto.ndpi"]] = peers_proto[peer][flow["proto.ndpi"]] + v

   ::continue::
   tot = tot + v
end

local res = {}

for peer,value in pairsByValues(peers, rev) do
   if(peers_proto[peer] ~= nil) then

      for proto,value in pairsByValues(ndpi, rev) do
	 if(peers_proto[peer][proto] ~= nil) then

	    host = interface.getHostInfo(peer)
	    if(host ~= nil) then
        local hostinfo = {host = host["ip"], vlan = host["vlan"]}

        if(isEmptyString(host["name"])) then
          host["name"] = resolveAddress(hostinfo)
        end

	local r = {host=peer, name=host.name, url=hostinfo2detailshref(hostinfo, nil, host.name), l7proto=proto, l7proto_url="<A HREF='"..ntop.getHttpPrefix().."/lua/flows_stats.lua?host=".. hostinfo2hostkey(hostinfo) .."&application="..proto.."'>"..proto.."</A>", traffic=peers_proto[peer][proto]}

	      res[#res + 1] = r
	    end
	 end
      end
   end
end
-- tprint(res)
print(json.encode(res, nil))