eeautocore: ethinfo: rewritten in lua

Signed-off-by: Tianling Shen <cnsztl@immortalwrt.org>
This commit is contained in:
Tianling Shen 2022-03-19 12:59:51 +08:00
parent 3adf1d9bbe
commit cd1e95514e
No known key found for this signature in database
GPG Key ID: 6850B6345C862176
3 changed files with 36 additions and 23 deletions

View File

@ -20,15 +20,14 @@ define Package/autocore-arm
MAINTAINER:=CN_SZTL
DEPENDS:=@(arm||aarch64) \
+TARGET_bcm27xx:bcm27xx-userland \
+TARGET_bcm53xx:nvram \
+ethtool
+TARGET_bcm53xx:nvram
VARIANT:=arm
endef
define Package/autocore-x86
TITLE:=x86/x64 auto core loadbalance script
MAINTAINER:=Lean
DEPENDS:=@TARGET_x86 +lm-sensors +ethtool
DEPENDS:=@TARGET_x86 +lm-sensors
VARIANT:=x86
endef

View File

@ -1,25 +1,39 @@
#!/bin/sh
#!/usr/bin/lua
-- Copyright (C) 2022 Tianling Shen <cnsztl@immortalwrt.org>
for i in $(ip address | grep -E 'eth[0-9]+' | awk -F ': ' '{print $2}' | xargs)
do
device_info="$(ethtool "$i" 2>"/dev/null")" || continue
local util = require "luci.util"
local jsonc = require "luci.jsonc"
if echo -e "$device_info" | grep -Eq 'Link detected.*yes'; then
link_stat=1
else
link_stat=0
fi
link_speed="$(echo -e "$device_info" | grep 'Speed' | awk -F ': ' '{print $2}' | tr -d 'Unknown!')"
[ -n "$link_speed" ] || link_speed=" - "
local eth_info = {}
local ifname, stat
for ifname, stat in pairs(util.ubus("network.device", "status")) do
if ifname:match("^(eth[0-9]+)$") == ifname then
local status, speed, duplex
if echo -e "$device_info" | grep -Eq 'Duplex.*Full'; then
link_mode=1
else
link_mode=0
fi
status = stat.carrier and 1 or 0
json_body="${json_body:+$json_body,}{\"name\": \"$i\", \"status\": $link_stat, \"speed\": \"$link_speed\", \"duplex\": $link_mode}"
done
if stat.speed:sub(1, 1) == "-" then
speed = " - "
else
speed = stat.speed:sub(1, -2) .. "Mb/s"
end
echo "[$json_body]"
if stat.carrier and stat.speed:sub(-1) == "F" then
duplex = 1
else
duplex = 0
end
eth_info[#eth_info+1] = { name = ifname, status = status,
speed = speed, duplex = duplex }
end
end
table.sort(eth_info,
function(a, b)
return tonumber(a.name:match("eth([0-9]+)$")) <
tonumber(b.name:match("eth([0-9]+)$"))
end)
print(jsonc.stringify(eth_info))

View File

@ -62,7 +62,7 @@
local eth_info = ""
if not has_switch then
eth_info = luci.sys.exec("ethinfo")
eth_info = luci.sys.exec("/sbin/ethinfo")
end
local rv = {