<h1 class="pagehead">Log Entries</h1>
<FORM ACTION="<%$r->uri%>" METHOD="get">
<DIV>
<TABLE CLASS="log-ent">
<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}};
        $odd++;
</%perl>
<TR>
    <TD CLASS="match-<%$odd%2%>"><SPAN STYLE="white-space:nowrap"><% $l->{class} %></SPAN></TD>
    <TD CLASS="match-<%$odd%2%>"><% $entry %></TD>
    <TD CLASS="match-<%$odd%2%>">
        <A CLASS="navbutton" HREF="<%$r->uri%>?log=<%$l->{id}%>&<%$r->args%>">X</A>
    </TD>
    <TD CLASS="match-<%$odd%2%>"><SPAN STYLE="white-space:nowrap"><% $time |h %></span></TD>
</TR>
% if ($details){
<TR>
    <TD CLASS="match-<%$odd%2%>">&nbsp;</TD>
    <TD CLASS="entry-<%$odd%2%>" COLSPAN=3><PRE><%$details |h%></PRE></TD>
</TR>
% }
%   }
%}
</TABLE>
<h2 class="subheader">Entry Types to Show</h2>
<TABLE class="log-key">
<TR>
    <TD>
% foreach my $c (keys %$db_classes){
        <%$c%>:<INPUT TYPE="checkbox" NAME="classes" VALUE="<%$c%>" <% grep(/^$c$/,@classes) ? 'CHECKED': ''%>> &nbsp;
%}
    </TD>
</TR>
<TR>
    <TD>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>
    </TD>
</TR>
<TR>
    <TD><INPUT TYPE="submit" CLASS="navbutton" VALUE="Show Entries"></TD>
</TR>
</TABLE>
</DIV>
</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$
%# vim:syntax=mason
