From 272e1f2444e68ed59533719f433b9435aa7f48a7 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Thu, 31 Jan 2008 16:23:48 +0000 Subject: Changing things so we use cfe for sending/receiving things from model and controller. The view-file is using loops instead to reduce code. git-svn-id: svn://svn.alpinelinux.org/acf/alpine-baselayout/trunk@668 ab2d0c66-481e-0410-8bed-d214d4d58bed --- syslog-config-html.lsp | 222 +++++++++++++++++++++++++------------------------ syslog-controller.lua | 87 ++++++++++++++++--- syslog-expert-html.lsp | 9 +- syslog-model.lua | 137 ++++++++++++++++++++---------- syslog-status-html.lsp | 67 +++++++++------ 5 files changed, 329 insertions(+), 193 deletions(-) diff --git a/syslog-config-html.lsp b/syslog-config-html.lsp index ddb73c4..8284dc7 100644 --- a/syslog-config-html.lsp +++ b/syslog-config-html.lsp @@ -1,128 +1,132 @@ - - -

SYSTEM INFO

- -
-
-

- -
-

-
- -

CONFIGURATION

-
+ +DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(form)) +io.write("
") +--]] +?> -

Advanced config

-

General settings

+

SYSTEM INFO

- -
>Log to given file
-
"/> - ", view.errors["SYSLOGD_OPTS"]["-O"] ,"

") end ?> -
- - -
>Set local log level
-
- - > - - -

(1=Quiet, ... , =Debug)

- ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
- - -
>Smaller logging output
-
/> - ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
+ 0) then io.write(" class='error'") end + io.write(">" .. val.label .. "\n") + + io.write("\t\t
" .. val.value .. "\n") + if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. val.descr .. "

\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. val.errtxt .. "

\n") end + io.write("\t\t
\n") + +end ?>
-

Save logs for a longer period

- -
- -
>Max size (KB) before rotate
-
"/> -

(default=200KB, 0=off)

- ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
- - -
>Number of rotated logs to keep
-
"/> -

(default=1, max=99, 0=purge)

- ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
-
+") + for k,v in pairs(tags) do + if (myform[v]) then + local val = myform[v] + io.write("\t 0) then io.write(" class='error'") end + io.write(">" .. val.label .. "\n") + + io.write("\t\t
" .. html.form[val.type](val) .. "\n") + if (val.descr) and (#val.descr > 0) then io.write("\t\t
" .. val.descr .. "
\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. val.errtxt .. "

\n") end + io.write("\t\t
\n") + end + end + io.write("") +end +?> -

Remote logging

-
+

CONFIGURATION

+ +" method="POST"> + + +

GUIDED CONFIGURATION

+

REMOTE LOGGING

+ + - -
>Log locally and via network
-
/> -

(default is network only if you have defined remote-logging)

- ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
- - -
>Log to IP or hostname on PORT
-
"/> -

(Empty=No remote logging) HOST[:PORT] (default PORT=514/UDP)

- ",view.errors["SYSLOGD_OPTS"][var] ,"

") - end ?> -
+

ADVANCED CONFIGURATION

+

GENERAL

+ +

LOGROTATE

+ -
+

REMOTE LOGGING

+ -

Save and apply above settings

-
-
Apply settings
-
- -
Errors in previous action
-
- -
+

SAVE AND APPLY ABOVE SETTINGS

+ -

MANAGEMENT

-
-
Program controll-panel
-
- - - -
+ +
+

MANAGEMENT

+
+
+
+ + + + + +
- -
Previous action result
-
- -
+ + + + + + + +
Previous action result
+
+ + +
+ + DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(form)) +io.write("
") --]] ?> + + diff --git a/syslog-controller.lua b/syslog-controller.lua index b183a89..900856c 100644 --- a/syslog-controller.lua +++ b/syslog-controller.lua @@ -13,28 +13,88 @@ mvc.on_load = function(self, parent) end end +local function getstatus(self) + local status = self.model.getstatus() + if (#status.status.value > 0) then + status.status.value = "Enabled" + else + status.status.value = "Disabled" + end + return status +end + function status(self) - return { status=self.model.getstatus() } + return { status=getstatus(self) } end function config(self) - local cmdresult = {} + local cmd_save_result = {} local cmderrors = {} if ( self.clientdata.cmddaemon) then startstop = self.model:startstop_service( self.clientdata.cmddaemon ) end - if ( self.clientdata.cmdsave) then - local variables="-O -l -S -s -b -L -R" -- Advanced-config options - --local variables="-R" -- Guided-config options + if ( self.clientdata.cmd_save) then + local variables="logfile loglevel smallerlogs maxsize numrotate localandnetworklog remotelogging" -- Advanced-config options + --local variables="remotelogging" -- Guided-config options for var in string.gmatch(variables, "%S+") do - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset + -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdresult[var],cmderrors[var] = self.model:setconfigs("SYSLOGD_OPTS",var,self.clientdata[var]) + cmd_save_result[var] = self.model:setconfigs("SYSLOGD_OPTS",var,self.clientdata[var]) end end - local status = self.model.getstatus() + local status = getstatus(self) +--[[ + if (#status.status.value > 0) then + status.status.value = "Enabled" + else + status.status.value = "Disabled" + end +--]] + local config, errors = self.model.getconfig() + -- Add a cmd button to the view + config.cmd_save = cfe({ name="cmd_save", + label="Save/Apply above settings", + value="Save", + type="submit", + }) + -- Add a management buttons + management = {} + management.start = cfe({ name="start", + label="Program control-panel", + value="Start", + type="submit", + }) + management.stop = cfe({ name="stop", + label="Program control-panel", + value="Stop", + type="submit", + }) + management.restart = cfe({ name="restart", + label="Program control-panel", + value="Restart", + type="submit", + }) + -- Disable management buttons based on if the process is running or not + if (string.lower(status.status.value) == "enabled" ) then + management.start.disabled = "yes" + management.stop.disabled = nil + else + management.start.disabled = nil + management.stop.disabled = "yes" + end + + -- Disable management buttons if there exist errors in the config + for k,v in pairs(config) do + if (config[k]["errtxt"] ~= "") then + management.start.disabled = "yes" + management.stop.disabled = "yes" + management.restart.disabled = "yes" + break + end + end + local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller local cmderrors_str = "" @@ -50,11 +110,16 @@ function config(self) if (cmderrors_str == "") then cmderrors_str = nil end return { + option={ script=ENV["SCRIPT_NAME"], + prefix=self.conf.prefix, + controller = self.conf.controller, + action = "config", }, status = status, startstop = startstop, clientdata = self.clientdata, - cmdresult = cmdresult, + management = management, config = config, + cmd_save_result = cmd_save_result, errors = errors, cmderrors = cmderrors_str, url = url, } @@ -70,7 +135,9 @@ function expert(self) startstop = self.model:startstop_service( self.clientdata.cmddaemon ) end - local status = self.model.getstatus() +-- local status = self.model.getstatus() + local status = getstatus(self) + local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller return { file = self.model:get_filedetails(), diff --git a/syslog-expert-html.lsp b/syslog-expert-html.lsp index 4378e9d..527ce22 100644 --- a/syslog-expert-html.lsp +++ b/syslog-expert-html.lsp @@ -3,10 +3,10 @@

SYSTEM INFO

-
+

-
+

@@ -55,7 +55,8 @@ DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(view)) +io.write("
") --]] ?> diff --git a/syslog-model.lua b/syslog-model.lua index 2013b76..0045b5b 100644 --- a/syslog-model.lua +++ b/syslog-model.lua @@ -25,13 +25,6 @@ local function getloglevels() end return loglevels end -local function isrunning(process) - if (procps.pidof("syslogd")) then - return "Enabled" - else - return "Disabled" - end -end -- ################################################################################ -- PUBLIC FUNCTIONS @@ -42,23 +35,26 @@ end function getstatus() local opts = getconfig() local status = {} - status.version = cfe({ name="Program version", + status.version = cfe({ name = "version", + label="Program version", value=get_version(), }) - status.status = cfe({ name="Program status", - value=isrunning("syslogd"), + status.status = cfe({ name="status", + label="Program status", + value=procps.pidof("syslogd"), }) - if (opts["SYSLOGD_OPTS"]) and not ((opts["SYSLOGD_OPTS"]["-R"] ~= "") and not (opts["SYSLOGD_OPTS"]["-L"])) then - status.logfile = cfe({ name="Locally logging to", - value=opts["SYSLOGD_OPTS"]["-O"], + if (opts["remotelogging"]) and not ((opts["remotelogging"]["value"] ~= "") and not (opts["localandnetworklog"]["value"])) then + status.logfile = cfe({ name="logfile", + label="Locally logging to", + value=opts["logfile"]["value"], }) end if (opts["SYSLOGD_OPTS"]) and (opts["SYSLOGD_OPTS"]["-R"]) and (opts["SYSLOGD_OPTS"]["-R"] ~= "") then - status.remote = cfe({ name="Remote logging to", + status.remote = cfe({ name="remotelogging", + label="Remote logging to", value=opts["SYSLOGD_OPTS"]["-R"], }) end ---]] return status end @@ -75,38 +71,71 @@ function get_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 + configcontent = 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"] = {} + config["configfile"] = "Config file '".. configfile .. "' is missing!" 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 selects which configurations we should show to the user + config["logfile"] = cfe({ + name="logfile", + label = "Log to given file", + value = configcontent["SYSLOGD_OPTS"]["-O"] or "/var/log/messages", + }) + config["loglevel"] = cfe({ + name="loglevel", + label = "Set local log level", + value = tonumber(configcontent["SYSLOGD_OPTS"]["-l"]) or 8, + type = "select", + option = getloglevels(), + descr = "1=Quiet, ... , " .. table.maxn(getloglevels()) .. "=Debug", + }) + config["smallerlogs"] = cfe({ + name="smallerlogs", + label = "Smaller logging output", + checked = configcontent["SYSLOGD_OPTS"]["-S"], + value = "smallerlogs", + type = "checkbox", + }) + config["maxsize"] = cfe ({ + name="maxsize", + label = "Max size (KB) before rotate", + descr = "Default=200KB, 0=off", + value = configcontent["SYSLOGD_OPTS"]["-s"], + }) + config["numrotate"] = cfe ({ name="numrotate", + label = "Number of rotated logs to keep", + descr = "Default=1, max=99, 0=purge", + value = configcontent["SYSLOGD_OPTS"]["-b"], + }) + config["localandnetworklog"] = cfe ({ + name="localandnetworklog", + label = "Log locally and via network", + checked = configcontent["SYSLOGD_OPTS"]["-L"], + value = "localandnetworklog", + type = "checkbox", + descr = "Default is network only if -R", + }) + config["remotelogging"] = cfe ({ + name="remotelogging", + label = "Log to IP or hostname on PORT", + descr = "host[:PORT] - Default PORT=514/UDP", + value = configcontent["SYSLOGD_OPTS"]["-R"], + }) + -- 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 + if (configcontent["SYSLOGD_OPTS"]["-l"]) and + ((tonumber(configcontent["SYSLOGD_OPTS"]["-l"]) == nil) or (tonumber(configcontent["SYSLOGD_OPTS"]["-l"]) > 8)) then + config["loglevel"]["errtxt"] = "Log value is out of range. Please select one of the above." 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." + + if (configcontent["SYSLOGD_OPTS"]["-L"] ~= nil) and (configcontent["SYSLOGD_OPTS"]["-R"] == "") then + config["localandnetworklog"]["errtxt"] = "Logging to local and network is possible unless you define a host for remote logging." end - return config, errors + return config end service_control = function ( self, srvcmd ) @@ -130,17 +159,33 @@ service_control = function ( self, srvcmd ) end function setconfigs(self,variable,parameter,value) - if (errors == nil) then - errors = {} - end + local variabletranslator = ({ + logfile = "-O", + loglevel = "-l", + smallerlogs = "-S", + maxsize = "-s", + numrotate = "-b", + localandnetworklog = "-L", + remotelogging = "-R", + }) + parameter = variabletranslator[parameter] + --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 if (value) and (parameter == "-O") then - value, errors["-O"] = validator.is_valid_filename(value, "/var/log" ) + local value, errors = validator.is_valid_filename(value, "/var/log" ) + if (errors) then + return cfe({ + name="setconfig", + errtxt= errors, + }) + end end + +--[[ if (value) and (parameter == "-R") then local port = string.match(value, ":(.-)$") local host = string.match(value, "^(.-):?") @@ -149,12 +194,14 @@ function setconfigs(self,variable,parameter,value) end errors["-R"] = "Port:'" .. tostring(port) .. "' Host:" .. tostring(host) end - +--]] -- Set specific variables (and their values) if (value) and ((parameter == "-S") or (parameter == "-L")) then value = "" end local retval, errorscmd = getopts.setoptsinfile(configfile,variable,parameter,value) - errors["commit"] = errorscmd - return retval, errors +-- if (errorscmd) and (#errorscmd > 0) then + local errorscmd = cfe({ name="variable", errtxt=errorscmd, }) +-- end + return errorscmd end function update_filecontent (self, modifications) diff --git a/syslog-status-html.lsp b/syslog-status-html.lsp index c5b1c0d..d6f5ced 100644 --- a/syslog-status-html.lsp +++ b/syslog-status-html.lsp @@ -1,35 +1,52 @@ - - -

SYSTEM INFO

+ +DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(form)) +io.write("
") +--]] +?> +

SYSTEM INFO

-
-

+ 0) then io.write(" class='error'") end + io.write(">" .. val.label .. "\n") + + io.write("\t\t
" .. val.value .. "\n") + if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. val.descr .. "

\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. val.errtxt .. "

\n") end + io.write("\t\t
\n") -
-

+end ?>

PROGRAM SPECIFIC OPTIONS/INFORMATION

- -
-

- - - -
-

- + 0) then io.write(" class='error'") end + io.write(">" .. val.label .. "\n") + + io.write("\t\t
" .. val.value .. "\n") + if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. val.descr .. "

\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. val.errtxt .. "

\n") end + io.write("\t\t
\n") + end +end ?>
-

DEBUGGING

-

CFE debugging

- - + -- cgit v1.2.3