#!/usr/bin/perl -w use Net::POP3; use Getopt::Long; use Time::HiRes qw(gettimeofday tv_interval); use constant TIMEOUT => 5; my $VERSION = "0.2 by d.angerer and g.stangl, 07.01.2008"; sub doexit { print "POP3 "; if ( $_[0] == 0 ) { print "OK: "; } if ( $_[0] == 1 ) { print "WARNING: "; } if ( $_[0] == 2 ) { print "CRITICAL: "; } print "$_[1]\n"; exit $_[0]; }; $SIG{ALRM} = sub { doexit (2, "Script timeout. No POP3 repsonse after @{[ TIMEOUT ]}s."); }; alarm(TIMEOUT); my $t_connect=TIMEOUT; my $t_login=TIMEOUT; my $t_quit=TIMEOUT; my $LINE1=""; my $LINE2=""; my ($anzahl, $pop); my $t0 = [ gettimeofday ]; my ($opt_verbose,$opt_host,$opt_username,$opt_password,$opt_timeout,$opt_help,$opt_version); Getopt::Long::Configure('bundling'); GetOptions( 'v|verbose' => \$opt_verbose, 'H|host=s' => \$opt_host, 'u|username=s' => \$opt_username, 'p|password=s' => \$opt_password, 't|timeout=i' => \$opt_timeout, 'h|help' => \$opt_help, 'V|version' => \$opt_version, ); if (!defined($opt_timeout)) { $opt_timeout=TIMEOUT; }; if ($opt_help) { print "\nCheck POP3 account for successful login and LIST command\n Usage: check_pop3.pl2 -H pop3-server -u user -p passwd [-t timeout] [-v] check_pop3.pl2 -h print help check_pop3.pl2 -V print version \n"; exit 3; } if ($opt_version) { print "\ncheck_pop3.pl2 VERSION $VERSION\n\n"; exit 3; } if ($opt_verbose) { print "DEBUG: starting POP3 connection\n"; } # _______________________________________________________________________ GO ! ___ $pop = Net::POP3->new( $opt_host , Timeout => $opt_timeout ) or doexit (2, "Can't connect to $opt_host: $!"); $t_connect = tv_interval ( $t0, [gettimeofday]); if ($opt_verbose) { print "DEBUG: connected after : $t_connect s\n"; } $anzahl=$pop->login($opt_username, $opt_password); if (!defined($anzahl)) { doexit (2, "$opt_username and $opt_password didn't work!"); } $t_login = tv_interval ( $t0, [gettimeofday]); if ($opt_verbose) { print "DEBUG: logged in after: $t_login s, found $anzahl messages \n"; } if ($opt_verbose) { print "DEBUG: listing messages\n"; } my $msgnums = $pop->list; # hashref of msgnum => size foreach my $msg (keys %$msgnums) { if ($opt_verbose) { print "DEBUG: msgnum: $msg size: ".$pop->list($msg)."\n"; } } if ($opt_verbose) { print "DEBUG: trying to disconnect\n"; } $pop->quit; $t_quit = tv_interval ( $t0, [gettimeofday]); if ($opt_verbose) { print "DEBUG: disconnected after: $t_quit s\n"; } if ($anzahl < 1) { $anzahl="no"; } $LINE1 = "User $opt_username has $anzahl messages. LIST time: ${t_quit}s."; $LINE2 = "T_connect=${t_connect}s; T_login=${t_login}s; T_list=${t_quit}s;"; doexit (0, "$LINE1|$LINE2");