# Register this routine with the plugin registry
$CHECKFUNCS{'logs'} = \&check_logs;

# Load the logmon modules
use Spong::Client::logmon;

# Scan through the log files and flag any messages.

sub check_logs { 
   my $color    = "green";
   my( $message, $summary, $word, $ret, $hit, @hit, $time, $critical );
   my( $lm, @logs, $stat, $key, $numprob, $status, $text, $file, $chk, $lc);

   # If @LOGMON is not defined, load the log monitoring checks
   if (! defined @LOGMON) {
      foreach $lc (@$LOGCHECKS) {
         $lm = Spong::Client::logmon->new($lc->{'logfile'});

         foreach $chk ( @{$lc->{'checks'}} )  {
            $lm->add_check($chk);
         }
         push @LOGMON, $lm
      }
   }

   # Run the checks for all of the log files
   foreach $lm (@LOGMON) { $lm->check(); }

   # Scan each log monitor stati, build the message and calculate summary
   $message = "";
   foreach $lm (@LOGMON) {
      $message .= $lm->logfile() . "\n";
      $stat = $lm->stati(); $numprob = 0;
      foreach $key (%$stat) {
         $numprob++;
         $status = $stat->{$key}->{'status'};
         $text   = $stat->{$key}->{'text'};
         if ( $text or $status ) {
            $message .= "$status : $text\n";
         }
         $color = "yellow" if $status eq "yellow" and $color eq "green";
         $color = "red" if $status eq "red";
      }
      if ($numprob == 0) {$message .= "No bad messages\n"; }
      else { push @logs,$lm->logfile(); }
   }

   $summary = "All logs ok." if $color eq "green";
   $summary = "log problems: @logs" if $color ne "green";

   &debug( "logs - $color, $summary" );
   &status( $SPONGSERVER, $HOST, "logs", $color, $summary, $message );
}

# I'm included perl code, I need this line.
1;
