Projet

Général

Profil

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

root / plugins / mail / imap_bandwidth @ 8589c6df

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

1 0aa2b8d1 Lars Kruse
#!/bin/sh
2 ea382ede Lars Kruse
#
3
# Revision 1.1  2012/02/26 03:43:27
4
# Improved labels
5
#
6
# Revision 1.0  2012/02/25 21:31:16
7
# Initial release
8
#
9 0aa2b8d1 Lars Kruse
10
: <<=cut
11
=head1 NAME
12
13
imap_bandwidth - Munin plugin to measure the current bandwidth of one or more remote IMAP servers.
14
15
=head1 APPLICABLE SYSTEMS
16
17
Any Linux system with the package "isync" (or "mbsync") installed.
18
19
=head1 CONFIGURATION
20
21
This configuration section shows a usable example for two imap servers:
22
   [imap_bandwidth]
23
   env.imap_servers			internal=imap-internal.example.org external=imap.example.org
24
   env.cert_file			/etc/munin-imap-cert.pem
25
   env.username			foo_user
26
   env.password			secret
27
   env.transfer_volume_kbyte	100
28
   env.use_ssl				yes
29
30
31
Both "use_ssl" and "transfer_volume_kbyte" are optional and default to the above
32
values.
33
All other parameters are required.
34
35
Generate the certificate file by running "mbsync-get-cert imap.example.org".
36
37
"imap_servers" is a space-separated list of key=value combinations. "key" is
38
used as a label in munin graphs. "value" is the full hostname or IP of the IMAP
39
server. All IMAP servers need to share the same authentication database (i.e.
40
accept the same username/password).
41
42
Reduce the "transfer_volume_kbyte" parameter if you need to minimize traffic.
43
44
Maybe you need to specify the "timeout" setting for this plugin if it takes
45
longer than munin's default timeout.
46
47
48
=head1 INTERPRETATION
49
50
The plugin simply shows the average bandwidth during one IMAP upload and one
51 8589c6df klemens
IMAP download of a file containing random bytes.
52 0aa2b8d1 Lars Kruse
53
You need to be aware that this measurement obviously increases the load on
54
your IMAP servers for the duration of the measurement.
55
56
You also need to be aware of the safety implications imposed by storing
57
sensitive information (username and password combinations) on your monitoring
58
server in plaintext.
59
60
=head1 VERSION
61
62
Version 1.0
63
64
=head1 BUGS
65
66
None known
67
68
Set the environment variable DEBUG=1 if you need to investigate problems.
69
70
=head1 AUTHOR
71
72
Lars Kruse <devel@sumpfralle.de>
73
74
=head1 LICENSE
75
76
GPLv3 or higher
77
78
=cut
79
80
81
set -eu
82
83
#%# family=auto
84
#%# capabilities=autoconf
85
86
TRANSFER_SIZE=${transfer_volume_kbyte:-100}
87
USE_SSL=${use_ssl:-yes}
88
IMAP_USERNAME=${username}
89
IMAP_PASSWORD=${password}
90
CERT_FILE=${cert_file}
91
92
# example value:
93
#    internal=imap-internal.example.org external=imap.example.org
94
SERVERS="$imap_servers"
95
96
97
if test -n "${DEBUG:-}"; then
98
	TRANSFER_SIZE=20
99
	set -x
100
fi
101
102
103
. $MUNIN_LIBDIR/plugins/plugin.sh
104
105
106
if [ "$1" = "autoconf" ]; then
107
	if ( which mbsync >/dev/null 2>&1 ); then
108
		echo yes
109
		exit 0
110
	else
111
		echo "no (could not run \"mbsync\")"
112
		exit 0
113
	fi
114
fi
115
116
if [ "$1" = "config" ]; then
117 ea382ede Lars Kruse
	echo 'graph_title IMAP bandwidth'
118
	echo 'graph_vlabel to (+) / from (-) server [bit/s]'
119 0aa2b8d1 Lars Kruse
	echo 'graph_category network'
120
	for item in $SERVERS; do
