diff -ruN nagiosplug-save/contrib/check_bgpstate.pl nagiosplug/contrib/check_bgpstate.pl
--- nagiosplug-save/contrib/check_bgpstate.pl	2003-02-23 17:10:39.000000000 +0000
+++ nagiosplug/contrib/check_bgpstate.pl	2004-03-27 21:36:34.000000000 +0000
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/usr/bin/perl -wT
 #
 # check_bgpstate.pl - nagios plugin 
 #
@@ -25,191 +25,168 @@
 
 
 
+require 5.004;
 use strict;
+use lib utils.pm ;
+use Plugin;
+use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
+			 $communityparameter $portparameter $timeoutparameter
+			 $verboseparameter);
 
 use Net::SNMP;
-use Getopt::Long;
-&Getopt::Long::config('auto_abbrev');
 
+use vars qw($opt_h $opt_H $opt_t $opt_C $opt_p $xml $PROGNAME);
+use utils qw(%ERRORS &usage);
 
-# whois programm for RIPE database queries
+$ENV{'PATH'}='';
+$ENV{'BASH_ENV'}='';
+$ENV{'ENV'}='';
+$ENV{'CDPATH'}='';
+
+sub adduplink ($);
+
+$portparameter->default(161);
+
+my $uplinkparameter = new Plugin::Parameter(-name => "uplink", -flags => [ 'u', 'uplink' ],
+					    -optional => "yes", -valueoptional => 0, -type => "ASNUMBER",
+					    -description => "AS number for a critical uplink",
+					    -binding => \&adduplink);
+my @commandparameterlist = ( $hostnameparameter,
+			     $communityparameter,
+			     $portparameter,
+			     $timeoutparameter,
+			     $uplinkparameter,
+			     $verboseparameter );
+
+my $plugin = new Plugin(-revision => '$Revision: 1.0 $',
+			-copyright => "2000 Christoph Kron, 2004 Howard Wilkinson <howard\@cohtech.com>",
+			-shortcomment => "Checks the bgpstate of a 'router'.",
+			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ] );
+
+# whois program for RIPE database queries
 my $whois = '/usr/bin/whois';
-my $status;
-my $TIMEOUT = 30;
 
+# Extra work needed to support different links
 # critical bgp sessions
-my %uplinks = ( 1273, 'Uplink ECRC',
-                1755, 'Uplink EBONE',
-                3300, 'Uplink AUCS'
-	      );
-
-my %ERRORS = ('UNKNOWN' , '-1',
-              'OK' , '0',
-              'WARNING', '1',
-              'CRITICAL', '2');
-
-
-my %bgpPeerState = (  
-           '1',"idle",
-           '2',"connect",
-           '3',"active",
-           '4',"opensent",
-           '5',"openconfirm",
-           '6',"established"
-        );
+my %uplinks = ( 1273 => 'Uplink ECRC',
+                1755 => 'Uplink EBONE',
+                3300 => 'Uplink AUCS' );
+
+my %bgpPeerState = ('1' => "idle",
+		    '2' => "connect",
+		    '3' => "active",
+		    '4' => "opensent",
+		    '5' => "openconfirm",
+		    '6' => "established" );
+
 my $state = "UNKNOWN";
 my $answer = "";
-my $snmpkey;
-my $snmpoid;
-my $key;
 my $community = "public";
 my $port = 161;
-my @snmpoids;
+
 my $snmpbgpPeerState = '1.3.6.1.2.1.15.3.1.2';
 my $snmpbgpPeerLocalAddr = '1.3.6.1.2.1.15.3.1.5';
 my $snmpbgpPeerRemoteAddr = '1.3.6.1.2.1.15.3.1.7';
 my $snmpbgpPeerRemoteAs = '1.3.6.1.2.1.15.3.1.9';
