summaryrefslogtreecommitdiffstats
path: root/unbound-model.lua
blob: 0b505a974c1b81e1e17210eee0ceaf45f31648dc (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
local mymodule = {}

-- Load libraries
modelfunctions = require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")

-- Set variables
local configfile = "/etc/unbound/unbound.conf"
local processname = "unbound"
local packagename = "unbound"

local config

-- ################################################################################
-- LOCAL FUNCTIONS

local parseconfig = function()
	if config then return config end
	local temp = format.parse_linesandwords(fs.read_file(configfile) or "", "#")
	config = {}
	local cur = config
	for i,line in ipairs(temp) do
		local attr = string.match(line[1], "(.*):")
		if #line == 1 then
			config[attr] = {}
			cur = config[attr]
		else
			table.remove(line, 1)
			cur[attr] = table.concat(line, " ")
		end
	end
	return config
end

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

function mymodule.get_startstop(self, clientdata)
        return modelfunctions.get_startstop(processname)
end

function mymodule.startstop_service(self, startstop, action)
        return modelfunctions.startstop_service(startstop, action)
end

function mymodule.getstatus()
	return modelfunctions.getstatus(processname, packagename, "Unbound Status")
end

function mymodule.get_filedetails()
	return modelfunctions.getfiledetails(configfile)
end

function mymodule.update_filedetails(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, {configfile})
end

function mymodule.get_logfile(f)
	local retval = cfe({ type="structure", value={}, label="Unbound Log Files" })
	config = config or parseconfig()
	-- Determine the log file from the config file
	if config.server and config.server.logfile and config.server.logfile ~= "" then
		retval.value[#retval.value+1] = {filename=config.server.logfile}
	else
		-- report syslog even if use-syslog == "no"
		retval.value[#retval.value+1] = {facility="daemon", grep="unbound"}
	end
	return retval
end

return mymodule