module(..., package.seeall) -- Load libraries require("fs") require("procps") require("format") require("daemoncontrol") require("validator") require("processinfo") -- Set variables local configfile = "/etc/opennhrp/opennhrp.conf" local processname = "opennhrp" local packagename = "opennhrp" local baseurl = "/etc/opennhrp/" local descr = { Type = { ['incomplete']="The protocol address is being resolved", ['negative']="This protocol address is not available", ['cached']="Protocol address was resolved successfully", ['route']="This is a dynamic shortcut", ['dynamic']="This entry is from a node that connected to us", ['local']="Local interface address", ['static']="Static mapping from configuration file (e.g. address of core)", }, Flags= { -- ['up']="Connection is fully usable", ['lower-up']="ipsec connections is up, but registration is not yet done", }, } -- ################################################################################ -- LOCAL FUNCTIONS local function opennhrpctl_show() local cmd_output_result={} local cmd_output_result_table={} local cmd_output_error, opennhrpstatus local cmd = "/usr/sbin/opennhrpctl show 2>/dev/null" local f = io.popen( cmd ) for line in f:lines() do if string.find(line, "^Status:") then opennhrpstatus=line else table.insert(cmd_output_result, line) end end f:close() local cnt = 0 for k,v in pairs(cmd_output_result) do if string.find(v,"^Interface") then cnt = cnt + 1 cmd_output_result_table[cnt] = {} end if ( cnt > 0 ) and (v ~= "") then local k = string.match(v,"^(.-):%s?.*") cmd_output_result_table[cnt][k]=cfe({value=string.match(v,"^.-:%s?(.*)")}) local statusdescription = string.lower(string.match(v,"^.-:%s?(.*)")) if (type(descr[k]) == "table") then cmd_output_result_table[cnt][k]['descr']=descr[k][statusdescription] end end end local peers_list = {} for k,v in pairs(cmd_output_result_table) do if (v.Interface.value) and not (peers_list[v.Interface.value]) then peers_list[v.Interface.value] = {} end table.insert(peers_list[v.Interface.value], v) end return peers_list,opennhrpstatus,cmd_output_error end function process_status_text(procname) local t = procps.pidof(procname) if (t) and (#t > 0) then return "Enabled" else return "Disabled" end end -- ################################################################################ -- PUBLIC FUNCTIONS function startstop_service ( self, action ) local cmd = action.value local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd) action.descr=cmdmessage action.errtxt=cmderror return cmdresult,action end function getstatus() local status = {} local value, errtxt = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", value=value, errtxt=errtxt, }) status.status = cfe({ name="status", label="Program status", value=process_status_text(processname), }) local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname) status.autostart = cfe({ name="autostart", label="Autostart sequence", value=autostart_sequense, errtxt=autostart_errtxt, }) local opennhrpctl_show, opennhrpctl_status = opennhrpctl_show() status.stats = cfe({ name="stats", label="Programstatus reports", value=opennhrpctl_status, }) status.show = cfe({ name="show", label="Peers", option=opennhrpctl_show, }) return status end function getconfig() local config = {} return config end function get_filedetails(self,num) local path if (num == "2") then path = configfile2 else path = configfile end local file = {} local filedetails = {} local config = {} local filenameerrtxt if (path) and (fs.is_file(path)) then filedetails = fs.stat(path) config = getconfig(path) else config = {} config.filename = {} config["filename"]["errtxt"]="Config file '".. path .. "' is missing!" end file["filename" .. (num or "")] = cfe({ name="filename" .. (num or ""), label="File name", value=path, errtxt=filenameerrtxt }) file["filesize" .. (num or "")] = cfe({ name="filesize" .. (num or ""), label="File size", value=filedetails.size or 0, }) file["mtime" .. (num or "")] = cfe({ name="mtime" .. (num or ""), label="File date", value=filedetails.mtime or "---", }) file["filecontent" .. (num or "")] = cfe({ type="longtext", name="filecontent" .. (num or ""), label="File content", value=fs.read_file(path), }) -- Sum all errors into one cfe local sumerrors = "" for k,v in pairs(config) do if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then sumerrors = sumerrors .. config[k]["errtxt"] .. "\n" end end if (sumerrors ~= "") then file["sumerrors" .. (num or "")] = cfe ({ name="sumerrors" .. (num or ""), label = "Configuration errors", errtxt = string.match(sumerrors, "(.-)\n$"), }) end return file end function update_filecontent (self, modifications) local path = configfile local file_result,err = fs.write_file(path, format.dostounix(modifications)) return file_result end function get_logfile () local file = {} local cmdtxt = "grep " .. processname .. " /var/log/messages" local cmd, error = io.popen(cmdtxt ,r) local cmdoutput = cmd:read("*a") cmd:close() file["filename"] = cfe({ name="filename", label="File name", value=cmdtxt, }) file["filecontent"] = cfe({ type="longtext", name="filecontent", label="File content", value=cmdoutput, }) return file end