#!/usr/bin/perl
##########################################################################
# $Id: cron,v 1.9 2002/10/12 02:08:18 kirk Exp $
##########################################################################

########################################################
# This was written and is maintained by:
#    Kirk Bauer <kirk@kaybee.org>
#
# Please send all comments, suggestions, bug reports,
#    etc, to kirk@kaybee.org.
########################################################

$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};

while (defined($ThisLine = <STDIN>)) {
   chomp($ThisLine);
   if (($ThisLine =~ s/^([^ ]+) \([^ ]+\)\s+//) or ($ThisLine =~ s/^... .. ..:..:.. [a-zA-Z0-9.\-_]+ \w+\[\d+\]: \((\S+)\)\s+//)) {
      $User = $1;
      if ($ThisLine =~ s/^CMD \((.+)\)\s*$/$1/) {
              $Runs->{$User}->{$ThisLine}++;
      } elsif ($ThisLine =~ s/^(BEGIN|END) EDIT \((.+)\)\s*$/$2/) {
              $Runs->{$ThisLine}->{'personal crontab edited'} += 0.5;
      } elsif ($ThisLine =~ s/^REPLACE \((.+)\)\s*$/$1/) {
              $Runs->{$ThisLine}->{'personal crontab replaced'}++;
      } elsif ($ThisLine =~ s/^LIST \((.+)\)\s*$/$1/) {
              $Runs->{$ThisLine}->{'personal crontab listed'}++;
      } elsif ($ThisLine =~ s/^DELETE \((.+)\)\s*$/$1/) {
              $Runs->{$User}->{'personal crontab deleted'}++;
      } elsif ($ThisLine =~ /^STARTUP \(fork ok\)\s*$/ ) {
              $Startups++;
      } elsif ( $ThisLine =~ /^RELOAD \(.+\)\s*$/ ) {
              $Runs->{$User}->{'personal crontab reloaded'}++;
      } else {
         # Report any unmatched entries...
         push @OtherList, "$ThisLine\n";
      }
   } elsif ( $ThisLine =~ /^RELOAD \(.+\)\s*$/ ) {
      $Reloads++;
   } elsif ( $ThisLine =~ /Updated timestamp for job/ ) {
      # Ignore
   } elsif ( $User = ($ThisLine =~ /^(.*) \([^ ]+\) RELOAD \(.*\)$/ ) ) {
      $UserReloads{$User}++;
   } else {
      # Report any unmatched entries...
      push @OtherList, "$ThisLine\n";
   }
}

if (keys %{$Runs} and ($Detail >= 5)) {
   print "Commands Run:\n";
   foreach $i (sort {$a cmp $b} keys %{$Runs}) {
      print "   User " . $i . ":\n";
      foreach $j (sort {$a cmp $b} keys %{$Runs->{$i}}) {
         print "      " . $j . ": " . $Runs->{$i}->{$j} . " Time(s)\n";
      }
   }
}

if ($Detail >= 10) {
   if (keys %UserReloads) {
      print "   User crontabs reloaded:\n";
      foreach $i (keys %UserReloads) {
         print '      ' . $i . ' ' . $UserReloads{$i} . " Time(s)\n";
      }
   }

   if ($Startups) {
      print "   CRON Restarted " . $Startups . " Time(s)\n";
   }

   if ($Reloads) {
      print "   CRON Reloaded /etc/crontab " . $Reloads . " Time(s)\n";
   }
}

if ($#OtherList >= 0) {
   print "\n**Unmatched Entries**\n";
   print @OtherList;
}

exit(0);
