#!/usr/bin/perl
# $Id: wview 215 2011-11-26 00:04:31Z mwall $
# logwatch script to process wview log files
# Copyright 2011 Matthew Wall, all rights reserved
#
# Revision History
#  0.3 25nov11
#   * do not print title if no matching lines
#  0.2 21nov11
#   * consolidate duplicate line entries
#   * strip time and host info from reported log entries
#  0.1 21nov11
#   * initial release

use strict;

my @cfg = ();
my @info = ();
my @warn = ();
my @fail = ();
my @unmatched = ();

my %cfgcnt = ();
my %infocnt = ();
my %warncnt = ();
my %failcnt = ();

while(defined($_ = <STDIN>)) {
    chomp;
    if (/configured/ ||
        /generating to/ ||
        /templates at/ ||
        /station interface/ ||
        /station location/ ||
        /station polling interval/ ||
        /station archive interval/ ||
        /Dual units will be displayed/ ||
        /rain\/ET YTD preset Year set to/ ||
        /Rain YTD preset set to/ ||
        /ET YTD preset set to/ ||
        /Rain Storm Stop Time set to/ ||
        /Rain Season Start Month set to \d+/ ||
        /Rain Storm Start Trigger set to/ ||
        /Rain YTD preset Year set to/) {
        record(\@cfg, \%cfgcnt, $_);
    } elsif (/SIGHUP - toggling log verbosity (\S+)/ ||
             /SIGHUP - re-reading image\/html config files/ ||
             /recv signal \d+/ ||
             /recv sig \d+/ ||
             /exiting normally/ ||
             /started as a daemon/ ||
             /wview .* starting/ ||
             /newest archive record/) {
        record(\@info, \%infocnt, $_);
    } elsif (/forecast html tags are disabled/ ||
             /PMON: .* disable/) {
        record(\@warn, \%warncnt, $_);
    } elsif (/createOutFile: cannot open.*/ ||
             /htmlgenOutputFiles: .* failed/) {
        record(\@fail, \%failcnt, $_);
    } elsif (/Generated: / ||
             /Adding \d+ minute sample/ ||
             /Adding day sample/ ||
             /computeDataDay: \d+/ ||
             /computeDataWeek: \d+/ ||
             /computeDataMonth: \d+/ ||
             /computeDataYear: \d+/ ||
             /computeDataAllTime: \d+/ ||
             /Station Init Start/ ||
             /Station Init Complete/ ||
             /Storing day history/ ||
             /Adding hour sample/ ||
             /Tag Search red-black tree/ ||
             /started on radlib system/ ||
             /initializing barometric pressure/ ||
             /initializing historical stores/ ||
             /initializing computed data values/ ||
             /starting html generation in/ ||
             /doing initial html generation now/ ||
             /running\.\.\./ ||
             /received station info from wviewd/ ||
             /htmlHistoryInit: DAY/ ||
             /htmlHistoryInit: WEEK/ ||
             /htmlHistoryInit: MONTH/ ||
             /htmlHistoryInit: YEAR/ ||
             /htmlHistoryInit: storing day history/ ||
             /htmlmgrInit: .* definitions added/ ||
             /htmlmgrInit: \d+ templates added/ ||
             /htmlmgrReReadImageFiles: / ||
             /QueueMsgHandler: removed empty MIIB/ ||
             /ARCREC: initializing archive browser files/ ||
             /HILOW: database OK/ ||
             /HILOW: OK/ ||
             /HILOW: beginning normal LOOP operation/ ||
             /NOAA DB: syncing/ ||
             /NOAA DB: done/ ||
             /NOAA DB: OK/ ||
             /NOAA DB: nothing to do/ ||
             /NOAA Generate: updating/ ||
             /NOAA Generate: creating/ ||
             /NOAA: initializing reports/ ||
             /NOAA: \d+ days, \d+ months/ ||
             /NOAA: \d+ months, \d+ years/ ||
             /PMON:/) {
        # ignore
    } else {
        push @unmatched, $_;
    }
}

report("failures", \@fail, \%failcnt) if $#fail >= 0;
report("warnings", \@warn, \%warncnt) if $#warn >= 0;
report("info", \@info, \%infocnt) if $#info >= 0;
report("configuration", \@cfg, \%cfgcnt) if $#cfg >= 0;
report("unmatched lines", \@unmatched) if $#unmatched >= 0;

exit 0;

sub record {
    my($aref, $href, $logstr) = @_;
    my($msg) = $logstr =~ /\S+\[\d+\]: <\d+> : (.*)/;
    if ($href->{$msg}) {
        $href->{$msg}++;
    } else {
        $href->{$msg} = 1;
        push @$aref, $msg;
    }
}

sub report {
    my($label, $aref, $href) = @_;
    print "\n$label:\n";
    foreach my $x (@$aref) {
        my $str = $x;
        if ($href && $href->{$x} > 1) {
            $str .= " ($href->{$x} times)";
        }
        print "  $str\n";
    }
}