121
		key="$(echo "$item" | cut -f 1 -d =)"
122
		clean_name="$(clean_fieldname "$key")"
123
		echo "download_${clean_name}.graph no"
124 ea382ede Lars Kruse
		echo "download_${clean_name}.label download"
125 0aa2b8d1 Lars Kruse
		echo "upload_${clean_name}.label $key"
126
		echo "upload_${clean_name}.negative download_${clean_name}"
127
	 done
128
	exit 0
129
fi
130
131
132
create_dummy_file() {
133
	dd if=/dev/urandom "of=$DUMMY_FILENAME" bs=1K count=${TRANSFER_SIZE} 2>/dev/null
134
}
135
136
is_dummy_file_missing() {
137
	test ! -e "$DUMMY_FILENAME"
138
}
139
140
remove_mail_files() {
141
	get_mail_files | while read fname; do rm "$fname"; done
142
}
143
144
get_mail_files() {
145
	find "$MAILDIR" -type f | grep -v "/\."
146
}
147
148
# run the synchronization
149
run_sync() {
150
	if test -n "${DEBUG:-}"; then
151
		echo yes | mbsync --config "$SYNCRC" sync || true
152
	else
153
		echo yes | mbsync --config "$SYNCRC" sync >/dev/null 2>/dev/null || true
154
	fi
155
}
156
157
# run the synchronization and determine the duration of this operation
158
speed_sync() {
159
	start=$(date +%s%N)
160
	run_sync
161
	end=$(date +%s%N)
162
	# did we wrap a minute?
163
	test "$end" -lt "$start" && end=$((end + 60 * 1000000000))
164
	delay=$((end - start))
165
	# use "bit" multiplier
166
	echo "$((8 * TRANSFER_SIZE * 1024 * 1000000000 / delay))"
167
}
168
169
for item in $SERVERS; do
170
	key="$(echo "$item" | cut -f 1 -d =)"
171
	host="$(echo "$item" | cut -f 2- -d =)"
172
	clean_name="$(clean_fieldname "$key")"
173
	MAILDIR="$(mktemp -d)"
174
	# this file needs to include a dot at the beginning - otherwise it gets cleaned up ...
175
	SYNCRC="$MAILDIR/.mbsyncrc"
176
	SYNC_STATE_FILE_PREFIX="$MAILDIR/.syncstate-"
177
178
	cat - >"$SYNCRC" <<- EOF
179
	SyncState	$SYNC_STATE_FILE_PREFIX
180
	Expunge Both
181
182
	MaildirStore local
183
	Path	$MAILDIR
184
	Inbox	$MAILDIR
185
186
	IMAPStore remote
187
	Host $host
188
	UseIMAPS $USE_SSL
189
	User $IMAP_USERNAME
190
	Pass $IMAP_PASSWORD
191
	CertificateFile $CERT_FILE
192
193
	Channel sync
194
	Master	:local:
195
	Slave	:remote:
196
EOF
197
198
	mkdir "$MAILDIR/new" "$MAILDIR/tmp" "$MAILDIR/cur"
199
	DUMMY_FILENAME="$MAILDIR/new/$(date +%N)"
200
	# download all existing files
201
	run_sync
202
	# remove all local files -> to be purged remotely later
203
	remove_mail_files
204
	# create dummy file for upload
205
	create_dummy_file "$MAILDIR"
206
	output="upload_${clean_name}.value $(speed_sync)"
207
	is_dummy_file_missing && echo "$output" || echo >&2 "upload failed"
208
	# remove local file
209
	remove_mail_files "$MAILDIR"
210
	# persuade mbsync that we have never seen the dummy file ...
211
	rm "$SYNC_STATE_FILE_PREFIX"*
212
	output="download_${clean_name}.value $(speed_sync)"
213
	get_mail_files | grep -q . && echo "$output" || echo >&2 "download failed"
214
	# remove the new file from the imap server
215
	remove_mail_files
216
	run_sync
217
	# clean up
218
	rm -r "$MAILDIR"
219
 done