[network/upnpc_] Cleanup shell code and centralise call to upnpc

Signed-off-by: Olivier Mehani <shtrom@ssji.net>
This commit is contained in:
Olivier Mehani 2019-09-25 22:16:16 +10:00 committed by Lars Kruse
parent f6c627bd56
commit 81bf32a5a5
1 changed files with 45 additions and 30 deletions

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -u
# -*- sh -*-
: << =cut
@ -16,7 +16,7 @@ router using UPnP. It can monitor the following aspects, and plot them as separa
=head1 APPLICABLE SYSTEMS
Linux systems with upnpc installed.
Linux systems with upnpc installed (miniupnpc package).
=head1 CONFIGURATION
@ -24,11 +24,13 @@ None needed.
=head1 AUTHOR
Olivier Mehani <shtrom+munin@ssji.net>
Olivier Mehani
Copyright (C) 2016,2019 Olivier Mehani <shtrom+munin@ssji.net>
=head1 LICENSE
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-License-Identifier: GPL-3.0-or-later
=head1 MAGIC MARKERS
@ -37,21 +39,38 @@ SPDX-License-Identifier: GPL-2.0-or-later
=cut
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
set -x
fi
if ! command -v upnpc >/dev/null; then
echo "upnpc not found (miniupnpc package)" >&2
exit 1
fi
PLUGIN_NAME="$(basename "${0}")"
MODE="$(echo "${PLUGIN_NAME}" | sed 's/.*_//')"
DATA="$(upnpc -s)"
SUPPORTED_MODES=$(
echo "${DATA}" | sed -n " \
s/.*Bytes.*/traffic/p; \
s/.*MaxBitRate.*/bitrate/p; \
s/.*Packets.*/pkts/p; \
s/.*uptime=.*/uptime/p; \
")
autoconf() {
which upnpc >/dev/null && upnpc -s >/dev/null 2>&1 && echo yes || echo "no (No upnpc or no UPnP router)"
test -n "${DATA}" && echo yes || echo "no (No UPnP router detected)"
}
suggest () {
upnpc -s | sed -n " \
s/.*uptime=.*/uptime/p; \
s/.*MaxBitRate.*/bitrate/p; \
s/.*Bytes.*/traffic/p; \
s/.*Packets.*/pkts/p; \
"
for mode in ${SUPPORTED_MODES}; do
echo "${mode}"
done
}
config () {
case $1 in
case ${1} in
"uptime")
cat << EOF
graph_title Uplink connection uptime
@ -71,11 +90,7 @@ graph_args --base 1000 -l 0
graph_category network
graph_vlabel bitrate down (-) / up (+)
down.label bps
down.warning 4194304:
down.critical 1048576:
up.label bps
up.warning 524288:
up.critical 131072:
down.graph no
up.negative down
EOF
@ -114,35 +129,35 @@ down.graph no
up.negative down
EOF
;;
"*")
echo "$0: unknown mode '$1'" >&2
*)
echo "unknown mode '${1}'" >&2
exit 1
;;
esac
}
fetch () {
case $1 in
case "${1}" in
"uptime")
upnpc -s | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
echo "${DATA}" | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
;;
"bitrate")
upnpc -s | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
echo "${DATA}" | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
;;
"traffic")
upnpc -s | sed -n "s/^Bytes:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
echo ${DATA} | sed -n "s/^Bytes:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
;;
"pkts")
upnpc -s | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
echo "${DATA}" | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
;;
"*")
echo "$0: unknown mode '$1'" >&2
*)
echo "unknown mode '${1}'" >&2
exit 1
;;
esac
}
mode=`echo $0 | sed 's/.*_//'`
case $1 in
case ${1:-} in
"autoconf")
autoconf
;;
@ -150,9 +165,9 @@ case $1 in
suggest
;;
"config")
config $mode
config "${MODE}"
;;
*)
fetch $mode
fetch "${MODE}"
;;
esac