File: get_host_categories.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 (113 lines) | stat: -rw-r--r-- 3,346 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
--
-- (C) 2013-16 - ntop.org
--

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

require "lua_utils"
require "graph_utils"

local json = require ("dkjson")

-- datatable parameters
local current_page = _GET["currentPage"]
local per_page     = _GET["perPage"]
local sort_column  = _GET["sortColumn"]
local sort_order   = _GET["sortOrder"]

interface.select(ifname)
local ifid = _GET["ifid"]
local host_info = url2hostinfo(_GET)
local host = interface.getHostInfo(host_info["host"],host_info["vlan"])
local categories = host["categories"]

if sort_column == nil then
   sort_column = getDefaultTableSort("host_categories")
else	
   if sort_column ~= "column_" and sort_column ~= "" then
      tablePreferences("host_categories", sort_column)
   end
end

if sort_column == "column_" then
sort_column = "column_bytes"
end

if sort_order == "asc" then
   funct = asc
else
   funct = rev
end

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

if per_page == nil then
   per_page = getDefaultTableSize()
else
   per_page = tonumber(per_page)
   tablePreferences("rows_number", per_page)
end
local to_skip = (current_page - 1) * per_page
if to_skip < 0 then to_skip = 0 end

local total_bytes = 0
for cat_name, cat_bytes in pairs(categories) do
   local cat_contents = {}
   cat_contents["column_id"] = cat_name
   cat_contents["column_label"] = capitalize(getCategoryLabel(cat_name))
   cat_contents["column_bytes"] = cat_bytes
   total_bytes = total_bytes + cat_bytes
   categories[cat_name] = cat_contents
end

local sorter = {}
for cat_name, cat_contents in pairs(categories) do
   if sort_column == "column_id" then sorter[cat_name] = cat_name
   elseif sort_column == "column_name" then sorter[cat_name] = cat_contents["column_name"]
   elseif sort_column == "column_bytes" then sorter[cat_name] = cat_contents["column_bytes"]
   else sorter[cat_name] = cat_name end
end

-- format bytes and add hyper links
for cat_name, cat_contents in pairs(categories) do
   if total_bytes > 0 then
      cat_contents["column_pct"] = round(cat_contents["column_bytes"] / total_bytes * 100, 2).."%"
   else
      cat_contents["column_pct"] = "NaN"
   end
   cat_contents["column_bytes"] = bytesToSize(cat_contents["column_bytes"])

   local label = getCategoryLabel(cat_name)
   local fname = getRRDName(ifid, hostinfo2hostkey(host_info), "categories/"..label..".rrd")
   if ntop.exists(fname) then
      cat_contents["column_label"] = "<A HREF=\""..ntop.getHttpPrefix().."/lua/host_details.lua?ifname="..ifid.."&"..hostinfo2url(host_info) .. "&page=historical&rrd_file=categories/".. label ..".rrd\"><b>"..cat_contents["column_label"].."</b></A>"
   end
end

local num_page = 0
local total_rows = 0
local result_data = {}
for cat_name,_ in pairsByValues(sorter, funct) do
   if to_skip > 0 then
      to_skip = to_skip - 1
   elseif num_page < per_page then
      table.insert(result_data, categories[cat_name])
      num_page = num_page + 1
   end
   total_rows = total_rows + 1
end
local result = {}
result["perPage"] = per_page
result["currentPage"] = current_page
result["totalRows"] = total_rows
result["data"] = result_data
result["sort"] = {{sort_column, sort_order}}

sendHTTPHeader('text/html; charset=iso-8859-1')
print(json.encode(result, nil))