-my $hostname;
-my $session;
-my $error;
-my $response;
-my %bgpStatus;
-my $bgpestablished =0 ;
-my $bgpcritical =0;
-my $bgpdown =0;
-my $bgpidle =0;
-my $bgpmessage;
-my $asname;
-my $remoteas;
-my @output;
-
-sub usage {
-  printf "\nMissing arguments!\n";
-  printf "\n";
-  printf "Perl bgpstate plugin for Nagios\n";
-  printf "monitors all BGP sessions\n";
-  printf "usage: \n";
-  printf "check_bgpstate.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>\n";
-  printf "Copyright (C) 2000 Christoph Kron\n";
-  printf "check_bgpstate.pl comes with ABSOLUTELY NO WARRANTY\n";
-  printf "This programm is licensed under the terms of the ";
-  printf "GNU General Public License\n(check source code for details)\n";
-  printf "\n\n";
-  exit $ERRORS{"UNKNOWN"};
-}
+my @snmpoids = ( $snmpbgpPeerState, $snmpbgpPeerLocalAddr, $snmpbgpPeerRemoteAddr, $snmpbgpPeerRemoteAs);
 
-# Just in case of problems, let's not hang Nagios
-$SIG{'ALRM'} = sub {
-     print ("ERROR: No snmp response from $hostname (alarm)\n");
-     exit $ERRORS{"UNKNOWN"};
-};
-alarm($TIMEOUT);
+$plugin->init();
 
