*** check_pop3.pl.orig 2002-02-27 22:42:53.000000000 -0800 --- check_pop3.pl 2003-05-19 14:57:06.000000000 -0700 *************** *** 23,32 **** # with perl -wT and 'use strict' # 2000/01/20 RM Corrected POP3 Exit State. # 2000/01/21 RM Fix Exit Codes Again!! # ------------------------------------------------------------------------------ # -----------------------------------------------------------------[ Require ]-- ! require 5.004; # --------------------------------------------------------------------[ Uses ]-- use Socket; --- 23,34 ---- # with perl -wT and 'use strict' # 2000/01/20 RM Corrected POP3 Exit State. # 2000/01/21 RM Fix Exit Codes Again!! + # 2003/05/17 CS Cleaned up little, simplifed logic flow, better + # checking of return status from server. # ------------------------------------------------------------------------------ # -----------------------------------------------------------------[ Require ]-- ! use 5.004; # --------------------------------------------------------------------[ Uses ]-- use Socket; *************** *** 37,43 **** $ENV{BASH_ENV} = ""; $|=1; # ------------------------------------------------------------------[ Global ]-- ! my $TIMEOUT = 60; # -------------------------------------------------------------------[ usage ]-- sub usage --- 39,46 ---- $ENV{BASH_ENV} = ""; $|=1; # ------------------------------------------------------------------[ Global ]-- ! my $TIMEOUT = 30; ! my $DEBUG = 0; # -------------------------------------------------------------------[ usage ]-- sub usage *************** *** 65,78 **** my $that; my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost); ! if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; } $sockaddr = 'S n a4 x8'; $this = pack($sockaddr, AF_INET, 0, $thisaddr); $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr); ! if (!bind(ClientSocket, $this)) { print "Connection Refused"; exit 2; } ! if (!connect(ClientSocket, $that)) { print "Connection Refused"; exit 2; } ! select(ClientSocket); $| = 1; select(STDOUT); ! return \*ClientSocket; } # ====================================================================[ MAIN ]== --- 68,88 ---- my $that; my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost); ! if (!socket(POPS,AF_INET, SOCK_STREAM, $proto)) { ! err("$!"); ! } $sockaddr = 'S n a4 x8'; $this = pack($sockaddr, AF_INET, 0, $thisaddr); $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr); ! if (!bind(POPS, $this)) { ! err("$!"); ! } ! if (!connect(POPS, $that)) { ! err("$!"); ! } ! select(POPS); $|=1; ! select(STDOUT); $|=1; ! return \*POPS; } # ====================================================================[ MAIN ]== *************** *** 86,92 **** # Just in case of problems, let's not hang Nagios $SIG{'ALRM'} = sub { ! print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n"; exit -1; }; --- 96,102 ---- # Just in case of problems, let's not hang Nagios $SIG{'ALRM'} = sub { ! print "POP3 Error: Timeout after $TIMEOUT seconds\n"; exit -1; }; *************** *** 94,144 **** chop($hostname = `hostname`); my ($name, $alias, $proto) = getprotobyname('tcp'); ! my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname); ! ! ! print ClientSocket "user $username\n"; ! ! #Debug Server ! #print "user $username\n"; ! ! #Sleep or 3 secs, incase server is slow. ! sleep 3; ! print ClientSocket "pass $password\n"; ! ! #Debug Server ! #print "pass $password\n"; ! ! while () { ! print ClientSocket "pass $password\n"; ! #Debug Server ! #print $_; ! err($_) if (m/\-ERR\s+(.*)\s+.*/); ! message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/); ! } } ! sub message { ! my $answer = "UNKNOWN"; ! $answer = "Pop3 OK - Total Messages On Server :- $1"; ! alarm(0); ! print ClientSocket "quit\n"; ! print "$answer"; ! exit 0; } sub err { ! my $answer = "UNKNOWN"; ! $answer = "Pop3 Error :- $1"; alarm(0); ! print ClientSocket "quit\n"; ! print "$answer"; exit 2; } --- 104,162 ---- chop($hostname = `hostname`); my ($name, $alias, $proto) = getprotobyname('tcp'); ! my $POPS = &bindRemote($remotehost,$remoteport,$hostname); ! # Wait for server to come ready ! alarm($TIMEOUT); ! $_ = ; ! if (! /^.OK/) { err($_, *POPS); } ! print STDERR "<< $_" if $DEBUG; ! # Send login name ! alarm($TIMEOUT); ! print STDERR ">> user $username\n" if $DEBUG; ! print POPS "user $username\n"; ! $_ = ; ! if (! /^.OK/) { err($_, *POPS); } ! print STDERR "<< $_" if $DEBUG; ! # Send login password ! alarm($TIMEOUT); ! print STDERR ">> pass $password\n" if $DEBUG; ! print POPS "pass $password\n"; ! $_ = ; ! if (! /^.OK/) { err($_, *POPS); } ! print STDERR "<< $_" if $DEBUG; ! ! # Return the good news ! print "POP3 OK - $_"; ! logout(*POPS); } ! sub logout { ! my $pops = shift; ! ! print STDERR ">> quit\n" if $DEBUG; ! print $pops "quit\n"; ! $_ = <$$pops>; ! print STDERR "<< $_" if $DEBUG; ! close($pops); ! print STDERR "\nClosed socket\n" if $DEBUG; ! } sub err { ! my ($msg, $pops) = @_; ! alarm(0); ! print "POP3 Error: $msg"; ! ! if (defined($pops)) { ! logout($pops); ! } exit 2; }