File: sqlite_utils.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 (290 lines) | stat: -rw-r--r-- 8,666 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--
-- (C) 2014-15 - ntop.org
--

-- This file contains the description of all functions
-- used to interact with sqlite

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

require "lua_utils"
require "template"
local j = require ("dkjson")

local SqliteClass = {} -- the table representing the class, which will double as the metatable for the instances
SqliteClass.__index = SqliteClass -- failed table lookups on the instances should fallback to the class table, to get methods

-- syntax equivalent to "SqliteClass.new = function..."
function SqliteClass.new(init)
  local self = setmetatable({}, SqliteClass)

  self.debug = false
  self.class_benchmark = os.clock()
  self.query_benchmark = os.clock()
  self.flows_benchmark = os.clock()

  -- Template
  self.flow_template = {
  ["IPV4_SRC_ADDR"]   = function (table,val) table["cli.ip"]          = val           end,
  ["L4_SRC_PORT"]     = function (table,val) table["cli.port"]        = tonumber(val) end,
  ["IPV4_DST_ADDR"]   = function (table,val) table["srv.ip"]          = val           end,
  ["L4_DST_PORT"]     = function (table,val) table["srv.port"]        = tonumber(val) end,
  ["PROTOCOL"]        = function (table,val)
  if (l4_template[tonumber(val)] ~= nil ) then
    table["proto.l4"]        = l4_template[tonumber(val)]
  else
    table["proto.l4"]        = val
  end
  end,
    -- ["SRC_VLAN"]        = function (table,val) table["vlan"]            = tonumber(val) end,
    -- ["DST_VLAN"]        = function (table,val) table["vlan"]            = tonumber(val) end,
    ["L7_PROTO_NAME"]   = function (table,val) table["proto.ndpi"]      = val           end,
    ["TCP_FLAGS"]       = function (table,val) table["tcp_flags"]       = tonumber(val) end,
    ["OUT_PKTS"]        = function (table,val) table["cli2srv.packets"] = tonumber(val) end,
    ["OUT_BYTES"]       = function (table,val) table["cli2srv.bytes"]   = tonumber(val) end,
    ["IN_PKTS"]         = function (table,val) table["srv2cli.packets"] = tonumber(val) end,
    ["IN_BYTES"]        = function (table,val) table["srv2cli.bytes"]   = tonumber(val) end,
  }

  -- Query
  self.response = nil
  self.db = nil;
  self.query = nil;
  self.number_rows = 0;

  -- Flows
  self.flows = nil
  self.flows_num = 0


  return self
end

-- ###################################
-- Getter and setter

function SqliteClass.setDebug(self, bool)
  if (bool == nil) then bool = false end
  self.debug = bool
  return self.debug
end

function SqliteClass.getDebug(self)     return self.debug end
function SqliteClass.getFlows(self)     return self.flows end
function SqliteClass.getFlowsNum(self)  return self.flows_num end
function SqliteClass.getResponse(self)  return self.response end
function SqliteClass.getDB(self)        return self.db end
function SqliteClass.getQuery(self)     return self.query end


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

function SqliteClass.execQuery(self, db ,query)
  if (db == nil) then return -1 end
  if (query == nil) then return -2 end

  self.db = db;
  self.query = query;

  self.query_benchmark = os.clock()
  -- io.write(dirs.workingdir ..db..'\n')
  self.response = ntop.execQuery(dirs.workingdir ..db , query)
  n_rows = ntop.execQuery(dirs.workingdir ..db , "SELECT COUNT (*) as rows_number FROM flows")

  if (n_rows ~= nil) then
    self.number_rows = n_rows[1]["rows_number"]
  end

  Sqlite:benchmark("query",os.clock())

  return self.response
end

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

function SqliteClass.getFlows(self)
  if (self.response == nil) then return nil end
  -- Init some parameters
  self.flows_benchmark = os.clock()
  self.flows = {}
  num = 0

  for _k,_v in pairs(self.response) do

       -- init table of table
       self.flows[num] = {}
       self.flows[num]["ID"] = tonumber(self.response[_k]["ID"])
       self.flows[num]["vlan"] = tonumber(self.response[_k]["vlan_id"])
       self.flows[num]["bytes"] = tonumber(self.response[_k]["bytes"])
       self.flows[num]["duration"] = tonumber(self.response[_k]["duration"])

       -- Throughput
       -- io.write((self.flows[num]["bytes"] / self.flows[num]["duration"])..'\n')
       self.flows[num]["throughput_bps"] = (self.flows[num]["bytes"] / 8 ) / (self.flows[num]["duration"])
       self.flows[num]["throughput_trend_bps"] = 3


       local info, pos, err = j.decode(self.response[_k]["json"], 1, nil)

       if (info == nil) then
        traceError(TRACE_ERROR,TRACE_CONSOLE,"Impossible read json form sqlite: ".. err)
      else

        for key,val in pairs(info) do

          label_key = nil

          -- Check if the option --jsonlabes is active
          if (rtemplate[tonumber(key)] ~= nil) then label_key = rtemplate[tonumber(key)] end
          if (template[key] ~= nil) then label_key = key end

          -- Convert template id into template name
          if (label_key ~= nil) then

            if (self.flow_template[label_key] ~= nil) then
              -- Call conversion function in order to convert some value to number if it is necessary
              self.flow_template[label_key](self.flows[num],val)
            else
              -- Leave the default key and value
              self.flows[num][key] = val
            end

          end
        end -- info loop

        -- Custom parameters
        if (self.flows[num]["proto.ndpi"] == nil) then
          self.flows[num]["proto.ndpi"] = "Unknown"
        end
      end -- info == nil

      num = num + 1
    end -- for


    -- self.writeFlows(self)
    self.flows_num = num
    Sqlite:benchmark("flows",os.clock())

    return self.flows
  end


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

