File: get_custom_categories_hosts.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 (128 lines) | stat: -rw-r--r-- 3,671 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
--
-- (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")
local categories_utils = require("categories_utils")
local page_utils = require("page_utils")

sendHTTPContentTypeHeader('text/html')


-- ################################################
-- Table parameters
local currentPage  = _GET["currentPage"]
local perPage      = _GET["perPage"]
local sortColumn   = _GET["sortColumn"]
local sortOrder    = _GET["sortOrder"]
local category_filter = _GET["l7proto"]

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

local sortPrefs = "custom_categories_hosts"

if((sortColumn == nil) or (sortColumn == "column_"))then
   sortColumn = getDefaultTableSort(sortPrefs)
else
   if((sortColumn ~= "column_")
    and (sortColumn ~= "")) then
      tablePreferences("sort_"..sortPrefs,sortColumn)
   end
end

if(sortOrder == nil) then
   sortOrder = getDefaultTableSortOrder(sortPrefs)
else
   if((sortColumn ~= "column_")
    and (sortColumn ~= "")) then
      tablePreferences("sort_order_"..sortPrefs,sortOrder)
   end
end

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

if perPage == nil then
   perPage = getDefaultTableSize()
else
   perPage = tonumber(perPage)
   tablePreferences("rows_number", perPage)
end

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

interface.select(ifname)
local ifid = getInterfaceId(ifname)

local to_skip = (currentPage-1) * perPage

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

local items = {}
local sorter = {}
local num_items = 0

local items = interface.getnDPICategories()

for item_name, item_id in pairs(items) do
   if not isEmptyString(category_filter) and (category_filter ~= item_id) then
      goto continue
   end

   local hosts_list = categories_utils.getCustomCategoryHosts(item_id)
   local num_hosts = #hosts_list
   local num_protocols = table.len(interface.getnDPIProtocols(tonumber(item_id)))

   items[item_name] = { name = item_name, id = item_id, num_hosts = num_hosts, hosts_list = hosts_list , num_protocols = num_protocols }
   num_items = num_items + 1

   if sortColumn == "column_" or sortColumn == "column_category_name" then
      sorter[item_name] = getCategoryLabel(item_name)
   elseif sortColumn == "column_num_hosts" then
      sorter[item_name] = num_hosts
   elseif sortColumn == "column_num_protos" then
      sorter[item_name] = num_protocols
   end

   ::continue::
end

local res_formatted = {}
local cur_num = 0
for sorted_item, _ 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 record = {}

   record["column_category_id"] = tostring(items[sorted_item]["id"])
   record["column_category_name"] = getCategoryLabel(items[sorted_item]["name"])
   record["column_num_hosts"] = tostring(items[sorted_item].num_hosts)
   record["column_num_protos"] = string.format('<a href="%s/lua/admin/edit_categories?tab=protocols&category=cat_%u">%s</a>', ntop.getHttpPrefix(), items[sorted_item]["id"], tostring(items[sorted_item].num_protocols))
   record["column_category_hosts"] = table.concat(items[sorted_item].hosts_list, ",")

   res_formatted[#res_formatted + 1] = record
   ::continue::
end

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

print(json.encode(result, nil))