File: host_sflow_distro.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 (217 lines) | stat: -rwxr-xr-x 5,636 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
207
208
209
210
211
212
213
214
215
216
217
--
-- (C) 2013-16 - ntop.org
--

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

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

sendHTTPHeader('text/html; charset=iso-8859-1')


local debug = false

-----------------------------------

function setAggregatedFlow(p_id,p_ip_address,p_value,p_what)
  if (what_array[p_id] == nil) then 
    what_array[p_id]  = {}
    what_array[p_id]["value"]  = 0
    what_array[p_id]["url"]  = url..p_what.."&host="..p_ip_address
  end

  if ((how_is_process == 1) or (how_is_latency == 1))then
    if ( what_array[p_id]["value"]  == 0) then
      what_array[p_id]["value"] = p_value
    end
  else
    what_array[p_id]["value"] = what_array[p_id]["value"] + p_value
  end
end

-----------------------------------

function getAggretationValue(flow,flow_key,type)
  l_how = 0;
  process_key = "client_process"
  bytes_key = "cli2srv.bytes"
  
  if (type == "server") then
    process_key = "server_process"
     bytes_key = "srv2cli.bytes"
  end
  
  if (how_is_process == 1) then
  
    l_how = flow[process_key][how]
  
  elseif (how_is_latency == 1) then
  
    flow_more_info = interface.findFlowByKey(flow_key)
    local info, pos, err = json.decode(flow_more_info["moreinfo.json"], 1, nil)
    for k,v in pairs(info) do
      if("Application latency (residual usec)" == getFlowKey(k)) then
        l_how = tonumber(handleCustomFlowField(k, v))
      end
    end
  
  else
  
    l_how = flow[bytes_key]
  
  end
  return l_how;
end

-----------------------------------

function setType(p_type)
  if((p_type == nil) or (p_type == "memory")) then
    how = "actual_memory"
    how_is_process = 1
  elseif (p_type == "bytes") then
    how = "bytes"
  elseif (p_type == "latency") then
    how_is_latency = 1
    how = "Application latency (residual usec)"
  end
  
  if (debug) then io.write("How:"..how.."\n"); end
end

-----------------------------------

function setMode(p_mode)
  if((p_mode == nil) or (p_mode == "user")) then
    what = "user_name"
    url = ntop.getHttpPrefix().."/lua/get_user_info.lua?user="
  elseif (p_mode == "process") then
    what = "name"
    url = ntop.getHttpPrefix().."/lua/get_process_info.lua?name="
  end
  if (debug) then io.write("what:"..what..",url:"..url.."\n"); end
end

-----------------------------------

function setFilter(p_filter)
  if((p_filter == nil) or (p_filter == "All")) then
    filter_client = 1
    filter_server = 1
  elseif (p_filter == "Client") then
    filter_client = 1
  elseif (p_filter == "Server") then
    filter_server = 1
  end
  if (debug) then io.write("Client:"..filter_client..", Server:"..filter_server.."\n"); end
end


-----------------------------------

mode = _GET["mode"] -- memory(actual-memory),bytes,latency
type = _GET["distr"] -- user,process(proc_name)
host = _GET["host"]
filter = _GET["filter"] -- all,client,server

interface.select(ifname)

if(host == nil) then
   print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> This flow cannot be found (expired ?)</div>")
else

  flows_stats,total = aggregateFlowsStats(interface.getFlowsInfo())
  
  -- Default values
  filter_client = 0
  filter_server = 0
  how_is_process = 0
  how_is_latency = 0
  url = ""
  what = ""
  how = ""

  -- Process parameter
  setType(type)
  setMode(mode)
  setFilter(filter)
  
  -- scan flows
  tot = 0
  what_array = {}
  num = 0
  
  for key, value in pairs(flows_stats) do
    client_process = 0
    server_process = 0
    flow = flows_stats[key]
    if (debug) then io.write("Client:"..flow["cli.ip"]..",Server:"..flow["srv.ip"].."\n"); end
    
    if((filter_client == 1) and (flow["cli.ip"] == host) and (flow.client_process ~= nil))then
      client_process = 1
    end

    if((filter_server == 1) and (flow["srv.ip"] == host) and (flow.server_process ~= nil))then
      server_process = 1
    end

    
    if ((client_process == 1))then
      current_what = flow["client_process"][what].." (client)"
      
      value = getAggretationValue(flow,key,"client")
      setAggregatedFlow(current_what,flow["cli.ip"],value,flow["client_process"][what])
      
      if (debug) then io.write("Find client_process:"..current_what..", Value:"..value..", Process:"..flow["client_process"]["name"]..",Pid:"..flow["client_process"]["pid"]..",Url:"..what_array[current_what]["url"].."\n"); end
    end
    
    if(server_process == 1) then
      current_what = flow["server_process"][what].." (server)"
      
      value = getAggretationValue(flow,key,"server")
      setAggregatedFlow(current_what,flow["srv.ip"],value,flow["server_process"][what])
      
      if (debug) then io.write("Find server_process:"..current_what..", Value:"..value..", Process:"..flow["server_process"]["name"]..",Pid:"..flow["server_process"]["pid"]..",Url:"..what_array[current_what]["url"].."\n"); end

    end
  end
  
  -- Print json
  print "[\n"
  num = 0
  s = 0

  tot = 0
  for key, value in pairs(what_array) do
     value = what_array[key]["value"]
     tot = tot + value
  end

  other = 0;
  thr = (tot * 5) / 100
  
  for key, value in pairs(what_array) do
     value = what_array[key]["value"]
     -- io.write("Val: "..value.."\n")
     if(value >= thr) then
	if(num > 0) then
	   print ",\n"
	end
	label = key
	url = what_array[key]["url"]
	print("\t { \"label\": \"" .. label .."\", \"value\": ".. value ..", \"url\": \"" .. url.."\" }") 
	num = num + 1
	s = s + value
     end
  end

  if(tot > s) then
    print(",\t { \"label\": \"Other\", \"value\": ".. (tot-s) .." }") 
  end

  print "\n]"

end