diff -ruN nagiosplug-save/contrib/check_email_loop.pl nagiosplug/contrib/check_email_loop.pl
--- nagiosplug-save/contrib/check_email_loop.pl	2002-02-28 06:42:53.000000000 +0000
+++ nagiosplug/contrib/check_email_loop.pl	2004-03-27 22:02:58.000000000 +0000
@@ -25,62 +25,128 @@
 # send later came back prior to another mail).
 # 
 
+require 5.004;
+use strict;
+use lib utils.pm ;
+use Plugin;
+use Plugin::Parameter qw($helpparameterlist $versionparameterlist $passwordparameter
+			 $timeoutparameter
+			 $verboseparameter);
+
 use Net::POP3;
 use Net::SMTP;
-use strict;
-use Getopt::Long;
-&Getopt::Long::config('auto_abbrev');
+
+use vars qw($opt_t $sender $receiver $pophost $popuser $poppasswd $poptimeout $smtphost
+	    $smtptimeout $statfile $pinginterval $lostwarn $lostcrit $pendwarn
+	    $pendcrit $PROGNAME);
+
+use utils qw (%ERRORS &usage);
+
+$timeoutparameter->default(120);
+$passwordparameter->binding(\$poppasswd);
+$passwordparameter->flags([ 'passwd' ]);
+$passwordparameter->description("Password for the POP3 user");
+my $fromparameter = new Plugin::Parameter(-name => "from", -flags => [ 'from' ],
+					  -optional => "no", -valueoptional => "no", -type => "EMAILADDRESS",
+					  -binding => \$sender,
+					  -description => "email address of sender (for mail return on errors)");
+my $toparameter = new Plugin::Parameter(-name => "to", -flags => [ 'to' ],
+					-optional => "no", -valueoptional => "no", -type => "EMAILADDRESS",
+					-binding => \$receiver,
+					-description => "email address to which the mails should be sent");
+my $pophostparameter = new Plugin::Parameter(-name => "pophost", -flags => [ "pophost" ],
+					     -optional => "no", -valueoptional => "no", -type => "HOSTNAME",
+					     -binding => \$pophost,
+					     -description => "Hostname or IP address for the POP3 server");
+my $popuserparameter = new Plugin::Parameter(-name => "popuser", -flags => [ "popuser" ],
+					     -optional => "no", -valueoptional => "no", -type => "USER",
+					     -binding => \$popuser,
+					     -description => "Username for the POP3 account");
+my $poptimeoutparameter = new Plugin::Parameter(-name => "poptimeout", -flags => [ "poptimeout" ],
+						-optional => "yes", -valueoptional => "no", -type => "TIMEOUT",
+						-default => 60, -binding => \$poptimeout,
+						-description => "Timeout in seconds for the POP3 server");
+my $smtphostparameter = new Plugin::Parameter(-name => "smtphost", -flags => [ 'smtphost' ],
+					      -optional => "no", -valueoptional => 'no', -type => "HOSTNAME",
+					      -binding => \$smtphost,
+					      -description => "Hostname or IP address for the SMTP server");
+my $smtptimeoutparameter = new Plugin::Parameter(-name => "smtptimeout", -flags => [ 'smtptimeout' ],
+						 -optional => "yes", -valueoptional => 'no', -type => "TIMEOUT",
+						 -default => 60, -binding => \$smtptimeout,
+						 -description => "Timeout in seconds for the SMTP server");
+my $statfileparameter = new Plugin::Parameter(-name => "statfile", -flags => [ 'statfile' ],
+					      -optional => "yes", -valueoptional => "no", -type => "FILENAME",
+					      -default => "/var/tmp/nagios/check_email_loop.stat", -binding => \$statfile,
+					      -description => "Faile to save ids of messages");
+my $lostwarnparameter = new Plugin::Parameter(-name => "lostwarn", -flags => [ "lostwarn" ],
+					      -optional => "yes", -valueoptional => "no", -type => "INTEGER",
+					      -default => 0, -binding => \$lostwarn,
+					      -description => "Report WARNING state if number of lost emails exceeds this number");
+my $lostcritparameter = new Plugin::Parameter(-name => "lostcrit", -flags => [ 'lostcrit' ],
+					      -optional => "yes", -valueoptional => "no", -type => "INTEGER",
+					      -default => 1, -binding => \$lostcrit,
+					      -description => "Report CRITICAL state if number of lost emails exceeds this number");
+my $pendwarnparameter = new Plugin::Parameter(-name => "pendwarn", -flags => [ 'pendwarn' ],
+					      -optional => "yes", -valueoptional => "no", -type => "INTEGER",
+					      -default => 1, -binding => \$pendwarn,
+					      -description => "Report WARNING state if number of pending emails exceeds this number");
+my $pendcritparameter = new Plugin::Parameter(-name => "pendcrit", -flags => [ 'pendcrit' ],
+					      -optional => "yes", -valueoptional => "no", -type => "INTEGER",
+					      -default => 2, -binding => \$pendcrit,
+					      -description => "Report CRITICAL state if number of pending emails exceeds this number");
+
+my @commandparameterlist = ( $fromparameter,
+			     $toparameter,
+			     $pophostparameter,
+			     $popuserparameter,
+			     $passwordparameter,
+			     $poptimeoutparameter,
+			     $smtphostparameter,
+			     $smtptimeoutparameter,
+			     $statfileparameter,
+			     $lostwarnparameter,
+			     $lostcritparameter,
+			     $pendwarnparameter,
+			     $pendcritparameter,
+			     $timeoutparameter,
+			     $verboseparameter );
+
+my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
+			-copyright => "2000 Benjamin Schmid, 2004 Howard Wilkinson <howard\@cohtech.com>",
+			-shortcomment => "This plugin send an email with a specific id in the subject via a given smtp server to a given email address. When the plugin is run again, it checks for this email (with its unique id) on a given pop3 account and sends another email.",
+			-longcomment => "Lost emails occur when later emails arrive first, pending emails are ones that have yet to arrive",
+			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ]);
+
+$plugin->init();
 
 # ----------------------------------------
 