-
-$status = GetOptions("community=s",\$community,
-                     "port=i",\$port);
-if ($status == 0)
-{
-        &usage;
-}
-  
-   #shift;
-   $hostname  = shift || &usage;
-
-
-push(@snmpoids, $snmpbgpPeerState);
-push(@snmpoids, $snmpbgpPeerLocalAddr);
-push(@snmpoids, $snmpbgpPeerRemoteAddr);
-push(@snmpoids, $snmpbgpPeerRemoteAs);
-
-foreach $snmpoid (@snmpoids) {
-
-   ($session, $error) = Net::SNMP->session(
-      -hostname  => $hostname,
-      -community => $community,
-      -port      => $port
-   );
-
-   if (!defined($session)) {
-      $state='UNKNOWN';
-      $answer=$error;
-      print ("$state: $answer");
-      exit $ERRORS{$state};
-   }
-
-   if (!defined($response = $session->get_table($snmpoid))) {
-      $answer=$session->error;
-      $session->close;
-      $state = 'CRITICAL';
-      print ("$state: $answer,$snmpkey");
-      exit $ERRORS{$state};
-   }
-
-   foreach $snmpkey (keys %{$response}) {
-      $snmpkey =~ m/.*\.(\d+\.\d+\.\d+\.\d+$)/;
-      $key =  $1;
-#      printf "debug: $snmpkey: $key -> $response->{$snmpkey}\n";
-      $bgpStatus{$key}{$snmpoid} = $response->{$snmpkey};
-   }
-   $session->close;
+my ($session, $error) = Net::SNMP->session(-hostname  => $opt_H,
+					   -community => $opt_C,
+					   -timeout   => $opt_t,
+					   -port      => $opt_p,
+					   -version   => "snmpv1",
+					   -translate => undef);
+if (!defined($session)) {
+  $state='UNKNOWN';
+  print ("$PROGNAME $state: $error\n");
+  exit $ERRORS{$state};
 }
 
-foreach $key (keys %bgpStatus) {
-   if ($bgpStatus{$key}{$snmpbgpPeerState} == 6 ) { 
-      $bgpestablished++;
-   }
-   elsif ($bgpStatus{$key}{$snmpbgpPeerState} == 1 ) {
-      $bgpidle++;
-   }
-   else { 
-       $bgpdown++ ;
-       if (exists($uplinks{$bgpStatus{$key}{$snmpbgpPeerRemoteAs}}) ) {
-          $bgpcritical++;
-       }
-       @output = `$whois -T aut-num AS$bgpStatus{$key}{$snmpbgpPeerRemoteAs}`;
-
-   $asname = "";
-   foreach (@output) {
-     if (m/as-name/) {
-        $asname = $_;
-        $asname =~ s/as-name://;
-        last;
-     }
-     if ( $asname =~ "" && m/descr/ ) {
-        $asname = $_;
-        $asname =~ s/descr://;
-     }
-   }
-   $asname =~ s/^\s*//;
-   $asname =~ s/\s*$//;
-       $bgpmessage .= sprintf("Peering with AS%s not established -> %s<BR>",
-                           $bgpStatus{$key}{$snmpbgpPeerRemoteAs},
-			   $asname);
-   }
+my %bgpStatus = ();
+
+foreach my $snmpoid (@snmpoids) {
+  my $response = $session->get_table(-baseoid => $snmpoid);
+  $answer=$session->error;
+  if (!defined $response) {
+    $state = 'CRITICAL';
+    print ("$PROGNAME $state: $answer\n");
+    exit $ERRORS{$state};
+  }
+
+  foreach my $snmpkey (keys %{$response}) {
+    $snmpkey =~ m/.*\.(\d+\.\d+\.\d+\.\d+$)/;
+    my $peer =  $1;
+    $bgpStatus{$peer}{$snmpoid} = $response->{$snmpkey};
+  }
 }
-   
 
-   if ($bgpdown > 0) {
-      if ($bgpcritical > 0) {
-         $state = 'CRITICAL';
+$session->close;
+
+my $bgpestablished = 0;
+my $bgpcritical = 0;
+my $bgpdown = 0;
+my $bgpidle = 0;
+my $bgpmessage = "";
+
+foreach my $peer (keys %bgpStatus) {
+  if ($bgpStatus{$peer}{$snmpbgpPeerState} == 6 ) {
+    $bgpestablished++;
+  } elsif ($bgpStatus{$peer}{$snmpbgpPeerState} == 1 ) {
+    $bgpidle++;
+  } else {
+    $bgpdown++ ;
+    if (exists($uplinks{$bgpStatus{$peer}{$snmpbgpPeerRemoteAs}}) ) {
+      $bgpcritical++;
+    }
+    my $asname = "";
+    foreach (`$whois -h whois.ripe.net -- -T aut-num AS$bgpStatus{$peer}{$snmpbgpPeerRemoteAs}`) {
+      if (m/^as-name:/) {
+	$asname = $_;
+	$asname =~ s/^as-name://;
+	chomp $asname;
+	next;
       }
-      else {
-         $state = 'WARNING';
+      if ( m/^descr:/ ) {
+	my $descr = $_;
+	$descr =~ s/^descr://;
+	chomp $descr;
+	$asname .= $descr;
+	next;
       }
-      $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d<BR>",
-                        $hostname,
-			$bgpestablished,
-			$bgpdown, $bgpidle);
-      $answer = $answer . $bgpmessage . "\n";
-   }
-   else {
-      $state = 'OK';
-      $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d\n",
-                        $hostname,
-			$bgpestablished,
-			$bgpdown,$bgpidle);
-   }
+    }
+    $asname =~ s/^\s*//;
+    $asname =~ s/\s*$//;
+    $bgpmessage .= sprintf("Peering not established with %s",
+			   (($asname)?$asname:"AS".$bgpStatus{$peer}{$snmpbgpPeerRemoteAs}));
+  }
+}
+
+if ($bgpdown > 0) {
+  if ($bgpcritical > 0) {
+    $state = 'CRITICAL';
+  } else {
+    $state = 'WARNING';
+  }
+  $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d. ",
+		    $opt_H,
+		    $bgpestablished,
+		    $bgpdown, $bgpidle);
+  $answer = $answer . $bgpmessage . "\n";
+} else {
+  $state = 'OK';
+  $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d\n",
+		    $opt_H,
+		    $bgpestablished,
+		    $bgpdown,$bgpidle);
+}
+
+print ("$PROGNAME $state: $answer");
 
-print ("$state: $answer");
 exit $ERRORS{$state};
 
+sub adduplink ($) {
+  my ($arg, $uplink) = @_;
+  $uplinks{$uplink} = $uplink;
+}
