#!/usr/bin/perl -w
##########################################################################
# $Id: stunnel,v 1.3 2003/12/15 18:09:23 kirk Exp $
##########################################################################

$^W=1;
use strict;

my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;

my $DebugCounter = 0;

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

my @OtherList = ();
my %OtherList = ();
my %connections = ();

sub other {
   my $msg = shift;
   unless (exists $OtherList{$msg}) {
      $OtherList{$msg} = 1;
      push(@OtherList, $msg);
   } else {
      $OtherList{$msg}++;
   }
}

my $ThisLine;
while (defined($ThisLine = <STDIN>)) {
   if ( $Debug >= 5 ) {
      print STDERR "DEBUG($DebugCounter): $ThisLine";
      $DebugCounter++;
   }
   chomp($ThisLine);
   my $origline = $ThisLine;
   if ($ThisLine =~ m/^(.+) connected from (\d+\.\d+\.\d+\.\d+)/) {
      my $service = $1;
      my $ip = $2;
      if (! exists($connections{$service}{$ip})) {
        $connections{$service}{$ip} = 0;
      }
      ++$connections{$service}{$ip};
   } elsif ($ThisLine =~ m/^Connection (reset|closed)/) {
      # ignore
   } else {
      # Report any unmatched entries...
      other($ThisLine);
   }
}

if (keys %connections) {
   print "\nconnections:\n";
   foreach my $service (sort keys %connections) {
     print "  $service\n";
     my $ips = $connections{$service};
     foreach my $ip (sort keys %$ips) {
        print "    $ip ", $ips->{$ip}, "\n";
     }
   }
}

if (@OtherList) {
   print "\n**Unmatched Entries**\n";
   for (@OtherList) {
     my $count = $OtherList{$_};
     print "($count) $_\n";
   }
}

exit(0);

# vi: shiftwidth=3 tabstop=3 et

