-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add wifi diagnostics script for network interface info and conn…
…ectivity checks Signed-off-by: Dengfeng Liu <[email protected]>
- Loading branch information
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/usr/bin/lua | ||
|
||
-- Required modules | ||
require "ubus" | ||
require "luci.util" | ||
local json = require "luci.jsonc" | ||
local nixio = require "nixio" | ||
local http = require("luci.httpclient") | ||
|
||
-- ubus show network interface info | ||
local function get_network_info(conn) | ||
local status = conn:call("network.interface", "dump", {}) | ||
if not status then | ||
error("Failed to get network interface info") | ||
end | ||
local html = "<h1>Network Interface Info</h1>" | ||
|
||
for _, v in ipairs(status.interface) do | ||
html = html .. "<h2>Interface: " .. v.interface .. "</h2>" | ||
html = html .. "<p>Status: " .. (v.up and "UP" or "DOWN") .. "</p>" | ||
html = html .. "<p>Device: " .. (v.device or "N/A") .. "</p>" | ||
html = html .. "<p>Protocol: " .. v.proto .. "</p>" | ||
html = html .. "<p>Available: " .. (v.available and "Yes" or "No") .. "</p>" | ||
if v.uptime then | ||
html = html .. "<p>Uptime: " .. v.uptime .. " seconds</p>" | ||
end | ||
|
||
-- IPv4 Addresses | ||
if v["ipv4-address"] and #v["ipv4-address"] > 0 then | ||
html = html .. "<p>IPv4 Address: " .. v["ipv4-address"][1].address .. "/" .. v["ipv4-address"][1].mask .. "</p>" | ||
end | ||
|
||
-- Routes | ||
if v.route and #v.route > 0 then | ||
html = html .. "<p>Default Gateway: " .. v.route[1].nexthop .. "</p>" | ||
end | ||
|
||
-- DNS Servers | ||
if v["dns-server"] and #v["dns-server"] > 0 then | ||
html = html .. "<p>DNS Server: " .. v["dns-server"][1] .. "</p>" | ||
end | ||
|
||
html = html .. "<hr>" | ||
end | ||
|
||
return html | ||
end | ||
|
||
local function is_internet_connected() | ||
local url = "http://worldtimeapi.org/api/timezone/Asia/Hong_Kong" | ||
local code, resp, buffer, sock = http.request_raw(url, { | ||
headers = { | ||
["Accept"] = "application/json" | ||
} | ||
}) | ||
|
||
if not code then | ||
return "Failed to make HTTP request" | ||
end | ||
|
||
if code ~= 200 then | ||
return string.format("HTTP request failed with status code: %d", code) | ||
end | ||
|
||
-- Parse JSON response | ||
local parsed = json.parse(buffer) | ||
if not parsed then | ||
return "Failed to parse JSON response" | ||
end | ||
|
||
-- Extract relevant data | ||
local datetime = parsed.datetime or "unknown" | ||
local timezone = parsed.timezone or "unknown" | ||
|
||
return string.format("Internet is connected. Time: %s, Timezone: %s", datetime, timezone) | ||
end | ||
|
||
local function get_nft_info() | ||
local cmd = "nft list ruleset" | ||
local f = io.popen(cmd) | ||
local output = f:read("*a") | ||
f:close() | ||
return output | ||
end | ||
|
||
local function get_wifidogx_info() | ||
local cmd = "wdctlx status" | ||
local f = io.popen(cmd) | ||
local output = f:read("*a") | ||
f:close() | ||
return output | ||
end | ||
|
||
local function get_mqtt_watchdog_timestamp() | ||
local file = "/tmp/apfree/mqtt-watchdog" | ||
local f = io.open(file, "r") | ||
if not f then | ||
return "N/A" | ||
end | ||
local timestamp = f:read("*a") | ||
f:close() | ||
-- translate timestamp to human readable format | ||
timestamp = os.date("%c", tonumber(timestamp)) | ||
return timestamp | ||
end | ||
|
||
local function main() | ||
local conn = ubus.connect() | ||
if not conn then | ||
error("Failed to connect to ubusd") | ||
end | ||
|
||
print("Content-Type: text/html\n") | ||
|
||
local html = "<html><head><title>Wifi Diagnostics</title></head><body>" | ||
html = html .. get_network_info(conn) | ||
html = html .. "<hr>" | ||
html = html .. "<h1>Internet Connectivity</h1>" | ||
html = html .. is_internet_connected() | ||
html = html .. "<hr>" | ||
html = html .. "<h1>NFT Ruleset</h1>" | ||
html = html .. "<pre>" .. get_nft_info() .. "</pre>" | ||
html = html .. "<hr>" | ||
html = html .. "<h1>Wifidogx Status</h1>" | ||
html = html .. "<pre>" .. get_wifidogx_info() .. "</pre>" | ||
html = html .. "<hr>" | ||
html = html .. "<h1>MQTT Watchdog Timestamp</h1>" | ||
html = html .. "<p>" .. get_mqtt_watchdog_timestamp() .. "</p>" | ||
html = html .. "</body></html>" | ||
print(html) | ||
|
||
|
||
|
||
conn:close() | ||
end | ||
|
||
main() |