Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / upnpc_ @ d7f54f3e

Historique | Voir | Annoter | Télécharger (5,61 ko)

1 81bf32a5 Olivier Mehani
#!/bin/sh -u
2 5b0aad06 Olivier Mehani
# -*- sh -*-
3
4
: << =cut
5
6
=head1 NAME
7
8
upnpc_ - Plugin to monitor routers via UPnP
9
10 20de5307 Olivier Mehani
This plugin uses the upnpc utility (package miniupnpc in Debian), to monitor an
11 076545b5 Olivier Mehani
router using UPnP. It can monitor the following aspects, and plot them as
12
separate graphs, or a single multigraph (if linked at upnpc or upnpc_multi:
13 20de5307 Olivier Mehani
* uptime: how long the link has been up;
14
* bitrate: the up and downlink bitrate (e.g., sync speed for DSL);
15
* traffic: the actual up and downstream traffic rate;
16
* pkts: the number of packets coming in and out.
17
18 5b0aad06 Olivier Mehani
=head1 APPLICABLE SYSTEMS
19
20 81bf32a5 Olivier Mehani
Linux systems with upnpc installed (miniupnpc package).
21 5b0aad06 Olivier Mehani
22
=head1 CONFIGURATION
23
24 3212af2e Olivier Mehani
If you do not want to show the link maximum bitrates, add the following
25
plugin-configuration:
26
27
  [upnpc*]
28
    env.traffic_remove_max true
29 5b0aad06 Olivier Mehani
30 282b32b0 Olivier Mehani
You can display the graph on another host (e.g., the actual router) than the
31
one running upnpc. To do so, first configure the plugin to use a different
32
hostname.
33
34
    env.host_name router
35
36
Then configure munin (in /etc/munin/munin-conf or /etc/munin/munin-conf.d), to
37
support a new host.
38
39
  [example.net;router]
40
    address 127.0.0.1
41
    use_node_name no
42
43 5b0aad06 Olivier Mehani
=head1 AUTHOR
44
45 81bf32a5 Olivier Mehani
Olivier Mehani
46
47
Copyright (C) 2016,2019 Olivier Mehani <shtrom+munin@ssji.net>
48 5b0aad06 Olivier Mehani
49
=head1 LICENSE
50
51 81bf32a5 Olivier Mehani
SPDX-License-Identifier: GPL-3.0-or-later
52 5b0aad06 Olivier Mehani
53
=head1 MAGIC MARKERS
54
55
 #%# family=auto
56
 #%# capabilities=autoconf suggest
57
58
=cut
59
60 81bf32a5 Olivier Mehani
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
61
    set -x
62
fi
63
64
PLUGIN_NAME="$(basename "${0}")"
65
MODE="$(echo "${PLUGIN_NAME}" | sed 's/.*_//')"
66 cf747932 Olivier Mehani
# If called without a mode, default to multigraph
67
[ "$MODE" = "upnpc" ] && MODE="multi"
68
69
get_data() {
70
	if ! command -v upnpc >/dev/null; then
71
		echo "upnpc not found (miniupnpc package)" >&2
72
		exit 1
73
	fi
74
75
	upnpc -s
76
}
77
78
get_supported_modes() {
79
	DATA=$1
80 81bf32a5 Olivier Mehani
	echo "${DATA}" | sed -n " \
81
		s/.*Bytes.*/traffic/p; \
82
		s/.*Packets.*/pkts/p; \
83
		s/.*uptime=.*/uptime/p; \
84 cf747932 Olivier Mehani
		"
85
}
86 81bf32a5 Olivier Mehani
87 5b0aad06 Olivier Mehani
autoconf() {
88 cf747932 Olivier Mehani
	if ! command -v upnpc >/dev/null; then
89
		echo "no (upnpc not found [miniupnpc package])"
90
		return
91
	fi
92
	upnpc -s 2>/dev/null | grep -q 'List.*devices.*found' && echo yes \
93
		|| echo "no (No UPnP router detected)"
94 5b0aad06 Olivier Mehani
}
95
96 076545b5 Olivier Mehani
97 5b0aad06 Olivier Mehani
suggest () {
98 81bf32a5 Olivier Mehani
	for mode in ${SUPPORTED_MODES}; do
99
		echo "${mode}"
100
	done
101 076545b5 Olivier Mehani
	echo "multi"
102 5b0aad06 Olivier Mehani
}
103
104
config () {
105 81bf32a5 Olivier Mehani
	case ${1} in
106 5b0aad06 Olivier Mehani
		"uptime")
107
			cat << EOF
108 282b32b0 Olivier Mehani
graph_title Uplink connection uptime${HOST_TITLE}
109 5b0aad06 Olivier Mehani
graph_args -l 0
110
graph_category network
111
graph_scale no
112
graph_vlabel uptime in hours
113
uptime.label uptime
114
uptime.draw AREA
115
uptime.cdef uptime,3600,/
116 282b32b0 Olivier Mehani
${HOST_NAME}
117 5b0aad06 Olivier Mehani
EOF
118
			;;
119
		"bitrate")
