From 4d42ae72cb319eaff33374f61fec515ee374bdbb Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 18 Sep 2008 19:32:39 +0000 Subject: Modified opennhrp to parse show result and display nicely. git-svn-id: svn://svn.alpinelinux.org/acf/opennhrp/trunk@1476 ab2d0c66-481e-0410-8bed-d214d4d58bed --- opennhrp-model.lua | 62 +++++++++++++++++--------------------------------- opennhrp-show-html.lsp | 44 ++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/opennhrp-model.lua b/opennhrp-model.lua index 7c31bbc..42497c9 100644 --- a/opennhrp-model.lua +++ b/opennhrp-model.lua @@ -9,7 +9,6 @@ local configfile = "/etc/opennhrp/opennhrp.conf" local processname = "opennhrp" local packagename = "opennhrp" ---[[ local descr = { Type = { ['incomplete']="The protocol address is being resolved", @@ -30,46 +29,34 @@ local descr = { -- LOCAL FUNCTIONS local function opennhrpctl_show() - local cmd_output_result={} - local cmd_output_result_table={} - local cmd_output_error, opennhrpstatus + local peers_list = cfe({ type="structure", value={}, label="Peers" }) + local opennhrpstatus = cfe({ value="Show report not available", label="Status" }) 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 + local content = f:read("*a") 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] + local current + for line in string.gmatch(content, "([^\n]*)\n?") do + if string.find(line, "^Status:") then + opennhrpstatus.value = string.match(line, ":%s*(%w+)") + elseif string.find(line,"^Interface:") then + local intf = string.match(line, ":%s*(%w+)") + current = {} + peers_list.value[intf] = peers_list.value[intf] or {} + table.insert(peers_list.value[intf], current) + elseif ( current ) and (line ~= "") then + local name,val = string.match(line,"^(.-):%s?(.*)") + if name and val then + current[name] = cfe({value=val, label=name}) + if (type(descr[name]) == "table") then + current[name].descr = descr[name][val] + end 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 + return cfe({ type="group", value={peers_list=peers_list, status=opennhrpstatus}, label="OpenNHRP Show Report" }) end ---]] local function parseconfigfile(self, configfile) if fs.is_file(configfile) then @@ -193,14 +180,7 @@ function getstatus() end function getshowreport() - local show = cfe({ type="longtext", label="OpenNHRP show report" }) - local cmd = "/usr/sbin/opennhrpctl show 2>/dev/null" - local f = io.popen( cmd ) - show.value = f:read("*a") - if not show.value or show.value == "" then - show.value = "No report available" - end - return show + return opennhrpctl_show() end function getconfig(self) diff --git a/opennhrp-show-html.lsp b/opennhrp-show-html.lsp index fce8a79..4074cf8 100644 --- a/opennhrp-show-html.lsp +++ b/opennhrp-show-html.lsp @@ -8,5 +8,47 @@ end %>

<%= data.label %>

-<% displayitem(data) %> +<% +displayitem(data.value.status) +if #data.value.peers_list then +%> +
<%= data.value.peers_list.label %>
+
+<% for intf,addresses in pairs(data.value.peers_list.value) do %> + + +<% for i,entries in ipairs(addresses) do + io.write("\n") + + for j,entry in pairs(entries) do + if j ~= "Protocol-Address" then + io.write("\n") + end + end + end + io.write("\t\t\t
<%= intf %>
" .. entries["Protocol-Address"].value .. "
"..entry.label..""..entry.value) + if entry.descr then + io.write(" (" .. entry.descr .. ")") + end + io.write("
") + end +%> +
+<% end %>
+ -- cgit v1.2.3