aredn/files/www/cgi-bin/api

212 lines
5.6 KiB
Plaintext
Raw Normal View History

#!/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_info = require("aredn.info")
require("nixio")
local json = require("luci.jsonc")
-- 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 getCurrentNeighbors()
local info={}
local links=getOLSRLinks()
for k,v in pairs(links) do
local host
local remip=v['remoteIP']
local remhost=nslookup(remip)
info[remip]={}
info[remip]['olsrInterface']=v['olsrInterface']
info[remip]['linkType']= getInterfaceType(v['olsrInterface'])
info[remip]['linkQuality']=v['linkQuality']*100
info[remip]['neighborLinkQuality']=v['neighborLinkQuality']*100
host = string.gsub(remhost,"dtdlink%.", "")
host = string.gsub(host,"mid%d.", "")
info[remip]['hostname']=host
-- services
info[remip]['services']={}
-- get TxMBPS
info[remip]['rate']="0"
end
return info
end
function getServicesByNode()
local nodes=getAllHosts()
return nodes
end
function getFreeMemory()
local info = aredn_info.getFreeMemory()
return info
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()
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]=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
end
end
-- Output the HTTP header for JSON
-- json_header()
json_header()
-- Output the info table as json
print(json.stringify(info,true))