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

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

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

local function get_version()
	local cmd = "/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,i)
	end
	return loglevels
end
-- ################################################################################
-- PUBLIC FUNCTIONS

function getstatus()
	local opts = getconfig()
	local status = {}
	status.version = get_version()
	status.enabled = procps.pidof("syslogd")
	if not ((opts["SYSLOGD_OPTS"]["-R"] ~= "") and not (opts["SYSLOGD_OPTS"]["-L"])) then
		status.logfile = opts["SYSLOGD_OPTS"]["-O"]
	end
	if (opts["SYSLOGD_OPTS"]["-R"]) and (opts["SYSLOGD_OPTS"]["-R"] ~= "") then
		status.remote = opts["SYSLOGD_OPTS"]["-R"]
	end
	return status
end

function get_filedetails()
	local filedetails = {}
	local path = configfile
	filedetails.details = fs.stat(path)
	filedetails.content = fs.read_file(path)
	return filedetails
end
function getconfig()
	local errors = {}
	errors["SYSLOGD_OPTS"] = {}
	errors["KLOGD_OPTS"] = {}
	local config = getopts.getoptsfromfile(configfile)
	config["SYSLOGD_OPTS"]["-O"] = config["SYSLOGD_OPTS"]["-O"] or "/var/log/messages"
	config["SYSLOGD_OPTS"]["-l_list"] = getloglevels()
	config["SYSLOGD_OPTS"]["-R"] = config["SYSLOGD_OPTS"]["-R"] or ""
	if (config["SYSLOGD_OPTS"]["-l"]) and 
	  ((tonumber(config["SYSLOGD_OPTS"]["-l"]) == nil) or (tonumber(config["SYSLOGD_OPTS"]["-l"]) > 8))  then
		errors["SYSLOGD_OPTS"]["-l"] = "Log value is out of range. Please select one of the above."
	else
		config["SYSLOGD_OPTS"]["-l"] = config["SYSLOGD_OPTS"]["-l"] or 8
	end

	return config, errors
end

service_control = function ( self, srvcmd ) 
	local srvcmd = string.lower(srvcmd)
	local retval = ""
	local line = ""
	if (srvcmd == "start") or (srvcmd == "stop") or (srvcmd == "restart") then
		local file = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/syslog " .. srvcmd .. " 2>&1" )
		if file ~= nil then
			line = file:read( "*l" )
			while line ~= nil do
				retval = retval .. "\n" .. line
				line = file:read( "*l" )
			end
			file:close()
		end
	else
		retval = "Unknown command!"
	end
	return retval
end

function setconfigs(self,value)
	--TODO: Validate so that user cant add values with '-'
end
function update_filecontent (self, modifications)
	local path = configfile
	local file_result,err = fs.write_file(path, format.dostounix(modifications))
	return file_result, err
end

-- ################################################################################
-- OTHER FUNCTIONS