Annotation of nagios/check_email/check_email, Revision 1.2
1.1 andrew 1: #!/usr/bin/perl
2: # check_email.pl - nagios plugin
3: #
4: #
5: # Copyright (C) 2003 andrew fresh
6: #
7: # This program is free software; you can redistribute it and/or
8: # modify it under the terms of the BSD License.
9: #
10: # Report bugs to: andrew@mad-techies.org
11: #
12: # 11.15.2003 Version 1.0
13: #
1.2 ! andrew 14: # $RedRiver: check_email,v 1.1 2006/08/29 22:28:49 andrew Exp $
1.1 andrew 15:
16: use POSIX;
17: use lib "/usr/local/libexec/nagios" ;
18: use utils qw($TIMEOUT %ERRORS &print_revision &support);
19:
20: use strict;
21: use warnings;
22: use diagnostics;
23:
24: use Net::SMTP;
25:
26: use Net::POP3;
27:
28:
29: use Getopt::Long;
30: Getopt::Long::Configure('bundling');
31:
32: my $PROGNAME = "check_email";
33:
34:
35: my $status;
36: my $state = "UNKNOWN";
37: my $answer = "";
38: my $opt_h ;
39: my $opt_V ;
40:
41: my $hostname;
42:
43: my $smtp_server;
44: my $from_address = "Nagios <>";
45: my $to_address;
46:
47: my $pop_server;
48: my $pop_user;
49: my $pop_password;
50:
51: my $wait_time = 1;
52:
53:
54: # Just in case of problems, let's not hang Nagios
55: $SIG{'ALRM'} = sub {
56: print ("ERROR: No response from $hostname (alarm timeout)\n");
57: exit $ERRORS{"UNKNOWN"};
58: };
59: alarm($TIMEOUT);
60:
61:
62:
63: #Option checking
64: $status = GetOptions(
65: "V" => \$opt_V, "version" => \$opt_V,
66: "h" => \$opt_h, "help" => \$opt_h,
67: "H=s" => \$hostname, "hostname=s" => \$hostname,
68: "S=s" => \$smtp_server, "smtpserver=s" => \$smtp_server,
69: "P=s" => \$pop_server, "popserver=s" => \$pop_server,
70: "t=s" => \$to_address, "to=s" => \$to_address,
71: "f=s" => \$from_address, "from=s" => \$from_address,
72: "u=s" => \$pop_user, "username=s" => \$pop_user,
73: "p=s" => \$pop_password, "password=s" => \$pop_password,
74: "w:i" => \$wait_time, "wait=i" => \$wait_time,
75: );
76:
77: if ($status == 0)
78: {
79: print_help() ;
80: exit $ERRORS{'OK'};
81: }
82:
83:
84: if ($opt_V) {
1.2 ! andrew 85: print_revision($PROGNAME,'$Revision: 1.1 $ ');
1.1 andrew 86: exit $ERRORS{'OK'};
87: }
88:
89: if ($opt_h) {
90: print_help();
91: exit $ERRORS{'OK'};
92: }
93:
94:
95: if (! utils::is_hostname($hostname) || ! defined $pop_user || ! defined $pop_password ){
96: usage();
97: exit $ERRORS{"UNKNOWN"};
98: }
99:
100: ### Actions go here
101:
102: my $subject = scalar localtime;
1.2 ! andrew 103: my $received_subject;
1.1 andrew 104:
105: $smtp_server ||= $hostname;
106: $pop_server ||= $hostname;
107: $to_address ||= ( $pop_user =~ /\@/ ) ? $pop_user : "$pop_user\@$smtp_server";
108:
109: my $smtp = Net::SMTP->new($smtp_server) || done('CRITICAL', "Couldn't connect to SMTP Server $smtp_server");
110:
111: $smtp->mail($from_address) || done('CRITICAL', "Couldn't specify smtp from address $from_address on SMTP server $smtp_server");
112: $smtp->to($to_address) || done('CRITICAL', "Couldn't specify smtp to address $to_address on SMTP server $smtp_server");
113:
114: $smtp->data() || done('CRITICAL', "Couldn't begin data on SMTP server $smtp_server");
115: $smtp->datasend("To: $to_address\n") || done('CRITICAL', "Couldn't smtp datasend To: header on SMTP server $smtp_server");
116: $smtp->datasend("From: $from_address\n") || done('CRITICAL', "Couldn't smtp datasend From: header on SMTP server $smtp_server");
117: $smtp->datasend("Subject: $subject\n") || done('CRITICAL', "Couldn't smtp datasend Subject: header on SMTP server $smtp_server");
118: $smtp->datasend("\n") || done('CRITICAL', "Couldn't smtp datasend blank line on SMTP server $smtp_server");
119: $smtp->datasend("A simple test message sent on $subject\n") || done('CRITICAL', "Couldn't smtp datasend message body on SMTP server $smtp_server");
120: $smtp->dataend() || done('CRITICAL', "Couldn't smtp dataend on SMTP server $smtp_server");
121:
122: $smtp->quit || done('CRITICAL', "Couldn't smtp quit on SMTP server $smtp_server");
123:
124: sleep $wait_time;
125:
126: my $pop = Net::POP3->new($pop_server) || done('CRITICAL', "Couldn't connect to POP server $pop_server");
127:
128: $pop->user($pop_user) || done('CRITICAL', "POP username $pop_user failed on POP server $pop_server");
129: $pop->pass($pop_password) || done('CRITICAL', "POP password for $pop_user failed on POP server $pop_server");
130:
131: my $messages = $pop->list() || done('CRITICAL', "Couldn't get message list from POP server $pop_server");
132:
133: foreach my $message (sort { $a <=> $b } keys %{ $messages } ) {
134: #print $message;
135: #print "\n";
136: my $lines = $pop->get($message) || done('CRITICAL', "Couldn't get message $message from POP server $pop_server");
137: foreach my $line ( @{ $lines } ) {
138: #print $line;
139: if ( $line =~ /^Subject:\s+(.*)$/o ) {
1.2 ! andrew 140: $received_subject = $1;
1.1 andrew 141: last;
142: }
143: }
144: $pop->delete($message) || done('CRITICAL', "Couldn't delete message $message from POP server $pop_server");
145: }
146: $pop->quit() || done('CRITICAL', "Couldn't quit from POP server $pop_server");
147:
148: #print "Subject: $subject\n";
1.2 ! andrew 149: #print "Received: $received_subject\n";
1.1 andrew 150:
1.2 ! andrew 151: if (not defined $received_subject) {
! 152: $state = "WARNING";
! 153: $answer = "No subject line received";
! 154: } elsif ($subject eq $received_subject) {
1.1 andrew 155: $state = "OK";
1.2 ! andrew 156: $answer = "Sent and received message correctly";
1.1 andrew 157: } else {
158: $state = "WARNING";
1.2 ! andrew 159: $answer = "Subject lines did not match wrong message received";
1.1 andrew 160: }
161:
162: done($state, $answer);
163:
164: sub done
165: {
166: my $state = shift;
167: my $answer = shift;
168: print ("$state: $answer");
169: exit $ERRORS{$state};
170: }
171:
172: sub usage {
173: print "\nMissing arguments!\n";
174: print "\n";
175: print "check_email -H <HOSTNAME> -u <USERNAME> -p <PASSWORD> \n";
176: print "Copyright (C) 2003 andrew fresh\n";
177: print "\n\n";
178: support();
179: exit $ERRORS{"UNKNOWN"};
180: }
181:
182: sub print_help {
183: print "check_email plugin for Nagios monitors operational \n";
184: print "status of email flow on the target host.\n";
185: print "It sends an e-mail to a test user, and then checks \n";
186: print "a POP account. Failing at any point during if there is \n";
1.2 ! andrew 187: print "a problem. It gives a WARNING if the mail is not received \n";
1.1 andrew 188: print "in time, and gives a CRITICAL if it cannot connect to any \n";
189: print "services. Do not use an account that you want to recieve \n";
190: print "mail from. This program will delete ALL mail in the mailbox \n";
191: print "that you specify.";
192: print "\n";
193: print "\nUsage:\n";
194: print " -H (--hostname) Hostname to query - (required)\n";
195: print " -u (--username) Username whose mail to check - (required)\n";
196: print " -p (--password) Password for user - (required)\n";
197: print " -P (--popserver) Server to check mail on, defaults to --hostname\n";
198: print " -S (--smtpserver) Server to send mail to, defaults to --hostname\n";
199: print " -t (--to) Address to send mail to, defaults to --username\@--hostname\n";
200: print " -f (--from) Address to send mail from, defaults to Nagios <>\n";
201: print " -w (--wait) Time (in seconds) to wait between sending the mail and checking for it, defaults to 1 second. \n";
1.2 ! andrew 202: print_revision($PROGNAME, '$Revision: 1.1 $');
1.1 andrew 203:
204: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>