File: edit_syslog_producer.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 (141 lines) | stat: -rw-r--r-- 3,469 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
--
-- (C) 2019-22 - ntop.org
--

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

require "lua_utils"
local json = require("dkjson")
local syslog_utils = require "syslog_utils"

sendHTTPContentTypeHeader('application/json')

local rv = {}

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

local action = _POST["action"]
local host = _POST["syslog_producer_host"]
local syslog_producer = _POST["syslog_producer"]

local ifid = interface.getId()
if tonumber(_GET["ifid"]) ~= nil then
  ifid = _GET["ifid"]
end

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

local function getProducersMapKey(ifid)
  return string.format("ntopng.syslog.ifid_%d.producers_map", ifid)
end

local function reportError(msg)
  print(json.encode({ error = msg, success = false }))
end

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

if not isAdministrator() then
  reportError(i18n("not_admin"))
  return
end

if ifid == nil then
  reportError(i18n("syslog.empty_action"))
  return 
end

if isEmptyString(action) then
  reportError(i18n("syslog.empty_action"))
  return
end

if isEmptyString(host) then
  reportError(i18n("missing_x_parameter", {param='syslog_producer_host'}))
  return
end

if isEmptyString(syslog_producer) then
  reportError(i18n("missing_x_parameter", {param='syslog_producer'}))
  return
end

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

if(action == "add") then
   local existing

   local existing = syslog_utils.hasProducer(ifid, host)

   if existing then
      reportError(i18n("syslog.host_exists", {host=host}))
      return
   end

   syslog_utils.addProducer(ifid, host, syslog_producer)
   rv.message = i18n("syslog.host_add_ok", {host=host})

elseif(action == "edit") then
   local existing
   local old_host = _POST["old_syslog_producer_host"]
   local old_syslog_producer = _POST["old_syslog_producer"]

   if isEmptyString(old_host) then
      reportError(i18n("missing_x_parameter", {param='old_syslog_producer_host'}))
      return
   end

   if isEmptyString(old_syslog_producer) then
      reportError(i18n("missing_x_parameter", {param='old_syslog_producer'}))
      return
   end

   existing = syslog_utils.hasProducer(ifid, old_host)

   if not existing then
      reportError(i18n("syslog.host_not_exists", {host=old_host}))
      return
   end

   if old_host ~= host or old_syslog_producer ~= syslog_producer then

      if old_host ~= host then
        existing = syslog_utils.hasProducer(ifid, host, syslog_producer)

        if existing then
	  reportError(i18n("syslog.host_exists", {host=host}))
	  return
        end
      end

      syslog_utils.deleteProducer(ifid, old_host, old_syslog_producer)
      syslog_utils.addProducer(ifid, host, syslog_producer)
   end

   rv.message = i18n("syslog.host_edit_ok", {host=old_host})

elseif(action == "delete") then
   local existing = syslog_utils.hasProducer(ifid, host, syslog_producer)

   if not existing then
      reportError(i18n("syslog.host_not_exists", {host=host}))
      return
   end

   syslog_utils.deleteProducer(ifid, host, syslog_producer)
   rv.message = i18n("syslog.host_delete_ok", {host=host})

else
   reportError(i18n("syslog.bad_action_param"))
   return
end

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

interface.updateSyslogProducers()

rv.success = true
print(json.encode(rv))