Home » Databases » Sybase » ASE » An easy way to view maintenance logs using a browser using Perl & Mason

An easy way to view maintenance logs using a browser using Perl & Mason

stats/index.html:

 < %method title>
  DBServices: Update Statistics Logs
 
 <center>
 <h1>Update Statistics Logs</h1>
 < & ‘../.components/ase/maint_logs.comp’, path => "/dbms/sybase/dba/out", file_part => "update_stats" &>
  </center>

stats/dhandler:

  < %perl>
   my $self = shift;
   
   $self->{once_header} = ”;
   $self->{header} = ”;
   $self->{body} = ”;
   $self->{footer} = ”;
   $self->{current_block} = ”;

   my $log_path = ‘/dbms/sybase/dba/out/’;
   my $file = $m->dhandler_arg;

   $file =~ s/\.html$//;

   if ($file =~ m/\.out$/) {
     if (-f $log_path . $file) {
       my $FH;
       if (open($FH, "< ", $log_path . $file)) {
         while (my $line = < $FH>) {
           $m->print($line);
         }
       } else {
         $m->abort(404);
       }
     } else {
       $m->abort(404);
     }
   } elsif ($file =~ m/\.gz$/) {
     if (-f $log_path . $file) {
       my $gFH;
       use Compress::Zlib;
       if ( my $gFH = gzopen($log_path . $file, "rb") ) {
         while ($gFH->gzreadline($_) > 0 ) {
           $m->print($_);
         }
         $gFH->gzclose;
       } else {
         $m->abort(404);
       }
     } else {
       $m->abort(404);
     }
   }

../.components/ase/maint_logs.comp:

  < %args>
   $path
   $file_part
 
 < %perl>
   use File::Find;
   our $part = $file_part;
   our %log_hash = ();
  sub wanted {
     return unless (/$part/);
     my $file_name = $_;
     if ($file_name =~ /$part\_(.+)\.out$/) {
       $log_hash{$1}{current} = $file_name;
     } elsif ($file_name =~ m/$part\_(.+)\.out\.(\d+)\.gz$/) {
       if ($1 && $2) {
         $log_hash{$1}{archive}{$2} = {
           file_name  => $file_name,
           file_date  => scalar localtime( (stat($File::Find::name))[9] )
         };
       }
     }
   }
   find(\&wanted, $path);
   $m->comp(‘.print_table_head’);
   foreach my $server (sort { lc($a) cmp lc($b) } keys %log_hash) {
     $m->comp(‘.print_table’,
       server     => $server,
       current_log    => $log_hash{$server}{current},
       archive_ref    => \%{ $log_hash{$server}{archive} }
     );
   }
   $m->comp(‘.print_table_tail’);
 
 < %def .print_table_head>
 <table BORDER=1 BORDERCOLOR="BLACK">
   <th>Server</th>
   <th>Current Log</th>
   <th>Archived Logs</th>
 
 < %def .print_table>
   < %args>
     $server
     $current_log
     $archive_ref
   
   <tr>
     <td>< % $server %></td>
     <td>< % sprintf "<a href=’%s’>%s", $current_log . ".html", $current_log %></td>
     <td>< %perl>
         my $str = "";

         foreach my $archive_num (sort { $a < => $b } keys %$archive_ref ) {
           $str .= sprintf "<a href=’%s’>%s</a> (%s)",
             $archive_ref->{$archive_num}{file_name} . ".html",
             $archive_ref->{$archive_num}{file_name},
             $archive_ref->{$archive_num}{file_date};
         }
         $m->print($str);
     </td>
   </tr>
 
 < %def .print_table_tail>
 </table>
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Facebook login by WP-FB-AutoConnect