1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-25 10:28:36 +00:00
Munin-Contrib/plugins/network/upnpc_
Olivier Mehani 81bf32a5a5 [network/upnpc_] Cleanup shell code and centralise call to upnpc
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
2019-10-03 17:28:04 +02:00

173 lines
3.2 KiB
Bash
Executable file

#!/bin/sh -u
# -*- sh -*-
: << =cut
=head1 NAME
upnpc_ - Plugin to monitor routers via UPnP
This plugin uses the upnpc utility (package miniupnpc in Debian), to monitor an
router using UPnP. It can monitor the following aspects, and plot them as separate graphs:
* uptime: how long the link has been up;
* bitrate: the up and downlink bitrate (e.g., sync speed for DSL);
* traffic: the actual up and downstream traffic rate;
* pkts: the number of packets coming in and out.
=head1 APPLICABLE SYSTEMS
Linux systems with upnpc installed (miniupnpc package).
=head1 CONFIGURATION
None needed.
=head1 AUTHOR
Olivier Mehani
Copyright (C) 2016,2019 Olivier Mehani <shtrom+munin@ssji.net>
=head1 LICENSE
SPDX-License-Identifier: GPL-3.0-or-later
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf suggest
=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() {
test -n "${DATA}" && echo yes || echo "no (No UPnP router detected)"
}
suggest () {
for mode in ${SUPPORTED_MODES}; do
echo "${mode}"
done
}
config () {
case ${1} in
"uptime")
cat << EOF
graph_title Uplink connection uptime
graph_args -l 0
graph_category network
graph_scale no
graph_vlabel uptime in hours
uptime.label uptime
uptime.draw AREA
uptime.cdef uptime,3600,/
EOF
;;
"bitrate")
cat << EOF
graph_title Uplink bitrate
graph_args --base 1000 -l 0
graph_category network
graph_vlabel bitrate down (-) / up (+)
down.label bps
up.label bps
down.graph no
up.negative down
EOF
;;
"traffic")
# ${graph_period} is not a shell variable
cat << 'EOF'
graph_title Uplink traffic
graph_args --base 1024 -l 0
graph_category network
graph_vlabel bytes in (-) / out (+) per ${graph_period}
down.label Bps
down.type DERIVE
down.min 0
up.label Bps
up.type DERIVE
up.min 0
down.graph no
up.negative down
EOF
;;
"pkts")
# ${graph_period} is not a shell variable
cat << 'EOF'
graph_title Uplink packets
graph_args --base 1000 -l 0
graph_category network
graph_vlabel packets in (-) / out (+) per ${graph_period}
down.label pps
down.type DERIVE
down.min 0
up.label pps
up.type DERIVE
up.min 0
down.graph no
up.negative down
EOF
;;
*)
echo "unknown mode '${1}'" >&2
exit 1
;;
esac
}
fetch () {
case "${1}" in
"uptime")
echo "${DATA}" | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
;;
"bitrate")
echo "${DATA}" | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
;;
"traffic")
echo ${DATA} | sed -n "s/^Bytes:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
;;
"pkts")
echo "${DATA}" | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
;;
*)
echo "unknown mode '${1}'" >&2
exit 1
;;
esac
}
case ${1:-} in
"autoconf")
autoconf
;;
"suggest")
suggest
;;
"config")
config "${MODE}"
;;
*)
fetch "${MODE}"
;;
esac