The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -P

# $RCSfile: scan_messages,v $$Revision: 4.1 $$Date: 92/08/07 17:20:37 $

# This prints out extraordinary console messages.  You'll need to customize.

chdir('/usr/adm/private/memories') || die "Can't cd to memories: $!\n";

$maxpos = `cat oldmsgs 2>&1`;

#if defined(mc300) || defined(mc500) || defined(mc700)
open(Msgs, '/dev/null') || die "scan_messages: can't open messages";
#else
open(Msgs, '/usr/adm/messages') || die "scan_messages: can't open messages";
#endif

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
   $blksize,$blocks) = stat(Msgs);

if ($size < $maxpos) {		# Did somebody truncate messages file?
    $maxpos = 0;
}

seek(Msgs,$maxpos,0);		# Start where we left off last time.

while (<Msgs>) {
    s/\[(\d+)\]/#/ && s/$1/#/g;
#ifdef vax
    $_ =~ s/[A-Z][a-z][a-z] +\w+ +[0-9:]+ +\w+ +//;
    next if /root@.*:/;
    next if /^vmunix: 4.3 BSD UNIX/;
    next if /^vmunix: Copyright/;
    next if /^vmunix: avail mem =/;
    next if /^vmunix: SBIA0 at /;
    next if /^vmunix: disk ra81 is/;
    next if /^vmunix: dmf. at uba/;
    next if /^vmunix: dmf.:.*asynch/;
    next if /^vmunix: ex. at uba/;
    next if /^vmunix: ex.: HW/;
    next if /^vmunix: il. at uba/;
    next if /^vmunix: il.: hardware/;
    next if /^vmunix: ra. at uba/;
    next if /^vmunix: ra.: media/;
    next if /^vmunix: real mem/;
    next if /^vmunix: syncing disks/;
    next if /^vmunix: tms/;
    next if /^vmunix: tmscp. at uba/;
    next if /^vmunix: uba. at /;
    next if /^vmunix: uda. at /;
    next if /^vmunix: uda.: unit . ONLIN/;
    next if /^vmunix: .*buffers containing/;
    next if /^syslogd: .*newslog/;
#endif
    next if /unknown service/;
    next if /^\.\.\.$/;
    if (/^[A-Z][a-z][a-z] [ 0-9][0-9] [ 0-9][0-9]:[0-9][0-9]/) {
	$pfx = '';
	next;
    }
    next if /^[ \t]*$/;
    next if /^[ 0-9]*done$/;
    if (/^A/) {
	next if /^Accounting [sr]/;
    }
    elsif (/^C/) {
	next if /^Called from/;
	next if /^Copyright/;
    }
    elsif (/^E/) {
	next if /^End traceback/;
	next if /^Ethernet address =/;
    }
    elsif (/^K/) {
	next if /^KERNEL MODE/;
    }
    elsif (/^R/) {
	next if /^Rebooting Unix/;
    }
    elsif (/^S/) {
	next if /^Sun UNIX 4\.2 Release/;
    }
    elsif (/^W/) {
	next if /^WARNING: clock gained/;
    }
    elsif (/^a/) {
	next if /^arg /;
	next if /^avail mem =/;
    }
    elsif (/^b/) {
	next if /^bwtwo[0-9] at /;
    }
    elsif (/^c/) {
	next if /^cgone[0-9] at /;
	next if /^cdp[0-9] at /;
	next if /^csr /;
    }
    elsif (/^d/) {
	next if /^dcpa: init/;
	next if /^done$/;
	next if /^dts/;
	next if /^dump i\/o error/;
	next if /^dumping to dev/;
	next if /^dump succeeded/;
	$pfx = '*' if /^dev = /;
    }
    elsif (/^e/) {
	next if /^end \*\*/;
	next if /^error in copy/;
    }
    elsif (/^f/) {
	next if /^found /;
    }
    elsif (/^i/) {
	next if /^ib[0-9] at /;
	next if /^ie[0-9] at /;
    }
    elsif (/^l/) {
	next if /^le[0-9] at /;
    }
    elsif (/^m/) {
	next if /^mem = /;
	next if /^mt[0-9] at /;
	next if /^mti[0-9] at /;
	$pfx = '*' if /^mode = /;
    }
    elsif (/^n/) {
	next if /^not found /;
    }
    elsif (/^p/) {
	next if /^page map /;
	next if /^pi[0-9] at /;
	$pfx = '*' if /^panic/;
    }
    elsif (/^q/) {
	next if /^qqq /;
    }
    elsif (/^r/) {
	next if /^read  /;
	next if /^revarp: Requesting/;
	next if /^root [od]/;
    }
    elsif (/^s/) {
	next if /^sc[0-9] at /;
	next if /^sd[0-9] at /;
	next if /^sd[0-9]: </;
	next if /^si[0-9] at /;
	next if /^si_getstatus/;
	next if /^sk[0-9] at /;
	next if /^skioctl/;
	next if /^skopen/;
	next if /^skprobe/;
	next if /^skread/;
	next if /^skwrite/;
	next if /^sky[0-9] at /;
	next if /^st[0-9] at /;
	next if /^st0:.*load/;
	next if /^stat1 = /;
	next if /^syncing disks/;
	next if /^syslogd: going down on signal 15/;
    }
    elsif (/^t/) {
	next if /^timeout [0-9]/;
	next if /^tm[0-9] at /;
	next if /^tod[0-9] at /;
	next if /^tv [0-9]/;
	$pfx = '*' if /^trap address/;
    }
    elsif (/^u/) {
	next if /^unit nsk/;
	next if /^use one of/;
	$pfx = '' if /^using/;
	next if /^using [0-9]+ buffers/;
    }
    elsif (/^x/) {
	next if /^xy[0-9] at /;
	next if /^write [0-9]/;
	next if /^xy[0-9]: </;
	next if /^xyc[0-9] at /;
    }
    elsif (/^y/) {
	next if /^yyy [0-9]/;
    }
    elsif (/^z/) {
	next if /^zs[0-9] at /;
    }
    $pfx = '*' if /^[a-z]+:$/;
    s/pid [0-9]+: //;
    if (/last message repeated ([0-9]+) time/) {
	$seen{$last} += $1;
	next;
    }
    s/^/$pfx/ if $pfx;
    unless ($seen{$_}++) {
	push(@seen,$_);
    }
    $last = $_;
}
$max = tell(Msgs);

open(tmp,'|sort >oldmsgs.tmp') || die "Can't create tmp file: $!\n";
while ($_ = pop(@seen)) {
    print tmp $_;
}
close(tmp);
open(tmp,'oldmsgs.tmp') || die "Can't reopen tmp file: $!\n";
while (<tmp>) {
    if (/^nd:/) {
	next if $seen{$_} < 20;
    }
    if (/NFS/) {
	next if $seen{$_} < 20;
    }
    if (/no carrier/) {
	next if $seen{$_} < 20;
    }
    if (/silo overflow/) {
	next if $seen{$_} < 20;
    }
    print $seen{$_},":\t",$_;
}

print `rm -f oldmsgs.tmp 2>&1; echo $max > oldmsgs 2>&1`;