/[nagios-plugins-perl]/trunk/plugins/check_mail_pop3.pl
ViewVC logotype

Contents of /trunk/plugins/check_mail_pop3.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download)
Thu Nov 12 13:29:04 2009 UTC (11 years ago) by racvision
File MIME type: text/plain
File size: 7875 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2002-2009 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty
12 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # you should have received a copy of the GNU General Public License
16 # along with this program (or with Nagios); if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA
19 #
20 #
21
22 use strict; # should never be differently :-)
23 use warnings;
24
25 use File::Basename; # get basename()
26 use lib dirname($0); # to get local libs
27 use libplugins qw(:DEFAULT );
28
29 use Net::POP3; # POP3 access
30 use Time::Local; # for date conversions
31
32 #use Data::Dumper; # debug tool
33
34 my $DEBUG=0;
35
36 # Default values :
37 my %DEFVALUES = (
38 'WARNING' => {
39 'v' => 15,
40 'desc' => _("Message age warning level (in mn)"),
41 },
42 'CRITICAL' => {
43 'v' => 30,
44 'desc' => _("Message age critical level (in mn)"),
45 },
46 'TIMEOUT' => {
47 'v' => 10,
48 'desc' => _("POP3 server connection timeout (in s)"),
49 },
50 'MAXMSG' => {
51 'v' => 50,
52 'desc' => _("Maximum number of messages in the mailbox"),
53 },
54 'MAXSIZE' => {
55 'v' => 1024,
56 'desc' => _("Maximum size of the mailbox (in kilobytes)"),
57 },
58 'VERSION' => {
59 'v' => "1.1",
60 'desc' => _("Plugin Version"),
61 },
62 );
63
64 # Get argument :
65 use vars qw( $opt_V $opt_h $opt_m );
66 use vars qw( $warn $crit $pop_user $pop_passwd $pop_server $pop_key);
67
68 Getopt::Long::GetOptions (
69 "V" => \$opt_V, "version" => \$opt_V,
70 "h|?" => \$opt_h, "help" => \$opt_m,
71 "man" => \$opt_m,
72 "d" => \$DEBUG, "debug=i" => \$DEBUG,
73
74 "w=i" => \$warn, "warning=i" => \$warn,
75 "c=i" => \$crit, "critical=i"=> \$crit,
76
77 "H=s" => \$pop_server, "host=s" => \$pop_server,
78 "u=s" => \$pop_user, "user=s" => \$pop_user,
79 "p=s" => \$pop_passwd, "passwd=s" => \$pop_passwd,
80 "k=s" => \$pop_key, "key=s" => \$pop_key,
81 );
82
83 my $state = "UNKNOWN";
84 my $answer = "";
85
86 if ( defined ($opt_h) || defined ($opt_m) ) {
87 &showUsage($opt_m,\%DEFVALUES);
88 } elsif (defined ($opt_V)) {
89 &showVersion($DEFVALUES{VERSION}{v});
90 }
91
92 if ( (!defined ($pop_server)) || ($pop_server eq "")) {
93 print _("Missing 'pop_server' parameter !")."\n";
94 &showUsage();
95 exit $EXIT_CODES{$state};
96 }
97 if ( (!defined ($pop_user)) || ($pop_user eq "")) {
98 print _("Missing 'pop_user' parameter !")."\n";
99 &showUsage();
100 exit $EXIT_CODES{$state};
101 }
102
103 $pop_passwd = $pop_passwd || '';
104 $pop_key = $pop_key || $pop_server;
105
106 $warn = $warn || $DEFVALUES{WARNING}{v};
107 $crit = $crit || $DEFVALUES{CRITICAL}{v};
108
109 $state = 'CRITICAL';
110
111 if ($DEBUG) {
112 print "DEBUG: pop_server='$pop_server' pop_user='$pop_user' pop_passwd='$pop_passwd' pop_key='$pop_key'\n"
113 }
114
115 my $pop3 = Net::POP3->new($pop_server, Timeout => $DEFVALUES{TIMEOUT}{v}, Debug => $DEBUG);
116 if (!$pop3) {
117 printf (_("Couldn't connect to server '%s' !")."\n",$pop_server);
118 exit $EXIT_CODES{$state};
119 }
120
121 my $nbmsg = $pop3->login($pop_user, $pop_passwd);
122
123 if (! defined ($nbmsg)) {
124 printf (_("Couldn't logon to server '%s' with user '%s' and password !")."\n",$pop_server,$pop_user);
125 exit $EXIT_CODES{$state};
126 }
127
128 my $lists = $pop3->list();
129
130 my $mbsize = int($pop3->popstat() / 102.40)/10;
131
132
133 if ($DEBUG) {
134 print "DEBUG: nbmsg='$nbmsg' size=".$mbsize."KB\n"
135 }
136
137
138 my $lastDate = 0;
139 my %msgs;
140
141 foreach my $id (keys %{$lists}) {
142 my $header = $pop3->top($id);
143
144 my %header;
145 my $lasttag ;
146 foreach my $line (@{$header}) {
147 if ( $line =~ /^([A-Z][\w\-]+)\: (.*)$/ ) {
148 $header{$1} = $2;
149 $lasttag = $1;
150 chomp($header{$1});
151 } elsif ( ($line =~ /^\s+(.*)$/) && ($lasttag) ) {
152 $header{$lasttag} .= "\n$1;"
153 } elsif ($DEBUG) {
154 print "BAD HEADER:$line\n";
155 }
156 }
157
158 if ( $DEBUG ) {
159 print "DEBUG: msg Date='".$header{Date}."' \n";
160 print "DEBUG: msg Subject='".$header{Subject}."' \n";
161 }
162 # if ( $header{Subject} =~ /^$pop_key\s(\d+)/ ) {
163 if ( $header{Subject} =~ /^$pop_key/o ) {
164 my %msg;
165 $msg{epok} = datetime_to_epok($header{Date});
166 $msg{Date} = $header{Date};
167 $msg{Subject} = $header{Subject};
168 $msgs{$id} = \%msg;
169
170 $lastDate = $msg{epok} > $lastDate ? $msg{epok} : $lastDate;
171 if ( $DEBUG ) {
172 print "DEBUG: msg lastDate='".$lastDate."' \n";
173 }
174 }
175
176 }
177
178 # remove old msg:
179 foreach my $id (keys %msgs) {
180 if ( $msgs{$id}{epok} < $lastDate ) {
181 if ( $DEBUG ) {
182 print "DEBUG: Delete msg id='".$id."' \n";
183 }
184 $pop3->delete($id);
185 }
186 }
187
188 $pop3->quit;
189
190 my $age = time() - $lastDate;
191
192 if ($DEBUG) {
193 print "DEBUG: lastDate='$lastDate' age='".getAge($age)."($age)'\n"
194 }
195
196
197 $state = 'OK';
198 $answer = sprintf(_("Mail system works fine (key %s, age %s)"),$pop_key,getAge($age));
199
200 if ( $lastDate == 0 ) {
201 $state = 'CRITICAL';
202 $answer = sprintf(_("No message using key '%s' found !"),$pop_key);
203
204 } elsif ( $age > $crit*60 ) {
205 $state = 'CRITICAL';
206 $answer = sprintf(_("Last message '%s' too old ! (%s)"),$pop_key,getAge($age));
207
208 } elsif ( $age > $warn*60 ) {
209 $state = 'WARNING';
210 $answer = sprintf(_("Last message '%s' too old ! (%s)"),$pop_key,getAge($age));
211
212 }
213
214 if ( $nbmsg > $DEFVALUES{'MAXMSG'}{v}) {
215 if ( $state eq 'OK') {
216 $state = 'WARNING';
217 }
218 $answer .= sprintf(_(" - Too many msg on the mailbox ! (%s)"),$nbmsg);
219 }
220
221 if ( $mbsize > $DEFVALUES{'MAXSIZE'}{v}) {
222 if ( $state eq 'OK') {
223 $state = 'WARNING';
224 }
225 $answer .= sprintf(_(" - Mailbox size too high ! (%s KB)"),$mbsize);
226 }
227
228 print $answer."\n";
229 exit $EXIT_CODES{$state};
230
231
232 sub datetime_to_epok {
233
234 my ($date) = @_;
235 my $epoque = 0;
236
237 # Tue, 01 Jun 2004 10:11:14 +0200 timelocal($sec,$min,$hour,$mday,$mon,$year);
238 if ( $date =~/^\w+\,\s+(\d+)\s+(\w+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+[+-]\d+/ ) {
239 my %months = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5,
240 Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11);
241 $epoque = timelocal($6,$5,$4,$1,$months{$2},$3-1900);
242 }
243 return $epoque;
244 }
245
246 sub getAge {
247 my ($sec) = @_;
248 my ($s,$d,$h,$m) = ($sec,0,0,0);
249 my $ret = '';
250 if ( $s / (3600*24) > 1) {
251 $d = int($s / (3600*24));
252 $s = $s % (3600*24);
253 $ret .= " $d "._("days");
254 }
255 if ( $s / (3600) > 1) {
256 $h = int($s / 3600);
257 $s = $s % 3600;
258 $ret .= " $h "._("h");
259 }
260 if ( $s / (60) > 1) {
261 $m = int($s / 60);
262 $s = $s % 60;
263 $ret .= " $m "._("mn");
264 }
265 $ret .= " $s "._("s");
266 return $ret;
267 }
268
269
270 __END__
271
272 =head1 NAME
273
274 Nagios plugins to check a specified pop3 account and check last mail freshness.
275
276 =head1 SYNOPSIS
277
278 B<check_mail_pop3.pl> S<-H I<pop3 server>> S<-u I<pop3 user>> S<-p I<pop3 password>> S<-k I<subject key>> [S<-w I<warn mn>>] [S<-c I<crit mn>>]
279
280 =head1 DESCRIPTION
281
282
283 =head1 OPTIONS
284
285 =over 4
286
287 =item B<-H> I<pop3 server>
288
289 The POP3 server.
290
291 =item B<-u> I<pop3 user>
292
293 POP3 login.
294
295 =item B<-p> I<pop3 password>
296
297 POP3 password.
298
299 =item B<-k> I<subject key>
300
301 The subject of the message must start with this key (see check_mail_smtp.pl).
302
303 =item B<-w> I<warn mn>
304
305 =item B<-w> I<crit mn>
306
307 Maximum age of the message. The plugin search the Date: line in message headers.
308
309
310 =back
311
312 =head1 NAGIOS CONGIGURATIONS
313
314 In F<checkcommands.cfg> you have to add :
315
316 # 'check_mail_pop3'
317 # @param $ARG1$ pop3 user
318 # @param $ARG3$ pop3 passwd
319 # @param $ARG3$ key
320 define command {
321 command_name check_mail_pop3
322 command_line $USER1$/check_mail_pop3.pl -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -k $ARG3$
323 }
324
325 In F<services.cfg> you can add :
326
327 define service {
328 host pop3.example.org
329 service_description Check Mail POP3
330 check_command check_mail_pop3!racmail!xxxxxx!test@example.org
331 }
332
333 =head1 AUTHOR
334
335 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
336
337 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8