#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# Netmon Nodewatcher (C) 2010-2012 Freifunk Oldenburg
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"
interface_data=""
# 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
#Get interface data for whitelisted interfaces
# shellcheck disable=SC2016
awkscript='
/ether/ { printf ""$2"" }
/mtu/ { printf ""$5"" }'
if inArray "$IPWHITELIST" "$iface"; then
# shellcheck disable=SC2016
awkscript=$awkscript'
/inet / { split($2, a, "/"); printf ""a[1]"" }
/inet6/ && /scope global/ { printf ""$2"" }
/inet6/ && /scope link/ { printf ""$2""}'
fi
addrs=$(ip addr show dev "${iface}" | awk "$awkscript")
traffic_rx=$(cat "$ifpath/statistics/rx_bytes")
traffic_tx=$(cat "$ifpath/statistics/tx_bytes")
interface_data=$interface_data"<$iface>$iface$addrs$traffic_rx$traffic_tx"
interface_data=$interface_data$(iwconfig "${iface}" 2>/dev/null | awk -F':' '
/Mode/{ split($2, m, " "); printf ""m[1]"" }
/Cell/{ split($0, c, " "); printf ""c[5]"" }
/ESSID/ { split($0, e, "\""); printf ""e[2]"" }
/Freq/{ split($3, f, " "); printf ""f[1]f[2]"" }
/Tx-Power/{ split($0, p, "="); sub(/[[:space:]]*$/, "", p[2]); printf ""p[2]"" }
')
interface_data=$interface_data$(iw dev "${iface}" info 2>/dev/null | awk '
/ssid/{ split($0, s, " "); printf ""s[2]"" }
/type/ { split($0, t, " "); printf ""t[2]"" }
/channel/{ split($0, c, " "); printf ""c[2]"" }
/width/{ split($0, w, ": "); sub(/ .*/, "", w[2]); printf ""w[2]"" }
')
interface_data=$interface_data"$iface>"
done
echo -n "$interface_data"
exit 0