Projet

Général

Profil

Révision 42f09647

ID42f09647f26187ff8c5980f7c5caeb67f39fe029
Parent 97a1b232
Enfant 9a6d3e44

Ajouté par Lars Kruse il y a plus de 7 ans

wireless_signal_noise_: various improvements

  • add perldoc header
  • support dirty config
  • support "arp" (tool) and /proc/net/arp
  • fix autoconf handling

Voir les différences:

plugins/wifi/wireless_signal_noise_
1 1
#!/bin/sh
2
#
3
# Show current signal strength and noise for all connected peers of wifi devices.
4
# This plugin is suitable for wifi interfaces with a stable selection of peers
5
# (e.g. infrastructure).
6
# Author: Lars Kruse, devel@sumpfralle.de
7
# License: GPL v3 or later
8
#
9
# Requirements:
10
#  * "iwinfo" tool (alternatively: fall back to "iw" - with incomplete data)
11
#  * root privileges (for "iw" and "iwinfo")
12
#
13
# Magic markers
14
#%# capabilities=autoconf suggest
15
#%# family=auto
2

  
3
: << =cut
4

  
5
=head1 NAME
6

  
7
wireless_signal_noise_ - Show signal strength and noise for all connected peers of wifi interface
8

  
9
=head1 APPLICABLE SYSTEMS
10

  
11
This plugin is suitable for wifi interfaces with a stable selection of peers (e.g. infrastructure).
12
It is probably not useful for hotspot-like scenarios.
13

  
14
Information is parsed from the output of the tool "iwinfo" (OpenWrt) or "iw" (most systems,
15
incomplete information).
16

  
17

  
18
=head1 CONFIGURATION
19

  
20
Symlink this plugin with the name of the wifi interface added (e.g. "wlan0").
21

  
22
Root permissions are probably required for accessing "iw".
23

  
24
  [wireless_signal_noise_*]
25
  user root
26

  
27

  
28
=head1 VERSION
29

  
30
  1.1
31

  
32

  
33
=head1 AUTHOR
34

  
35
Lars Kruse <devel@sumpfralle.de>
36

  
37

  
38
=head1 LICENSE
39

  
40
GPLv3 or above
41

  
42

  
43
=head1 MAGIC MARKERS
44

  
45
  #%# family=auto
46
  #%# capabilities=autoconf suggest
47

  
48
=cut
16 49

  
17 50

  
18 51
set -eu
19 52

  
20 53

  
54
SCRIPT_PREFIX="wireless_signal_noise_"
55

  
56

  
21 57
# prefer "iwinfo" for information retrieval, if it is available
22 58
if which iwinfo >/dev/null; then
23 59
	# "iwinfo" has a stable output format but is only available on openwrt
......
34 70
	# TODO: there seems to be no way to retrieve the noise level via "iw"
35 71
	get_wifi_noise() { echo; }
36 72
fi
73
if which arp >/dev/null; then
74
	# openwrt does not provide 'arp' by default
75
	get_arp() { arp -n; }
76
else
77
	get_arp() { cat /proc/net/arp; }
