module(..., package.seeall) require("fs") require("procps") require("getopts") require("format") 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 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 if (fs.is_file(path)) then filedetails.details = fs.stat(path) filedetails.content = fs.read_file(path) else filedetails.details = {path="File is missing", size="0",mtime=""} filedetails.content = "" end return filedetails end function getconfig() local errors = {} errors["SYSLOGD_OPTS"] = {} errors["KLOGD_OPTS"] = {} if (fs.is_file(configfile)) then config = getopts.getoptsfromfile(configfile) else 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 "" 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 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,value) --TODO: Validate so that user cant add values with '-' 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