rework sysinfo.json to improve structure (#185)

* rework sysinfo.json to improve structure
re-organize "info" functions into their own lib

* change egrep to grep -E to eliminate shellcheck warning
This commit is contained in:
dman776 2018-09-10 13:37:50 -05:00 committed by GitHub
parent cb35580fd2
commit c2894f8df9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 283 additions and 81 deletions

View File

@ -43,6 +43,7 @@ end
function json_header()
print("Content-type: application/json\r")
print("Cache-Control: no-store\r")
print("Access-Control-Allow-Origin: *\r")
print("\n")
end

View File

@ -0,0 +1,225 @@
#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2016 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 lip=require("luci.ip")
require("nixio")
require("ubus")
function getNodeName()
css=getUciConfType("system", "system")
return css[0]['hostname']
end
function getNodeDescription()
css=getUciConfType("system", "system")
return css[0]['description']
end
function getLatLon()
local llfname="/etc/latlon"
local lat=""
local lon=""
if file_exists(llfname) then
llfile=io.open(llfname,"r")
if llfile~=nil then
lat=llfile:read()
lon=llfile:read()
llfile:close()
end
end
return lat,lon
end
function getGridSquare()
local gsfname="/etc/gridsquare"
local grid=""
if file_exists(gsfname) then
gsfile=io.open(gsfname,"r")
if gsfile~=nil then
grid=gsfile:read()
gsfile:close()
end
end
return grid
end
function getFirmwareVersion()
local relfile=io.open("/etc/mesh-release","r")
local fv=""
if relfile~=nil then
fv=relfile:read():chomp()
relfile:close()
end
return fv
end
function getModel()
m=os.capture("/usr/local/bin/get_model")
return m:chomp()
end
function getSSID()
-- SSID
local myssid=""
local wif=getUciConfType("wireless", "wifi-iface")
for pos, t in pairs(wif) do
if wif[pos]['network']=="wifi" then
myssid=wif[pos]['ssid']
end
end
return myssid
end
function getMeshRadioDevice()
--Determine radio device for mesh
local radio=""
local wifiinterfaces=getUciConfType("wireless","wifi-iface")
for pos,i in pairs(wifiinterfaces) do
if wifiinterfaces[pos]['mode']=="adhoc" then
radio=wifiinterfaces[pos]['device']
end
end
return radio
end
function getBand(radio)
return ""
end
function getFrequency(radio)
return ""
end
function getChannel(radio)
--Wifi Channel Number
local ctx = uci.cursor()
if not ctx then
error("Failed to get uci cursor")
end
local chan=""
chan = tonumber(ctx:get("wireless", radio, "channel"))
-- 3GHZ channel -> Freq conversion
if (chan >= 76 and chan <= 99) then
chan=(chan * 5) + 3000
end
return tostring(chan)
end
function getChannelBW(radio)
--Wifi Bandwidth
ctx = uci.cursor()
if not ctx then
error("Failed to get uci cursor")
end
local chanbw=""
chanbw = ctx:get("wireless", radio, "chanbw")
return chanbw
end
function getUptime()
local mynix=nixio.sysinfo()
local upsecs=mynix['uptime']
return secondsToClock(upsecs)
end
function getDate()
return os.date("%a %b %d %Y")
end
function getTime()
return os.date("%H:%M:%S %Z")
end
function getLoads()
local loads={}
local mynix=nixio.sysinfo()
loads=mynix['loads']
for n,x in ipairs(loads) do
loads[n]=round2(x,2)
end
return loads
end
function getFreeMemory()
local mem={}
local mynix=nixio.sysinfo()
mem['freeram']=mynix['freeram']/1024
mem['sharedram']=mynix['sharedram']/1024
mem['bufferram']=mynix['bufferram']/1024
return mem
end
function getFSFree()
local fsf={}
local mynix=nixio.fs.statvfs("/")
fsf['rootfree']=mynix['bfree']*4
mynix=nixio.fs.statvfs("/tmp")
fsf['tmpfree']=mynix['bfree']*4
mynix=nil
return fsf
end
function getOLSRInfo()
local info={}
tot=os.capture('/sbin/ip route list table 30|wc -l')
info['entries']=tot:chomp()
nodes=os.capture('/sbin/ip route list table 30|grep -E "/"|wc -l')
info['nodes']=nodes:chomp()
return info
end
function getInterfaceIPAddress(interface)
return getUciConfSectionOption("network",interface,"ipaddr")
end
function getDefaultGW()
local gw=""
local rt=lip.route("8.8.8.8")
if rt ~= "" then
gw=tostring(rt.gw)
end
return gw
end
function getWAN()
local cubus = ubus.connect()
niws=cubus:call("network.interface.wan","status",{})
return niws['ipv4-address'][1]['address']
end

View File

@ -42,3 +42,8 @@ function getUciConfType(conf,type)
curs:foreach(conf,type,function(s) ifce[s[".index"]]=s end)
return ifce
end
function getUciConfSectionOption(conf,sect,option)
local curs=uci.cursor()
return curs:get(conf,sect,option)
end

View File

@ -54,11 +54,26 @@ function adjust_rate(r,b)
return ar
end
function get_bandwidth()
local curs=uci.cursor()
local b
b=curs:get("wireless","radio0","chanbw")
return tonumber(b)
function string:split(delim)
local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((self:gsub("(.-)"..delim, helper)))
return t
end
function parseQueryString(qs)
local qsa={}
if qs ~=nil then
for i,j in pairs(qs:split("&")) do
z=j:split("=")
qsa[z[1]]=z[2]
end
end
return qsa
end
function setContains(set, key)
return set[key] ~= nil
end
function sleep(n) -- seconds

View File

@ -35,6 +35,7 @@
--]]
require("aredn.utils")
require("aredn.info")
require("uci")
require("aredn.uci")
local nxo=require("nixio")
@ -297,7 +298,8 @@ end
)
-- get the current bandwidth setting
bandwidth=get_bandwidth()
local radio=getMeshRadioDevice()
bandwidth=getChannelBW(radio)
-- iterate over all the stations
for mstation in pairs(stations) do

