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

243 lines
6.1 KiB
JSON
Executable File

#!/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.info")
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.5"
info['node_details']={}
-- NODE name
css=getUciConfType("system", "system")
info['node_details']['node']=css[0]['hostname']
-- MODEL
info['node_details']['model']=getModel()
-- DESCRIPTION
info['node_details']['description']=getNodeDescription()
-- BOARD ID
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['node_details']['firmware_mfg']=fw_mfg
-- Firmware version
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['tunnels']["tunnel_installed"]=tunnel_installed
-- ACTIVE TUNNELS
local atc=""
atc=os.capture("ifconfig|grep tun|wc -l")
info['tunnels']["active_tunnel_count"]=atc:chomp()
-- Location info
info['location']={}
-- LAT/LON
local lat, lon = getLatLon()
info['location']["lat"]=lat
info['location']["lon"]=lon
-- GRID SQUARE
info['location']["grid_square"]=getGridSquare()
-- UPTIME AND LOADAVGS
mynix=nixio.sysinfo()
info['sysinfo']={}
upsecs=mynix['uptime']
info['sysinfo']['uptime']=secondsToClock(upsecs)
info['sysinfo']['loads']=mynix['loads']
for n,x in ipairs(info['sysinfo']['loads']) do
info['sysinfo']['loads'][n]=round2(x,2)
end
-- 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" and i.name ~= "wlan1-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]*)\t#.*")
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
-- LOCALLY HOSTED SERVICES ONLY
if string.find(nixio.getenv("QUERY_STRING"):lower(),"services_local=1") then
local filelines={}
local lclsrvs={}
local lclsrvfile=io.open("/etc/config/services", "r")
if lclsrvfile~=nil then
for line in lclsrvfile:lines() do
table.insert(filelines, line)
end
lclsrvfile:close()
for pos,val in pairs(filelines) do
local service={}
local link,protocol,name = string.match(val,"^([^|]*)|(.+)|([^\t]*).*")
if link and protocol and name then
service['name']=name
service['protocol']=protocol
service['link']=link
table.insert(lclsrvs, service)
end
end
else
service['error']="Cannot read local services file"
table.insert(lclsrvs, service)
end
info['services_local']=lclsrvs
end
-- Output the HTTP header for JSON
json_header()
-- Output the info table as json
print(json.stringify(info,true))