File: host_get_json.lua

package info (click to toggle)
ntopng 2.4%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 15,888 kB
  • ctags: 8,091
  • sloc: cpp: 21,442; ansic: 10,999; sh: 1,627; makefile: 423; pascal: 312; ruby: 34; exp: 4
file content (126 lines) | stat: -rw-r--r-- 4,668 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
--
-- (C) 2013-16 - ntop.org
--

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

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

-- whether to return host statistics: on by default
local host_stats           = _GET["host_stats"]

-- whether to return statistics regarding host flows: off by default
local host_stats_flows     = _GET["host_stats_flows"]
local host_stats_flows_num = _GET["host_stats_flows_num"]

host_info = url2hostinfo(_GET)
host = _GET["host"]

if(host_info["host"] == nil) then
   sendHTTPHeader('text/html; charset=iso-8859-1')
   ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
   dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
   print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> Host parameter is missing (internal error ?)</div>")
   return
end

function flows2protocolthpt(flows)
   local protocol_thpt = {}
   for _, flow in pairs(flows) do
      local proto_ndpi = ""
      if flow["proto.ndpi"] == nil or flow["proto.ndpi"] == "" then
	 goto continue
      else
	 proto_ndpi = flow["proto.ndpi"]
      end

      if protocol_thpt[proto_ndpi] == nil then
	 protocol_thpt[proto_ndpi] =
	    {["cli2srv"]={["throughput_bps"]=0, ["throughput_pps"]=0},
	       ["srv2cli"]={["throughput_bps"]=0, ["throughput_pps"]=0}}
      end

      for _, dir in pairs({"cli2srv", "srv2cli"}) do
	 for _, dim in pairs({"bps", "pps"}) do
	    protocol_thpt[proto_ndpi][dir]["throughput_"..dim] =
	       protocol_thpt[proto_ndpi][dir]["throughput_"..dim] + flow[dir..".throughput_"..dim]
	 end
      end
      ::continue::
   end
   return protocol_thpt
end

if_name = (_GET["if_name"] or _GET["ifname"])
ifid = (_GET["id"] or _GET["ifId"])
-- parse interface names and possibly fall back to the selected interface:
-- priority goes to the interface id
if ifid ~= nil and ifid ~= "" then
   if_name = getInterfaceName(ifid)

-- if not interface id is specified we look for the interface name
elseif if_name ~= nil and if_name ~= "" then
   ifid = tostring(interface.name2id(if_name))

-- finally, we fall back to the default selected interface name
else
   -- fall-back to the default interface
   if_name = ifname
   ifid = interface.name2id(ifname)
end

interface.select(if_name)
host = interface.getHostInfo(host_info["host"], host_info["vlan"])

if(host == nil) then
   sendHTTPHeader('text/html; charset=iso-8859-1')
   ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
   dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
   print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> Host ".. host_info["host"] .. " Vlan" ..host_info["vlan"].." cannot be found (expired ?)</div>")
   return
else
   sendHTTPHeader('application/json')
   local hj = {}
   -- hosts stats are on by default, one must explicitly disable them
   if host_stats == nil or host_stats == "" or host_stats == "true" or host_stats == "1" then
      hj = json.decode(host["json"])
      hj["http"] = host["http"]
   end

   -- host flow stats are off by default and must be explicitly enabled
   if host_stats_flows ~= nil and host_stats_flows ~= "" then
      if host_stats_flows_num == nil or tonumber(host_stats_flows_num) == nil then
	 -- default: do not limit the number of flows
	 host_stats_flows_num = 99999
      else
	 -- ... unless otherwise specified
	 host_stats_flows_num = tonumber(host_stats_flows_num)
      end
      local total = 0

      local paginfo = {["sortColumn"]="column_bytes", ["a2zSortOrder"]=false,
	 ["maxHits"]=host_stats_flows_num, ["toSkip"]=0, ["detailedResults"]=true}
      --local flows = interface.getFlowsInfo(host_info["host"], nil, "column_bytes", host_stats_flows_num, 0, false)
      local flows = interface.getFlowsInfo(host_info["host"], paginfo)
      flows,total = aggregateFlowsStats(flows)
      for i, fl in ipairs(flows) do
	 flows[i] = {
	    ["srv.ip"] = fl["srv.ip"], ["cli.ip"] = fl["cli.ip"],
	    ["srv.port"] = fl["srv.port"], ["cli.port"] = fl["cli.port"],
	    ["proto.ndpi_id"] = fl["proto.ndpi_id"], ["proto.ndpi"] = fl["proto.ndpi"],
	    ["bytes"] = fl["bytes"],
	    ["cli2srv.throughput_bps"] = round(fl["throughput_cli2srv_bps"], 2),
	    ["srv2cli.throughput_bps"] = round(fl["throughput_srv2cli_bps"], 2),
	    ["cli2srv.throughput_pps"] = round(fl["throughput_cli2srv_pps"], 2),
	    ["srv2cli.throughput_pps"] = round(fl["throughput_srv2cli_pps"], 2)
	 }
      end
      hj["ndpiThroughputStats"] = flows2protocolthpt(flows)
      hj["flows"] = flows
      hj["flows_count"] = total
   end
   print(json.encode(hj, nil))
end