aredn/files/www/cgi-bin/api

344 lines
9.9 KiB
Lua
Executable File

#!/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 <http://www.gnu.org/licenses/>.
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()
info['first_boot']=aredn_info.getFirstBoot()
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.getFreq(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
-- 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 = 0
end
if noise_dbm=="null" then
noise_dbm = 0
end
-- signal and noise might also be nil, so convert them to numbers
signal_dbm = tonumber(signal_dbm or 0)
noise_dbm = tonumber(noise_dbm or 0)
--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()]
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
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={}
-- 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]=aredn_info.getLocalHosts()
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))