File: get_processes_graph_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 (131 lines) | stat: -rw-r--r-- 3,589 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
--
-- (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"

sendHTTPHeader('application/json')

local host_info = url2hostinfo(_GET)
local host_key = hostinfo2hostkey(host_info)

local pageinfo = {
   -- ["sortColumn"] = "column_bytes",
   -- ["maxHits"] = 15,
   -- ["a2zSortOrder"] = false,
   ["hostFilter"] = host_key,
   ["detailsLevel"] = "high", -- to obtain processes information
}

local flows_stats = interface.getFlowsInfo(host_key, pageinfo)
flows_stats = flows_stats["flows"] or {}

local proc2proc_edges = {}
local proc2host_edges = {}
local host2proc_edges = {}

local procs = {}
local hosts = {}

local res = {}

local num = 0
for _, flow in ipairs(flows_stats) do
   local cli_key = hostinfo2hostkey({host = flow["cli.ip"], vlan = flow["cli.vlan"]})
   local srv_key = hostinfo2hostkey({host = flow["srv.ip"], vlan = flow["srv.vlan"]})

   if not flow["client_process"] and not flow["server_process"] then
      goto continue
   end

   local has_source_process
   local source
   if flow["client_process"] then
      has_source_process = true
      source = flow["client_process"]["pid"]

      if not procs[source] then
	 procs[source] = {name = flow["client_process"]["name"]}
      end
   else
      has_source_process = false
      source = cli_key

      if not hosts[source] then
	 hosts[source] = {name = ip2label(flow["cli.ip"], flow["cli.vlan"])}
      end
   end

   local has_target_process
   local target
   if flow["server_process"] then
      has_target_process = true
      target = flow["server_process"]["pid"]

      if not procs[target] then
	 procs[target] = {name = flow["server_process"]["name"]}
      end
   else
      has_target_process = false
      target = cli_key

      if not hosts[target] then
	 hosts[target] = {name = ip2label(flow["srv.ip"], flow["srv.vlan"])}
      end
   end

   local edges
   if has_source_process and has_target_process then
      edges = proc2proc_edges
   elseif has_source_process and not has_target_process then
      edges = proc2host_edges
   elseif not has_source_process and has_target_process then
      edges = host2proc_edges
   end

   if not edges[source] then
	 edges[source] = {}
   end

   if not edges[source][target] then
      edges[source][target] = 0
   end

   edges[source][target] = edges[source][target]
      + flow["srv2cli.bytes"]
      + flow["cli2srv.bytes"]

   ::continue::
end

local r = {}

for source, targets in pairs(proc2proc_edges) do
   for target, weigth in pairs(targets) do
      r[#r + 1] = {source = source, source_type = "proc", source_pid = source, source_name = procs[source]["name"],
		   target = target, target_type = "proc", target_pid = target, target_name = procs[target]["name"],
		   type = "proc2proc"}
   end
end

for source, targets in pairs(proc2host_edges) do
   for target, weigth in pairs(targets) do
      r[#r + 1] = {source = source, source_type = "proc", source_pid = source, source_name = procs[source]["name"],
		   target = target, target_type = "host", target_pid = -1, target_name = hosts[target]["name"],
		   type = "proc2host"}
   end
end

for source, targets in pairs(host2proc_edges) do
   for target, weigth in pairs(targets) do
      r[#r + 1] = {source = source, source_type = "host", source_pid = -1, source_name = hosts[source]["name"],
		   target = target, target_type = "proc", target_pid = target, target_name = procs[target]["name"],
		   type = "host2proc"}
   end
end

print(json.encode(r))