View File

@ -37,6 +37,7 @@
require("uci")
require("aredn.uci")
require("aredn.utils")
require("aredn.info")
require("aredn.http")
require("nixio")
require("iwinfo")
@ -73,7 +74,8 @@ end
local wifiiface=get_ifname("wifi")
-- get bandwidth
local bandwidth=get_bandwidth()
local radio=getMeshRadioDevice()
local bandwidth=getChannelBW(radio)
if parms.realtime then
-- REALTIME

View File

@ -37,6 +37,7 @@
require("uci")
require("aredn.uci")
require("aredn.utils")
require("aredn.info")
require("aredn.http")
require("nixio")
local ipc = require("luci.ip")
@ -55,110 +56,61 @@ end
info={}
-- API version
info['api_version']="1.4"
info['api_version']="1.5"
info['node_details']={}
-- NODE name
css=getUciConfType("system", "system")
info['node']=css[0]['hostname']
info['node_details']['node']=css[0]['hostname']
-- MODEL
m=os.capture("/usr/local/bin/get_model")
info['model']=m:chomp()
info['node_details']['model']=getModel()
-- DESCRIPTION
info['description']=css[0]['description']
info['node_details']['description']=getNodeDescription()
-- BOARD ID
info['board_id']=hardware_boardid()
info['node_details']['board_id']=hardware_boardid()
-- Firmware Manufacturer
local fw_mfg="AREDN"
if not file_exists("/www/AREDN.png") then
fw_mfg = "Other"
end
info['firmware_mfg']=fw_mfg
info['node_details']['firmware_mfg']=fw_mfg
-- Firmware version
local relfile=io.open("/etc/mesh-release","r")
local fv=""
if relfile~=nil then
fv=relfile:read()
relfile:close()
end
info['firmware_version']=fv:chomp()
info['node_details']['firmware_version']=getFirmwareVersion()
-- Mesh RF info
info["meshrf"]={}
info["meshrf"]["ssid"]=getSSID()
local radio=getMeshRadioDevice()
info["meshrf"]["channel"]=getChannel(radio)
info["meshrf"]['chanbw']=getChannelBW(radio)
-- Tunnel info
info['tunnels']={}
-- Tunnel Installed
local tunnel_installed=false
if file_exists("/usr/sbin/vtund") then
tunnel_installed="true"
end
info["tunnel_installed"]=tunnel_installed
-- SSID
local myssid=""
wif=getUciConfType("wireless", "wifi-iface")
for pos, t in pairs(wif) do
if wif[pos]['network']=="wifi" then
myssid=wif[pos]['ssid']
end
end
info["ssid"]=myssid
--Determine radio device for mesh
local radio=""
local wifiinterfaces=getUciConfType("wireless","wifi-iface")
for pos,i in pairs(wifiinterfaces) do
if wifiinterfaces[pos]['mode']=="adhoc" then
radio=wifiinterfaces[pos]['device']
end
end
--Wifi Channel Number
local chan=""
chan = tonumber(ctx:get("wireless", radio, "channel"))
-- 3GHZ channel -> Freq conversion
if (chan >= 76 and chan <= 99) then
chan=(chan * 5) + 3000
end
info["channel"]=tostring(chan)
--Wifi Bandwidth
local chanbw=""
chanbw = ctx:get("wireless", radio, "chanbw")
info["chanbw"]=chanbw
info['tunnels']["tunnel_installed"]=tunnel_installed
-- ACTIVE TUNNELS
local atc=""
atc=os.capture("ifconfig|grep tun|wc -l")
info["active_tunnel_count"]=atc:chomp()
info['tunnels']["active_tunnel_count"]=atc:chomp()
-- Location info
info['location']={}
-- LAT/LON
local llfname="/etc/latlon"
local lat=""
local lon=""
if file_exists(llfname) then
llfile=io.open(llfname,"r")
if llfile~=nil then
lat=llfile:read()
lon=llfile:read()
llfile:close()
end
end
info["lat"]=lat
info["lon"]=lon
local lat, lon = getLatLon()
info['location']["lat"]=lat
info['location']["lon"]=lon
-- GRID SQUARE
local gsfname="/etc/gridsquare"
local grid=""
if file_exists(gsfname) then
gsfile=io.open(gsfname,"r")
if gsfile~=nil then
grid=gsfile:read()
gsfile:close()
end
end
info["grid_square"]=grid
info['location']["grid_square"]=getGridSquare()
-- UPTIME AND LOADAVGS
mynix=nixio.sysinfo()