#!/usr/bin/perl

@lines=`/usr/local/opmon/bin/opmon -v /usr/local/opmon/etc/opmon.cfg`;

$errors=0;
$warnings=0;
$text_errors="";
$error_detected=0;
foreach $line (@lines) {
   $search_pattern="Total Warnings";
   if ($line=~/$search_pattern/) {
      ($text1,$text2,$warnings)=split(/[: ]+/,$line);
   }
   $search_pattern="Total Errors";
   if ($line=~/$search_pattern/) {
      ($text1,$text2,$errors)=split(/[: ]+/,$line);
   }
   $search_pattern="Error:";
   if ($line=~/$search_pattern/) {
      chomp($line);
      $text_errors=$text_errors."/ $line / ";
      $error_detected=1;
   }
}
chomp($warnings);
chomp($errors);

if ($warnings==1) {
   print "WARNING - $warnings warnings encontrados no OpMon\n";
   exit(1);
}
if ($error_detected==1) {
   print "CRITICAL - Erros no OpMon $text_errors\n";
   exit(2);
}
if ($errors>0) {
   print "CRITICAL - $errors erros encontrados no OpMon\n";
   exit(2);
}
print "OK - $errors erros encontrados, $warnings warnings encontrados\n";
exit(0);
