Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Linux message log webifier

by hans_moleman (Beadle)
on Aug 31, 2001 at 08:33 UTC ( [id://109292]=sourcecode: print w/replies, xml ) Need Help??
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;
    
    
}
Replies are listed 'Best First'.
Re: Linux message log webifier
by wog (Curate) on Aug 31, 2001 at 09:00 UTC
    ... since you asked for comments .... Two key issues here:

    • Please use warnings and strict.
    • Always check the return value of system calls like open, so you don't get very odd things happening when/if they fail, e.g.:
      open (TEMPOUT,">$outfile") or die "opening $outfile: $!\n";

    Less importantly:

    • You could improve much of your HTML outputing code with heredocs:
      print TEMPOUT <<"END"; <html><body> ... END
    • You may wish to look at the $. line-counting variable documented in perlvar as an alternative to your counter variable.
    • You output malformed HTML. Please close all tags,use DOCTYPE at the beginning of your document, and quote attribute names not containing only alphanumeric characters, etc.

      thanks for your comments!

      it is appreciated...

      Actually, I found the script was difficult to debug because of file issues, so your point about evaluating system calls is especially well taken. ;)

      You'll note I have implemented most of your suggestions. Sure looks a lot cleaner ouputing the HTML that way... Thanks again.

(Ovid) Re: Linux message log webifier
by Ovid (Cardinal) on Aug 31, 2001 at 20:07 UTC
    (my $month, my $day, my $time, my $host, my $service, undef) = split;

    This is better written as:

    my ( $month, $day, $time, $host, $service ) = split;

    You can leave off the "undef" as Perl will simply discard that last element for you.

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://109292]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-11-10 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (37 votes). Check out past polls.