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

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

local configfile = "/etc/conf.d/syslog"
local config = {}

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 startstop_service ( self, state )
	return daemoncontrol.daemoncontrol("syslog", state)
end

function getstatus()
	local opts = getconfig()
	local status = {}
	status.version = get_version()
	status.enabled = procps.pidof("syslogd")
	if (opts["SYSLOGD_OPTS"]) and not ((opts["SYSLOGD_OPTS"]["-R"] ~= "") and not (opts["SYSLOGD_OPTS"]["-L"])) then
		status.logfile = opts["SYSLOGD_OPTS"]["-O"]
	end
	if (opts["SYSLOGD_OPTS"]) and (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 = {path=path, size="0",mtime=""}
	filedetails.content = ""
	if (fs.is_file(path)) then
		filedetails.details = fs.stat(path)
		filedetails.content = fs.read_file(path)
	end
	return filedetails
end
function getconfig()
	local config = {}
	config['SYSLOGD_OPTS']={}
	local errors = {}
	errors["SYSLOGD_OPTS"] = {}
	errors["KLOGD_OPTS"] = {}
	if (fs.is_file(configfile)) then
		config = getopts.getoptsfromfile(configfile) or config
		if (type(config["SYSLOGD_OPTS"]) == "string") then
			config["SYSLOGD_OPTS"] = {}
		end
	else
		errors["configfile"] = "Config file '".. configfile .. "' is missing!"
	end
	-- Next section is to set/show default values when a option is not configured in the configfile
	if not (config["SYSLOGD_OPTS"]) then
		config["SYSLOGD_OPTS"] = {}
	end
	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 ""
	config["SYSLOGD_OPTS"]["-s"] = config["SYSLOGD_OPTS"]["-s"] or ""
	config["SYSLOGD_OPTS"]["-b"] = config["SYSLOGD_OPTS"]["-b"] or ""
	-- Next section is to print errormessages when configs are wrong
	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
	if (config["SYSLOGD_OPTS"]["-L"] ~= nil) and (config["SYSLOGD_OPTS"]["-R"] == "") then
		errors["SYSLOGD_OPTS"]["-L"] = "Logging to local and network is possible unless you define a host for remote logging."
	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,variable,parameter,value)
	--TODO: Validate so that user cant add values with '-' (could cause major breakage next time you do getopts)
	--If file is missing... then create a new one
--	if not (fs.is_file(configfile)) then fs.write_file(configfile, "SYSLOGD_OPTS=\"\"\nKLOGD_OPTS=\"\"") end
	if not (variable) or not (parameter) then return nil, "Usage setconfigs(variable,parameter,value)" end
	if not (string.find(parameter, "-%a$")) then return nil, "Parameter must be formated '-a' (where a is one upper/lowercase letter [a-z])" end
	-- Set specific variables (and their values
	if (value) and ((parameter == "-S") or (parameter == "-L")) then value = "" end
	local retval, errors = getopts.setoptsinfile(configfile,variable,parameter,value)
	return retval, errors
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