Projet

Général

Profil

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

root / plugins / network / upnpc_ @ f018bada

Historique | Voir | Annoter | Télécharger (6,36 ko)

1
#!/bin/sh -u
2
# -*- sh -*-
3

    
4
: << =cut
5

    
6
=head1 NAME
7

    
8
upnpc_ - Plugin to monitor routers via UPnP
9

    
10
This plugin uses the upnpc utility (package miniupnpc in Debian), to monitor an
11
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
* 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
=head1 APPLICABLE SYSTEMS
19

    
20
Linux systems with upnpc installed (miniupnpc package).
21

    
22
=head1 CONFIGURATION
23

    
24
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

    
30
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
=head1 AUTHOR
44

    
45
Olivier Mehani
46

    
47
Copyright (C) 2016--2021 Olivier Mehani <shtrom+munin@ssji.net>
48

    
49
=head1 LICENSE
50

    
51
SPDX-License-Identifier: GPL-3.0-or-later
52

    
53
=head1 MAGIC MARKERS
54

    
55
 #%# family=auto
56
 #%# capabilities=autoconf suggest
57

    
58
=cut
59

    
60
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
# 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
	echo "${DATA}" | sed -n " \
81
		s/.*Bytes.*/traffic/p; \
82
		s/.*Packets.*/pkts/p; \
83
		s/.*uptime=.*/uptime/p; \
84
		"
85
}
86

    
87
autoconf() {
88
	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
}
95

    
96

    
97
suggest () {
98
	for mode in ${SUPPORTED_MODES}; do
99
		echo "${mode}"
100
	done
101
	echo "multi"
102
}
103

    
104
config () {
105
	case ${1} in
106
		"uptime")
107
			cat << EOF
108
graph_title Uplink connection uptime${HOST_TITLE}
109
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
${HOST_NAME}
117
EOF
118
			;;
119
		"bitrate")
120
			cat << EOF
121
graph_title [DEPRECATED] Uplink bitrate${HOST_TITLE}
122
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
${HOST_NAME}
130
EOF
131
			;;
132
		"root")
133
			cat << EOF
134
graph_title Uplink traffic${HOST_TITLE}
135
graph_args --base 1000 -l 0
136
graph_category network
137
graph_vlabel bits per second in (-) / out (+)
138
EOF
139
			graph_order="down=traffic.down up=traffic.up"
140
			if [ "${traffic_remove_max:-false}" != 'true' ]; then
141
				graph_order="${graph_order} maxdown=traffic.maxdown maxup=traffic.maxup"
142
			fi
143
			echo "graph_order ${graph_order}"
144
			if [ "${traffic_remove_max:-false}" != 'true' ]; then
145
				cat << EOF
146
maxdown.label bps (max)
147
maxup.label bps (max)
148
maxdown.graph no
149
maxup.negative maxdown
150
EOF
151
			fi
152
			cat << EOF
153
down.label bps
154
down.cdef down,8,*
155
down.graph no
156
up.label bps
157
up.cdef up,8,*
158
up.negative down
159
${HOST_NAME}
160
EOF
161
			;;
162
		"traffic")
163
			cat << EOF
164
graph_title Uplink traffic${HOST_TITLE}
165
graph_args --base 1000 -l 0
166
graph_category network
167
graph_vlabel bits per second in (-) / out (+)
168
EOF
169
			if [ "${traffic_remove_max:-false}" != 'true' ]; then
170
				cat << EOF
171
maxdown.label bps (max)
172
maxup.label bps (max)
173
maxdown.graph no
174
maxup.negative maxdown
175
EOF
176
			fi
177
			cat << EOF
178
down.label bps
179
down.type DERIVE
180
down.min 0
181
down.cdef down,8,*
182
up.label bps
183
up.type DERIVE
184
up.min 0
185
up.cdef up,8,*
186
down.graph no
187
up.negative down
188
${HOST_NAME}
189
EOF
190
			;;
191
		"pkts")
192
			# ${graph_period} is not a shell variable
193
			cat << EOF
194
graph_title Uplink packets${HOST_TITLE}
195
graph_args --base 1000 -l 0
196
graph_category network
197
EOF
198
			# ${graph_period} is not a shell variable
199
			# shellcheck disable=SC2016
200
			echo 'graph_vlabel packets in (-) / out (+) per ${graph_period}'
201
			cat << EOF
202
down.label pps
203
down.type DERIVE
204
down.min 0
205
up.label pps
206
up.type DERIVE
207
up.min 0
208
down.graph no
209
up.negative down
210
${HOST_NAME}
211
EOF
212
			;;
213
		"multi")
214
			echo "${HOST_NAME}"
215
			# Don't repeat HOST_NAME in sub-configs
216
			HOST_NAME=""
217
			for mode in ${SUPPORTED_MODES}; do
218
				echo "multigraph ${PLUGIN_NAME}.${mode}"
219
				config "${mode}"
220
			done
221
			echo "multigraph ${PLUGIN_NAME}"
222
			config "root"
223
			;;
224
		*)
225
			echo "unknown mode '${1}'" >&2
226
			exit 1
227
			;;
228
	esac
229
}
230

    
231
fetch () {
232
	case "${1}" in
233
		"uptime")
234
		echo "${DATA}" | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
235
			;;
236
		"bitrate")
237
		echo "${DATA}" | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
238
			;;
239
		"root")
240
			# Nothing to do: all values loaned from the traffic graph
241
			;;
242
		"traffic")
243
		echo "${DATA}" | sed -n "
244
		s/^Bytes:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
245
		if [ "${traffic_remove_max:-false}" != 'true' ]; then
246
			echo "${DATA}" | sed -n "
247
			s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/maxdown.value \1\nmaxup.value \2/p"
248
		fi
249
			;;
250
		"pkts")
251
		echo "${DATA}" | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
252
			;;
253
		"multi"|"upnpc")
254
			for mode in ${SUPPORTED_MODES}; do
255
				echo "multigraph ${PLUGIN_NAME}.${mode}"
256
				fetch "${mode}"
257
			done
258
			echo "multigraph ${PLUGIN_NAME}"
259
			fetch "root"
260
			;;
261
		*)
262
			echo "unknown mode '${1}'" >&2
263
			exit 1
264
			;;
265
	esac
266
}
267

    
268
if [ "${1:-}" = "autoconf" ]; then
269
	autoconf
270
	exit 0
271
fi
272

    
273
# do data-based detection here, rather than in
274
# config() as we don't want to do this multiple times
275
# when the function calls itself for multigraphs
276
DATA=$(get_data)
277
SUPPORTED_MODES=$(get_supported_modes "${DATA}")
278

    
279
HOST=${host_name:-}
280
HOST_TITLE=""
281
HOST_NAME="host_name ${HOST}"
282
if [ -z "${HOST}" ]; then
283
	HOST=$(echo "${DATA}" | sed -n "s#.*desc: http://\([^/:]\+\).*#\1#p")
284
	# Only add the host name to the title if autodetected
285
	HOST_TITLE=" ($HOST)"
286
	# ...but not as a separate host
287
	HOST_NAME=""
288
fi
289

    
290
case ${1:-} in
291
	"suggest")
292
		suggest
293
		;;
294
	"config")
295
		config "${MODE}"
296
		if [ "${MUNIN_CAP_DIRTYCONFIG:-0}" = "1" ]; then
297
			fetch "${MODE}"
298
		fi
299
		;;
300
	*)
301
		fetch "${MODE}"
302
		;;
303
esac