aredn/files/www/cgi-bin/sysinfo.json

242 lines
5.5 KiB
JSON
Raw Normal View History

#!/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")
require("nixio")
local ipc = require("luci.ip")
local json = require ("luci.jsonc")
-- Function extensions
os.capture = capture
-- ==== MAIN =====
ctx = uci.cursor()
if not ctx then
error("Failed to get uci cursor")
end
info={}
-- API version
info['api_version']="1.1"
-- NODE name
css=getUciConfType("system", "system")
info['node']=css[0]['hostname']
-- MODEL
m=os.capture("/usr/local/bin/get_model")
info['model']=m:chomp()
-- BOARD ID
info['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
-- 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()
-- 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
--Wifi Channel Number
local chan=""
chan = tonumber(ctx:get("wireless", "radio0", "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", "radio0", "chanbw")
info["chanbw"]=chanbw
-- ACTIVE TUNNELS
local atc=""
atc=os.capture("ifconfig|grep tun|wc -l")
info["active_tunnel_count"]=atc:chomp()
-- 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
-- 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
-- INTERFACES
local tif={}
local ift=get_interfaces()
for pos, i in pairs(ift) do
local nim={}
local ipv4=""
if (i.name ~= "lo" and i.name ~= "wlan0-1") then
--table.print(i)
nim['name']=i.name
ipv4=tostring(i.ipaddrs[1])
nim['ip']=ipFromCIDR(ipv4)
if i.macaddr~=nil then -- ie. tunXX interfaces have nil for macaddr
nim['mac']=i.macaddr:upper()
end
table.insert(tif,nim)
end
end
info["interfaces"]=tif
-- HOSTS
if string.find(nixio.getenv("QUERY_STRING"):lower(),"hosts=1") then
local hosts={}
local lines={}
local pos, val
local hfile=io.open("/var/run/hosts_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local host={}
-- local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")
local data,comment = string.match(val,"^([^#;]+)[#;]*(.*)$")
if data then
--local ip, name=string.match(data,"^%s*([%x%.%:]+)%s+(%S.*)\t%s*$")
local ip, name=string.match(data,"^([%x%.%:]+)%s+(%S.*)\t%s*$")
if ip and name then
if not string.match(name,"^(dtdlink[.]).*") then
if not string.match(name,"^(mid[0-9][.]).*") then
host['name']=name
host['ip']=ip
table.insert(hosts,host)
end
end
end
end
end
else
host['error']="Cannot read hosts file"
table.insert(hosts,host)
end
info["hosts"]=hosts
end
-- SERVICES
if string.find(nixio.getenv("QUERY_STRING"):lower(),"services=1") then
local services={}
local lines={}
local pos, val
local hfile=io.open("/var/run/services_olsr","r")
if hfile~=nil then
for line in hfile:lines() do
table.insert(lines,line)
end
hfile:close()
for pos,val in pairs(lines) do
local service={}
local link,protocol,name = string.match(val,"^(.*)|(.+)|(.*)\t+%s+.*")
if link and protocol and name then
service['link']=link
service['protocol']=protocol
service['name']=name
table.insert(services,service)
end
end
else
service['error']="Cannot read services file"
table.insert(services,service)
end
info["services"]=services
end
-- Output the HTTP header for JSON
json_header()
-- Output the info table as json
print(json.stringify(info,true))