module(..., package.seeall) require("fs") require("procps") require("getopts") require("format") require("daemoncontrol") require("validator") local configfile = "/etc/conf.d/syslog" local config = {} 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 function getloglevels() local loglevels = {} for i=1,8 do table.insert(loglevels,i) end return loglevels end -- ################################################################################ -- PUBLIC FUNCTIONS -- action should be a CFE function startstop_service ( self, action ) local cmd = action.value local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol("syslog", cmd) action.descr=cmdmessage action.errtxt=cmderror -- Reporting back (true|false, the original acition) return cmdresult,action end function getstatus() local opts = getconfig() local status = {} status.version = cfe({ name = "version", label="Program version", value=get_version(), }) status.status = cfe({ name="status", label="Program status", value=procps.pidof("syslogd"), }) 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="remotelogging", label="Remote logging to", value=opts["SYSLOGD_OPTS"]["-R"], }) end return status end function get_filedetails() local path = configfile local filedetails = fs.stat(path) local config = getconfig(path) local file = {} file["filename"] = cfe({ name="filename", label="File name", value=path, }) file["filesize"] = cfe({ name="filesize", label="File size", value=filedetails.size or 0, }) file["mtime"] = cfe({ name="mtime", label="File date", value=filedetails.mtime or "---", }) file["filecontent"] = cfe({ type="longtext", name="filecontent", label="File content", value=fs.read_file(path), }) file["sumerrors"] = config.sumerrors return file end function getconfig() local config = {} if (fs.is_file(configfile)) then configcontent = getopts.getoptsfromfile(configfile) or config if (type(config["SYSLOGD_OPTS"]) == "string") then config["SYSLOGD_OPTS"] = {} end else config["configfile"] = "Config file '".. configfile .. "' is missing!" end -- 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 (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!\nCurrent value in config is '" .. configcontent["SYSLOGD_OPTS"]["-l"] .. "' - This is invalid!\nPlease select one of the above and save your changes." table.insert(config["loglevel"]["option"], tonumber(configcontent["SYSLOGD_OPTS"]["-l"])) end if (configcontent["SYSLOGD_OPTS"]["-L"] ~= nil) and ((configcontent["SYSLOGD_OPTS"]["-R"] == nil) or (configcontent["SYSLOGD_OPTS"]["-R"] == "")) then config["localandnetworklog"]["errtxt"] = "Logging to local and network (-L) is not possible unless you define a host (-R) for remote logging or remove this option." end -- Sum all errors into one cfe local sumerrors = "" for k,v in pairs(config) do if (config[k]["errtxt"] ~= "") then sumerrors = sumerrors .. config[k]["errtxt"] .. "\n" end end if (sumerrors ~= "") then config["sumerrors"] = cfe ({ name="sumerrors", label = "Configuration errors", errtxt = sumerrors, }) end return config end -- IMPORTANT! This function is a exception! It's not fed with CFE's -- Parameter should be one of the ones defined in the variable 'variabletranslator'. -- value should be whatever the new value should be. function setconfigs(self,parameter,value) -- Set variables local variable = "SYSLOGD_OPTS" local variabletranslator = ({ logfile = "-O", loglevel = "-l", smallerlogs = "-S", maxsize = "-s", numrotate = "-b", localandnetworklog = "-L", remotelogging = "-R", }) cmdparameter = variabletranslator[parameter] -- Report a error if someone tryes to use a invalid parameter if not (cmdparameter) then local availablevariables = "" for k,v in pairs(variabletranslator) do availablevariables = k .. ", " .. availablevariables end parameter = parameter or "" return false, cfe({ name="syslog.model.setconfigs()", errtxt="'" .. parameter .. "' is not a valid parameter!\nValid options are: " .. availablevariables, }) end --TODO: Validate so that user cant add values with '-' (could cause major breakage next time you do getopts) -- This config-file only accepts one type of parameters (report error if someone uses wrong parameter) if not (string.find(cmdparameter, "-%a$")) then return false, cfe({ name="syslog.model.setconfigs()", errtxt="Parameter must be formated '-a' (where a is one upper/lowercase letter [a-z])", }) end -- Validate userinput (if valid path/filename) if (value) and (cmdparameter == "-O") then local cmdresult, cmdmessage = validator.is_valid_filename(value, "/var/log" ) if not (cmdresult) then return false, cfe({ name="syslog.model.setconfigs()", errtxt=cmdmessage, }) end end -- Validate userinput (Has the user entered a valid hostname and/or port) if (value) and (cmdparameter == "-R") then local hostport = format.string_to_table(value, ":") local host = hostport[1] local port = hostport[2] if (port) and not (validator.is_port(port)) then return false, cfe({ name="syslog.model.setconfigs.getopts.setoptsinfile()", errtxt="You entered '" .. tostring(port) .. "' as port - This is not valid!", }) end end -- Set/Unset checkbox variables if (value) and ((cmdparameter == "-S") or (cmdparameter == "-L")) then value = "" end local cmdresult, cmdmessage, cmderror = getopts.setoptsinfile(configfile,variable,cmdparameter,value) if (cmderror) then return false, cfe({ name="syslog.model.setconfigs.getopts.setoptsinfile()", errtxt=cmderror, }) end return true, cfe({ name="syslog.model.setconfigs()", value=cmdmessage, }) end -- modifications should be a CFE function update_filecontent (self, modifications) local path = configfile local file_result,err = fs.write_file(path, format.dostounix(modifications)) return file_result, err end