-my $TIMEOUT = 120;
 my %ERRORS = ('UNKNOWN' , '-1',
               'OK' , '0',
               'WARNING', '1',
               'CRITICAL', '2');
 
 my $state = "UNKNOWN";
-my ($sender,$receiver, $pophost, $popuser, $poppasswd, $smtphost);
-my ($poptimeout,$smtptimeout,$pinginterval)=(60,60,5);
-my ($lostwarn, $lostcrit,$pendwarn, $pendcrit);
 
 # Internal Vars
 my ($pop,$msgcount,@msglines,$statinfo,@messageids,$newestid);
-my ($matchcount,$statfile) = (0,"check_email_loop.stat");
+my ($matchcount) = (0);
 
 # Subs declaration
-sub usage;
 sub messagematchs;
 sub nsexit;
 
-# Just in case of problems, let's not hang Nagios
-$SIG{'ALRM'} = sub {
-     print ("ERROR: $0 Time-Out $TIMEOUT s \n");
-     exit $ERRORS{"UNKNOWN"};
-};
-alarm($TIMEOUT);
-
-
-# Evaluate Command Line Parameters
-my $status = GetOptions(
-		        "from=s",\$sender,
-			"to=s",\$receiver,
-                        "pophost=s",\$pophost,
-                        "popuser=s",\$popuser,
-			"passwd=s",\$poppasswd,
-			"poptimeout=i",\$poptimeout,
-			"smtphost=s",\$smtphost,
-			"smtptimeout=i",\$smtptimeout,
-			"statfile=s",\$statfile,
-			"interval=i",\$pinginterval,
-			"lostwarr=i",\$lostwarn,
-			"lostcrit=i",\$lostcrit,
-			"pendwarn=i",\$pendwarn,
-			"pendcrit=i",\$pendcrit,
-			);
-usage() if ($status == 0 || ! ($pophost && $popuser && $poppasswd &&
-	$smtphost && $receiver && $sender ));
 
