File: get_flow_process_tree.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 (94 lines) | stat: -rw-r--r-- 3,106 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
--
-- (C) 2021 - ntop.org
--

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

require "lua_utils"
local json = require "dkjson"

sendHTTPHeader('text/json')

local flow_key = _GET["flow_key"]
local flow_hash_id = _GET["flow_hash_id"]
local flow = interface.findFlowByKeyAndHashId(tonumber(flow_key), tonumber(flow_hash_id))
local tree = {}

local function format_proc(name, pid)
   return string.format("%s [pid: %u]", name, pid)
end

local function proc_branch(host, proc)
   local proc_link = ntop.getHttpPrefix().."/lua/process_details.lua?pid="..proc.pid.."&pid_name="..proc.name.."&host=".. host .."&page=flows"
   local proc_leaf = {name = format_proc(proc.name, proc.pid), link = proc_link, type = "proc", children = {}}

   if (proc.pid ~= 1) and (proc.father_pid ~= nil) then
      local father_leaf = {name = format_proc(proc.father_name, proc.father_pid), type = "proc", children = {}}

      father_leaf["children"] = {proc_leaf}
      proc_leaf = father_leaf

      -- TODO: rather than simply adding the system, it would be desirable to
      -- go up into the tree recursively and get all the processes
      -- if proc.father_pid ~= 1 then
      --	 local systemd_leaf = {name = format_proc("systemd", 1), type="proc", children = {}}

      --	 systemd_leaf["children"] = {proc_leaf}
      --	 proc_leaf = systemd_leaf
      -- end
   end

   return proc_leaf
end

local function host_branch(host_name, host, procs)
   local link = hostinfo2detailsurl(host, {page = "flows"})

   local children = {}
   for _, proc in pairs(procs) do
      children[#children + 1] = proc
   end

   local branch = {name = host_name, type = "host", link = link, children = children}

   return branch
end

-- EXAMPLE of tree format
-- tree = {name = "", type="root",
--	children = {
--	   {name="client", type="host",
--	    children={{name="systemd", type="proc"}, {name="p", type="proc", children = {{name="q", type = "proc"}}}}},
--	   {name="server", type="host",
--	    children={}}}}


if flow then
   if flow.client_process and flow.server_process then
      if flow["cli.ip"] ~= flow["srv.ip"] then
	 tree = {name = "", type = "root",
		 children = {
		    host_branch(flowinfo2hostname(flow, "cli"), flow2hostinfo(flow, "cli"),
				{proc_branch(flow["cli.ip"], flow.client_process)}),
		    host_branch(flowinfo2hostname(flow, "srv"), flow2hostinfo(flow, "srv"),
				{proc_branch(flow["srv.ip"], flow.server_process)})
		 }
	 }
      else
	 tree = host_branch(flowinfo2hostname(flow, "cli"), flow2hostinfo(flow, "cli"),
			    {proc_branch(flow["cli.ip"], flow.client_process),
			     proc_branch(flow["srv.ip"], flow.server_process)})
      end

   elseif flow.client_process then
      tree = host_branch(flowinfo2hostname(flow, "cli"), flow2hostinfo(flow, "cli"),
			 {proc_branch(flow["cli.ip"], flow.client_process)})

   elseif flow.server_process then
      tree = host_branch(flowinfo2hostname(flow, "srv"), flow2hostinfo(flow, "srv"),
			 {proc_branch(flow["srv.ip"], flow.server_process)})
   end
end

print(json.encode(tree))