#!/usr/bin/perl -w
use strict;
use warnings;
use Nagios::Plugin 0.15;

my $interface = 'eth1';
my $accessconcentrator = '';
my $failedconn = 0;
my $verbose = 0;
my $np = Nagios::Plugin->new( shortname => "CHECK_PPPOE" );

open ( OUT, "/usr/sbin/pppoe -I $interface -A 2>&1 |" )
   or $np->nagios_die( "can't start /usr/sbin/pppoe" );

while (<OUT>) {
   print "$_" if ($verbose);
   chomp $_;
   if ( /Access-Concentrator:\s+([^ ]+.*$)/ ) {
      $accessconcentrator = $1;
   } elsif ( /Timeout waiting for PADO packets/ ) {
     $failedconn++;
   }
}
close (OUT);

SWITCH: {
   if ( "$accessconcentrator" ne "" ) {
      $np->nagios_exit( OK, "Access Concentrator: $accessconcentrator");
      last SWITCH;
   }
   if ( $failedconn > 0 ) {
       $np->nagios_exit(CRITICAL, "no DSL link" );
      last SWITCH;
   }
   $np->nagios_die( "for unknown reasons" );
}
