summaryrefslogtreecommitdiffstats
path: root/syslog-model.lua
blob: 3d500317df6f99237731b95f5e45b8dc3a829605 (plain)
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
module(..., package.seeall)

require("fs")
require("procps")
require("getopts")
require("format")
require("daemoncontrol")
require("validator")

local configfile = "/etc/conf.d/syslog"
local processname = "syslog"

local function get_version()
	local cmd = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin apk_version -v -s busybox | cut -d ' ' -f 1"
	local cmd_output = io.popen( cmd )
	local cmd_output_result = cmd_output:read("*a") or ""
	cmd_output:close()
	return cmd_output_result
end

local function getloglevels()
	local loglevels = {}
	for i=1,8 do
		table.insert(loglevels,tostring(i))
	end
	return loglevels
end

local writeconfig = function (config)
	-- Set variables
	local variable = "SYSLOGD_OPTS"
	local variabletranslator = { 
		logfile = "-O", 
		loglevel = "-l",
		smallerlogs = "-S",
		maxsize = "-s",
		numrotate = "-b",
		localandnetworklog = "-L",
		remotelogging = "-R",
		}

	local configdata = {}

	for name, entry in pairs(variabletranslator) do
		if config.value[name] and config.value[name].type == "boolean" then
			if config.value[name].value then configdata[entry] = "" end
		elseif config.value[name] and config.value[name].value and #config.value[name].value > 0 then
			configdata[entry] = config.value[name].value
		else
			configdata[entry] = nil
		end
	end

	local cmdresult, cmdmessage, cmderror = getopts.setoptsinfile(configfile,"",variable,'"' .. getopts.table_to_opts(configdata) .. '"')
	return cmdmessage, cmderror
end

local makeconfig = function(configcontent)
	local config = {}
	configcontent = configcontent or {}
	-- Next section selects which configurations we should show to the user
	config["logfile"] = cfe({ 
		label = "Log to given file",
		descr = "File must be in /var/log directory",
		value = configcontent["-O"] or "/var/log/messages",
		})
	config["loglevel"] = cfe({ 
		label = "Set local log level",
		value = configcontent["-l"] or "8",
		type = "select",
		option = getloglevels(),
		descr = "1=Quiet, ... , " .. table.maxn(getloglevels()) .. "=Debug",
		})
	config["smallerlogs"] = cfe({ 
		label = "Smaller logging output",
		value = (configcontent["-S"] == ""),
		type = "boolean",
		})
	config["maxsize"] = cfe ({ 
		label = "Max size (KB) before rotate",
		descr = "Default=200KB, 0=off",
		value = configcontent["-s"] or "",
		})
	config["numrotate"] = cfe ({
		label = "Number of rotated logs to keep",
		descr = "Default=1, max=99, 0=purge",
		value = configcontent["-b"] or "",
		})
	config["localandnetworklog"] = cfe ({ 
		label = "Log locally and via network",
		value = (configcontent["-L"] == ""),
		type = "boolean",
		descr = "Default is network only if host[:PORT] is defined",
		})
	config["remotelogging"] = cfe ({ 
		label = "Log to IP or hostname on PORT",
		descr = "host[:PORT] - Default PORT=514/UDP",
		value = configcontent["-R"] or "",
		})

	return cfe({ type="group", value=config, label="Configuration" })
end

local validateconfig = function(config)
	local success = true
	-- Validate entries and create error strings
	if not validator.is_valid_filename(config.value.logfile.value, "/var/log") then
		config.value.logfile.errtxt = "Not a valid file name in /var/log"
		success = false
	end
	if not validator.is_integer_in_range(config.value.loglevel.value, 1, 8) then
		config.value.loglevel.errtxt = "Out of range!"
		success = false
	end
	if config.value.maxsize.value ~= "" and not validator.is_integer(config.value.maxsize.value) then
		config.value.maxsize.errtxt = "Must be an integer"
		success = false
	end
	if config.value.numrotate.value ~= "" and not validator.is_integer_in_range(config.value.numrotate.value, 0, 99) then
		config.value.numrotate.errtxt = "Out of range!"
		success = false
	end
	if config.value.localandnetworklog.value == true and config.value.remotelogging.value == "" then
		config.value.localandnetworklog.errtxt = "Logging to local and network is not possible unless you define a host for remote logging"
		success = false
	end
	local hostport = format.string_to_table(config.value.remotelogging.value, ":")
	if hostport[2] and not (validator.is_port(hostport[2])) then
		config.value.remotelogging.errtxt = "Not a valid port"
		success = false
	end

	return success, config
end

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service ( self, action )
	-- action is validated in daemoncontrol
	local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, action)
	return cfe({ type="boolean", value=cmdresult, descr=cmdmessage, errtxt=cmderror, label="Start/Stop result" })
end

function getversion()
	return cfe({ 
		label="Program version",
		value=get_version(),
		 })
end

function getstatus()
	local status = cfe({
		label="Program status",
		value=procps.pidof("syslogd"),
		})
	if (#status.value > 0) then
		status.value = "Enabled"
	else
		status.value = "Disabled"
	end
	return status
end

function getlogging()
	local status = {}
	local opts = getconfig()
	if (opts.value.remotelogging.value == "") or (opts.value.localandnetworklog.value) then
		status.logfile = cfe({
			label="Locally logging to",
			value=opts.value.logfile.value,
			})
	end
	if (opts.value.remotelogging.value ~= "") then
		status.remote = cfe({
			label="Remote logging to",
			value=opts.value.remotelogging.value,
			})
	end

	return cfe({ type="group", value=status })
end

function get_filedetails()
	local path = configfile
	local filedetails = fs.stat(path)
	local config = getconfig(path)
	local file = {}
	file["filename"] = cfe({ 
		label="File name",
		value=path,
		})
	file["filesize"] = cfe({ 
		label="File size",
		value=filedetails.size or "0",
		})
	file["mtime"] = cfe({ 
		label="File date",
		value=filedetails.mtime or "---",
		})
	file["filecontent"] = cfe({ 
		type="longtext",
		label="File content",
		value=fs.read_file(path),
		})

	return cfe({ type="group", value=file, label="Config file details" })
end

function getconfig()
	local config = {}
	if (fs.is_file(configfile)) then
		local configcontent = getopts.getoptsfromfile(configfile, "", "SYSLOGD_OPTS", true) or {}
		config = makeconfig(configcontent)
	else
		config = makeconfig()
		config["errtxt"] = "Config file '".. configfile .. "' is missing!"
	end

	return config
end

function updateconfig (config)
	local success = true
	success, config = validateconfig(config)

	if success == true then
		config.descr, config.errtxt = writeconfig(config)
	else
		config.errtxt = "Failed to save config!"
	end

	return config
end

function update_filecontent (modifications)
	-- Validation before writing
	local configcontent = getopts.getoptsfromfile(format.dostounix(modifications), "", "SYSLOGD_OPTS", true) or {}
	local config = makeconfig(configcontent)
	local success, errtxt
	success, config = validateconfig(config)
	if success == true then
		fs.write_file(configfile, format.dostounix(modifications))
	else
		local errormessages = {}
		for x,y in pairs(config.value) do
			if y.errtxt then
				errormessages[#errormessages + 1] = y.label .. " - " .. y.errtxt
			end
		end
		errtxt = table.concat(errormessages, "\n")
	end

	return cfe({ type="boolean", value=success, errtxt=errtxt, label="Update filecontent result" })
end