+$plugin->start_timeout($opt_t, "plugin timed out");
+
+if (! -f $statfile) {
+  $statfile =~ m/^(.*)\/([^\/]*$)/;
+  my ($dir, $file) = ($1, $2);
+  if ( ! -d $dir) {
+    mkdir $dir;
+  }
+}
+      
 # Try to read the ids of the last send emails out of statfile
 if (open STATF, "$statfile") {
   @messageids = <STATF>;
@@ -90,7 +156,8 @@
 
 # Try to open statfile for writing 
 if (!open STATF, ">$statfile") {
-  nsexit("Failed to open mail-ID database $statfile for writing",'CRITICAL');
+  $plugin->usage();
+  usage("$PROGNAME UNKNOWN: Failed to open mail-ID database $statfile for writing\n");
 }
 
 # Ok - check if it's time to release another mail
@@ -102,8 +169,12 @@
 $serial = "ID#" . $serial . "#$$";
 
 # sending new ping email
-my $smtp = Net::SMTP->new($smtphost,Timeout=>$smtptimeout) 
-  || nsexit("SMTP connect timeout ($smtptimeout s)",'CRITICAL');
+my $smtp = Net::SMTP->new($smtphost,Timeout=>$smtptimeout);
+if (!$smtp) {
+  my $elapsed = time() - $serial;
+  my $error = ($elapsed >= $smtptimeout)?"timed out":"unknown error";
+  nsexit("SMTP failed ($error)", 'CRITICAL');
+}
 ($smtp->mail($sender) &&
  $smtp->to($receiver) &&
  $smtp->data() &&
@@ -113,13 +184,13 @@
 		 "Serial No: $serial\n") &&
  $smtp->dataend() &&
  $smtp->quit
- ) || nsexit("Error delivering message",'CRITICAL');
+) || nsexit("Error delivering message",'CRITICAL');
 
-# no the interessting part: let's if they are receiving ;-)
 
-$pop = Net::POP3->new( $pophost, 
-		 Timeout=>$poptimeout) 
-       || nsexit("POP3 connect timeout (>$poptimeout s, host: $pophost)",'CRITICAL');
+# now the interessting part: let's see if they are receiving ;-)
+
+$pop = Net::POP3->new( $pophost, Timeout=>$poptimeout)
+  || nsexit("POP3 connect timeout (>$poptimeout s, host: $pophost)",'CRITICAL');
 
 $msgcount=$pop->login($popuser,$poppasswd);
 
@@ -132,7 +203,7 @@
   @msglines = @{$pop->get($msgcount)};
 
   for (my $i=0; $i < scalar @messageids; $i++) {
-    if (messagematchsid(\@msglines,$messageids[$i])) { 
+    if (messagematchsid(\@msglines,$messageids[$i])) {
       $matchcount++;
       # newest received mail than the others, ok remeber id.
       $newestid = $messageids[$i] if ($messageids[$i] > $newestid || !defined $newestid);
@@ -140,13 +211,15 @@
       splice @messageids, $i, 1;# remove id from List
       last;                     # stop looking in list
     }
-  } 
+  }
 
   $msgcount--;
 }
 
 $pop->quit();  # necessary for pop3 deletion!
 
