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

require("fs")
require("posix")

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 is_running = function( process )
	local statusreport = nil
	local cmdoutput = {}
	local cmd, error = io.popen("pidof " .. process ,r)
	local cmdoutput = string.gsub(cmd:read("*a"), "%s", "")
	cmd:close()
	if (cmdoutput ~= "") then
		statusreport = "Yes"
	end
	return statusreport
end

local function readopts(optname)
	local opts = {}
	local line
	local f = io.open("/etc/conf.d/syslog", "r")
	if (f == nil) then
		return nil
	end
	for line in f:lines() do
		local optstr = string.match(line, "^" .. optname .. "=\"?(.*)%\"+")
		if optstr then
		local option = ""
		local optvalue = ""
			opts["org"]=optstr
			for j = 1, string.len(optstr) do
			if (string.sub(optstr, j, j) == "-") then
				option=string.sub(optstr, j, j+1)
					for k = j+1, string.len(optstr) do
						if (string.sub(optstr, k, k) == "-") then
							opts[option] = string.match(string.sub(optstr, j+2, k-1),"^%s*(.*)%s?")
							break
						end
						if (k == string.len(optstr)) then
							opts[option] = string.match(string.sub(optstr, j+2, k),"^%s*(.*)%s?")
							break
						end
	
					end
				end
			end
		end
	end
	return opts
end

local function file_info ( path )
	local filedetails = posix.stat(path)	
	filedetails["owner"]=rawget((posix.getpasswd(filedetails["uid"])),"name")
	filedetails["group"]=rawget((posix.getgroup(filedetails["gid"])),"name")
	filedetails["atimelong"]=os.date("%c", filedetails["atime"])
	filedetails["mtimelong"]=os.date("%c", filedetails["mtime"])
	filedetails["path"]=path
	filedetails["name"]=basename(path)

	if ( filedetails["size"] > 1073741824 ) then
		filedetails["size"]=((filedetails["size"]/1073741824) - (filedetails["size"]/1073741824%0.1)) .. "G"
	elseif ( filedetails["size"] > 1048576 ) then
		filedetails["size"]=((filedetails["size"]/1048576) - (filedetails["size"]/1048576%0.1))  .. "M"
	elseif ( filedetails["size"] > 1024 ) then
		filedetails["size"]=((filedetails["size"]/1024) - (filedetails["size"]/1024%0.1)) .. "k"
	else
		filedetails["size"]=filedetails["size"]
	end
	return filedetails

end
-- ################################################################################
-- EXPERIMENTAL LOCAL FUNCTIONS


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

function getstatus()
	local opts = readopts("SYSLOGD_OPTS")
	local status = {}
	status.version = get_version()
	status.enabled = is_running("syslogd")
	status.logfile = opts["-O"] or "/var/log/messages"
	status.remote = opts["-R"]
	return status
end

function get_filedetails()
	local filedetails = {}
	local path = "/etc/conf.d/syslog"
	filedetails.details = file_info(path)
	filedetails.content = fs.read_file(path)
	return filedetails
end
function getconfig()
	local opts = readopts("SYSLOGD_OPTS")
	local config = {}
	config.debug = opts
	config.logfile = cfe({value=opts["-O"], type="text", option="", errtxt="" ,label="Log to given file", })
	config.loglevel = cfe({value=opts["-l"], type="radio", option="", errtxt="", helptxt="(1=Quiet, 5=Noisy, Default=Not specifyed any loglevel in the config)", label="Set local log level", })
	config.smallog =  cfe({value=opts["-S"], type="select", option="", errtxt="", label="Smaller logging output", })
	config.maxsize = cfe({value=opts["-s"], type="text", option="", errtxt="", label="Max size (KB) before rotate", })
	config.logrotate = cfe({value=opts["-b"], type="text", option="", errtxt="", label="Number of rotated logs to keep", })
	config.remote = cfe({value=opts["-R"], type="text", option="", errtxt="", label="Log to IP or hostname on PORT", })
	return config
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

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

-- we actually need root permissions to get the process list
--[[
priv = {}
function priv.is_enabled()
	return (pidof.pidof("syslogd"))
end

function priv.enable(opts)
	if opts and opts.remote then
		local f = io.open("/etc/conf.d/syslog", "w")
		if f == nil then
			return nil
		end
		f:write("# this file was written by and will be overwritten by acf\n")
		f:write("SYSLOGD_OPTS=\"-R "..opts.remote.."\"")
		f:close()
	end
	os.system("/etc/init.d/syslogd restart; rc_add -k -s 20 syslog")
end
--]]

--[[
-- this func does not need privileges
local function readopts()
	local opts = {}
	local line
	local f = io.open("/etc/conf.d/syslog", "r")
	if f == nil then
		return nil
	end
	for line in f:lines() do
		local optstr = string.match(line, "^SYSLOGD_OPTS=\"?(.*)\"?")
		if optstr then
			opts.remote = string.match(optstr, "-R%s*(.*)%s*.*$")
			opts.optstr = optstr
		end
	end
	return opts
end
--]]