File: get_historical_favourites.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 (118 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
114
115
116
117
118
--
-- (C) 2013-22 - ntop.org
--

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "db_utils"
local json = require ("dkjson")

sendHTTPContentTypeHeader('text/html')

local ifid = _GET["ifid"]
if ifid == nil or ifid == "" then
   ifid = getInterfaceId(ifname)
end

-- use this two params to see statistics of a single host
-- or for a pair of them
local host = _GET["peer1"]
local peer = _GET["peer2"]
if peer and not host then
   host = peer
   peer = nil
end

-- this is to retrieve L7 application data
local l7_proto_id = _GET["l7_proto_id"]

-- specify the type of stats
local action = _GET["fav_action"]
if action == nil or (action ~= "set" and action ~= "get" and action ~= "del" and action ~= "del_all") then
   -- default to get
   stats_type = "get"
end

local stats_type = _GET["stats_type"]
if stats_type == nil or (stats_type ~= "top_talkers" and stats_type ~= "top_applications") then
   -- default to top traffic
   stats_type = "top_talkers"
end

local favourite_type = _GET["favourite_type"]
if favourite_type == nil or
   (favourite_type ~= "talker" and favourite_type ~= "apps_per_host_pair" and
    favourite_type ~= "app" and favourite_type ~= "host_peers_by_app") then
   -- default to talkers
   -- infer the favourite type by looking at peers
   favourite_type = "talker"
end

-- start building the response
local res = {["status"] = "unable to parse the request, please check input parameters."}

-- prepare the redis key
local k = getRedisPrefix("ntopng.prefs")..'.'..tostring(ifid)..'.historical_favourites.'..stats_type..'.'..favourite_type

if action == "get" then
   -- retrieve all the elements set for this kind of preference
   res = ntop.getHashKeysCache(k)
   if res == nil then res = {} end
   -- now it's time to retrieve has values that contain resolved addresses
   -- and are a more-user friendly way to represent hosts
   for h, _ in pairs(res) do
      res[h] = ntop.getHashCache(k, h)
      if res[h] == "" or res[h] == nil then res[h] = h end
   end
elseif action == "set" or action == "del" then
   local entry = ""
   local resolved = ""

   -- TOP TALKERS favourites
   if favourite_type == "talker" or favourite_type == "apps_per_host_pair" then
      if host ~= "" and host ~= nil then
	 entry = host

	 local hinfo = hostkey2hostinfo(host)
	 resolved = hostinfo2label(hinfo)

	 if peer ~= "" and peer ~= nil then
	    entry = entry..','..peer

	    local hinfo = hostkey2hostinfo(peer)
	    resolved = resolved..','..hostinfo2label(hinfo)
	 end
      end

   -- TOP APPLICATIONS favourites
   elseif favourite_type == "app" or favourite_type == "host_peers_by_app" then
      if l7_proto_id ~= "" and l7_proto_id ~= nil then
	 entry = l7_proto_id
	 resolved = interface.getnDPIProtoName(tonumber(l7_proto_id))
	 if host ~= "" and host ~= nil then
	    entry = entry..','..host

	    local hinfo = hostkey2hostinfo(host)
	    resolved = resolved..','..hostinfo2label(hinfo)
	 end
      end
   end

   if entry ~= "" then
      if action == "set" then
	 ntop.setHashCache(k, entry, resolved)
      elseif action == "del" then
	 ntop.delHashCache(k, entry)
      end
   end
   res = {}
elseif action == "del_all" then
   ntop.delCache(k)
   res = {}
else
   -- should never be reached

end

print(json.encode(res, nil))