function SqliteClass.getRowsNumber(self)
  return (self.number_rows);
end

-- ###################################
-- Utils

function SqliteClass.benchmark(self,type,time)
  if ((time ~= nil) and (self.debug))then
    bk_text = "class"
    bk = self.class_benchmark

    if (type == "query") then
     bk = self.query_benchmark
     bk_text = type
     elseif (type == "flows") then
      bk = self.flows_benchmark
      bk_text = type
    end

    traceError(TRACE_DEBUG,TRACE_CONSOLE,string.format(bk_text .. ": elapsed time: %.4f", time - bk))
  end
end

---------------------------------------

function SqliteClass.writeFlows(self)
  if (self.flows ~= nil) then
    for key,val in pairs(self.flows) do
      traceError(TRACE_DEBUG,TRACE_CONSOLE,'Flow: '..key)
      for k,v in pairs(self.flows[key]) do
        traceError(TRACE_DEBUG,TRACE_CONSOLE,'\t'..k..' - '..v)
      end
    end
  end
end


function getParameters(datetime,action)

  datetime_tbl = cleanDateTime(datetime, action)

  datetime_tbl["displayed"] = os.date("%m/%d/%Y %I:%M %p",datetime_tbl["epoch"])
  traceError(TRACE_DEBUG,TRACE_CONSOLE,'Displayed: ['..datetime_tbl["displayed"] .. ']')
  datetime_tbl["query"] = os.date("/%Y/%m/%d/%H/%M",datetime_tbl["epoch"])
  traceError(TRACE_DEBUG,TRACE_CONSOLE,'query: ['..datetime_tbl["query"] .. ']')

  return datetime_tbl
end



function cleanDateTime(datetime,action)

  if (datetime == nil) then return {} end
  traceError(TRACE_DEBUG,TRACE_CONSOLE,'Initial date and time: '..datetime)

  tbl = split(datetime," ")
  q_date = tbl[1]
  q_time = tbl[2]
  q_type = tbl[3]

  traceError(TRACE_DEBUG,TRACE_CONSOLE,'Default value: ['..q_date..']['..q_time .. '][' .. q_type..']')

  date_tbl  = split(q_date,"/")
  q_month    = tonumber(date_tbl[1])
  q_day      = tonumber(date_tbl[2])
  q_year     = tonumber(date_tbl[3])

  traceError(TRACE_DEBUG,TRACE_CONSOLE,'Default value: ['..q_day..']['..q_month .. '][' .. q_year..']')

  time_tbl = split(q_time,":")
  q_hour   = tonumber(time_tbl[1])
  q_min  = tonumber(time_tbl[2])

  if (q_type == "PM") then q_hour = q_hour + 12 end

  traceError(TRACE_DEBUG,TRACE_CONSOLE,'Default value: ['..q_hour..']['..q_min .. ']')

  q_epoch = os.time({year=q_year, month=q_month, day=q_day, hour=q_hour, min=q_min})

  traceError(TRACE_DEBUG,TRACE_CONSOLE,'epoch: ['.. q_epoch ..']')

  if (action ~= nil) then
    if (action == "newer") then
      q_epoch = q_epoch + 300 -- plus 5 min
    else
      q_epoch = q_epoch - 300 -- min 5 min
    end
  end

  mktime = os.date("%m/%d/%Y %I:%M %p", q_epoch)

  traceError(TRACE_DEBUG,TRACE_CONSOLE,'time: ['.. mktime ..']')

  ret_tbl = {}
  ret_tbl["year"] = q_year
  ret_tbl["month"] = month
  ret_tbl["day"] = q_day

  ret_tbl["q_hour"] = q_q_hour
  ret_tbl["q_min"] = q_q_min

  ret_tbl["epoch"] = q_epoch

  return ret_tbl

end

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

-- Don't remove below line
Sqlite = SqliteClass.new()
-- Sqlite:benchmark(os.clock())
Sqlite:setDebug(false)