
##########################################################################
# $Id: dialup 81 2007-05-01 09:40:24Z weizen_42 $
##########################################################################

use Logwatch ':all';

$Debug = ValueOrDefault($ENV{'LOGWATCH_DEBUG'}, 0);
$Detail = ValueOrDefault($ENV{'LOGWATCH_DETAIL_LEVEL'}, 0);

# Avoid "Use of uninitialized value" warning messages.
sub ValueOrDefault {
	my ($value, $default) = @_;
	return ($value ? $value : $default);
}

if ( $Debug >= 5 ) {
	print STDERR "\n\nDEBUG: Inside DIALUP Filter \n\n";
	$DebugCounter = 1;
}

while (defined($ThisLine = <STDIN>)) {
  if ( $Debug >= 5 ) {
    print STDERR "DEBUG($DebugCounter): $ThisLine";
    $DebugCounter++;
  }
  chomp($ThisLine);

  ($month,$day,$time,$host,$process,$msg)=split(/ +/,$ThisLine,6);

  unless ( $process =~ /pppd/ )
  {
    next;		
  }

  if ( $Debug >= 10 ) {
    print STDERR "Process: $msg \n";
  }

  if ( $process =~ /ipppd/ )
  {
    if ( $msg =~ /^init_unit: (\d+)/ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found IPPP start\n";
      }
      $IPPPStarts++;
    }
    elsif ( $msg =~ /^local.*IP address (.*)/ )
    {
      if ($Debug >= 5) 
      {	
         print STDERR "DEBUG: Found IPPP connect\n";
      }
      $IPPPUps++;
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found IP\n";
      }
      push @OtherList, "   $time  $1\n";
    }
    elsif ( $msg =~ /^link (\d+) closed , linkunit: (\d+)/ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found IPPP down\n";
      }
      $IPPPDowns++;
    }
  }
  elsif ( $process =~ /pppd/ )
  {
    if ( $msg =~ /^pppd (\d+).(\d+).(\d+) started by root, uid (\d+)/ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found PPP start\n";
      }
      $PPPStarts++;
    }
    elsif ( $msg =~ /^Connection terminated./ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found PPP down\n";
      }
      $PPPDowns++;
    }
    elsif ( $msg =~ /^PPP session is (\d+)/ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found PPP connect\n";
      }
      $PPPUps++;
    }
    elsif ( $msg =~ /^Connect time (\d+).(\d+) minutes./ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found PPP connecttime $1\n";
      }
      $PPPUptime += $1 + ($2 / 10);
    }
    elsif ( $msg =~ /^local.*IP address (.*)/ )
    {
      if ($Debug >= 5) 
      {
        print STDERR "DEBUG: Found IP\n";
      }
       push @OtherList, "   $time  $1\n";
    }
  }
}

###########################################################

if ( $PPPStarts )
{
    print "PPP Dial attempts: " . $PPPStarts . " Time(s)\n";
}
if ( $PPPUps )
{
  print "PPP Connected: " . $PPPUps . " Time(s)\n";
}
if ( $PPPDowns )
{
  print "PPP Disconnected: " . $PPPDowns . " Time(s)\n";
}
if ( $PPPUptime )
{
  print "Total connect time: " . $PPPUptime . " Minutes\n";
}

if ( $IPPPStarts )
{
  print "ISDN PPP Dial attempts: " . $IPPPStarts . " Time(s)\n";
}
if ( $IPPPUps )
{
  print "ISDN PPP Connected: " . $IPPPUps . " Time(s)\n";
}
if ( $IPPPDowns )
{
  print "ISDN PPP Disconnected: " . $IPPPDowns . " Time(s)\n";
}
if ($#OtherList >= 0) {
    print "\nIP Addresses:\n";
    print @OtherList;
}


exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et
