File: sys_utils.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 (179 lines) | stat: -rw-r--r-- 4,503 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
--
-- (C) 2013-22 - ntop.org
--

local sys_utils = {}

-- If false, some commands and config files are not executed/created really
-- This will be overwritten with the config value
local REAL_EXEC = ntop.getPref("ntopng.prefs.nedge_real_exec") == "1"

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

local FileMock = {}
FileMock.__index = FileMock

function FileMock.open(fname, mode)
  local obj = {}
  setmetatable(obj, FileMock)
  traceError(TRACE_NORMAL, TRACE_CONSOLE, "[File::" .. fname .. "]")
  return obj
end

function FileMock:write(data)
  local lines = split(data, "\n")

  for _, line in pairs(lines) do
    if not isEmptyString(line) then
      traceError(TRACE_NORMAL, TRACE_CONSOLE, "\t"..line)
    end
  end

  return true
end

function FileMock:close()
  traceError(TRACE_NORMAL, TRACE_CONSOLE, "[File::END]")
  return true
end

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

function sys_utils.setRealExec(new_v)
  REAL_EXEC = new_v
  local v = ternary(REAL_EXEC, "1", "0")

  if ntop.getPref("ntopng.prefs.nedge_real_exec") ~= v then
    ntop.setPref("ntopng.prefs.nedge_real_exec", v)
  end
end

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

function sys_utils.openFile(fname, mode)
  local cls

  if REAL_EXEC then
    cls = io
  else
    cls = FileMock
  end

  return cls.open(fname, mode)
end

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

-- Execute a system command
function sys_utils.execCmd(cmd)
   if(REAL_EXEC) then
    -- traceError(TRACE_NORMAL, TRACE_CONSOLE, "[execCmd] ".. cmd)
    return(os.execute(cmd))
   else
    traceError(TRACE_NORMAL, TRACE_CONSOLE, "[execCmd] ".. cmd)
    return 0
   end
end

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

-- execCmd with command output
-- If input is specified, it is provided as stdin to the command
-- otherwise the command is executed with no input and the output returned
-- NOTE: no check for REAL_EXEC, the command will always be executed!
function sys_utils.execShellCmd(cmd, input)
   local f, s
   
   if input then
      f = io.popen(cmd, 'w')
      if f then
         f:write(input)
         s = true
      else
         s = false      
      end
   else 
      f = assert(io.popen(cmd, 'r'))
      s = assert(f:read('*a'))
   end

   if f then
      f:close()
   end     

   return s
end

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

local function _isServiceStatus(service_name, status)
  local check_cmd = "systemctl is-"..status.." " .. service_name
  local is_active = sys_utils.execShellCmd(check_cmd)

  return ternary(string.match(is_active, "^"..status), true, false)
end

function sys_utils.isActiveService(service_name)
   return _isServiceStatus(service_name, "active")
end

function sys_utils.isFailedService(service_name)
   return _isServiceStatus(service_name, "failed")
end

function sys_utils.isActiveFailedService(service_name)
  local check_cmd = "systemctl is-active " .. service_name
  local is_active = sys_utils.execShellCmd(check_cmd)

  return ternary(string.match(is_active, "inactive"), false, true)
end

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

function sys_utils.enableService(service_name)
  return sys_utils.execCmd("systemctl enable " .. service_name)
end

function sys_utils.disableService(service_name)
  return sys_utils.execCmd("systemctl disable " .. service_name)
end

function sys_utils.restartService(service_name)
  return sys_utils.execCmd("systemctl restart " .. service_name)
end

function sys_utils.stopService(service_name)
  return sys_utils.execCmd("systemctl stop " .. service_name)
end

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

function sys_utils.rebootSystem()
  local do_reboot = ternary(REAL_EXEC, "reboot", nil)
  ntop.shutdown(do_reboot)
end

function sys_utils.shutdownSystem()
  local do_shutdown = ternary(REAL_EXEC, "poweroff", nil)
  ntop.shutdown(do_shutdown)
end

function sys_utils.restartSelf()
  local do_restart_self = ternary(REAL_EXEC, "restart_self", nil)
  ntop.shutdown(do_restart_self)
end

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

function sys_utils.loadConntrack()
  local info = ntop.getInfo(false)
  local os = info.OS

  if not string.find(os, "Ubuntu 20%.") then
    sys_utils.execCmd("modprobe nf_conntrack_ipv4")
  end
end

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

return sys_utils