Debian Codegraph @

check_email_delivery/src/docs/check_imap_receive.pod

raw
1
2
3
4 =pod
5
6 =head1 NAME
7
8 check_imap_receive - connects to and searches an IMAP account for messages
9
10 =head1 SYNOPSIS
11
12 check_imap_receive -vV
13 check_imap_receive -?
14 check_imap_receive --help
15
16 =head1 OPTIONS
17
18 =over
19
20 =item --warning <seconds>
21
22 Warn if it takes longer than <seconds> to connect to the IMAP server. Default is 15 seconds.
23 Also known as: -w <seconds>
24
25 =item --critical <seconds>
26
27 Return a critical status if it takes longer than <seconds> to connect to the IMAP server. Default is 30 seconds.
28 See also: --capture-critical <messages>
29 Also known as: -c <seconds>
30
31 =item --timeout <seconds>
32
33 Abort with critical status if it takes longer than <seconds> to connect to the IMAP server. Default is 60 seconds.
34 The difference between timeout and critical is that, with the default settings, if it takes 45 seconds to
35 connect to the server then the connection will succeed but the plugin will return CRITICAL because it took longer
36 than 30 seconds.
37 Also known as: -t <seconds>
38
39 =item --imap-check-interval <seconds>
40
41 How long to wait after searching for a matching message before searching again. Only takes effect
42 if no messages were found. Default is 5 seconds.
43
44 =item --imap-retries <number>
45
46 How many times to try searching for a matching message before giving up. If you set this to 0 then
47 messages will not be searched at all. Setting this to 1 means the plugin only tries once. Etc.
48 Default is 10 times.
49
50 =item --hostname <server>
51
52 Address or name of the IMAP server. Examples: mail.server.com, localhost, 192.168.1.100
53 Also known as: -H <server>
54
55 =item --port <number>
56
57 Service port on the IMAP server. Default is 143. If you use SSL, default is 993.
58 Also known as: -p <number>
59
60 =item --username <username>
61
62 =item --password <password>
63
64 Username and password to use when connecting to IMAP server.
65 Also known as: -U <username> -P <password>
66
67 =item --mailbox <mailbox>
68
69 Use this option to specify the mailbox to search for messages. Default is INBOX.
70 Also known as: -m <mailbox>
71
72 =item --search <string>
73
74 Use this option to filter the messages. Default is not to filter. You may (must) use this option
75 multiple times in order to create any valid IMAP search criteria. See the examples and see also
76 http://www.ietf.org/rfc/rfc2060.txt (look for section 6.4.4, the SEARCH command)
77
78 This is the way to find messages matching a given subject:
79 -s SUBJECT -s "a given subject"
80
81 You can use the following technique for any header, including Subject. To find "Header-Name: some value":
82 -s HEADER -s Header-Name -s "some value"
83
84 Modern IMAP servers that support rfc5032 extensions allow you to search for messages
85 older or younger than a number of seconds. So to find messages received in the past hour,
86 you can do:
87
88 -s YOUNGER -s 3600
89
90 Or to find messages received more than 5 minutes ago, you can do:
91
92 -s OLDER -s 300
93
94 Also known as: -s <string>
95
96 =item --download
97
98 =item --nodownload
99
100 This option causes all messages in the specified mailbox to be downloaded from the server
101 and searched locally. See --download-max if you only want to download a few messages.
102 Currently only the following RFC 2060 search criteria are supported:
103 TEXT, BODY, SUBJECT, HEADER, NOT, OR, SENTBEFORE, SENTON, SENTSINCE.
104
105 Requires Email::Simple to be installed. It is available on CPAN.
106
107 This option may be particularly useful to you if your mail server is slow to index
108 messages (like Exchange 2003), causing the plugin not to find them with IMAP SEARCH
109 even though they are in the inbox.
110
111 It's also useful if you're searching for messages that have been on the server for a
112 specified amount of time, like some minutes or hours, because the standard IMAP search
113 function only allows whole dates. For this, use the standard search keywords but you
114 can specify either just a date like in RFC 2060 or a date and a time.
115
116 If you use SENTBEFORE, SENTON, or SENTSINCE, you must have Date::Manip installed
117 on your system.
118
119
120 =item --download-max
121
122 Limits the number of messages downloaded from the server when the --download option is used.
123 Default is to download and search all messages.
124
125 =item --search-critical-min <messages>
126
127 This option will trigger a CRITICAL status if the number of messages found by the search criteria
128 is below the given number. Use in conjunction with --search.
129
130 This parameter defaults to 1 so that if no messages are found, the plugin will exit with a CRITICAL status.
131
132 If you want the original behavior where the plugin exits with a WARNING status when no messages are found,
133 set this parameter to 0.
134
135 =item --search-critical-max <messages>
136
137 This option will trigger a CRITICAL status if the number of messages found by the search criteria
138 is above the given number. Use in conjunction with --search.
139
140 This parameter defaults to -1 meaning it's disabled. If you set it to 10, the plugin will exit with
141 CRITICAL if it finds 11 messages. If you set it to 1, the plugin will exit with CRITICAL if it finds
142 any more than 1 message. If you set it to 0, the plugin will exit with CRITICAL if it finds any messages
143 at all. If you set it to -1 it will be disabled.
144
145 =item --search-warning-min <messages>
146
147 This option will trigger a WARNING status if the number of messages found by the search criteria
148 is below the given number. Use in conjunction with --search.
149
150 This parameter defaults to 1 so that if no messages are found, the plugin will exit with a WARNING status.
151
152 If you want to suppress the original behavior where the plugin exits with a WARNING status when no messages are found,
153 set this parameter to 0. When this parameter is 0, it means that you expect the mailbox not to have any messages.
154
155 =item --search-warning-max <messages>
156
157 This option will trigger a WARNING status if the number of messages found by the search criteria
158 is above the given number. Use in conjunction with --search.
159
160 This parameter defaults to -1 meaning it's disabled. If you set it to 10, the plugin will exit with
161 WARNING if it finds 11 messages. If you set it to 1, the plugin will exit with WARNING if it finds
162 any more than 1 message. If you set it to 0, the plugin will exit with WARNING if it finds any messages
163 at all. If you set it to -1 it will be disabled.
164
165 =item --capture-max <regexp>
166
167 In addition to specifying search arguments to filter the emails in the IMAP account, you can specify
168 a "capture-max" regexp argument and the eligible emails (found with search arguments)
169 will be compared to each other and the OK line will have the highest captured value.
170
171 The regexp is expected to capture a numeric value.
172
173 =item --capture-min <regexp>
174
175 In addition to specifying search arguments to filter the emails in the IMAP account, you can specify
176 a "capture-min" regexp argument and the eligible emails (found with search arguments)
177 will be compared to each other and the OK line will have the lowest captured value.
178
179 The regexp is expected to capture a numeric value.
180
181 =item --delete
182
183 =item --nodelete
184
185 Use the delete option to delete messages that matched the search criteria. This is useful for
186 preventing the mailbox from filling up with automated messages (from the check_smtp_send plugin, for example).
187 THE DELETE OPTION IS TURNED *ON* BY DEFAULT, in order to preserve compatibility with an earlier version.
188
189 Use the nodelete option to turn off the delete option.
190
191 =item --nodelete-captured
192
193 If you use both the capture-max and delete arguments, you can also use the nodelete-captured argument to specify that the email
194 with the highest captured value should not be deleted. This leaves it available for comparison the next time this plugin runs.
195
196 If you do not use the delete option, this option has no effect.
197
198 =item --ssl
199
200 =item --nossl
201
202 Enable SSL protocol. Requires IO::Socket::SSL.
203
204 Using this option automatically changes the default port from 143 to 993. You can still
205 override this from the command line using the --port option.
206
207 Use the nossl option to turn off the ssl option.
208
209 =item --ssl-ca-file
210
211 Use this to verify the server SSL certificate against a local .pem file. You'll need to
212 specify the path to the .pem file as the parameter.
213
214 You can use the imap_ssl_cert utility included in this distribution to connect to your IMAP
215 server and save its SSL certificates into your .pem file. Usage is like this:
216
217 imap_ssl_cert -H imap.server.com > ca_file.pem
218
219 Only applicable when --ssl option is enabled.
220
221 =item --template
222
223 =item --notemplate
224
225 Enable (or disable) processing of IMAP search parameters. Requires Text::Template and Date::Manip.
226
227 Use this option to apply special processing to IMAP search parameters that allows you to use the
228 results of arbitrary computations as the parameter values. For example, you can use this feature
229 to search for message received up to 4 hours ago.
230
231 Modern IMAP servers that support rfc5032 extensions allow searching with the YOUNGER and OLDER
232 criteria so a message received up to 4 hours ago is -s YOUNGER -s 14400. But if your mail server
233 doesn't support that, you could use the --template option to get similar functionality.
234
235 When you enable the --template option, each parameter you pass to the -s option is parsed by
236 Text::Template. See the Text::Template manual for more information, but in general any expression
237 written in Perl will work.
238
239 A convenience function called rfc2822dateHeader is provided to you so you can easily compute properly
240 formatted dates for use as search parameters. The rfc2822date function can take one or two
241 parameters itself: the date to format and an optional offset. To use the current time as a
242 search parameter, you can write this:
243
244 $ check_imap_receive ... --template -s HEADER -s Delivery-Date -s '{rfc2822dateHeader("now")}'
245
246 The output of {rfc2822dateHeader("now")} looks like this: Wed, 30 Sep 2009 22:44:03 -0700 and
247 is suitable for use with a date header, like HEADER Delivery-Date.
248
249 To use a time in the past relative to the current time or day, you can use a second convenience function
250 called rfc2822date and write this:
251
252 $ check_imap_receive ... --template -s SENTSINCE -s '{rfc2822date("now","-1 day")}'
253
254 The output of {rfc2822date("now","-1 day")} looks like this: 29-Sep-2009 and is suitable for use
255 with BEFORE, ON, SENTBEFORE, SENTON, SENTSINCE, and SINCE.
256
257 I have seen some email clients use a different format in the Date field,
258 like September 17, 2009 9:46:51 AM PDT. To specify an arbitrary format like this one, write this:
259
260 $ check_imap_receive ... --template -s HEADER -s Delivery-Date -s '{date("%B %e, %Y %i:%M:%S %p %Z","now","-4 hours")}'
261
262 You can use BEFORE, ON, SENTBEFORE, SENTON, SENTSINCE, or SINCE to search for messages that arrived
263 on, before, or after a given day but not on, before, or after a specific time on that day.
264
265 To search for messages that arrived on, before, or after a specific time you have to use the
266 Delivery-Date or another date field, like with -s HEADER -s Delivery-Date in the example above.
267
268 See the Date::Manip manual for more information on the allowed expressions for date and delta strings.
269
270 =item --hires
271
272 Use the Time::HiRes module to measure time, if available.
273
274 =item --verbose
275
276 Display additional information. Useful for troubleshooting. Use together with --version to see the default
277 warning and critical timeout values.
278
279 If the selected mailbox was not found, you can use verbosity level 3 (-vvv) to display a list of all
280 available mailboxes on the server.
281
282 Also known as: -v
283
284 =item --version
285
286 Display plugin version and exit.
287 Also known as: -V
288
289 =item --help
290
291 Display this documentation and exit. Does not work in the ePN version.
292 Also known as: -h
293
294 =item --usage
295
296 Display a short usage instruction and exit.
297
298 =back
299
300 =head1 EXAMPLES
301
302 =head2 Report how many emails are in the mailbox
303
304 $ check_imap_receive -H mail.server.net --username mailuser --password mailpass
305 -s ALL --nodelete
306
307 IMAP RECEIVE OK - 1 seconds, 7 found
308
309 =head2 Report the email with the highest value
310
311 Suppose your mailbox has some emails from an automated script and that a message
312 from this script typically looks like this (abbreviated):
313
314 To: [email protected]
315 From: [email protected]
316 Subject: Results of Autoscript
317 Date: Wed, 09 Nov 2005 08:30:40 -0800
318 Message-ID: <[email protected]>
319
320 Homeruns 5
321
322 And further suppose that you are interested in reporting the message that has the
323 highest number of home runs, and also to leave this message in the mailbox for future
324 checks, but remove the other matching messages with lesser values:
325
326 $ check_imap_receive -H mail.server.net --username mailuser --password mailpass
327 -s SUBJECT -s "Results of Autoscript" --capture-max "Homeruns (\d+)" --nodelete-captured
328
329 IMAP RECEIVE OK - 1 seconds, 3 found, 1 captured, 5 max, 2 deleted
330
331 =head2 Troubleshoot your search parameters
332
333 Add the --nodelete and --imap-retries=1 parameters to your command line.
334
335 =head1 EXIT CODES
336
337 Complies with the Nagios plug-in specification:
338 0 OK The plugin was able to check the service and it appeared to be functioning properly
339 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
340 2 Critical The plugin detected that either the service was not running or it was above some "critical" threshold
341 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
342
343 =head1 NAGIOS PLUGIN NOTES
344
345 Nagios plugin reference: http://nagiosplug.sourceforge.net/developer-guidelines.html
346
347 This plugin does NOT use Nagios DEFAULT_SOCKET_TIMEOUT (provided by utils.pm as $TIMEOUT) because
348 the path to utils.pm must be specified completely in this program and forces users to edit the source
349 code if their install location is different (if they realize this is the problem). You can view
350 the default timeout for this module by using the --verbose and --version options together. The
351 short form is -vV.
352
353 Other than that, it attempts to follow published guidelines for Nagios plugins.
354
355 =head1 SEE ALSO
356
357 http://nagios.org/
358 http://search.cpan.org/~djkernen/Mail-IMAPClient-2.2.9/IMAPClient.pod
359 http://search.cpan.org/~markov/Mail-IMAPClient-3.00/lib/Mail/IMAPClient.pod
360
361 =head1 CHANGES
362
363 Wed Oct 29 11:00:00 PST 2005
364 + version 0.1
365
366 Wed Nov 9 09:53:32 PST 2005
367 + added delete/nodelete option. deleting found messages is still default behavior.
368 + added capture-max option
369 + added nodelete-captured option
370 + added mailbox option
371 + added eval/alarm block to implement -c option
372 + now using an inline PluginReport package to generate the report
373 + copyright notice and GNU GPL
374 + version 0.2
375
376 Thu Apr 20 14:00:00 CET 2006 (by Johan Nilsson <johann (at) axis.com>)
377 + version 0.2.1
378 + added support for multiple polls of imap-server, with specified intervals
379
380 Tue Apr 24 21:17:53 PDT 2007
381 + now there is an alternate version (same but without embedded perl POD) that is compatible with the new new embedded-perl Nagios feature
382 + added patch from Benjamin Ritcey <[email protected]> for SSL support on machines that have an SSL-enabled
383 + version 0.2.3
384
385 Fri Apr 27 18:56:50 PDT 2007
386 + fixed problem that "Invalid search parameters" was not printed because of missing newline to flush it
387 + warnings and critical errors now try to append error messages received from the IMAP client
388 + changed connection error to display timeout only if timeout was the error
389 + documentation now mentions every command-line option accepted by the plugin, including abbreviations
390 + added abbreviations U for username, P for password, m for mailbox
391 + fixed bug that imap-check-interval applied even after the last try (imap-retries) when it was not necessary
392 + the IMAP expunge command is not sent unless at least one message is deleted
393 + fixed bug that the "no messages" warning was printed even if some messages were found
394 + version 0.3
395
396 Sun Oct 21 14:08:07 PDT 2007
397 + added port info to the "could not connect" error message
398 + fixed bug that occurred when using --ssl --port 143 which caused port to remain at the default 993 imap/ssl port
399 + added clarity shortcuts --search-subject and --search-header
400 + port is no longer a required option. defaults to 143 for regular IMAP and 993 for IMAP/SSL
401 + version 0.3.1
402
403 Sun Oct 21 20:41:56 PDT 2007
404 + reworked ssl support to use IO::Socket::SSL instead of the convenience method Mail::IMAPClient->Ssl (which is not included in the standard Mail::IMAPClient package)
405 + removed clarity shortcuts (bad idea, code bloat)
406 + version 0.4
407
408 Tue Dec 4 07:05:27 PST 2007
409 + added version check to _read_line workaround for SSL-related bug in Mail::IMAPClient version 2.2.9 ; newer versions fixed the bug
410 + added --usage option because the official nagios plugins have both --help and --usage
411 + added --timeout option to match the official nagios plugins
412 + fixed some minor pod formatting issues for perldoc
413 + version 0.4.1
414
415 Sat Dec 15 07:39:59 PST 2007
416 + improved compatibility with Nagios embedded perl (ePN)
417 + version 0.4.2
418
419 Mon Jan 7 21:35:23 PST 2008
420 + changed version check for Mail::IMAPClient version 2.2.9 to use string comparison le "2.2.9"
421 + fixed bug where script was dying on socket->autoflush when socket does not exist because autoflush was being called before checking the socket object
422 + version 0.4.3
423
424 Mon Feb 11 19:13:38 PST 2008
425 + fixed a bug for embedded perl version, variable "%status" will not stay shared in load_modules
426 + version 0.4.4
427
428 Mon May 26 08:33:27 PDT 2008
429 + fixed a bug for number captured, it now reflects number of messages captured instead of always returning "1"
430 + added --capture-min option to complement --capture-max
431 + added --search-critical-min to trigger a CRITICAL alert if number of messages found is less than argument, with default 1.
432 + fixed warning and critical messages to use "more than" or "less than" instead of the angle brackets, to make them more web friendly
433 + version 0.5
434
435 Wed Jul 2 14:59:05 PDT 2008
436 + fixed a bug for not finding a message after the first try, by reselecting the mailbox before each search
437 + version 0.5.1
438
439 Sat Dec 13 08:57:29 PST 2008
440 + added --download option to allow local searching of messages (useful if your server has an index that handles searching but it takes a while before new emails show up and you want immediate results), supports only the TEXT, BODY, SUBJECT, and HEADER search keys
441 + added --download-max option to set a limit on number of messages downloaded with --download
442 + version 0.6.0
443
444 Wed Sep 30 23:25:33 PDT 2009
445 + fixed --download-max option (was incorrectly looking for --download_max). currently both will work, in the future only --download-max will work
446 + added --template option to allow arbitrary substitutions for search parameters, and provided three convenience functions for working with dates
447 + added date search criteria to the --download option: SENTBEFORE, SENTON, and SENTSINCE which check the Date header and allow hours and minutes in addition to dates (whereas the IMAP standard only allows dates)
448 + added --search-critical-max to trigger a CRITICAL alert if number of messages found is more than argument, disabled by default.
449 + fixed a bug in --download --search where messages would match even though they failed the search criteria
450 + changed behavior of --download-max to look at the most recent messages first (hopefully); the IMAP protocol doesn't guarantee the order that the messages are returned but I observed that many mail servers return them in chronological order; so now --download-max reverses the order to look at the newer messages first
451 + added performance data for use with PNP4Nagios!
452 + version 0.7.0
453
454 Fri Oct 2 15:22:00 PDT 2009
455 + added --search-warning-max and --search-warning-min to trigger a WARNING alert if number of messages is more than or less than the specified number.
456 + fixed --download option not to fail with CRITICAL if mailbox is empty; now this can be configured with --search-warning-min or --search-critical-min
457 + version 0.7.1
458
459 Sat Nov 21 18:27:17 PST 2009
460 + fixed problem with using --download option on certain mail servers by turning on the IgnoreSizeErrors feature in IMAPClient
461 + added --peek option to prevent marking messages as seen
462 + version 0.7.2
463
464 Tue Jan 5 12:13:53 PST 2010
465 + added error message and exit with unknown status when an unrecognized IMAP search criteria is encountered by the --download --search option
466
467 Wed May 5 11:14:51 PDT 2010
468 + added mailbox list when mailbox is not found and verbose level 3 is on (-vvv)
469 + version 0.7.3
470
471 Tue Mar 8 18:58:14 AST 2011
472 + updated documentation for --search and --template to mention rfc5032 extensions (thanks to Stuart Henderson)
473
474 Fri May 6 08:35:09 AST 2011
475 + added --hires option to enable use of Time::Hires if available
476 + version 0.7.4
477
478 Fri Nov 11 01:51:40 AST 2011
479 + added --ssl-ca-file option to allow verifying the server certificate against a local .pem file (thanks to Alexandre Bezroutchko)
480 + added imap_ssl_cert.pl utility (not in this file) to conveniently save the server's SSL certificates into a local .pem file
481 + version 0.7.5
482
483 =head1 AUTHOR
484
485 Jonathan Buhacoff <[email protected]>
486
487 =head1 COPYRIGHT AND LICENSE
488
489 Copyright (C) 2005-2011 Jonathan Buhacoff
490
491 This program is free software; you can redistribute it and/or modify
492 it under the terms of the GNU General Public License as published by
493 the Free Software Foundation; either version 3 of the License, or
494 (at your option) any later version.
495
496 This program is distributed in the hope that it will be useful,
497 but WITHOUT ANY WARRANTY; without even the implied warranty of
498 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
499 GNU General Public License for more details.
500
501 You should have received a copy of the GNU General Public License
502 along with this program. If not, see <http://www.gnu.org/licenses/>.
503
504 http://www.gnu.org/licenses/gpl.txt
505
506 =cut
507