module(..., package.seeall) require("fs") require("procps") require("getopts") require("format") require("daemoncontrol") 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 function startstop_service ( self, state ) return daemoncontrol.daemoncontrol("syslog", state) end function getstatus() local opts = getconfig() local status = {} status.version = get_version() status.enabled = procps.pidof("syslogd") if (opts["SYSLOGD_OPTS"]) and not ((opts["SYSLOGD_OPTS"]["-R"] ~= "") and not (opts["SYSLOGD_OPTS"]["-L"])) then status.logfile = opts["SYSLOGD_OPTS"]["-O"] end if (opts["SYSLOGD_OPTS"]) and (opts["SYSLOGD_OPTS"]["-R"]) and (opts["SYSLOGD_OPTS"]["-R"] ~= "") then status.remote = opts["SYSLOGD_OPTS"]["-R"] end return status end function get_filedetails() local filedetails = {} local path = configfile filedetails.details = {path=path, size="0",mtime=""} filedetails.content = "" if (fs.is_file(path)) then filedetails.details = fs.stat(path) filedetails.content = fs.read_file(path) end return 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 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"] = {} 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 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 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." end return config, errors end service_control = function ( self, srvcmd ) local srvcmd = string.lower(srvcmd) local retval = "" local line = "" if (srvcmd == "start") or (srvcmd == "stop") or (srvcmd == "restart") then local file = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/syslog " .. srvcmd .. " 2>&1" ) if file ~= nil then line = file:read( "*l" ) while line ~= nil do retval = retval .. "\n" .. line line = file:read( "*l" ) end file:close() end else retval = "Unknown command!" end return retval end function setconfigs(self,variable,parameter,value) --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 -- Set specific variables (and their values if (value) and ((parameter == "-S") or (parameter == "-L")) then value = "" end local retval, errors = getopts.setoptsinfile(configfile,variable,parameter,value) return retval, errors end function update_filecontent (self, modifications) local path = configfile local file_result,err = fs.write_file(path, format.dostounix(modifications)) return file_result, err end -- ################################################################################ -- OTHER FUNCTIONS