File: get_host_daily_activity.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 (106 lines) | stat: -rw-r--r-- 4,081 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
--
-- (C) 2013-16 - ntop.org
--

local debug = true

function host2id(d, name)
   sql = 'SELECT idx FROM hosts WHERE host_name="'..name..'"'
   if(debug) then print(sql.."\n") end
   for idx in d:urows(sql) do return(tonumber(idx)) end

   return nil
end

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

require "lua_utils"
require "graph_utils"

host_ip     = _GET["host"]
mode        = _GET["mode"]
family      = _GET["family"]
-- #####################################
currentPage = _GET["currentPage"]
perPage     = _GET["perPage"]
sortColumn  = _GET["sortColumn"]
sortOrder   = _GET["sortOrder"]
protocol_id = _GET["protocol"]
mode        = _GET["mode"]
host        = _GET["host"]
format      = _GET["format"]

t = os.time() -- -86400
when = os.date("%y%m%d", t)
dump_dir = fixPath(dirs.workingdir .. "/datadump/")
db_name = dump_dir.."20"..when..".sqlite"

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

if(perPage == nil) then
   perPage = getDefaultTableSize()
else
   perPage = tonumber(perPage)
end

if(sortOrder == "asc") then
   funct = asc
else
   funct = rev
end

if(sortOrder == nil) then
   sortOrder = ""
end

if(sortColumn == nil) then
   sortColumn = "column_"
end

sendHTTPHeader('application/json')

-- print(db_name.."\n")

db = sqlite3.open(db_name)
if((db == nil) or (host_ip == nil)) then
   print("{ }\n")
else
   interface.select(ifname)
   
   if(mode == "overview") then
      contact_type = 0 -- contacted_by
      contact_type = 1 -- contacted_peers
      
      sql = 'select contacts.contact_type,hosts.host_name,sum(contacts.num_contacts) as tot from contacts,hosts where contacts.activity_idx in (select activities.idx from activities,hosts where activities.host_idx=hosts.idx and hosts.host_name="'..host_ip..'") and contacts.contact_family !=254 and contacts.host_idx=hosts.idx group by contacts.contact_type,hosts.host_name order by tot desc,contacts.contact_type,hosts.host_name limit '..perPage
      elseif(mode == "overview_1") then
      sql = 'select contacts.contact_type,hosts.host_name,contacts.contact_family,sum(contacts.num_contacts) as tot from contacts,hosts where contacts.activity_idx in (select activities.idx from activities,hosts where activities.host_idx=hosts.idx and hosts.host_name="'..host_ip..'") and contacts.host_idx=hosts.idx group by contacts.contact_type,contacts.contact_family,hosts.host_name order by tot desc,contacts.contact_type,hosts.host_name limit '..perPage
      elseif(mode == "overview_family") then
      if(family == nil) then family = 5 end -- DNS
      sql = 'select contacts.contact_type,hosts.host_name,sum(contacts.num_contacts) as tot from contacts,hosts where contacts.activity_idx in (select activities.idx from activities,hosts where activities.host_idx=hosts.idx and hosts.host_name="'..host_ip..'") and contacts.contact_family='.. family..' and contacts.host_idx=hosts.idx group by contacts.contact_type,hosts.host_name order by tot desc,contacts.contact_type,hosts.host_name limit '..perPage
      --print(sql.."\n")
   else
      sql = 'select contacts.contact_type,hosts.host_name,contacts.contact_family,contacts.num_contacts from contacts,hosts where contacts.activity_idx in (select activities.idx from activities,hosts where activities.host_idx=hosts.idx and hosts.host_name="'..host_ip..'") and contacts.host_idx=hosts.idx order by contacts.contact_type,hosts.host_name'
      
   end
end

print("{ \"currentPage\" : " .. currentPage .. ",\n \"data\" : [\n")

n = 0
for contact_type,host_name,contact_family,num_contacts in db:urows(sql) do 
   if(n > 0) then print(",\n") end
   print("{ \"column_contact_type\" :  "..contact_type..", \"column_host_name\": \""..host_name.."\", \"column_contact_family\": "..contact_family..", \"column_num_contacts\": "..num_contacts.." }")
   n = n + 1
end

print("\n], \"perPage\" : " .. perPage .. ",\n")
print("\"sort\" : [ [ \"" .. sortColumn .. "\", \"" .. sortOrder .."\" ] ],\n")
print("\"totalRows\" : " .. n .. " \n}")


db:close()