#!/usr/bin/lua --[[ Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks Copyright (C) 2018 Darryl Quinn See Contributors file for additional contributors This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Additional Terms: Additional use restrictions exist on the AREDN(TM) trademark and logo. See AREDNLicense.txt for more info. Attributions to the AREDN Project must be retained in the source code. If importing this code into a new or existing project attribution to the AREDN project must be added to the source code. You must not misrepresent the origin of the material contained within. Modified versions must be modified to attribute to the original source and be marked in reasonable ways as differentiate it from the original version --]] require("uci") require("aredn.uci") require("aredn.utils") require("aredn.http") local aredn_olsr = require("aredn.olsr") local aredn_info = require("aredn.info") require("nixio") local json = require("luci.jsonc") require("iwinfo") -- Function extensions os.capture = capture function getSysinfo() local info={} info['node']=aredn_info.getNodeName() info['description']=aredn_info.getNodeDescription() info['firmware_version']=aredn_info.getFirmwareVersion() info['model']=aredn_info.getModel() -- info['date']=aredn_info.getDate() info['time']=aredn_info.getTime() -- info['uptime']=aredn_info.getUptime() info['loads']=aredn_info.getLoads() return info end function getStatusMeshRF() local info={} info['ssid']=aredn_info.getSSID() info['device']=aredn_info.getMeshRadioDevice() info['channel']=aredn_info.getChannel(info['device']) info['chanbw']=aredn_info.getChannelBW(info['device']) info['band']=aredn_info.getBand(info['device']) info['frequency']=aredn_info.getFrequency(info['device']) return info end function getStatusIp() local info={} info['gateway']=aredn_info.getDefaultGW() info['wifi']=aredn_info.getInterfaceIPAddress('wifi') info['lan']=aredn_info.getInterfaceIPAddress('lan') info['wan']=aredn_info.getInterfaceIPAddress('wan') return info end function getLocationInfo() local info={} local lat, lon= aredn_info.getLatLon() local gs= aredn_info.getGridSquare() info['lat']=lat info['lon']=lon info['gridsquare']=gs return info end function getFreeMemory() local info = aredn_info.getFreeMemory() return info end function getSignal(realtime) local defnoise=-95 local ol={} local dpa={} -- datapoint table/array local dirname="/tmp/snrlog" local values={} local counter=0 local datepattern="(%d+)/(%d+)/(%d+)%s(%d+):(%d+):(%d+)" local parms={} parms=parsecgi(nixio.getenv("QUERY_STRING") or "") -- make sure snrlog dir is created if dir_exists(dirname) then nixio.fs.mkdir(dirname) end -- get wifi ifname local wifiiface=get_ifname("wifi") -- get bandwidth local radio=aredn_info.getMeshRadioDevice() local bandwidth=aredn_info.getChannelBW(radio) if realtime then -- REALTIME local d=os.date("%H:%M:%S") local n -- noise local m -- margin local s -- signal local dp={} -- datapoint local yinfo={} if (parms.device=="strongest" or parms.device=="" or (not parms.device)) then -- REALTIME/STRONGEST SIGNAL -- get radio noise floor n=iwinfo["nl80211"].noise(wifiiface) if (n < -101 or n > -50) then n=defnoise end -- get strongest signal s=iwinfo["nl80211"].signal(wifiiface) m=tonumber(s)-tonumber(n) if s==0 then s="null" end tx_rate="N/A" rx_rate="N/A" tx_mcs="N/A" rx_mcs="N/A" dp.tx_rate=tx_rate dp.rx_rate=rx_rate dp.tx_mcs=tx_mcs dp.rx_mcs=rx_mcs else -- REALTIME/SPECIFIC SIGNAL -- split out device to mac-host local mac,host=string.match(parms.device,"([^-]*)-(.*)") local macinfo=iwinfo["nl80211"].assoclist(wifiiface)[mac:upper()] n=macinfo.noise s=macinfo.signal n=tonumber(n) s=tonumber(s) m=s-n tx_rate=macinfo.tx_rate/1000 tx_rate=adjust_rate(tx_rate,bandwidth) rx_rate=macinfo.rx_rate/1000 rx_rate=adjust_rate(rx_rate,bandwidth) tx_mcs=macinfo.tx_mcs rx_mcs=macinfo.rx_mcs m=tonumber(s)-tonumber(n) end if s==0 then s="null" end table.insert(yinfo,s) table.insert(yinfo,n) dp.label=d dp.y=yinfo dp.m=m dp.tx_rate=tx_rate dp.rx_rate=rx_rate dp.tx_mcs=tx_mcs dp.rx_mcs=rx_mcs table.insert(dpa,dp) table.insert(ol,dpa) else -- ARCHIVE local filename if not parms.device or parms.device=="" then -- get the first file from dirlist for maclist in nixio.fs.glob(dirname.."/".."*") do filename=maclist parms.device=maclist break end else filename=dirname.."/"..parms.device end if filename then for line in io.lines(filename) do local yinfo={} local dp={} local dt,s,n,tm,tr,rm,rr=line:match("(.*)%,(.*)%,(.*),(.*),(.*),(.*),(.*)") local dtm,dtd,dty,dth,dtmin,dts=dt:match(datepattern) local dtseconds=os.time({month=dtm,day=dtd,year=dty,hour=dth,min=dtmin,sec=dts}) dtseconds=dtseconds*1000 dtseconds=math.floor(dtseconds) n=tonumber(n) if s~="null" then s=tonumber(s) m=s-n else m="N/A" end table.insert(yinfo,s) table.insert(yinfo,n) dp.label=dt dp.x=dtseconds dp.y=yinfo dp.m=m dp.tx_rate=tr dp.rx_rate=rr dp.tx_mcs=tm dp.rx_mcs=rm table.insert(dpa,dp) end table.insert(ol,dpa) end end return ol end function getScanList() local device = aredn_info.getMeshRadioDevice() local scanlist = iwinfo["nl80211"].scanlist(device) return scanlist end function getFreqList() local device = aredn_info.getMeshRadioDevice() local freqlist = iwinfo["nl80211"].freqlist(device) return freqlist end -- ==== MAIN ===== ctx = uci.cursor() if not ctx then error("Failed to get uci cursor") end info={} -- get/process query string local qsset={} if (arg[1]==nil and arg[2]==nil) then qs=nixio.getenv("QUERY_STRING") if qs~="" then qsset=parseQueryString(qs) else -- maybe default to a help page qsset["api"]="help" end else qsset[arg[1]]=arg[2] end info['pages']={} for page, comps in pairs(qsset) do -- ---------------- /mesh page if not setContains(info['pages'],page) then info['pages'][page]={} end if page=="api" then info['pages'][page]=nil info['api_help']="AREDN API. This API's primary function is to drive the Web UI." elseif page=="status" then for i,comp in pairs(comps:split(',')) do if comp=="meshrf" then info['pages'][page][comp]=getStatusMeshRF() elseif comp=="ip" then info['pages'][page][comp]=getStatusIp() elseif comp=="sysinfo" then info['pages'][page][comp]=getSysinfo() elseif comp=="memory" then info['pages'][page][comp]=getFreeMemory() elseif comp=="storage" then info['pages'][page][comp]=aredn_info.getFSFree() elseif comp=="olsr" then info['pages'][page][comp]=aredn_info.getOLSRInfo() elseif comp=="location" then info['pages'][page][comp]=getLocationInfo() elseif comp=="freqlist" then info['pages'][page][comp]=getFreqList() end end elseif page=="mesh" then for i,comp in pairs(comps:split(',')) do if comp=="sysinfo" then info['pages'][page][comp]=getSysinfo() elseif comp=="localhosts" then info['pages'][page][comp]={} elseif comp=="remotenodes" then info['pages'][page][comp]={} elseif comp=="currentneighbors" then info['pages'][page][comp]=aredn_olsr.getCurrentNeighbors() elseif comp=="previousneighbors" then info['pages'][page][comp]={} end end elseif page=="services" then for i,comp in pairs(comps:split(',')) do if comp=="sysinfo" then info['pages'][page][comp]=getSysinfo() elseif comp=="bynode" then info['pages'][page][comp]=getServicesByNode() elseif comp=="byservice" then info['pages'][page][comp]=getServicesByService() end end elseif page=="chart" then for i,comp in pairs(comps:split(',')) do if comp=="realtime" then info['pages'][page][comp]=getSignal(true) elseif comp=="archive" then info['pages'][page][comp]=getSignal(false) end end elseif page=="scan" then for i,comp in pairs(comps:split(',')) do if comp=="scanlist" then info['pages'][page][comp]=getScanList() end end end end -- Output the HTTP header for JSON -- json_header() json_header() -- Output the info table as json print(json.stringify(info,true))