File: traffic_extraction_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 (160 lines) | stat: -rw-r--r-- 4,621 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
--
-- (C) 2013-22 - ntop.org
--

dirs = ntop.getDirs()

package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local format_utils = require "format_utils"
local recording_utils = require "recording_utils"
local json = require "dkjson"

sendHTTPHeader('application/json')

if not recording_utils.isAvailable() then
  return
end

-- ################################################
-- Table parameters

local currentPage  = _GET["currentPage"]
local perPage      = _GET["perPage"]
local sortColumn   = _GET["sortColumn"]
local sortOrder    = _GET["sortOrder"]

-- ################################################
--  Sorting and Pagination

local sortPrefs = "traffic_extraction_jobs"

if isEmptyString(sortColumn) or sortColumn == "column_" then
   sortColumn = getDefaultTableSort(sortPrefs)
elseif sortColumn ~= "" then
   tablePreferences("sort_"..sortPrefs, sortColumn)
end

if isEmptyString(_GET["sortColumn"]) then
   sortOrder = getDefaultTableSortOrder(sortPrefs, true)
end

if _GET["sortColumn"] ~= "column_" and _GET["sortColumn"] ~= "" then
   tablePreferences("sort_order_"..sortPrefs, sortOrder, true)
end

if currentPage == nil then
   currentPage = 1
else
   currentPage = tonumber(currentPage)
end

if perPage == nil then
   perPage = 10
else
   perPage = tonumber(perPage)
   tablePreferences("rows_number_policies", perPage)
end

local to_skip = (currentPage-1) * perPage

if sortOrder == "desc" then sOrder = rev_insensitive else sOrder = asc_insensitive end

-- ################################################

interface.select(ifname)

local ifstats = interface.getStats()

local jobs = recording_utils.getExtractionJobs(ifstats.id)
local num_results = 0

local sorter = {}
for id,job in pairs(jobs) do
  if sortColumn == "column_id" then
    sorter[id] = job.id
  elseif sortColumn == "column_extracted_packets" then
    sorter[id] = job.extracted_pkts
  elseif sortColumn == "column_extracted_bytes" then
    sorter[id] = job.extracted_bytes
  elseif sortColumn == "column_status" then
    sorter[id] = job.status
  else -- sortColumn == column_job_time
    sorter[id] = job.time
  end

  num_results = num_results + 1
end

local res = {}
local cur_num = 0
for id, _ in pairsByValues(sorter, sOrder) do
  cur_num = cur_num + 1
  if cur_num <= to_skip then
    goto continue
  elseif cur_num > to_skip + perPage then
    break
  end

  local job = jobs[id]

  local action_links = ""
  local job_files = 0

  if job.status == "completed" or job.status == "stopped" then
    job_files = recording_utils.getJobFiles(job.id)
    job_files = #job_files
  end

  local status_desc = i18n("traffic_recording."..job.status) 
  if job.status == "failure" then
    local error_desc
    if job.error_code == 2 or job.error_code == 3 then error_desc = i18n("traffic_recording.err_alloc")
    elseif job.error_code == 4 or job.error_code == 6 then error_desc = i18n("traffic_recording.err_open")
    elseif job.error_code == 5 then error_desc = i18n("traffic_recording.err_filter")
    elseif job.error_code == 9 then error_desc = i18n("traffic_recording.err_stuck")
    else error_desc = i18n("traffic_recording.err_unknown")
    end
    status_desc = status_desc.." ("..error_desc..")"
  end

  local chart_link = nil
  if not isEmptyString(job.chart_url) then
    chart_link = '<a href="'.. job.chart_url ..'"><i class="fas fa-lg fa-chart-area"></i></a>'
  end

  local bpf_filter = "-"
  if not isEmptyString(job.filter) then
    bpf_filter = shortenString(job.filter, 45)

    if bpf_filter ~= job.filter then
      -- string was shortened, show full filter into a tooltip
      bpf_filter = '<span title="'.. job.filter ..'">' .. bpf_filter .. '</span>'
    end
  end

  res[#res + 1] = { 
    column_id = job.id, 
    column_job_time = format_utils.formatEpoch(job.time),
    column_job_files = job_files,
    column_status = status_desc,
    column_chart = chart_link,
    column_status_raw = job.status,
    column_begin_time = format_utils.formatEpoch(job.time_from),
    column_end_time = format_utils.formatEpoch(job.time_to),
    column_bpf_filter = bpf_filter,
    column_extracted_packets = ternary(job.status == "completed" and job.extracted_pkts, formatPackets(job.extracted_pkts), "-"),
    column_extracted_bytes = ternary(job.status == "completed" and job.extracted_bytes, bytesToSize(job.extracted_bytes), "-"),
  }

  ::continue::
end

local result = {}
result["perPage"] = perPage
result["currentPage"] = currentPage
result["totalRows"] = num_results
result["data"] = res
result["sort"] = {{sortColumn, sortOrder}}

print(json.encode(result))