From 2024cca12da31eb3d6abd8e85a1f6ce34d308940 Mon Sep 17 00:00:00 2001 From: Dengfeng Liu Date: Thu, 16 Jan 2025 19:11:59 +0800 Subject: [PATCH] feat: add wifi diagnostics script for network interface info and connectivity checks Signed-off-by: Dengfeng Liu --- wifi-diag | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 wifi-diag diff --git a/wifi-diag b/wifi-diag new file mode 100644 index 00000000..da0b5ab8 --- /dev/null +++ b/wifi-diag @@ -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 = "

Network Interface Info

" + + for _, v in ipairs(status.interface) do + html = html .. "

Interface: " .. v.interface .. "

" + html = html .. "

Status: " .. (v.up and "UP" or "DOWN") .. "

" + html = html .. "

Device: " .. (v.device or "N/A") .. "

" + html = html .. "

Protocol: " .. v.proto .. "

" + html = html .. "

Available: " .. (v.available and "Yes" or "No") .. "

" + if v.uptime then + html = html .. "

Uptime: " .. v.uptime .. " seconds

" + end + + -- IPv4 Addresses + if v["ipv4-address"] and #v["ipv4-address"] > 0 then + html = html .. "

IPv4 Address: " .. v["ipv4-address"][1].address .. "/" .. v["ipv4-address"][1].mask .. "

" + end + + -- Routes + if v.route and #v.route > 0 then + html = html .. "

Default Gateway: " .. v.route[1].nexthop .. "

" + end + + -- DNS Servers + if v["dns-server"] and #v["dns-server"] > 0 then + html = html .. "

DNS Server: " .. v["dns-server"][1] .. "

" + end + + html = html .. "
" + 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 = "Wifi Diagnostics" + html = html .. get_network_info(conn) + html = html .. "
" + html = html .. "

Internet Connectivity

" + html = html .. is_internet_connected() + html = html .. "
" + html = html .. "

NFT Ruleset

" + html = html .. "
" .. get_nft_info() .. "
" + html = html .. "
" + html = html .. "

Wifidogx Status

" + html = html .. "
" .. get_wifidogx_info() .. "
" + html = html .. "
" + html = html .. "

MQTT Watchdog Timestamp

" + html = html .. "

" .. get_mqtt_watchdog_timestamp() .. "

" + html = html .. "" + print(html) + + + + conn:close() +end + +main() \ No newline at end of file