File: get_host_data.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 (130 lines) | stat: -rw-r--r-- 4,292 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
--
-- (C) 2013-22 - ntop.org
--

local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require "dkjson"
local custom_column_utils = require "custom_column_utils"   
local custom_column = _GET["custom_column"]

sendHTTPHeader('application/json')

function getNetworkStats(network)
   local hosts_stats = interface.getHostsInfo()
   hosts_stats = hosts_stats["hosts"]

   local my_network

   for key, value in pairs(hosts_stats) do
      h = hosts_stats[key]
      nw_name = h["local_network_name"]

      if(h["local_network_name"] == network) then
	 --io.write(nw_name.."\n")

	 if(nw_name ~= nil) then
	    if(my_network == nil) then
	       h["ip"] = nw_name
	       h["name"] = nw_name
	       my_network = h
	    else
	       my_network["num_alerts"] = my_network["num_alerts"] + h["num_alerts"]
	       my_network["throughput_bps"] = my_network["throughput_bps"] + h["throughput_bps"]
	       my_network["throughput_pps"] = my_network["throughput_pps"] + h["throughput_pps"]
	       my_network["bytes.sent"] = my_network["bytes.sent"] + h["bytes.sent"]
	       my_network["bytes.rcvd"] = my_network["bytes.rcvd"] + h["bytes.rcvd"]

	       if(my_network["seen.first"] > h["seen.first"]) then
		  my_network["seen.first"] = h["seen.first"]
	       end

	       if(my_network["seen.last"] < h["seen.last"]) then
		  my_network["seen.last"] = h["seen.last"]
	       end
	    end
	 end
      end
   end

   return(my_network)
end

local host_info = url2hostinfo(_GET)

local criteria = _GET["criteria"]
if(criteria == nil) then criteria = "" end

if(host_info["host"] ~= nil) then
   if(string.contains(host_info["host"], "/")) then
      -- This is a network
      host = getNetworkStats(host_info["host"])
   else
      host = interface.getHostInfo(host_info["host"], host_info["vlan"])
   end
else
   host = interface.getAggregatedHostInfo(host_info["host"])
end

local res = {}

if host then
   local now = os.time()
   -- Get from redis the throughput type bps or pps
   local throughput_type = getThroughputType()

   res["column_since"] = secondsToTime(now-host["seen.first"]+1)
   res["column_last"] = secondsToTime(now-host["seen.last"]+1)
   res["column_traffic"] = bytesToSize(host["bytes.sent"]+host["bytes.rcvd"])

   if not isEmptyString(custom_column) and custom_column_utils.isCustomColumn(custom_column) then
      local custom_column_key, custom_column_format = custom_column_utils.label2criteriakey(custom_column)
      local val = custom_column_utils.hostStatsToColumnValue(host, custom_column_key, true)
      res["column_custom"] = val
   end

   if((host["throughput_trend_"..throughput_type] ~= nil)
   and (host["throughput_trend_"..throughput_type] > 0)) then
      local res_thpt

      if(throughput_type == "pps") then
	 res_thpt = pktsToSize(host["throughput_bps"])
      else
	 res_thpt = bitsToSize(8*host["throughput_bps"])
      end

      -- See ValueTrend in ntop_typedefs.h for values
      if host["throughput_trend_"..throughput_type] == 1 then
	 res_thpt = res_thpt .. " <i class='fas fa-arrow-up'></i>"
      elseif host["throughput_trend_"..throughput_type] == 2 then
	 res_thpt = res_thpt .. " <i class='fas fa-arrow-down'></i>"
      elseif host["throughput_trend_"..throughput_type] == 3 then
	 res_thpt = res_thpt .. " <i class='fas fa-minus'></i>"
      end

      res["column_thpt"] = res_thpt
   else
      res["column_thpt"] = "0 "..throughput_type
   end

   res["column_num_flows"] = host["active_flows.as_client"] + host["active_flows.as_server"]

   if isBridgeInterface(interface.getStats()) then
      res["column_num_dropped_flows"] = (host["flows.dropped"] or 0)
   end

   if((host["num_alerts"] ~= nil) and (host["num_alerts"] > 0)) then
      res["column_alerts"] = host["num_alerts"]
   else
      res["column_alerts"] = 0
   end
   
   sent2rcvd = round((host["bytes.sent"] * 100) / (host["bytes.sent"]+host["bytes.rcvd"]), 0)
   if(sent2rcvd == nil) then sent2rcvd = 0 end
   res["column_breakdown"] = "<div class='progress'><div class='progress-bar bg-warning' style='width: "
	  .. sent2rcvd .."%;'>Sent</div><div class='progress-bar bg-success' style='width: " .. (100-sent2rcvd) .. "%;'>Rcvd</div></div>"

end

print(json.encode(res))