File: traffic_stats.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 (206 lines) | stat: -rw-r--r-- 5,453 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
--
-- (C) 2014-15 - ntop.org
--

-- ########

function updateKey(hash_name, key_name, value)
  io.write("# "..key_name.." += "..value.."\n")

  if(hash_name[key_name] == nil) then
     hash_name[key_name] = value
  else
     hash_name[key_name] = hash_name[key_name] + value
  end
end

-- ######

function hash2json(hash_name, label)
  print "[\n"

  n = 0
  for k, v in pairs(hash_name) do
    if(n > 0) then print(',\n') end
    print('{ "label": "'..k..'", "url": "'..label..'='..k..'", "value": '..v..' }\n')
    n = n + 1
  end

  print("]")
end

-- ######

function sliceHash(hash_name, max_num_entries, sort_direction, url, url_trailer)
  local sortedKeys

  if(sort_direction == "desc") then
    sortedKeys = getKeysSortedByValue(hash_name, function(a, b) return a > b end)
  else
    sortedKeys = getKeysSortedByValue(hash_name, function(a, b) return a < b end)
  end

  print "[\n"
  n = 0

  for _, key in ipairs(sortedKeys) do
     if((max_num_entries > 0) and (n > max_num_entries)) then
        break
     else
        if(url == "") then
          print('{ "label": "'..key..'", "url": "", "value": '..hash_name[key]..' }\n')
        else
          print('{ "label": "'..key..'", "url": "'..url..'='..key..url_trailer..'", "value": '..hash_name[key]..' }\n')
        end
     end
     n = n + 1
  end

  print("]")
end


-- ######

-- host, vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
function getTalkers(ifname, vlan, unit, mode, max_num_entries, sort_direction)
   interface.select(ifname)
   hosts_stats,total = aggregateHostsStats(interface.getHostsInfo())

   hosts = { }
   for key, value in pairs(hosts_stats) do
      skip = false

      io.write(key.."\n")
      if(hosts_stats[key]["ip"] == nil) then
        skip = true
      else
        if(vlan ~= 0) then
          if(hosts_stats[key]["vlan"] ~= vlan) then
	    skip = true
          end
        end
      end

      if(skip == false) then
      if((mode == "recv") or (mode == "sent")) then
      	v = hosts_stats[key][unit.."."..mode]
      else
        v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
      end

       updateKey(hosts, key, v)
      end
   end

   sliceHash(hosts, max_num_entries, sort_direction, "/lua/host_details.lua?host", "")
end

-- ########

-- vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
function getVLANTraffic(ifname, vlan, unit, mode, max_num_entries, sort_direction)
   interface.select(ifname)
   hosts_stats,total = aggregateHostsStats(interface.getHostsInfo())

   hosts = { }
   for key, value in pairs(hosts_stats) do
      skip = false

      io.write(key.."\n")
      if(hosts_stats[key]["ip"] == nil) then
        skip = true
      else
        if(vlan ~= 0) then
          if(hosts_stats[key]["vlan"] ~= vlan) then
	    skip = true
          end
        end
      end

      if(skip == false) then
      if((mode == "recv") or (mode == "sent")) then
      	v = hosts_stats[key][unit.."."..mode]
      else
        v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
      end

       updateKey(hosts, hosts_stats[key]["vlan"], v)
      end
   end

   sliceHash(hosts, max_num_entries, sort_direction, "", "")
end

-- ########

-- AS, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
function getASTraffic(ifname, vlan, as, unit, mode, max_num_entries, sort_direction)
   interface.select(ifname)
   hosts_stats,total = aggregateHostsStats(interface.getHostsInfo())

   hosts = { }
   for key, value in pairs(hosts_stats) do
      skip = false

      io.write(key.."\n")
      if(hosts_stats[key]["ip"] == nil) then
        skip = true
      else
        if(vlan ~= 0) then
          if(hosts_stats[key]["vlan"] ~= vlan) then
	    skip = true
          end
        end
      end

      if(skip == false) then
      if((mode == "recv") or (mode == "sent")) then
      	v = hosts_stats[key][unit.."."..mode]
      else
        v = hosts_stats[key][unit..".sent"] + hosts_stats[key][unit..".rcvd"]
      end

       updateKey(hosts, hosts_stats[key]["asn"], v)
      end
   end

   sliceHash(hosts, max_num_entries, sort_direction, "https://www.robtex.com/as/as", ".html")
end

-- host, vlan, unit (bytes/packets), mode (rcvd/sent/both), max_num_entries (10), sort(desc/asc)
function getFlowTalkers(ifname, vlan, unit, mode, max_num_entries, sort_direction)
   interface.select(ifname)
   hosts_stats = interface.getFlowsInfo()

   hosts = { }
   for key, value in pairs(hosts_stats) do
      skip = false

      if(vlan ~= 0) then
         if(hosts_stats[key]["vlan"] ~= vlan) then
	   skip = true
         end
      end

      if(skip == false) then
      if(mode == "recv") then
      	updateKey(hosts, hosts_stats[key]["cli.ip"], hosts_stats[key]["srv2cli."..unit])
      	updateKey(hosts, hosts_stats[key]["srv.ip"], hosts_stats[key]["cli2srv."..unit])
      elseif(mode == "sent") then
      	updateKey(hosts, hosts_stats[key]["cli.ip"], hosts_stats[key]["cli2srv."..unit])
      	updateKey(hosts, hosts_stats[key]["srv.ip"], hosts_stats[key]["srv2cli."..unit])
      else
        v = hosts_stats[key]["cli2srv."..unit] + hosts_stats[key]["srv2cli."..unit]
      	updateKey(hosts, hosts_stats[key]["cli.ip"], v)
      	updateKey(hosts, hosts_stats[key]["srv.ip"], v)
      end
      end
   end

   for k, v in pairs(hosts) do
     io.write(k.."="..v.."\n")
   end

end