summaryrefslogtreecommitdiffstats
path: root/logfiles-model.lua
blob: 4c3f5662e268b8b557f786be8780e4fc2979b191 (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
-- acf model for displaying logfiles
local mymodule = {}

posix = require("posix")
modelfunctions = require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")

-- These folders (and their subfolders) are going to be listed
local logfilepaths = {"/var/log"}

local function is_file_in_use(path)
	return (modelfunctions.run_executable({"fuser", path}) or "unknown") ~= ""
end

-- Function to recursively insert all filenames in a dir into an array
-- Links are followed and only actual files are listed
local function recursedir(path, filearray)
	filearray = filearray or {}
	local k,v
	for k,v in pairs(posix.dir(path) or {}) do
		-- Ignore files that begins with a '.'
		if not string.match(v, "^%.") then
			local file = path .. "/" .. v
			-- If subfolder exists, list files in this subfolder
			local st = posix.stat(file)
			while st and st.type == "link" do
				file = posix.readlink(file)
				st = posix.stat(file)
			end
			if st and st.type == "directory" then
				recursedir(file, filearray)
			elseif st and st.type == "regular" then
				table.insert(filearray, file)
			end
		end
	end
	return filearray
end

-- Function to list available files for view/delete
local function list_files()
	local files = {}
	-- Generate a single table with all the files
	for i,p in pairs(logfilepaths) do
		recursedir(p, files)
	end
	table.sort(files)
	return files
end

local do_grep = function(filecontent, grep)
	if grep and grep ~= "" then
		local lines = {}
		for line in string.gmatch(filecontent.value, "[^\n]*\n?") do
			if string.match(line, grep) then
				lines[#lines+1] = line
			end
		end
		filecontent.value = table.concat(lines) or ""
	end
end

mymodule.get_filedetails = function(self, clientdata)
	local retval = cfe({ type="group", value={}, label="Logfile details" })
	retval.value.filename = cfe({ label="File name", key=true })
	retval.value.grep = cfe({ label="Grep", key=true })
	self.handle_clientdata(retval, clientdata)

	local success = false
	for i,file in ipairs(list_files()) do
		if file == retval.value.filename.value then
			success = true
			break
		end
	end
	if success then
		local file = retval.value.filename.value
		local st = posix.stat(file)
		while st.type == "link" do
			file = posix.readlink(file)
			st = posix.stat(file)
		end

		local filedetails = modelfunctions.getfiledetails(file)
		for n,v in pairs(filedetails.value) do
			if n ~= "filename" then
				retval.value[n] = v
			end
		end
		if filedetails.errtxt then
			retval.errtxt = filedetails.errtxt
		else
			do_grep(retval.value.filecontent, retval.value.grep.value)
		end
	else
		retval.errtxt = "Invalid log file"
	end
	return retval
end

mymodule.tail = function(self, clientdata)
	local retval = cfe({ type="group", value={}, label="Tail Logfile" })
	retval.value.filename = cfe({ label="File name", key=true })
	retval.value.offset = cfe({ value="0", label="File offset", key=true })
	retval.value.grep = cfe({ label="Grep", key=true })
	self.handle_clientdata(retval, clientdata)

	retval.value.size = cfe({ value="0", label="File size" })
	retval.value.filecontent = cfe({ type="longtext", label="File content" })

	retval.value.filename.errtxt = "File not found"
	for i,file in ipairs(list_files()) do
		if ( file == retval.value.filename.value ) then
			retval.value.filename.errtxt = nil
			local f = io.open(retval.value.filename.value)
			if tonumber(retval.value.offset.value) then
				local offset = tonumber(retval.value.offset.value)
				if offset < 0 then
					f:seek("end", offset)
				else
					f:seek("set", offset)
				end
				retval.value.filecontent.value = f:read("*all")
				retval.value.size.value = f:seek()
			else
				retval.value.size.value = f:seek("end")
				retval.value.offset.value = retval.value.size.value
			end
			f:close()
			do_grep(retval.value.filecontent, retval.value.grep.value)
			break
		end
	end

	return retval
end

mymodule.get = function ()
	local retval = {}
	for i,file in pairs(list_files()) do
		local details = posix.stat(file)
		details.filename = file
		details.inuse = is_file_in_use(file)
		table.insert(retval, details)
	end
	table.sort(retval, function(a,b) return a.filename < b.filename end)
	return cfe({ type="structure", value=retval, label="Log Files" })
end

mymodule.get_delete = function()
	local filename = cfe({ type="select", label="File name", option=list_files() })

	return cfe({ type="group", value={filename=filename}, label="Delete logfile" })
end

-- Function to check if a file is deletable, and if it is, then delete it.
mymodule.delete = function (self, filetodelete)
	local success = modelfunctions.validateselect(filetodelete.value.filename)

	if success then
		-- Check if file is deletable (or in use)
		if is_file_in_use(filetodelete.value.filename.value) then
			success = false
			filetodelete.value.filename.errtxt = "File in use"
		else
			local file = filetodelete.value.filename.value
			local st = posix.stat(file)
			while st.type == "link" do
				file = posix.readlink(file)
				st = posix.stat(file)
			end
			local status, err = os.remove(file)
			if err then
				success = false
				filetodelete.value.filename.errtxt = err
			end
		end
	end

	if not success then
		filetodelete.errtxt = "Failed to delete file"
	end

	return filetodelete
end

return mymodule