120
			cat << EOF
121 282b32b0 Olivier Mehani
graph_title [DEPRECATED] Uplink bitrate${HOST_TITLE}
122 5b0aad06 Olivier Mehani
graph_args --base 1000 -l 0
123
graph_category network
124
graph_vlabel bitrate down (-) / up (+)
125
down.label bps
126
up.label bps
127
down.graph no
128
up.negative down
129 282b32b0 Olivier Mehani
${HOST_NAME}
130 5b0aad06 Olivier Mehani
EOF
131
			;;
132
		"traffic")
133 01edaf00 Olivier Mehani
			cat << EOF
134 282b32b0 Olivier Mehani
graph_title Uplink traffic${HOST_TITLE}
135 3212af2e Olivier Mehani
graph_args --base 1000 -l 0
136 5b0aad06 Olivier Mehani
graph_category network
137 6ad466b1 Olivier Mehani
graph_vlabel bits per second in (-) / out (+)
138 3212af2e Olivier Mehani
EOF
139
			if [ "${traffic_remove_max:-false}" != 'true' ]; then
140
				cat << EOF
141
maxdown.label bps (max)
142
maxup.label bps (max)
143
maxdown.graph no
144
maxup.negative maxdown
145
EOF
146
			fi
147
			cat << EOF
148
down.label bps
149 5b0aad06 Olivier Mehani
down.type DERIVE
150
down.min 0
151 3212af2e Olivier Mehani
down.cdef down,8,*
152
up.label bps
153 5b0aad06 Olivier Mehani
up.type DERIVE
154
up.min 0
155 3212af2e Olivier Mehani
up.cdef up,8,*
156 5b0aad06 Olivier Mehani
down.graph no
157
up.negative down
158 282b32b0 Olivier Mehani
${HOST_NAME}
159 5b0aad06 Olivier Mehani
EOF
160
			;;
161
		"pkts")
162 92db831b Olivier Mehani
			# ${graph_period} is not a shell variable
163 01edaf00 Olivier Mehani
			cat << EOF
164 282b32b0 Olivier Mehani
graph_title Uplink packets${HOST_TITLE}
165 5b0aad06 Olivier Mehani
graph_args --base 1000 -l 0
166
graph_category network
167 01edaf00 Olivier Mehani
EOF
168
			# ${graph_period} is not a shell variable
169
			# shellcheck disable=SC2016
170
			echo 'graph_vlabel packets in (-) / out (+) per ${graph_period}'
171
			cat << EOF
172 5b0aad06 Olivier Mehani
down.label pps
173
down.type DERIVE
174
down.min 0
175
up.label pps
176
up.type DERIVE
177
up.min 0
178
down.graph no
179
up.negative down
180 282b32b0 Olivier Mehani
${HOST_NAME}
181 5b0aad06 Olivier Mehani
EOF
182
			;;
183 cf747932 Olivier Mehani
		"multi")
