#!/opt/lnxall_app/bin/lua -- Created by jiaqiang.ye@lnxall.com -- Simple Network Information Collector -- 2025/12/09 local invoker = require "invoker" local gfmt = string.format local g_line = "============================================================================\n" local g_light = "----------------------------------------------------------------------------\n" local g_grepf = " -e NetworkManager -e dhclient -e br-lan -e dhcp" local g_outfile = nil local function collect_init() invoker.setenv("PATH", "/opt/lnxall_app/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin") -- get GWSN via factory get local _, output = invoker.invoke(invoker.OUTPUT, "factory", "get") if type(output) ~= "string" or not string.find(output, "SN=", 1, true) then -- and via `/app/config/fac.ini output = invoker.readfile("/app/config/fac.ini") end if type(output) ~= "string" then output = "" end gwsn = string.match(output, "SN%s*=%s*([^%s\r\n]+)") if not gwsn then gwsn = string.match(output, "sn%s*=%s*([^%s\r\n]+)") end if type(gwsn) ~= "string" then gwsn = "unknown" end -- generate output filename local now = os.date("*t") g_outfile = gfmt("/root/netinfo-%s-%d%02d%02d_%02d%02d%02d.txt", gwsn, now.year, now.month, now.day, now.hour, now.min, now.sec) io.stdout:write(gfmt("Generating output file: %s\n", g_outfile)) io.stdout:flush() return true end local function remove_old_files() local files = invoker.glob("/root/netinfo-*.txt", 0x4) if not files then return true end local now = os.time() for _, file in ipairs(files) do local ftype, _, _, _, mtime = invoker.statfile(file) if ftype == "regular" and (now - mtime) >= (15 * 86400) then invoker.unlink(file) io.stdout:write(gfmt("Warning, remove old file: %s\n", file)) io.stdout:flush() end end return true end local function collect_item(what) local gfd = invoker.open(g_outfile, 525377) if type(gfd) == "number" and gfd >= 0 then invoker.writefd(gfd, g_line) invoker.writefd(gfd, g_line) invoker.writefd(gfd, what) invoker.writefd(gfd, "\n") invoker.writefd(gfd, g_light) invoker.closefd(gfd) end local cmd = gfmt("%s >>%s 2>&1", what, g_outfile) invoker.invoke(0, "bash", "-c", cmd) end local function iscandev(ndev) local len = string.len(ndev) if len > 3 and ndev:sub(1, 3) == "can" then return true end if len > 6 and ndev:sub(1, 6) == "lnxcan" then return true end return false end local function glob_netdevs() local olddir = invoker.getcwd() if not invoker.chdir("/sys/class/net") then io.stderr:write("Error, failed to goto directory: /sys/class/net\n") io.stderr:flush() return nil end local netdevs = invoker.glob("*") if not netdevs then invoker.chdir(olddir) io.stderr:write("Error, nothing found in /sys/class/net\n") io.stderr:flush() return nil end local nettab, netnum = {}, 0 for _, ndev in ipairs(netdevs) do if invoker.statfile(ndev .. "/device") and not iscandev(ndev) then netnum = netnum + 1 nettab[netnum] = ndev end end invoker.chdir(olddir) if netnum == 0 then io.stderr:write("Error, no real network device found.\n") io.stderr:flush() return nil end return nettab end local function collect_netinfo() collect_item("cat /etc/signal_4g.txt") collect_item("cat /etc/config/network") collect_item("cat /etc/config/wireless") collect_item("cat /tmp/status_info.json") collect_item("cat /tmp/.modem_4ginfo.json") collect_item("tail -n 32 /etc/poweroff-check.log") collect_item("lspci") collect_item("lsusb") collect_item("uptime") collect_item("uname -a") collect_item("factory get") collect_item("ls -l /dev/ttyUSB*") collect_item("cat /tmp/4g_modem_tty") collect_item("apptime | grep -e lua") collect_item("/lib/dyiot/init.d/deb4gnet status") collect_item("ip neigh") collect_item("ip addr show") collect_item("ip route show") collect_item("nmcli con show") if invoker.statfile("/sys/bus/spi/drivers/w5100") then collect_item("ls -l /sys/bus/spi/drivers/w5100") end collect_item("systemctl status bdhcpd") collect_item("systemctl status lnxhostapd") collect_item("systemctl status NetworkManager") local filter = {} local netlist = glob_netdevs() or {} for i, net in ipairs(netlist) do collect_item("ethtool " .. net) filter[2 * i - 1] = "-e" filter[2 * i] = net end if #filter == 0 then filter = g_grepf else filter = table.concat(filter, " ") .. g_grepf end local syslogs = invoker.glob("/var/log/compressed/syslog*.gz") or {} if not syslogs then syslogs = {} else local gfd = invoker.open(g_outfile, 525377) if type(gfd) == "number" and gfd >= 0 then invoker.writefd(gfd, g_line) invoker.writefd(gfd, g_line) invoker.writefd(gfd, gfmt("grep %s\n", filter)) invoker.writefd(gfd, g_light) invoker.closefd(gfd) end end local now = os.time() for _, syslog in ipairs(syslogs) do local ftype, _, _, _, mtime = invoker.statfile(syslog) if ftype == "regular" and (now - mtime) >= 0 and (now - mtime) <= (2 * 86400) then invoker.invoke(0, "bash", "-c", gfmt("gzip -d -c %s | grep -a %s >> %s", syslog, filter, g_outfile)) end end if invoker.statfile("/tmp/syslog") then invoker.invoke(0, "bash", "-c", gfmt("cat /tmp/syslog | grep -a %s >> %s", filter, g_outfile)) end collect_item("dmesg -T") io.stdout:write("Done.\n") io.stdout:flush() end collect_init() remove_old_files() collect_netinfo()