Debian Codegraph @

check_email_delivery/src/check_imap_quota

raw ◀ ▶
2 Debian patches touch this file
1 #!/usr/bin/perl
•2 use strict;
3 my $VERSION = '0.2';
4 my $COPYRIGHT = 'Copyright (C) 2005-2011 Jonathan Buhacoff <[email protected]>';
5 my $LICENSE = 'http://www.gnu.org/licenses/gpl.txt';
6 my %status = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3 );
7
8 #### IDEA AND INITIAL IMPLEMENTATION BASED ON CHECK_IMAP_RECEIVE WAS CONTRIBUTED BY JOHAN ROMME from THE NETHERLANDS 14 Oct 2011
9
10 # look for required modules
11 exit $status{UNKNOWN} unless load_modules(qw/Getopt::Long Mail::IMAPClient/);
12
13 BEGIN {
14 if( grep { /^--hires$/ } @ARGV ) {
15 eval "use Time::HiRes qw(time);";
16 warn "Time::HiRes not installed\n" if $@;
17 }
18 }
19
20 # get options from command line
21 Getopt::Long::Configure("bundling");
22 my $verbose = 0;
23 my $help = "";
24 my $help_usage = "";
25 my $show_version = "";
26 my $imap_server = "";
27 my $default_imap_port = "143";
28 my $default_imap_ssl_port = "993";
29 my $imap_port = "";
30 my $username = "";
31 my $password = "";
32 my $mailbox = "INBOX";
33 my $warntime = 15;
34 my $criticaltime = 30;
35 my $timeout = 60;
36 my $peek = "";
37 my $ssl = 0;
38 my $tls = 0;
39 my $time_hires = "";
40 my $ok;
41 $ok = Getopt::Long::GetOptions(
42 "V|version"=>\$show_version,
43 "v|verbose+"=>\$verbose,"h|help"=>\$help,"usage"=>\$help_usage,
44 "w|warning=i"=>\$warntime,"c|critical=i"=>\$criticaltime,"t|timeout=i"=>\$timeout,
45 # imap settings
46 "H|hostname=s"=>\$imap_server,"p|port=i"=>\$imap_port,
47 "U|username=s"=>\$username,"P|password=s"=>\$password, "m|mailbox=s"=>\$mailbox,
48 "ssl!"=>\$ssl, "tls!"=>\$tls,
49 # search settings
50 "peek!"=>\$peek,
51 # Time
52 "hires"=>\$time_hires,
53 );
54
55 if( $show_version ) {
56 print "$VERSION\n";
57 if( $verbose ) {
58 print "Default warning threshold: $warntime seconds\n";
59 print "Default critical threshold: $criticaltime seconds\n";
60 print "Default timeout: $timeout seconds\n";
61 }
62 exit $status{UNKNOWN};
63 }
64
65 if( $help ) {
66 exec "perldoc", $0 or print "Try `perldoc $0`\n";
67 exit $status{UNKNOWN};
68 }
69
70 my @required_module = ();
71 push @required_module, 'IO::Socket::SSL' if $ssl || $tls;
72 exit $status{UNKNOWN} unless load_modules(@required_module);
73
74 if( $help_usage
75 ||
76 ( $imap_server eq "" || $username eq "" || $password eq "" )
77 ) {
78 print "Usage: $0 -H host [-p port] -U username -P password [--imap-retries <tries>]\n";
79 exit $status{UNKNOWN};
80 }
81
82
83 # initialize
84 my $report = new PluginReport;
85 my $time_start = time;
86
87 # connect to IMAP server
88 print "connecting to server $imap_server\n" if $verbose > 2;
89 my $imap;
90 eval {
91 local $SIG{ALRM} = sub { die "exceeded timeout $timeout seconds\n" }; # NB: \n required, see `perldoc -f alarm`
92 alarm $timeout;
93
94 if( $ssl || $tls ) {
95 $imap_port = $default_imap_ssl_port unless $imap_port;
96 my $socket = IO::Socket::SSL->new("$imap_server:$imap_port");
97 die IO::Socket::SSL::errstr() unless $socket;
98 $socket->autoflush(1);
99 $imap = Mail::IMAPClient->new(Socket=>$socket, Debug => 0 );
100 $imap->State(Mail::IMAPClient->Connected);
101 $imap->_read_line() if "$Mail::IMAPClient::VERSION" le "2.2.9"; # necessary to remove the server's "ready" line from the input buffer for old versions of Mail::IMAPClient. Using string comparison for the version check because the numeric didn't work on Darwin and for Mail::IMAPClient the next version is 2.3.0 and then 3.00 so string comparison works
102 $imap->User($username);
103 $imap->Password($password);
104 $imap->login() or die "$@";
105 }
106 else {
107 $imap_port = $default_imap_port unless $imap_port;
108 $imap = Mail::IMAPClient->new(Debug => 0 );
109 $imap->Server("$imap_server:$imap_port");
110 $imap->User($username);
111 $imap->Password($password);
112 $imap->connect() or die "$@";
113 }
114
115 $imap->Peek(1) if $peek;
116 $imap->Ignoresizeerrors(1);
117
118 alarm 0;
119 };
120 if( $@ ) {
121 chomp $@;
122 print "IMAP QUOTA CRITICAL - Could not connect to $imap_server port $imap_port: $@\n";
123 exit $status{CRITICAL};
124 }
125 unless( $imap ) {
126 print "IMAP QUOTA CRITICAL - Could not connect to $imap_server port $imap_port: $@\n";
127 exit $status{CRITICAL};
128 }
129 my $time_connected = time;
130
131 my $quotaUsed;
132 my $quotaLimit;
133 my $quotaPercentage;
134 my $quotaPercentageWarning = 80;
135 my $quotaPercentageCritical = 90;
136 my $quotaMessage;
137
138 # look for the quota limits
139 my $tries = 0;
140 my @msgs;
141
142 eval {
143 my $k;
144 my @l = $imap->getquotaroot();
145 foreach $k (@l) {
146 print "$k\n" if $verbose > 2;
147 if ($k =~ /STORAGE +(\d+) +(\d+)/) {
148 $quotaUsed = $1;
149 $quotaLimit = $2;
150 }
151 }
152 if (!length($quotaUsed) && !length($quotaLimit)) {
153 print "no answer from imap host\n" if $verbose > 2;
154 } elsif (!length($quotaUsed) || !length($quotaLimit) {
155 print "incorrect answer from imap host\n";
•156 $imap->close();
157 exit $status{UNKNOWN};
158 } else {
159 $quotaPercentage = sprintf("%.1f", (100 * $quotaUsed) / $quotaLimit);
160 $quotaMessage = "$quotaUsed $quotaLimit - $quotaPercentage%";
161 }
162 };
163 if( $@ ) {
164 chomp $@;
165 print "IMAP QUOTA CRITICAL - Could not check quota at $imap_server port $imap_port: $@\n";
166 exit $status{CRITICAL};
167 }
168
169
170 # disconnect from IMAP server
171 print "disconnecting from server\n" if $verbose > 2;
172 $imap->logout();
173
174 # calculate elapsed time and issue warnings
175 my $time_end = time;
176 my $elapsedtime = $time_end - $time_start;
177 $report->{seconds} = $elapsedtime;
178
179 # print report and exit with known status
180
181 if($quotaPercentage >= $quotaPercentageCritical) {
182 print "IMAP QUOTA CRITICAL - $quotaMessage\n";
183 exit $status{CRITICAL};
184 }
185 if($quotaPercentage >= $quotaPercentageWarning) {
186 print "IMAP QUOTA WARNING - $quotaMessage\n";
187 exit $status{WARNING};
188 }
189 print "IMAP QUOTA OK - $quotaMessage\n";
190 exit $status{OK};
191
192
193 # utility to load required modules. exits if unable to load one or more of the modules.
194 sub load_modules {
195 my @missing_modules = ();
196 foreach( @_ ) {
197 eval "require $_";
198 push @missing_modules, $_ if $@;
199 }
200 if( @missing_modules ) {
201 print "Missing perl modules: @missing_modules\n";
202 return 0;
203 }
204 return 1;
205 }
206
207
208 # NAME
209 # PluginReport
210 # SYNOPSIS
211 # $report = new PluginReport;
212 # $report->{label1} = "value1";
213 # $report->{label2} = "value2";
214 # print $report->text(qw/label1 label2/);
215 package PluginReport;
216
217 sub new {
218 my ($proto,%p) = @_;
219 my $class = ref($proto) || $proto;
220 my $self = bless {}, $class;
221 $self->{$_} = $p{$_} foreach keys %p;
222 return $self;
223 }
224
225 sub text {
226 my ($self,@labels) = @_;
227 my @report = map { "$self->{$_} $_" } grep { defined $self->{$_} } @labels;
228 my $text = join(", ", @report);
229 return $text;
230 }
231
232
233 package main;
234 1;
235
236 __END__
237
238
239 =pod
240
241 =head1 NAME
242
243 check_imap_quota - connects to an IMAP account and checks the quota
244
245 =head1 SYNOPSIS
246
247 check_imap_quota -vV
248 check_imap_quota -?
249 check_imap_quota --help
250
251 =head1 OPTIONS
252
253 =over
254
255 =item --warning <seconds>
256
257 Warn if it takes longer than <seconds> to connect to the IMAP server. Default is 15 seconds.
258 Also known as: -w <seconds>
259
260 =item --critical <seconds>
261
262 Return a critical status if it takes longer than <seconds> to connect to the IMAP server. Default is 30 seconds.
263 See also: --capture-critical <messages>
264 Also known as: -c <seconds>
265
266 =item --timeout <seconds>
267
268 Abort with critical status if it takes longer than <seconds> to connect to the IMAP server. Default is 60 seconds.
269 The difference between timeout and critical is that, with the default settings, if it takes 45 seconds to
270 connect to the server then the connection will succeed but the plugin will return CRITICAL because it took longer
271 than 30 seconds.
272 Also known as: -t <seconds>
273
274 =item --hostname <server>
275
276 Address or name of the IMAP server. Examples: mail.server.com, localhost, 192.168.1.100
277 Also known as: -H <server>
278
279 =item --port <number>
280
281 Service port on the IMAP server. Default is 143. If you use SSL, default is 993.
282 Also known as: -p <number>
283
284 =item --username <username>
285
286 =item --password <password>
287
288 Username and password to use when connecting to IMAP server.
289 Also known as: -U <username> -P <password>
290
291 =item --mailbox <mailbox>
292
293 Use this option to specify the mailbox to search for messages. Default is INBOX.
294 Also known as: -m <mailbox>
295
296 =item --ssl
297
298 =item --nossl
299
300 Enable SSL protocol. Requires IO::Socket::SSL.
301
302 Using this option automatically changes the default port from 143 to 993. You can still
303 override this from the command line using the --port option.
304
305 Use the nossl option to turn off the ssl option.
306
307 =item --hires
308
309 Use the Time::HiRes module to measure time, if available.
310
311 =item --verbose
312
313 Display additional information. Useful for troubleshooting. Use together with --version to see the default
314 warning and critical timeout values.
315
316 If the selected mailbox was not found, you can use verbosity level 3 (-vvv) to display a list of all
317 available mailboxes on the server.
318
319 Also known as: -v
320
321 =item --version
322
323 Display plugin version and exit.
324 Also known as: -V
325
326 =item --help
327
328 Display this documentation and exit. Does not work in the ePN version.
329 Also known as: -h
330
331 =item --usage
332
333 Display a short usage instruction and exit.
334
335 =back
336
337 =head1 EXAMPLES
338
339 =head2 Report how many emails are in the mailbox
340
341 $ check_imap_receive -H mail.server.net --username mailuser --password mailpass
342 -s ALL --nodelete
343
344 IMAP RECEIVE OK - 1 seconds, 7 found
345
346 =head2 Report the email with the highest value
347
348 Suppose your mailbox has some emails from an automated script and that a message
349 from this script typically looks like this (abbreviated):
350
351 To: [email protected]
352 From: [email protected]
353 Subject: Results of Autoscript
354 Date: Wed, 09 Nov 2005 08:30:40 -0800
355 Message-ID: <[email protected]>
356
357 Homeruns 5
358
359 And further suppose that you are interested in reporting the message that has the
360 highest number of home runs, and also to leave this message in the mailbox for future
361 checks, but remove the other matching messages with lesser values:
362
363 $ check_imap_receive -H mail.server.net --username mailuser --password mailpass
364 -s SUBJECT -s "Results of Autoscript" --capture-max "Homeruns (\d+)" --nodelete-captured
365
366 IMAP RECEIVE OK - 1 seconds, 3 found, 1 captured, 5 max, 2 deleted
367
368 =head2 Troubleshoot your search parameters
369
370 Add the --nodelete and --imap-retries=1 parameters to your command line.
371
372 =head1 EXIT CODES
373
374 Complies with the Nagios plug-in specification:
375 0 OK The plugin was able to check the service and it appeared to be functioning properly
376 1 Warning The plugin was able to check the service, but it appeared to be above some "warning" threshold or did not appear to be working properly
377 2 Critical The plugin detected that either the service was not running or it was above some "critical" threshold
378 3 Unknown Invalid command line arguments were supplied to the plugin or the plugin was unable to check the status of the given hosts/service
379
380 =head1 NAGIOS PLUGIN NOTES
381
382 Nagios plugin reference: http://nagiosplug.sourceforge.net/developer-guidelines.html
383
384 This plugin does NOT use Nagios DEFAULT_SOCKET_TIMEOUT (provided by utils.pm as $TIMEOUT) because
385 the path to utils.pm must be specified completely in this program and forces users to edit the source
386 code if their install location is different (if they realize this is the problem). You can view
387 the default timeout for this module by using the --verbose and --version options together. The
388 short form is -vV.
389
390 Other than that, it attempts to follow published guidelines for Nagios plugins.
391
392 =head1 SEE ALSO
393
394 http://nagios.org/
395 http://search.cpan.org/~djkernen/Mail-IMAPClient-2.2.9/IMAPClient.pod
396 http://search.cpan.org/~markov/Mail-IMAPClient-3.00/lib/Mail/IMAPClient.pod
397
398 =head1 CHANGES
399
400 Fri Nov 11 04:53:09 AST 2011
401 + version 0.1 created with quota code contributed by Johan Romme
402
403 Tue Dec 20 17:38:04 PST 2011
404 + fixed bug where a quota of 0 was reported as an incorrect response from the server, thanks to Eike Arndt
405 + version 0.2
406
407 =head1 AUTHOR
408
409 Jonathan Buhacoff <[email protected]>
410
411 =head1 COPYRIGHT AND LICENSE
412
413 Copyright (C) 2011 Jonathan Buhacoff
414
415 This program is free software; you can redistribute it and/or modify
416 it under the terms of the GNU General Public License as published by
417 the Free Software Foundation; either version 3 of the License, or
418 (at your option) any later version.
419
420 This program is distributed in the hope that it will be useful,
421 but WITHOUT ANY WARRANTY; without even the implied warranty of
422 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
423 GNU General Public License for more details.
424
425 You should have received a copy of the GNU General Public License
426 along with this program. If not, see <http://www.gnu.org/licenses/>.
427
428 http://www.gnu.org/licenses/gpl.txt
429
430 =cut
431