+$plugin->stop_timeout();
+
 # traverse through the message list and mark the lost mails
 # that mean mails that are older than the last received mail.
 if (defined $newestid) {
@@ -183,7 +256,6 @@
 if ((defined $pendwarn || defined $pendcrit || defined $lostwarn 
      || defined $lostcrit) && ($state eq 'UNKNOWN')) {$state='OK';}
 
-
 # Append Status info
 $statinfo = $statinfo . ", $matchcount mail(s) came back,".
             " $pendingm pending, $lostm lost.";
@@ -191,50 +263,12 @@
 # Exit in a Nagios-compliant way
 nsexit($statinfo);
 
-# ----------------------------------------------------------------------
-
-sub usage {
-  print "check_email_loop 1.0 Nagios Plugin - Real check of a E-Mail system\n";
-  print "=" x 75,"\nERROR: Missing or wrong arguments!\n","=" x 75,"\n";
-  print "This script sends a mail with a specific id in the subject via an given\n";
-  print "smtp-server to a given email-adress. When the script is run again, it checks\n";
-  print "for this Email (with its unique id) on a given pop3 account and sends \n";
-  print "another mail.\n";
-  print "\nThe following options are available:\n";
-  print	"   -from=text         email adress of send (for mail returnr on errors)\n";
-  print	"   -to=text           email adress to which the mails should send to\n";
-  print "   -pophost=text      IP or name of the POP3-host to be checked\n";
-  print "   -popuser=text      Username of the POP3-account\n";
-  print	"   -passwd=text       Password for the POP3-user\n";
-  print	"   -poptimeout=num    Timeout in seconds for the POP3-server\n";
-  print "   -smtphost=text     IP oder name of the SMTP host\n";
-  print "   -smtptimeout=num   Timeout in seconds for the SMTP-server\n";
-  print "   -statfile=text     File to save ids of messages ($statfile)\n";
-#  print "   -interval=num      Time (in minutes) that must pass by before sending\n"
-#  print "                      another Ping-mail (gibe a new try);\n"; 
-  print "   -lostwarn=num      WARNING-state if more than num lost emails\n";
-  print "   -lostcrit=num      CRITICAL \n";
-  print "   -pendwarn=num      WARNING-state if more than num pending emails\n";
-  print "   -pendcrit=num      CRITICAL \n";
-  print " Options may abbreviated!\n";
-  print " LOST mails are mails, being sent before the last mail arrived back.\n";
-  print " PENDING mails are those, which are not. (supposed to be on the way)\n";
-  print "\nExample: \n";
-  print " $0 -poph=host -pa=pw -popu=popts -smtph=host -from=root\@me.com\n ";
-  print "      -to=remailer\@testxy.com -lostc=0 -pendc=2\n";
-  print "\nCopyleft 19.10.2000, Benjamin Schmid\n";
-  print "This script comes with ABSOLUTELY NO WARRANTY\n";
-  print "This programm is licensed under the terms of the ";
-  print "GNU General Public License\n\n";
-  exit $ERRORS{"UNKNOWN"};
-}
-
 # ---------------------------------------------------------------------
 
 sub nsexit {
   my ($msg,$code) = @_;
   $code=$state if (!defined $code);
-  print "$code: $msg\n" if (defined $msg);
+  print "$PROGNAME $code: $msg\n" if (defined $msg);
   exit $ERRORS{$code};
 }
 
@@ -244,23 +278,12 @@
   my ($mailref,$id) = (@_);
   my (@tmp);
   my $match = 0;
- 
+
   # ID
   $id =~ s/^LI/ID/;    # evtl. remove lost mail mark
   @tmp = grep /Subject: E-Mail Ping \[/, @$mailref;
   chomp @tmp;
-  if (($tmp[0] =~ /$id/)) 
-    { $match = 1; }
-
-  # Sender:
-#  @tmp = grep /^From:\s+/, @$mailref;
-#  if (@tmp && $sender ne "") 
-#    { $match = $match && ($tmp[0]=~/$sender/); }
-
-  # Receiver:
-#  @tmp = grep /^To: /, @$mailref;
-#  if (@tmp && $receiver ne "") 
-#    { $match = $match && ($tmp[0]=~/$receiver/); }
+  if (($tmp[0] =~ /$id/)) { $match = 1; }
 
   return $match;
 }
