Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New sysinfo json changes #405

Merged
merged 5 commits into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 109 additions & 3 deletions files/usr/lib/lua/aredn/info.lua
Expand Up @@ -37,10 +37,12 @@
require("uci")
local aredn_uci = require("aredn.uci")
require("aredn.utils")
local olsr=require("aredn.olsr")
-- require("aredn.http")
local lip=require("luci.ip")
require("nixio")
require("ubus")
require("iwinfo")

-------------------------------------
-- Public API is attached to table
Expand Down Expand Up @@ -171,10 +173,114 @@ function model.getBand(radio)
end

-------------------------------------
-- TODO: Return Frequency
-- Return Frequency
-------------------------------------
function model.getFrequency(radio)
return ""
function model.getFreq()
local wlanInf=get_ifname('wifi')
local freq=""
freq=os.capture("iwinfo " .. wlanInf .. " info | egrep 'Mode:'")
freq=freq:gsub("^%s*(.-)%s*$", "%1")
freq=string.match(freq, "%((.-)%)")
return freq
end

-------------------------------------
-- Return locally hosted services (for sysinfo.json)
-------------------------------------
function model.local_services()
local filelines={}
local lclsrvs={}
local lclsrvfile=io.open("/etc/config/services", "r")
if lclsrvfile~=nil then
for line in lclsrvfile:lines() do
table.insert(filelines, line)
end
lclsrvfile:close()
for pos,val in pairs(filelines) do
local service={}
local link,protocol,name = string.match(val,"^([^|]*)|(.+)|([^\t]*).*")
if link and protocol and name then
service['name']=name
service['protocol']=protocol
service['link']=link
table.insert(lclsrvs, service)
end
end
else
service['error']="Cannot read local services file"
table.insert(lclsrvs, service)
end
return lclsrvs
end

