File: iface_local_stats.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 (97 lines) | stat: -rw-r--r-- 3,153 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
--
-- (C) 2013-22 - ntop.org
--

local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
if((dirs.scriptdir ~= nil) and (dirs.scriptdir ~= "")) then package.path = dirs.scriptdir .. "/lua/modules/?.lua;" .. package.path end

require "lua_utils"

sendHTTPContentTypeHeader('text/html')

local ifid = _GET["ifid"]
interface.select(ifid)
local ifstats = interface.getStats()

print('[')

if(_GET["iflocalstat_mode"] == "distribution") then
   if(ifstats["localstats"]["bytes"] == 0) then
      print('{ "label": "No traffic yet", "value": 0 }\n')
   else
      local ifname = getInterfaceName(ifid)
      local eth = ifstats["eth"]
      local n = 0

      local sum = eth.IPv4_packets+eth.IPv6_packets+eth.ARP_packets+eth.MPLS_packets+eth.other_packets
      local five = 0.05*sum
      local tot = 0
      if(eth.IPv4_packets > five) then
	 print('{ "label": "IPv4", "value": '..eth.IPv4_packets..' }') n = 1 tot = tot + eth.IPv4_packets
      end
      if(eth.IPv6_packets > five) then
	 if(n == 1) then print(',') n = 0 end
	 print('{ "label": "IPv6", "value": '..eth.IPv6_packets..' }')
	 n = 1 tot = tot + eth.IPv6_packets
      end
      if(eth.ARP_packets > five) then
	 if(n == 1) then print(',') n = 0 end
	 print('{ "label": "ARP", "value": '..eth.ARP_packets..' }')
	 n = 1 tot = tot + eth.ARP_packets
      end
      if(eth.MPLS_packets > five) then
	 if(n == 1) then print(',') n = 0 end
	 print('{ "label": "MPLS", "value": '..eth.MPLS_packets..' }')
	 n = 1 tot = tot + eth.MPLS_packets
      end

      local leftover = sum - tot
      if(leftover > 0) then
	 if(n == 1) then print(',') n = 0 end
	 print('{ "label": "Other", "value": '..leftover..' }') n = 1
      end
   end
else

   local bytes = ifstats["localstats"]["bytes"]

   local sum = bytes["local2remote"]+bytes["local2local"]+bytes["remote2local"]+bytes["remote2remote"]
   local five = 0.05*sum
   local other = 0

   local n = 0

   if(bytes["local2remote"] > five) then
      print [[  { "label": "Local->Remote", "value": ]]
      print(bytes["local2remote"].."") print [[ } ]] n = n + 1
   else
      other = other + bytes["local2remote"]
   end
   if(bytes["local2local"] > five) then
      if(n > 0) then print(",") end
      print [[   { "label": "Local->Local", "value": ]]
      print(bytes["local2local"].."") print [[ } ]]  n = n + 1 else other = other + bytes["local2local"]
   end
   if(bytes["remote2local"] > five) then
      if(n > 0) then print(",") end
      print [[   { "label": "Remote->Local", "value": ]] print(bytes["remote2local"].."") print [[ } ]]  n = n + 1
   else
      other = other + bytes["remote2remote"]
   end
   if(bytes["remote2remote"] > five) then
      if(n > 0) then print(",") end
      print [[   { "label": "Remote->Remote", "value": ]] print(bytes["remote2remote"].."") print [[ } ]]  n = n + 1
   else
      other = other + bytes["remote2remote"]
   end

   if(other > 0) then
      if(n > 0) then print(",") end
      print('{ "label": "Other", "value": '..other..' }\n')
   end

   if(sum == 0) then print('{ "label": "No traffic yet", "value": 0 }\n') end
end

print(']')