Projet

Général

Profil

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

root / plugins / network / upnpc_ @ 63505c50

Historique | Voir | Annoter | Télécharger (5,61 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,2019 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
		"traffic")
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
			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
down.type DERIVE
150
down.min 0
151
down.cdef down,8,*
152
up.label bps
153
up.type DERIVE
154
up.min 0
155
up.cdef up,8,*
156
down.graph no
157
up.negative down
158
${HOST_NAME}
159
EOF
160
			;;
161
		"pkts")
162
			# ${graph_period} is not a shell variable
163
			cat << EOF
164
graph_title Uplink packets${HOST_TITLE}
165
graph_args --base 1000 -l 0
166
graph_category network
167
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
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
${HOST_NAME}
181
EOF
182
			;;
183
		"multi")
184
			echo "${HOST_NAME}"
185
			# Don't repeat HOST_NAME in sub-configs
186
			HOST_NAME=""
187
			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
		*)
195
			echo "unknown mode '${1}'" >&2
196
			exit 1
197
			;;
198
	esac
199
}
200

    
201
fetch () {
202
	case "${1}" in
203
		"uptime")
204
		echo "${DATA}" | sed -n "s/.*uptime=\([0-9]\+\)s.*/uptime.value \1/p"
205
			;;
206
		"bitrate")
207
		echo "${DATA}" | sed -n "s/^MaxBitRateDown : \([0-9]\+\) bps.*MaxBitRateUp \([0-9]\+\) bps.*/down.value \1\nup.value \2/p"
208
			;;
209
		"traffic")
210
		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
			;;
217
		"pkts")
218
		echo "${DATA}" | sed -n "s/^Packets:\s*Sent:\s*\([0-9]\+\).*Recv:\s*\([0-9]\+\).*/up.value \1\ndown.value \2/p"
219
			;;
220
		"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
		*)
229
			echo "unknown mode '${1}'" >&2
230
			exit 1
231
			;;
232
	esac
233
}
234

    
235
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
case ${1:-} in
258
	"suggest")
259
		suggest
260
		;;
261
	"config")
262
		config "${MODE}"
263
		if [ "${MUNIN_CAP_DIRTYCONFIG:-0}" = "1" ]; then
264
			fetch "${MODE}"
265
		fi
266
		;;
267
	*)
268
		fetch "${MODE}"
269
		;;
270
esac