########################################################
# $Id: dovecot,v 1.1 2005/09/18 17:01:05 bjorn Exp $
########################################################
# $Log: dovecot,v $
# Revision 1.1  2005/09/18 17:01:05  bjorn
# Dovecot filters written by Patrick Vande Walle.
#
########################################################
# Please send all comments, suggestions, bug reports,
#    etc, to logwatch-devel@logwatch.org
########################################################
# The Dovecot script was written by:
#   Patrick Vande Walle <patrick@isoc.lu>
# Based on previous work by 
#    Pawel Golaszewski <blues@gda.pl>
#
# TODO:
# - use printf features to align text in table
# 
########################################################

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

if ( $Debug >= 5 ) {
    print STDERR "\n\nDEBUG \n\n";
}

while (defined($ThisLine = <STDIN>)) {
     # remove timestamp.  We can't use *RemoveHeaders because we need the
     # service name
     $ThisLine =~ s/^\w{3} .\d \d\d:\d\d:\d\d [^ ]* //;
     if ( ($User, $Host) = ( $ThisLine =~ /^pop3-login: Login: (.*?) (\[.*\])/ ) ) {
      $Login{$User}{$Host}++;
      $ConnectionPOP3{$Host}++;
      $Connection{$Host}++; 
   } elsif ( ($User, $Host) = ( $ThisLine =~ /^imap-login: Login: (.*?) (\[.*\])/ ) ) {
      $Login{$User}{$Host}++;
      $ConnectionIMAP{$Host}++;
      $Connection{$Host}++; 
   } elsif ($ThisLine =~ /Disconnected /) {
      # ignore
   } elsif (($Reason, $Host) = ($ThisLine =~ /Disconnected: (.*) (\[.*\])/) ) {
      # $Host not used - is it helpful?
      $Disconnected{$Reason}++;
   } elsif (($Reason, $Host) = ($ThisLine =~ /TLS initialization failed/) ) {
      $TLSInitFail++;
   } elsif (($Host) = ($ThisLine =~ /Aborted login (\[.*\])/) ) {
      $Aborted{$Host}++;
   } else {
      # Report any unmatched entries...
      chomp($ThisLine);
      $OtherList{$ThisLine}++;
   }
}

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

if ( ( $Detail >= 5 ) and (keys %Connection)) {
   print     "\n[Dovecot IMAP and POP3] Connections:".
             "\n====================================".
             "\n                      Host |     POP3    |   IMAP   |   Total ".
             "\n-------------------------- | ----------- |--------- | ---------";
  
   $TLSInitFail = 0;
   foreach $Host (sort keys %Connection) {
      $Total = $Connection{$Host};
      if (defined ($ConnectionPOP3{$Host})) {
         $Conns = $ConnectionPOP3{$Host};
      } else {
         $Conns = 0;
      }
      if (defined ($ConnectionIMAP{$Host})) {
         $IMAP = $ConnectionIMAP{$Host};
      } else {
         $IMAP = 0;
      }
      $HostLength = length($Host);
      $HostSpaceLength = 26 - $HostLength;
      $CountLength = length("$Conns");
      $CountSpaceLength = 12 - $CountLength;
      $IMAPLength = length("$IMAP");
      $IMAPSpaceLength = 9 - $IMAPLength;
      $TotalLenght = length("$Total");
      $TotalSpaceLength = 10 - $TotalLenght;
      print "\n" ." " x $HostSpaceLength . $Host . " |" . " " x $CountSpaceLength .  $Conns .
            " |" . " " x $IMAPSpaceLength . $IMAP . " |" . " " x $TotalSpaceLength . $Total;
      $POP3Count += $Conns;
      $IMAPCount += $IMAP;
      $TotalCount += $Total;
   }
   $POP3Length = length("$POP3Count");
   $POP3SpaceLength = 40 - $POP3Length;
   $IMAPLength = length("$IMAPCount");
   $IMAPSpaceLength = 9 - $IMAPLength;
   $TotalLength = length("$TotalCount");
   $totalSpaceLength = 10 - $TotalLength;
   print "\n" . "-" x 63;
   print "\n" . " " x $POP3SpaceLength . $POP3Count . " |" . " " x $IMAPSpaceLength . $IMAPCount .
                 " |" . " " x $totalSpaceLength . $TotalCount . "\n";
}

if ( ( $Detail >= 10 ) and (keys %Login)) {
   print "\n\nDovecot IMAP and POP3 Successful Logins:";
   $LoginCount = 0;
   foreach my $User (keys %Login) {
      print "\n\n  User $User:";
      $UserCount = 0;
      foreach $Host (keys %{$Login{$User}}) {
         $HostCount = $Login{$User}{$Host};
         print "\n    From $Host: $HostCount Time(s)";
         $UserCount += $HostCount;
      }
      $LoginCount += $UserCount;
      print "\n  Total: $UserCount Time(s)";
   }
   print "\n\nTotal: $LoginCount successful logins";
}

if (keys %Disconnected) {
   print "\n\nDovecot disconnects:";
   foreach my $Reason (sort keys %Disconnected) {
      print "\n   $Reason: $Disconnected{$Reason} Time(s)";
   }
}

if ((keys %Aborted) && ($Detail >= 10)) {
   print "\n\nLogout/aborts:";
   foreach my $Host (sort keys %Aborted) {
      print "\n   $Host: $Aborted{$Host} Time(s)";
   }
}

if ($TLSInitFail > 0) {
   print "\n\nTLS Initialization failed $TLSInitFail Time(s)";
}

if (keys %OtherList) {
   print "\n\n**Unmatched Entries**\n";
   foreach $line (sort {$a cmp $b} keys %OtherList) {
      print "   $line: $OtherList{$line} Time(s)\n";
   }
}

exit(0);


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