184 282b32b0 Olivier Mehani
			echo "${HOST_NAME}"
185
			# Don't repeat HOST_NAME in sub-configs
186
			HOST_NAME=""
187 076545b5 Olivier Mehani
			echo "multigraph ${PLUGIN_NAME}"
188
			config "traffic"
189
			for mode in ${SUPPORTED_MODES}; do
190
				echo "multigraph ${PLUGIN_NAME}.${mode}"
191
				config "${mode}"
192
			done
193
			;;
194 81bf32a5 Olivier Mehani
		*)
195
			echo "unknown mode '${1}'" >&2
196 5b0aad06 Olivier Mehani
			exit 1
197 81bf32a5 Olivier Mehani
			;;
198 17f78427 Lars Kruse
	esac
199 5b0aad06 Olivier Mehani
}
200
201
fetch () {
202 81bf32a5 Olivier Mehani
	case "${1}" in
203 5b0aad06 Olivier Mehani
		"uptime")
204 81bf32a5 Olivier Mehani
		echo "${DATA}" | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
205 5b0aad06 Olivier Mehani
			;;
206
		"bitrate")
207 81bf32a5 Olivier Mehani
		echo "${DATA}" | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
208 5b0aad06 Olivier Mehani
			;;
209
		"traffic")
210 3212af2e Olivier Mehani
		echo "${DATA}" | sed -n "
211
		s/^Bytes:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
212
		if [ "${traffic_remove_max:-false}" != 'true' ]; then
213
			echo "${DATA}" | sed -n "
214
			s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/maxdown.value \1\nmaxup.value \2/p"
215
		fi
216 5b0aad06 Olivier Mehani
			;;
217
		"pkts")
218 81bf32a5 Olivier Mehani
		echo "${DATA}" | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
219 5b0aad06 Olivier Mehani
			;;
220 076545b5 Olivier Mehani
		"multi"|"upnpc")
221
			echo "multigraph ${PLUGIN_NAME}"
222
			fetch "traffic"
223
			for mode in ${SUPPORTED_MODES}; do
224
				echo "multigraph ${PLUGIN_NAME}.${mode}"
225
				fetch "${mode}"
226
			done
227
			;;
228 81bf32a5 Olivier Mehani
		*)
229
			echo "unknown mode '${1}'" >&2
230 5b0aad06 Olivier Mehani
			exit 1
231 81bf32a5 Olivier Mehani
			;;
232 17f78427 Lars Kruse
	esac
233 5b0aad06 Olivier Mehani
}
234
235 cf747932 Olivier Mehani
if [ "${1:-}" = "autoconf" ]; then
236
	autoconf
237
	exit 0
238
fi
239
240
# do data-based detection here, rather than in
241
# config() as we don't want to do this multiple times
242
# when the function calls itself for multigraphs
243
DATA=$(get_data)
244
SUPPORTED_MODES=$(get_supported_modes "${DATA}")
245
246
HOST=${host_name:-}
247
HOST_TITLE=""
248
HOST_NAME="host_name ${HOST}"
249
if [ -z "${HOST}" ]; then
250
	HOST=$(echo "${DATA}" | sed -n "s#.*desc: http://\([^/:]\+\).*#\1#p")
251
	# Only add the host name to the title if autodetected
252
	HOST_TITLE=" ($HOST)"
253
	# ...but not as a separate host
254
	HOST_NAME=""
255
fi
256
257 81bf32a5 Olivier Mehani
case ${1:-} in
258 5b0aad06 Olivier Mehani
	"suggest")
259
		suggest
260
		;;
261
	"config")
262 81bf32a5 Olivier Mehani
		config "${MODE}"
263 5382722f Olivier Mehani
		if [ "${MUNIN_CAP_DIRTYCONFIG:-0}" = "1" ]; then
264
			fetch "${MODE}"
265
		fi
266 5b0aad06 Olivier Mehani
		;;
267
	*)
268 81bf32a5 Olivier Mehani
		fetch "${MODE}"
269 5b0aad06 Olivier Mehani
		;;
270
esac