<FORM ACTION="<%$r->uri%>" METHOD="get">
<h3>Log Entries</h3>
<TABLE CELLSPACING=0 CELLPADDING=3 WIDTH=100% BORDER=0>
<TR>
    <TH>Class</TH>
    <TH>Entry</TH>
    <TH>View Log</TH>
    <TH>Time Stamp</TH>
</TR>
<%perl>
my $odd = 0;
foreach my $class (sort keys %log_sort){
    next unless grep(/^$class$/,@classes);
    foreach my $l (sort {$b->{timestamp} <=> $a->{timestamp} } 
                   @{$log_sort{$class}}
                  ) {
        my $uri  = $r->uri;
        my $time = localtime($l->{timestamp});
        my $entry = $l->{entry};
        my $logfile = defined $l->{logfile} ? 1 : 0;
        # Make each nocdp log entry into links to device_search
        if ($class =~ /(nocdp|timeout)/){
            my @ret;
            foreach my $nocdp (split / /, $entry){
                  push (@ret, "<A HREF=\"device.html?ip=$nocdp\">$nocdp</A>");
            }
            $entry = join(' ',@ret);
        } elsif ($class =~ /(nosnmp)/){
            my @ret;
            foreach my $nosnmp (split / /, $entry){
                  push (@ret, "<A HREF=\"node.html?node=$nosnmp\">$nosnmp</A>");
            }    
            $entry = join(' ',@ret);
        }
        my $details = $logfiles->{$l->{id}};
</%perl>
<TR CLASS="match-<%++$odd%2%>">
    <TD><% $l->{class} %></TD>
    <TD><% $entry %></TD>
    <TD><% $logfile ? "<INPUT TYPE=CHECKBOX NAME=\"log\" VALUE=\"$l->{id}\">" : '&nbsp;'%></TD>
    <TD><% $time %></TD>
</TR>
% if ($details){
<TR CLASS="match-<%$odd%2%>">
    <TD COLSPAN=4 ALIGN=LEFT><PRE><%$details |h%></PRE></TD>
</TR>
% }
%   }
%}
</TABLE>
<h4>Entry Types to Show</h4>
% foreach my $c (keys %$db_classes){
<%$c%>:<INPUT TYPE="checkbox" NAME="classes" VALUE="<%$c%>" <% grep(/^$c$/,@classes) ? 'CHECKED': ''%>> &nbsp;
%}
<BR>
Number of Entries per category:
<SELECT NAME="number">
% foreach my $num (1,5,10,20,30,40,50,60) {
    <OPTION VALUE="<%$num%>" <% $number eq $num ? 'SELECTED' : '' %>><%$num%>
%}
</SELECT>
<BR>
<INPUT TYPE="submit" VALUE="Show Entries">
</FORM>
<%args>
@classes => qw/arp discover mac netbios/
$number  => 1
@log     => ()
</%args>
<%shared>
my $logfiles = {};
</%shared>
<%init>

# Check for stuffing of non-numerics
$number = 5 unless $number =~ /^\d+$/;

my $db_classes = sql_column('log',['distinct(class)','true']); 

my $log = [];
# Get the log entries we want
foreach my $class (@classes){
    # Check for non existant
    next unless defined $db_classes->{$class};
    my $logs = sql_rows('log', 
                    ['id','extract(epoch from creation) as timestamp','class','entry','logfile'],
                    {'class' => $class},undef,
                    "order by timestamp desc limit $number");
    push (@$log,@$logs);
}

# Get the Log Files if requested
foreach my $l (@log){
    my $db_entry = sql_hash('log',['id','logfile'],{'id'=>$l});
    unless (defined $db_entry){
        $logfiles->{$l} = 'Not found in Database'; 
        next;
    }
    my $file = $db_entry->{logfile};
    
    # Uncompressed Log Files
    if (-r $file){
        $logfiles->{$l} = '';
        my $ok = open (LOG, "<$file");
        unless ($ok){
            $logfiles->{$l} = "Couldn't open $file. $!\n";
            next;
        }
        while (<LOG>){
            $logfiles->{$l} .= $_;
        }
        close LOG;


    # Gzipped Log Files
    } elsif (-r "$file.gz"){
        eval {
            use Compress::Zlib;
        };
        if ($@){
            $logfiles->{$l} = 'Log File is Gzipped. Please install Perl module Compress::Zlib';
            next;
        }
        my $gz = gzopen("$file.gz", "rb");
        unless (defined $gz){
            $logfiles->{$l} = "Problem with gzcat. $gzerrno\n";
            next;
        }
        my $buffer;
        $logfiles->{$l} .= $buffer while $gz->gzread($buffer) > 0;
        $gz->gzclose();

    # Unknown / Permission files.
    } else {
        my $t1 = -r $file;  $t1 = $!;
        my $t2 = -r "$file.gz";  $t2 = $!;
        $logfiles->{$l} = "I can't read/find $file ($t1) or $file.gz ($t2).\n";
    }
}

# Split them up by class into separate arrays stored in this hash
my %log_sort;
foreach my $l (@$log){
   my $class = $l->{class};
   push (@{$log_sort{$class}},$l);
}

</%init>
%#
%# title()
%#
<%method title>
- Log \
</%method>
%# $Id: log.html,v 1.7 2004/11/09 20:17:55 maxbaker Exp $
%# vim:syntax=mason
