#!/usr/bin/perl
##########################################################################
# $Id: cron,v 1.4 1999/01/23 18:10:55 kirk Exp $
##########################################################################
# $Log: cron,v $
# Revision 1.4  1999/01/23 18:10:55  kirk
# Prepping for Version 1.6.4
#
# Revision 1.3  1998/04/06 23:36:05  kirk
# Applied changes submitted by Luuk de Boer <luuk_de_boer@pi.net>.
#
# Revision 1.2  1998/03/19 06:43:13  kirk
# Added support for /var/log/secure
#
# Revision 1.1  1998/03/19 04:28:00  kirk
# Added support form /var/log/cron messages
#
##########################################################################

########################################################
# 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>)) {
    if ( ($User, $Command) = ($ThisLine =~ /^([^ ]+) \([^ ]+\) CMD \((.+)\)$/ ) ) {
	$Runs->{$User}->{$Command}++;
    }
    elsif ( ($User) = ($ThisLine =~ /^([^ ]+) \([^ ]+\) (BEGIN|END) EDIT \((.+)\)$/ ) ) {
	$Runs->{$User}->{'personal crontab editted'} += 0.5;
    }
    elsif ( ($User) = ($ThisLine =~ /^([^ ]+) \([^ ]+\) REPLACE \((.+)\)$/ ) ) {
	$Runs->{$User}->{'personal crontab replaced'}++;
    }
    elsif ( ($User) = ($ThisLine =~ /^([^ ]+) \([^ ]+\) LIST \((.+)\)$/ ) ) {
	$Runs->{$User}->{'personal crontab listed'}++;
    }
    elsif ( ($User) = ($ThisLine =~ /^([^ ]+) \([^ ]+\) DELETE \((.+)\)$/ ) ) {
	$Runs->{$User}->{'personal crontab deleted'}++;
    }
    elsif ( $ThisLine =~ /^CRON \([^ ]+\) STARTUP \(fork ok\)$/ ) {
	$Startups++;
    }
    elsif ( $ThisLine =~ /^\*system\* \([^ ]+\) RELOAD \(\/etc\/crontab\)$/ ) {
	$Reloads++;
    }
    elsif ( $User = ($ThisLine =~ /^(.*) \([^ ]+\) RELOAD \(.*\)$/ ) ) {
	$UserReloads{$User}++;
    }
    else {
	# Report any unmatched entries...
	push @OtherList,$ThisLine;
    }
}

if ($Detail >= 5) {

    print "\n\n --------------------- Cron Begin ------------------------ \n";

    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 (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;
    }

    print "\n\n ---------------------- Cron End ------------------------- \n\n";

}

exit(0);