78
fi
37 79

  
38 80

  
39 81
clean_fieldname() {
......
43 85

  
44 86
get_ip_for_mac() {
45 87
	local ip
46
	ip=$(arp -n | grep -iw "$1$" | awk '{print $1}' | sort | head -1)
88
	ip=$(get_arp | grep -iw "$1$" | awk '{print $1}' | sort | head -1)
47 89
	[ -n "$ip" ] && echo "$ip" && return 0
48 90
	# no IP found - return MAC instead
49 91
	echo "$1"
......
54 96
	local suffix
55 97
	local real_dev
56 98
	# pick the part after the basename of the real file
57
	suffix=$(basename "$0" | sed "s/^$(basename "$(readlink "$0")")//")
99
	suffix=$(basename "$0" | sed "s/^$SCRIPT_PREFIX//")
58 100
	for real_dev in $(get_wifi_interfaces); do
59 101
		[ "$suffix" != "$(clean_fieldname "$real_dev")" ] || echo "$real_dev"
60 102
	done | head -1
61 103
}
62 104

  
63 105

  
106
do_config() {
107
	local wifi
108
	wifi=$(get_wifi_device_from_suffix)
109
	[ -z "$wifi" ] && echo >&2 "Missing wifi: $wifi" && return 1
110
	echo "graph_title Wireless signal quality - $wifi"
111
	echo "graph_args --upper-limit 0"
112
	echo "graph_vlabel Signal and noise [dBm]"
113
	echo "graph_category network"
114
	echo "graph_info This graph shows the signal and noise for all wifi peers"
115
	echo "noise.label Noise floor"
116
	echo "noise.draw LINE"
117
	# sub graphs for all peers
118
	get_wifi_peers "$wifi" | while read -r mac signal; do
119
		fieldname=$(clean_fieldname "peer_${mac}")
120
		peer=$(get_ip_for_mac "$mac")
121
		echo "signal_${fieldname}.label $peer"
122
		echo "signal_${fieldname}.draw LINE"
123
	done
124
}
125

  
126

  
127
do_fetch() {
128
	local wifi
129
	local peer_data
130
	local noise
131
	wifi=$(get_wifi_device_from_suffix)
132
	[ -z "$wifi" ] && echo >&2 "Missing wifi: $wifi" && return 1
133
	peer_data=$(get_wifi_peers "$wifi")
134
	echo "$peer_data" | while read -r mac signal; do
135
		# ignore empty datasets
136
		[ -z "$signal" ] && continue
137
		fieldname=$(clean_fieldname "peer_${mac}")
138
		echo "signal_${fieldname}.value $signal"
139
	done
140
	noise=$(get_wifi_noise "$wifi")
141
	echo "noise.value ${noise:-U}"
142
}
143

  
144

  
64 145
ACTION="${1:-}"
65 146

  
66 147
case "$ACTION" in
67 148
	config)
68
		wifi=$(get_wifi_device_from_suffix)
69
		echo "graph_title Wireless signal quality - $wifi"
70
		echo "graph_args --upper-limit 0"
71
		echo "graph_vlabel Signal and noise [dBm]"
72
		echo "graph_category network"
73
		echo "graph_info This graph shows the signal and noise for all wifi peers"
74
		echo "noise.label Noise floor"
75
		echo "noise.draw LINE"
76
		# sub graphs for all peers
77
		get_wifi_peers "$wifi" | while read mac signal; do
78
			fieldname=$(clean_fieldname "peer_${mac}")
79
			peer=$(get_ip_for_mac "$mac")
80
			echo "signal_${fieldname}.label $peer"
81
			echo "signal_${fieldname}.draw LINE"
82
		done
149
		do_config || exit 1
150
		[ "${MUNIN_CAP_DIRTYCONFIG:-0}" = 1 ] && do_fetch
83 151
		;;
84 152
	autoconf)
85
		[ -z "$(get_wifi_interfaces)" ] && echo "no (no wifi interfaces found)" && exit 1
86
		echo "yes"
153
		if [ -z "$(get_wifi_interfaces)" ]; then
154
			echo "no (no wifi interfaces found)"
155
		else
156
			echo "yes"
157
		fi
87 158
		;;
88 159
	suggest)
89
		get_wifi_interfaces | while read ifname; do
160
		get_wifi_interfaces | while read -r ifname; do
90 161
			clean_fieldname "$ifname"
91 162
		done
92 163
		;;
93 164
	"")
94
		wifi=$(get_wifi_device_from_suffix)
95
		peer_data=$(get_wifi_peers "$wifi")
96
		echo "$peer_data" | while read mac signal; do
97
			# ignore empty datasets
98
			[ -z "$signal" ] && continue
99
			fieldname=$(clean_fieldname "peer_${mac}")
100
			echo "signal_${fieldname}.value $signal"
101
		done
102
		echo "noise.value $(get_wifi_noise "$wifi")"
165
		do_fetch
103 166
		;;
104 167
	*)
105
		echo >&2 "Invalid action (valid: config)"
168
		echo >&2 "Invalid action (valid: config / suggest / autoconf / <empty>)"
106 169
		echo >&2
170
		exit 2
107 171
		;;
108 172
esac

Formats disponibles : Unified diff