summaryrefslogtreecommitdiffstats
path: root/postgresql-model.lua
blob: 8bfe047ebe64621258e2ef2e99941739813bbfff (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
local mymodule = {}

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

-- Set variables
local confdfile = "/etc/conf.d/postgresql"
local conffile = "postgresql.conf"
local processname = "postgresql"
local packagename = "postgresql"

local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "

local datadirectory
local filelist
local confcontent

function mymodule.set_processname(p)
	processname = p
	confdfile = "/etc/conf.d/"..processname
end

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

local determinefilelist = function()
	if not filelist then
		datadirectory = datadirectory or format.get_ini_entry(fs.read_file(confdfile) or "", "", "PGDATA")
		filelist = fs.find_files_as_array(".*%.conf", datadirectory)
	end
	return filelist
end

local getconfvalue = function(param)
	datadirectory = datadirectory or format.get_ini_entry(fs.read_file(confdfile) or "", "", "PGDATA")
	confcontent = confcontent or fs.read_file(datadirectory.."/"..conffile) or ""
	conftable = conftable or format.parse_configfile(confcontent)
	local val
	if conftable then
		val = conftable[param]
	end
	if val then
		-- Remove starting '=', if present
		val = string.match(val, "[ =]*(.*)")
		-- Remove "'"
		val = string.match(val, "[^']+")
	end
	return val
end
	
local determineconnection = function()
	-- Determine the connection parameters from the config files
	local listen_addresses = getconfvalue("listen_addresses")
	if not listen_addresses or listen_addresses == "" or listen_addresses == "*" or listen_addresses == "localhost" then
		listen_addresses = ""
	elseif string.find(listen_addresses, "'") then
		listen_addresses = string.match(listen_addresses, "[^']+")
	end
	local port = getconfvalue("port")
	if not port then port = "5432" end
	return db.create(db.engine.postgresql, nil, nil, nil, listen_addresses, port)
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, "Postgresql Status")
end

function mymodule.getstatusdetails()
	return cfe({ type="longtext", value="", label="Postgresql Status Details" })
end

function mymodule.getfilelist()
	local listed_files = {}

	for i,name in ipairs(determinefilelist()) do
		local filedetails = fs.stat(name) or {}
		table.insert ( listed_files , {filename=name, mtime=filedetails.mtime or "---", filesize=filedetails.size or "0"} )
	end

	table.sort(listed_files, function (a,b) return (a.filename < b.filename) end )

	return cfe({ type="list", value=listed_files, label="Postgresql File List" })
end

function mymodule.getfiledetails(filename)
	return modelfunctions.getfiledetails(filename, determinefilelist())
end

function mymodule.updatefiledetails(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, determinefilelist())
end

function mymodule.get_logfile(self, clientdata)
	local retval = cfe({ type="structure", value={}, label="Log File Configuration" })

	if string.find(getconfvalue("log_destination") or "", "syslog") then
		retval.value[#retval.value+1] = {facility=getconfvalue("syslog_facility") or "local0", grep=getconfvalue("syslog_ident") or "postgres"}
	end

	if getconfvalue("logging_collector") == "on" then
		local logdir = getconfvalue("log_directory") or "pg_log"
		if not string.find(logdir, "^/") then
			logdir = datadirectory.."/"..logdir
		end
		local logfile = getconfvalue("log_filename") or "postgresql-"
		-- drop the escapes
		logfile = string.match(logfile, "^[^%%]*")
		local files = fs.find_files_as_array(format.escapemagiccharacters(logfile)..".*", logdir)
		for i,f in ipairs(files) do
			retval.value[#retval.value+1] = {filename=f}
		end
	end

	if #retval.value == 0 then
		retval.value[#retval.value+1] = {facility="daemon", grep="postgres"}
	end

	retval.value[#retval.value+1] = {filename=datadirectory.."/postmaster.log"}

	return retval
end

for n,f in pairs(dbmodelfunctions) do
	mymodule[n] = function(...)
		return f(determineconnection, ...)
	end
end

return mymodule