Annotation of nagios/check_email/check_email, Revision 1.1
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: #
! 14: # $RedRiver$
! 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) {
! 85: print_revision($PROGNAME,'$Revision: 1.3 $ ');
! 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;
! 103: my $recieved_subject;
! 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 ) {
! 140: $recieved_subject = $1;
! 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";
! 149: #print "Recieved: $recieved_subject\n";
! 150:
! 151: if ($subject eq $recieved_subject) {
! 152: $state = "OK";
! 153: $answer = "Sent and recieved message correctly";
! 154: } else {
! 155: $state = "WARNING";
! 156: $answer = "Subject lines did not match wrong message recieved";
! 157: }
! 158:
! 159: done($state, $answer);
! 160:
! 161: sub done
! 162: {
! 163: my $state = shift;
! 164: my $answer = shift;
! 165: print ("$state: $answer");
! 166: exit $ERRORS{$state};
! 167: }
! 168:
! 169: sub usage {
! 170: print "\nMissing arguments!\n";
! 171: print "\n";
! 172: print "check_email -H <HOSTNAME> -u <USERNAME> -p <PASSWORD> \n";
! 173: print "Copyright (C) 2003 andrew fresh\n";
! 174: print "\n\n";
! 175: support();
! 176: exit $ERRORS{"UNKNOWN"};
! 177: }
! 178:
! 179: sub print_help {
! 180: print "check_email plugin for Nagios monitors operational \n";
! 181: print "status of email flow on the target host.\n";
! 182: print "It sends an e-mail to a test user, and then checks \n";
! 183: print "a POP account. Failing at any point during if there is \n";
! 184: print "a problem. It gives a WARNING if the mail is not recieved \n";
! 185: print "in time, and gives a CRITICAL if it cannot connect to any \n";
! 186: print "services. Do not use an account that you want to recieve \n";
! 187: print "mail from. This program will delete ALL mail in the mailbox \n";
! 188: print "that you specify.";
! 189: print "\n";
! 190: print "\nUsage:\n";
! 191: print " -H (--hostname) Hostname to query - (required)\n";
! 192: print " -u (--username) Username whose mail to check - (required)\n";
! 193: print " -p (--password) Password for user - (required)\n";
! 194: print " -P (--popserver) Server to check mail on, defaults to --hostname\n";
! 195: print " -S (--smtpserver) Server to send mail to, defaults to --hostname\n";
! 196: print " -t (--to) Address to send mail to, defaults to --username\@--hostname\n";
! 197: print " -f (--from) Address to send mail from, defaults to Nagios <>\n";
! 198: print " -w (--wait) Time (in seconds) to wait between sending the mail and checking for it, defaults to 1 second. \n";
! 199: print_revision($PROGNAME, '$Revision: 1.0 $');
! 200:
! 201: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>