http://www.perlmonks.org?node_id=109292
Category: Utility Scripts
Author/Contact Info doug@nextdimensioninc.com
Description:

This is a script I threw together to provide a web version of the Linux message log, indexed by service name. I'm a new perl coder so comments and especially suggestions would be much appreciated...

NOTE: This code has been modified as a result of the helpful suggestions given to me... ;)

09/02/2001 - heavier modifications now, I have put the file write stuff into a sub where it belongs and added code to read the logfile both by service and by date. By the way, this script should work for any syslog type log, I have tried it on /var/log/secure and it works like a charm...

09/03/2001 - added variables for HTML colour settings, kind of a poor man's style sheet ;).

#!/usr/bin/perl -w

use strict;

# 'global' variable declarations go here

my $bgcolor= "silver";
my $fontcolor = "white";
my $headcolor = "#33ccff";
my $linkcolor = "red";
my $outpath = "/var/www/html";
my $msg = "/var/log/messages";
my %messages;
my %msgbydate;
my $tempcounter;
my %flag;
my $key;
my $date;
my %number = (     "Jan" => "1",
        "Feb" => "2",
        "Mar" => "3",
        "Apr" => "4",
        "May" => "5",
        "Jun" => "6",
        "Jul" => "7",
        "Aug" => "8",
        "Sep" => "9",
        "Oct" =>"10",
        "Nov" =>"11",
        "Dec" =>"12");
open (MESSAGELOG,$msg) or die "failed to open $msg: $!\n";
while (<MESSAGELOG>)
{
    my ($month,$day,$time,$host,$service,undef) = split;
    $date = $number{$month}."-".$day;

#    strip out anything between non word characters (basically looking
+ to eliminate pids)
    $service =~s/(\W[\d\w\W]+\W)//g;
    $service =~s/://g;
#    print "- $service\n";
    $messages{$service}++;
    $msgbydate{$date}++;
    unless ($service=~m/--/) {&writefile ($flag{$service},$service,$_)
+;}
    $flag{$service} = 1;
    &writefile ($flag{$date},$date,$_);
    $flag{date}=1;
}
close MESSAGELOG;
my $outfile = $outpath ."/messages.html";
open (TEMPOUT,">$outfile") or die "failed to open $outfile: $!\n";
print TEMPOUT <<"END";
<html>
<body>
<table bgcolor=silver text=white link=white border=1>
<tr>
<td colspan=2 bgcolor=#33ccff text=white>
<b>Message Log Viewer</b>
</td>
</tr><tr>
<td bgcolor=$bgcolor text=$fontcolor>
<a href="messageservices.html">by service</a>
</td>
<td bgcolor=$bgcolor text=$fontcolor>
<a href="messagedates.html">by date</a>
</td>
</tr>
</table>
</body>
</html>
END
close TEMPOUT;
$outfile = $outpath ."/messageservices.html";
open (TEMPOUT,">$outfile") or die "failed to open $outfile: $!\n";

print TEMPOUT <<"END";
<html>
<body>
<h1>Summary of $msg</h1>
<br>
<table>
<tr>
<td>
<table bgcolor=$bgcolor text=$fontcolor link=$linkcolor border=1>
<tr>
    <td bgcolor=$headcolor text=$fontcolor>Service</td>
    <td bgcolor=$headcolor text=$fontcolor># of Msgs</td>
</tr>
END

foreach $key (sort keys %messages)
{
    unless ($key=~m/--/)
    {
        print TEMPOUT "<tr><td bgcolor=$bgcolor text=$fontcolor>$key</
+td><td><a href=$key.html>$messages{$key}</a></td></tr>";
    }
}
print TEMPOUT <<"END";
</table>
</td>
</tr>
</table>
</body>
</html>
END
close TEMPOUT;
$outfile = $outpath."/messagedates.html";
open (TEMPOUT,">$outfile") or die "failed to open $outfile: $!\n";
print TEMPOUT <<"END";
<html>
<body>
<h1>Summary of $msg</h1>
<br>
<table>
<tr>
<td valign=top>
<table bgcolor=$bgcolor text=$fontcolor link=$linkcolor border=1>
<tr>
    <td bgcolor=$headcolor text=$fontcolor>Date</td>
    <td bgcolor=$headcolor text=$fontcolor># of Msgs</td>
</tr>
END
foreach $key (sort keys %msgbydate)
{
    print TEMPOUT "<tr><td bgcolor=silver text=white>$key</td><td><a h
+ref=$key.html>$msgbydate{$key}</a></td></tr>";
}
print TEMPOUT <<"END";
</table>
</table>
</body>
</html>
END
close TEMPOUT;

sub writefile
{
    my $flag = $_[0];
    my $label = $_[1];
    my $line = $_[2];
        unless ($flag)
    {
        open(TEMPOUT,">".$outpath."/".$label.".html") or die "failed t
+o open $date: $!\n";
        $flag{$date}=1;
    }
    else
    {
        open(TEMPOUT,">>".$outpath."/".$label.".html") or die "failed 
+to open $date: $!\n";        
    }
        print TEMPOUT $line;
        print TEMPOUT "<br>";
        close TEMPOUT;
    
    
}