firmware/src/packages/fff/fff-network/files/usr/lib/nodewatcher.d/21-interfaces.sh

98 lines
3.7 KiB
Bash

#!/bin/sh
IFACEBLACKLIST=$(uci get nodewatcher.@network[0].iface_blacklist)
IPWHITELIST=$(uci get nodewatcher.@network[0].ip_whitelist)
debug() {
(>&2 echo "nodewatcher: $1")
}
inArray() {
local value
for value in $1; do
[ "$value" = "$2" ] && return 0
done
return 1
}
debug "Collecting information from network interfaces"
. /usr/share/libubox/jshn.sh
json_load_file /tmp/nodewatcher.json
json_add_array "interfaces"
# Loop through interfaces: for entry in $IFACES; do
for filename in $(grep 'up\|unknown' /sys/class/net/*/operstate); do
ifpath=${filename%/operstate*}
iface=${ifpath#/sys/class/net/}
inArray "$IFACEBLACKLIST" "$iface" && continue
json_add_object ""
json_add_string "name" "$iface"
#Get interface data for whitelisted interfaces
# shellcheck disable=SC2016
mac_addr=$(ip addr show dev $iface | awk '/ether/ { printf $2 }')
json_add_string "mac_addr" "$mac_addr"
mtu=$(ip addr show dev $iface | awk '/mtu/ { printf $5 }')
json_add_string "mtu" "$mtu"
if inArray "$IPWHITELIST" "$iface"; then
# shellcheck disable=SC2016
json_add_array ipv4_addr
IN=$(ip addr show dev $iface | awk '/inet / { split($2, a, "/"); printf a[1]";" }')
IFS=';'; set $IN; IFS=$' \t\n'
for a; do json_add_string "" "$a"; done
json_close_object #ipv4_addr
json_add_array ipv6_addr
IN=$(ip addr show dev $iface | awk '/inet6/ && /scope global/ { printf $2";" }')
IFS=';'; set $IN; IFS=$' \t\n'
for a; do json_add_string "" "$a"; done
json_close_object #ipv6_addr
json_add_array ipv6_link_local_addr
IN=$(ip addr show dev $iface | awk '/inet6/ && /scope link/ { printf $2 }')
IFS=';'; set $IN; IFS=$' \t\n'
for a; do json_add_string "" "$a"; done
json_close_object #ipv6_link_local_addr
fi
json_add_object "traffic"
traffic_rx=$(cat "$ifpath/statistics/rx_bytes")
json_add_string "rx" "$(cat "$ifpath/statistics/rx_bytes")"
traffic_tx=$(cat "$ifpath/statistics/tx_bytes")
json_add_string "tx" "$traffic_tx"
json_close_object #traffic
wlan_mode=$(iwconfig $iface 2>/dev/null | awk -F':' '/Mode/{ split($2, m, " "); printf m[1] }')
if [[ $wlan_mode ]]; then
json_add_object "wlan"
wlan_cell=$(iwconfig $iface 2>/dev/null | awk -F':' '/Cell/{ split($0, c, " "); printf c[5] }')
wlan_essid=$(iwconfig $iface 2>/dev/null | awk -F':' '/ESSID/ { split($0, e, "\""); printf e[2] }')
wlan_frequency=$(iwconfig $iface 2>/dev/null | awk -F':' '/Freq/{ split($3, f, " "); printf f[1]f[2] }')
wlan_tx_power=$(iwconfig $iface 2>/dev/null | awk -F':' '/Tx-Power/{ split($0, p, "="); sub(/[[:space:]]*$/, "", p[2]); printf p[2] }')
wlan_ssid=$(iw dev $iface info 2>/dev/null | awk '/ssid/{ split($0, s, " "); printf s[2]}')
wlan_type=$(iw dev $iface info 2>/dev/null | awk '/type/ { split($0, t, " "); printf t[2]}')
wlan_channel=$(iw dev $iface info 2>/dev/null | awk '/channel/{ split($0, c, " "); printf c[2]}')
wlan_width=$(iw dev $iface info 2>/dev/null | awk '/width/{ split($0, w, ": "); sub(/ .*/, "", w[2]); printf w[2]}')
[[ $wlan_mode ]] && json_add_string "mode" "$wlan_mode"
[[ $wlan_cell ]] && json_add_string "cell" "$wlan_cell"
[[ $wlan_essid ]] && json_add_string "essid" "$wlan_essid"
[[ $wlan_frequency ]] && json_add_string "frequency" "$wlan_frequency"
[[ "$wlan_tx_power" ]] && json_add_string "tx_power" "$wlan_tx_power"
[[ $wlan_ssid ]] && json_add_string "ssid" "$wlan_ssid"
[[ $wlan_type ]] && json_add_string "type" "$wlan_type"
[[ $wlan_channel ]] && json_add_string "channel" "$wlan_channel"
[[ $wlan_width ]] && json_add_string "width" "$wlan_width"
json_close_object #wlan
fi
json_close_object #iface object
done
json_dump > /tmp/nodewatcher.json
#exit 0