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

72 lines
2.4 KiB
Bash
Raw Normal View History

nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
# 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")
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
}
inArray() {
local value
for value in $1; do
[ "$value" = "$2" ] && return 0
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
done
return 1
}
debug "Collecting information from network interfaces"
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
interface_data=""
# Loop through interfaces: for entry in $IFACES; do
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
for filename in $(grep 'up\|unknown' /sys/class/net/*/operstate); do
ifpath=${filename%/operstate*}
iface=${ifpath#/sys/class/net/}
inArray "$IFACEBLACKLIST" "$iface" && continue
nodewatcher: split into nodewatcher.d scripts for individual task This splits up the data extraction/assembly of the nodewatcher script into several parts and distributes them across packages, so that each nodewatcher.d subscript is located in the package providing the relevant functionality. This allows to extend the nodewatcher data by enabling/disabling packages. This scheme is not perfectly fulfilled for fff-network vs. fff-wireless, as data cannot uniquely assigned there and the XML syntax does not allow separation anyway. In general, this moves code without applying code improvements, yielding at an easy comparison of moved fragments. However, the following changes were done to improve experience: - The function writing debug output has been renamed from "err" to "debug" - Since we catch the stdout of the nodewatcher.d functions anyway, those scripts were adjusted to echo output directly instead of first writing it into a variable and then outputting it at the end. - The uci config has been kept, but initialization for the network part has been moved to the fff-network package. - Space indent has been changed to tab, which is more common in the firmware and requires less space. - Remove support for nodewatcher run without uci config. Script-based nodewatcher on other platforms will have altered code anyway, and splitting it up will prevent effective use as a blueprint for those cases. After this change, nodewatcher in firmware is supposed to be used only for this firmware. Note that since the nodewatcher.d scripts are evaluated by using their echo output, having a function created uncaught output to stdout there will corrupt the XML. Signed-off-by: Tim Niemeyer <tim@tn-x.org> [rebase and adjustments for current master, use simpler mechanism to call nodewatcher.d scripts, use tab indent, remove debug() definition where not needed, do not remove uci config, add commit message, use echo -n] Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Fabian Bläse <fabian@blaese.de> [remove 'local' modifier for variable not inside a function, fix typo] Signed-off-by: Fabian Bläse <fabian@blaese.de>
2020-01-07 14:40:20 +01:00
#Get interface data for whitelisted interfaces
# shellcheck disable=SC2016
awkscript='
/ether/ { printf "<mac_addr>"$2"</mac_addr>" }
/mtu/ { printf "<mtu>"$5"</mtu>" }'
if inArray "$IPWHITELIST" "$iface"; then
# shellcheck disable=SC2016
awkscript=$awkscript'
/inet / { split($2, a, "/"); printf "<ipv4_addr>"a[1]"</ipv4_addr>" }
/inet6/ && /scope global/ { printf "<ipv6_addr>"$2"</ipv6_addr>" }
/inet6/ && /scope link/ { printf "<ipv6_link_local_addr>"$2"</ipv6_link_local_addr>"}'
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><name>$iface</name>$addrs<traffic_rx>$traffic_rx</traffic_rx><traffic_tx>$traffic_tx</traffic_tx>"
interface_data=$interface_data$(iwconfig "${iface}" 2>/dev/null | awk -F':' '
/Mode/{ split($2, m, " "); printf "<wlan_mode>"m[1]"</wlan_mode>" }
/Cell/{ split($0, c, " "); printf "<wlan_bssid>"c[5]"</wlan_bssid>" }
/ESSID/ { split($0, e, "\""); printf "<wlan_essid>"e[2]"</wlan_essid>" }
/Freq/{ split($3, f, " "); printf "<wlan_frequency>"f[1]f[2]"</wlan_frequency>" }
/Tx-Power/{ split($0, p, "="); sub(/[[:space:]]*$/, "", p[2]); printf "<wlan_tx_power>"p[2]"</wlan_tx_power>" }
')
interface_data=$interface_data$(iw dev "${iface}" info 2>/dev/null | awk '
/ssid/{ split($0, s, " "); printf "<wlan_ssid>"s[2]"</wlan_ssid>" }
/type/ { split($0, t, " "); printf "<wlan_type>"t[2]"</wlan_type>" }
/channel/{ split($0, c, " "); printf "<wlan_channel>"c[2]"</wlan_channel>" }
/width/{ split($0, w, ": "); sub(/ .*/, "", w[2]); printf "<wlan_width>"w[2]"</wlan_width>" }
')
interface_data=$interface_data"</$iface>"
done
echo -n "<interface_data>$interface_data</interface_data>"
exit 0