#!/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") local nettools = require("aredn.nettools") local API_VERSION="1.5" -- Function extensions os.capture = capture function getSysinfo() local info={} info['api_version']=API_VERSION info['node']=aredn_info.getNodeName() info['tactical']=aredn_info.getTacticalName() 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() info['first_boot']=aredn_info.getFirstBoot() info['target_type']=aredn_info.getTargetType() return info end function getAlerts() local info={} info['aredn']=aredn_info.getArednAlert() info['local']=aredn_info.getLocalAlert() return info end function getStatusMeshRF() local info={} local dev = aredn_info.getMeshRadioDevice() if dev ~= "" then info['device']= dev info['ssid']=aredn_info.getSSID() 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.getFreq(info['device']) end 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 get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end end return nil end function getRemoteNodes() local info = {} local neighbors = {} for _, v in ipairs(aredn_olsr.getOLSRLinks()) do local remoteIP = v.remoteIP neighbors[remoteIP] = true local name = nixio.getnameinfo(remoteIP) if name then local cname = name:match("^dtdlink%.(.*)$") or name:match("^mid%d+%.(.*)$") if cname then local addrs = nixio.getaddrinfo(cname) if addrs then neighbors[addrs[1].address] = true end end end end local routeetx = {} for _, v in ipairs(aredn_olsr.getOLSRRoutes()) do routeetx[v.destination] = string.format("%.2f", v.etx) end for line in io.lines("/var/run/hosts_olsr.stable") do local ip, hostname = line:match("^(%d+%.%d+%.%d+%.%d+)%s+(%S+)%s+#.*$") if ip and not neighbors[ip] and not (hostname:match("^dtdlink%.") or hostname:match("^mid%d+%.")) then local etx = routeetx[ip] if etx then info[#info + 1] = { name = hostname, ip = ip, etx = etx } end end end table.sort(info, function(a,b) return tonumber(a.etx) < tonumber(b.etx) end) return info end -- gets signal and noise data from either iwinfo (realtime = true) or the files archived in /tmp/snrlog (realtime = false) -- the files in /tmp/snrlog are written once per minute by the script /usr/local/bin/snrlog function getSignal(realtime) local defnoise=-95 local dpa={} -- datapoint table/array local dirname="/tmp/snrlog" 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) local timestamp_s=os.time() local signal_dbm, noise_dbm, tx_rate_mbps, rx_rate_mbps, tx_rate_mcs_index, rx_rate_mcs_index --[[ -- insertResult() transforms data from iwinfo (realtime) and snrlog (archive) so that the responses are always the same. -- the front end expects data points that look like this: { timestamp: string; signal_dbm: number; noise_dbm: number; tx_rate_mbps: number; rx_rate_mbps: number; tx_rate_mcs_index: number | undefined; rx_rate_mcs_index: number | undefined; } ]]-- function insertResult(timestamp_s_epoch, signal_dbm, noise_dbm, tx_rate_mbps, rx_rate_mbps, tx_rate_mcs_index, rx_rate_mcs_index) local dp={} -- datapoint local timestamp -- snrlog sometimes has the string "null" for signal/noise, if so then assume they are zero if signal_dbm=="null" then signal_dbm = -95 end if noise_dbm=="null" then noise_dbm = -95 end -- signal and noise might also be nil, so convert them to numbers signal_dbm = tonumber(signal_dbm or -95) noise_dbm = tonumber(noise_dbm or -95) --snrlog sometimes has -1 for mcs indices, if so then make them undefined if tx_rate_mcs_index == -1 or tx_rate_mcs_index == "-1" then tx_rate_mcs_index = nil end if rx_rate_mcs_index == -1 or rx_rate_mcs_index == "-1" then rx_rate_mcs_index = nil end -- convert from seconds since epoch to iso 8601 format timestamp=os.date("%Y-%m-%dT%H:%M:%SZ", timestamp_s_epoch) dp.timestamp = timestamp dp.signal_dbm=signal_dbm dp.noise_dbm=noise_dbm dp.tx_rate_mbps=tonumber(tx_rate_mbps or 0) dp.rx_rate_mbps=tonumber(rx_rate_mbps or 0) dp.tx_rate_mcs_index=tonumber(tx_rate_mcs_index) dp.rx_rate_mcs_index=tonumber(rx_rate_mcs_index) table.insert(dpa,dp) end if realtime then -- REALTIME if (parms.device=="strongest" or parms.device=="" or (not parms.device)) then -- REALTIME/STRONGEST SIGNAL signal_dbm=iwinfo["nl80211"].signal(wifiiface) noise_dbm=iwinfo["nl80211"].noise(wifiiface) 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()] if macinfo then signal_dbm=macinfo.signal noise_dbm=macinfo.noise tx_rate_mbps=macinfo.tx_rate/1000 tx_rate_mbps=adjust_rate(tx_rate_mbps,bandwidth) rx_rate_mbps=macinfo.rx_rate/1000 rx_rate_mbps=adjust_rate(rx_rate_mbps,bandwidth) tx_rate_mcs_index=macinfo.tx_mcs rx_rate_mcs_index=macinfo.rx_mcs end end insertResult(timestamp_s, signal_dbm, noise_dbm, tx_rate_mbps, rx_rate_mbps, tx_rate_mcs_index, rx_rate_mcs_index ) 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 timestamp timestamp,signal_dbm,noise_dbm,tx_rate_mcs_index,tx_rate_mbps,rx_rate_mcs_index,rx_rate_mbps=line:match("(.*)%,(.*)%,(.*),(.*),(.*),(.*),(.*)") -- convert the timestamp to seconds since epoch local dtm,dtd,dty,dth,dtmin,dts=timestamp:match(datepattern) timestamp_s = os.time({month=dtm,day=dtd,year=dty,hour=dth,min=dtmin,sec=dts}) insertResult(timestamp_s, signal_dbm, noise_dbm, tx_rate_mbps, rx_rate_mbps, tx_rate_mcs_index, rx_rate_mcs_index) end end end return dpa 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={} info['api_version'] = API_VERSION -- 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=="common" then for i,comp in pairs(comps:split(',')) do if comp=="sysinfo" then info['pages'][page][comp]=getSysinfo() elseif comp=="alerts" then info['pages'][page][comp]=getAlerts() end end 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() elseif comp=="alerts" then info['pages'][page][comp]=getAlerts() end end elseif page=="traceroute" then for i,tonode in pairs(comps:split(',')) do -- Validate that input as ip or hostname inside the mesh if tonode:match("^[%d%.]+$") or tonode:match("^[%d%a%-%.%_]+$") then info['pages'][page][tonode]=nettools.getTraceroute(tonode) else info['pages'][page][tonode]="Invalid input!" end end elseif page=="ping" then for i,tonode in pairs(comps:split(',')) do -- Validate that input as ip or hostname inside the mesh if tonode:match("^[%d%.]+$") or tonode:match("^[%d%a%-%.%_]+$") then info['pages'][page][tonode]=nettools.getPing(tonode) else info['pages'][page][tonode]="Invalid input!" end end elseif page=="iperf3" then local protocol = "tcp" for i,tonode in pairs(comps:split(',')) do -- Validate that input as ip or hostname inside the mesh if tonode == "tcp" or tonode == "udp" then protocol = tonode elseif tonode:match("^[%d%.]+$") or tonode:match("^[%d%a%-%.%_]+$") then info['pages'][page][tonode]=nettools.getIperf3(tonode, protocol) else info['pages'][page][tonode]="Invalid input!" 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=="allhosts" then info['pages'][page][comp]=aredn_info.all_hosts() elseif comp=="localhosts" then info['pages'][page][comp]=aredn_info.getLocalHosts() elseif comp=="remotenodes" then info['pages'][page][comp]=getRemoteNodes() elseif comp=="currentneighbors" then info['pages'][page][comp]=aredn_olsr.getCurrentNeighbors(true) elseif comp=="services" then info['pages'][page][comp]=aredn_info.all_services() elseif comp=="previousneighbors" then info['pages'][page][comp]={} elseif comp=="topology" then info['pages'][page][comp]=fetch_json("http://127.0.0.1:9090/topology") 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))