-------------------------------------
-- Return *All* Network Services
-------------------------------------
function model.all_services()
local services={}
local lines={}
local pos, val
local hfile=io.open("/var/run/services_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local service={}
local link,protocol,name = string.match(val,"^([^|]*)|(.+)|([^\t]*)\t#.*")
if link and protocol and name then
service['link']=link
service['protocol']=protocol
service['name']=name
table.insert(services,service)
end
end
else
service['error']="Cannot read services file"
table.insert(services,service)
end
return services
end

-------------------------------------
-- Return *All* Hosts
-------------------------------------
function model.all_hosts()
local hosts={}
local lines={}
local pos, val
local hfile=io.open("/var/run/hosts_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local host={}

-- local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")
local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")

if data then
--local ip, name=string.match(data,"^%s*([%x%.%:]+)%s+(%S.*)\t%s*$")
local ip, name=string.match(data,"^([%x%.%:]+)%s+(%S.*)\t%s*$")
if ip and name then
if not string.match(name,"^(dtdlink[.]).*") then
if not string.match(name,"^(mid[0-9][.]).*") then
host['name']=name
host['ip']=ip
table.insert(hosts,host)
end
end
end
end
end
else
host['error']="Cannot read hosts file"
table.insert(hosts,host)
end
return hosts
end

-------------------------------------
Expand Down
41 changes: 35 additions & 6 deletions files/usr/lib/lua/aredn/olsr.lua
Expand Up @@ -57,7 +57,8 @@ function model.getOLSRInterfaceType(iface)
return it
end

function model.getCurrentNeighbors()
function model.getCurrentNeighbors(RFinfo)
local RFinfo = RFinfo or false
local info={}
local links=model.getOLSRLinks()
for k,v in pairs(links) do
Expand All @@ -69,13 +70,41 @@ function model.getCurrentNeighbors()
info[remip]['linkType']= model.getOLSRInterfaceType(v['olsrInterface']) -- RF or DTD or TUN
info[remip]['linkQuality']=v['linkQuality']
info[remip]['neighborLinkQuality']=v['neighborLinkQuality']
host = string.gsub(remhost,"dtdlink%.", "")
host = string.gsub(host,"mid%d.", "")
info[remip]['hostname']=host
if remhost ~= nil then
host = string.gsub(remhost,"dtdlink%.", "")
end
if host ~= nil then
host = string.gsub(host,"mid%d.", "")
info[remip]['hostname']=host
else
info[remip]['hostname']=remip
end
-- services
-- info[remip]['services']={}
-- get TxMBPS
-- info[remip]['rate']="0"
if info[remip]['linkType'] == "RF" and RFinfo then
-- get additional info for RF link
require("aredn.utils")
require("iwinfo")
local wlan=get_ifname('wifi')
local RFneighbors=iwinfo['nl80211'].assoclist(wlan)
local mac2node=mac2host()
for i, mac_host in pairs(mac2node) do
local mac=string.match(mac_host, "^(.-)\-")
mac=mac:upper()
local node=string.match(mac_host, "\-(.*)")
if host == node or remip == node then
for stn in pairs(RFneighbors) do
stnInfo=iwinfo['nl80211'].assoclist(wlan)[mac]
if stnInfo ~= nil then
info[remip]["signal"]=tonumber(stnInfo.signal)
info[remip]["noise"]=tonumber(stnInfo.noise)
info[remip]["tx_rate"]=adjust_rate(stnInfo.tx_rate/1000,bandwidth)
info[remip]["rx_rate"]=adjust_rate(stnInfo.rx_rate/1000,bandwidth)
end
end
end
end
end
end
return info
end
Expand Down
22 changes: 17 additions & 5 deletions files/usr/lib/lua/aredn/utils.lua
Expand Up @@ -198,13 +198,12 @@ function nslookup(ip)
rip = o4.."."..o3.."."..o2.."."..o1
nso = capture("nslookup "..ip)
hostname = nso:match(rip.."%.in%-addr%.arpa[%s]+name[%s]+=[%s]+(.*)")
hostname=hostname:chomp()
hostname=hostname:chomp()
if hostname=="" then
hostname=nil
if hostname ~= nil then
hostname=hostname:chomp()
hostname=hostname:chomp()
return hostname
end
end
return hostname
end

function file_trim(filename, maxl)
Expand Down Expand Up @@ -285,6 +284,19 @@ function capture(cmd)
return(result)
end

-- Return list of MAC to Hostname files
function mac2host(dir)
dir = dir or "/tmp/snrlog"
local i, list, popen = 0, {}, io.popen
local pfile = popen("ls -A " .. dir)
for filename in pfile:lines() do
i = i + 1
list[i] = filename
end
pfile:close()
return list
end

--[[
LuCI - System library

Expand Down
101 changes: 18 additions & 83 deletions files/www/cgi-bin/sysinfo.json
Expand Up @@ -37,6 +37,7 @@
require("uci")
require("aredn.utils")
local aredn_info = require("aredn.info")
local aredn_olsr = require("aredn.olsr")
require("aredn.http")
require("nixio")
local ipc = require("luci.ip")
Expand All @@ -45,7 +46,6 @@ local json = require ("luci.jsonc")
-- Function extensions
os.capture = capture


-- ==== MAIN =====
ctx = uci.cursor()
if not ctx then
Expand All @@ -55,7 +55,7 @@ end
info={}

-- API version
info['api_version']="1.6"
info['api_version']="1.7"


-- NODE name
Expand Down Expand Up @@ -84,11 +84,13 @@ info['node_details']['firmware_version']=aredn_info.getFirmwareVersion()
-- Mesh RF info
info['meshrf']={}
local radio=aredn_info.getMeshRadioDevice()

if ( radio ~= nill and radio ~= "" ) then
info['meshrf']['status']="on"
info['meshrf']['ssid']=aredn_info.getSSID()
info['meshrf']['channel']=aredn_info.getChannel(radio)
info['meshrf']['chanbw']=aredn_info.getChannelBW(radio)
info['meshrf']['freq']=aredn_info.getFreq()
else
info['meshrf']['status']="off"
end
Expand Down Expand Up @@ -147,95 +149,28 @@ info['interfaces']=tif

-- HOSTS
if string.find(nixio.getenv("QUERY_STRING"):lower(),"hosts=1") then
local hosts={}
local lines={}
local pos, val
local hfile=io.open("/var/run/hosts_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local host={}

-- local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")
local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")

if data then
--local ip, name=string.match(data,"^%s*([%x%.%:]+)%s+(%S.*)\t%s*$")
local ip, name=string.match(data,"^([%x%.%:]+)%s+(%S.*)\t%s*$")
if ip and name then
if not string.match(name,"^(dtdlink[.]).*") then
if not string.match(name,"^(mid[0-9][.]).*") then
host['name']=name
host['ip']=ip
table.insert(hosts,host)
end
end
end
end
end
else
host['error']="Cannot read hosts file"
table.insert(hosts,host)
end
info['hosts']=hosts
info['hosts']=aredn_info.all_hosts()
end

-- SERVICES
if string.find(nixio.getenv("QUERY_STRING"):lower(),"services=1") then
local services={}
local lines={}
local pos, val
local hfile=io.open("/var/run/services_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local service={}
local link,protocol,name = string.match(val,"^([^|]*)|(.+)|([^\t]*)\t#.*")
if link and protocol and name then
service['link']=link
service['protocol']=protocol
service['name']=name
table.insert(services,service)
end
end
else
service['error']="Cannot read services file"
table.insert(services,service)
end
info['services']=services
info['services']=aredn_info.all_services()
end

-- LOCALLY HOSTED SERVICES ONLY
if string.find(nixio.getenv("QUERY_STRING"):lower(),"services_local=1") then
local filelines={}
local lclsrvs={}
local lclsrvfile=io.open("/etc/config/services", "r")
if lclsrvfile~=nil then
for line in lclsrvfile:lines() do
table.insert(filelines, line)
end
lclsrvfile:close()
for pos,val in pairs(filelines) do
local service={}
local link,protocol,name = string.match(val,"^([^|]*)|(.+)|([^\t]*).*")
if link and protocol and name then
service['name']=name
service['protocol']=protocol
service['link']=link
table.insert(lclsrvs, service)
end
end
else
service['error']="Cannot read local services file"
table.insert(lclsrvs, service)
end
info['services_local']=lclsrvs
info['services_local']=aredn_info.local_services()
end

-- LOCAL NEIGHBORS LINK INFO
if string.find(nixio.getenv("QUERY_STRING"):lower(),"link_info=1") then
info['link_info']=aredn_olsr.getCurrentNeighbors(true)
end

-- MESHMAP INFO
if string.find(nixio.getenv("QUERY_STRING"):lower(),"meshmap=1") then
info['services_local']=aredn_info.local_services()
info['link_info']=aredn_olsr.getCurrentNeighbors(true)
end

-- Output the HTTP header for JSON
Expand Down