ASNMTAP

 view release on metacpan or  search on metacpan

plugins/templates/check_template-cluster.pl  view on Meta::CPAN

if ( $method =~ /^(?:non-OK|percentage|weight)$/ ) {
  $warning            = $objectPlugins->getOptionsArgv ('warning');
  $critical           = $objectPlugins->getOptionsArgv ('critical');

  if ( $method eq 'non-OK' ) {
    $objectPlugins->printUsage ( 'Critical threshold ' .$critical. ' should be larger than warning threshold ' .$warning ) unless ( $critical > $warning );
    $objectPlugins->printUsage ( 'Warning threshold should be less than count uKey' ) unless ( $warning < $uKeyCount );
    $objectPlugins->printUsage ( 'Critical threshold should be less or equal than count uKey' ) unless ( $critical <= $uKeyCount );

    $downgradingStatus  =  $objectPlugins->getOptionsArgv ('downgradingStatus') ? $objectPlugins->getOptionsArgv ('downgradingStatus') : 'T';
    $objectPlugins->printUsage ( 'Command line parameters for downgradingStatus: F(alse) or T(rue)' ) unless ( $downgradingStatus =~ /^[FT]$/ );
  } elsif ( $method eq 'percentage' ) {
    $objectPlugins->printUsage ( 'Warning percentage should be less or equal than 100' ) unless ( $warning <= 100 );
    $objectPlugins->printUsage ( 'Critical percentage should be less or equal than 100' ) unless ( $critical <= 100 );
  } elsif ( $method eq 'weight' ) {
    $objectPlugins->printUsage ( 'Critical weight ' .$critical. ' should be larger than warning weight ' .$warning ) unless ( $critical > $warning );
  }
}

my $outOfDate         =  $objectPlugins->getOptionsArgv ('outOfDate');
$objectPlugins->printUsage ( 'Missing command line argument out of date' ) unless ( defined $outOfDate );
$outOfDate *= 60; # convert to seconds

my $ignoreDependent   =  $objectPlugins->getOptionsArgv ('ignoreDependent')   ? $objectPlugins->getOptionsArgv ('ignoreDependent')   : 'F';
$objectPlugins->printUsage ( 'Command line parameters for ignoreDependent: F(alse) or T(rue)' ) unless ( $ignoreDependent  =~ /^[FT]$/ );

my $ignoreOffline     =  $objectPlugins->getOptionsArgv ('ignoreOffline')     ? $objectPlugins->getOptionsArgv ('ignoreOffline')     : 'F';
$objectPlugins->printUsage ( 'Command line parameters for ignoreOffline: F(alse) or T(rue)' ) unless ( $ignoreOffline =~ /^[FT]$/ );

my $ignoreNoTest      =  $objectPlugins->getOptionsArgv ('ignoreNoTest')      ? $objectPlugins->getOptionsArgv ('ignoreNoTest')      : 'F';
$objectPlugins->printUsage ( 'Command line parameters for ignoreNoTest: F(alse) or T(rue)' ) unless ( $ignoreNoTest =~ /^[FT]$/ );

my $serverDB          = $objectPlugins->getOptionsArgv ('server')   ? $objectPlugins->getOptionsArgv ('server')   : 'localhost';
my $port              = $objectPlugins->getOptionsArgv ('port')     ? $objectPlugins->getOptionsArgv ('port')     : 3306;
my $database          = $objectPlugins->getOptionsArgv ('database') ? $objectPlugins->getOptionsArgv ('database') : 'asnmtap';
my $username          = $objectPlugins->getOptionsArgv ('username') ? $objectPlugins->getOptionsArgv ('username') : 'asnmtap';
my $password          = $objectPlugins->getOptionsArgv ('password') ? $objectPlugins->getOptionsArgv ('password') : '<PASSWORD>';

my $debug             = $objectPlugins->getOptionsValue ('debug');

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

foreach $_uKey ( @uKeys ) { $uKeys{$_uKey}{status} = $ERRORS{'NO DATA'}; }
foreach $_status ( $ERRORS{'OK'} .. $ERRORS{'NO DATA'} ) { $status{$STATE{$_status}} = 0; }
$cluster{non_OK} = 0; foreach $_status ( $ERRORS{'OK'} .. $ERRORS{UNKNOWN} ) { $cluster{$STATE{$_status}} = 0; }
foreach $_status ( $ERRORS{OK} .. $ERRORS{CRITICAL} ) { $weight{$STATE{$_status}} = 0; };

my ( $dbh, $sth );

$dbh = DBI->connect ("DBI:mysql:$database:$serverDB:$port", "$username", "$password") or _ErrorTrapDBI ( 'Could not connect to MySQL server '. $serverDB, "$DBI::err ($DBI::errstr)" );

if ( $dbh ) {
  my $rv = 1;

  foreach $_uKey ( keys ( %uKeys ) ) {
    my ($catalogID, $id, $uKey, $status, $step, $timeslot, $instability, $persistent, $downtime);

    my $sqlSTRING = "SELECT catalogID, id, uKey, status, step, timeslot, instability, persistent, downtime FROM `events` WHERE catalogID='$CATALOGID' and uKey='$_uKey' ORDER BY timeslot DESC LIMIT 1";
    $sth = $dbh->prepare( $sqlSTRING ) or $rv = _ErrorTrapDBI ( \$objectPlugins,  'Cannot dbh->prepare: '. $sqlSTRING );
    $sth->execute() or $rv = _ErrorTrapDBI ( \$objectPlugins,  'Cannot dbh->execute: '. $sqlSTRING ) if $rv;
    $sth->bind_columns( \$catalogID, \$id, \$uKey, \$status, \$step, \$timeslot, \$instability, \$persistent, \$downtime ) or $rv = _ErrorTrapDBI ( \$objectPlugins,  'Cannot dbh->bind: '. $sqlSTRING ) if $rv;
    print "$CATALOGID & $_uKey\n    $sqlSTRING\n" if ( $debug );

    if ( $rv ) {
      if ( $sth->rows ) {
        my $currentTimeslot = timelocal ( (localtime)[0,1,2,3,4,5] );

        while( $sth->fetch() ) {
          $objectPlugins->appendPerformanceData ( "'$uKey'=" .$ERRORS{$status}. ';1;2;0;7' );

          $uKeys{$uKey}{id}          = $id;
          $uKeys{$uKey}{uKey}        = $uKey;
          $uKeys{$uKey}{status}      = $status;
          $uKeys{$uKey}{step}        = $step;
          $uKeys{$uKey}{timeslot}    = $timeslot;
          $uKeys{$uKey}{instability} = $instability;
          $uKeys{$uKey}{persistent}  = $persistent;
          $uKeys{$uKey}{downtime}    = $downtime;
          print "$catalogID, $id, $uKey, $status, $step, $timeslot, $instability, $persistent, $downtime\n" if ( $debug );

          if ( $currentTimeslot - $timeslot > $outOfDate ) {
            $objectPlugins->pluginValues ( { alert => "Result for $uKey out of date: $currentTimeslot - $timeslot > $outOfDate" }, $TYPE{APPEND} );
            $status = $STATE{$ERRORS{UNKNOWN}};
          } elsif ( $status eq $STATE{$ERRORS{DEPENDENT}} and $ignoreDependent eq 'T' ) {
            $objectPlugins->pluginValues ( { alert => "Result for $uKey ignored: $status" }, $TYPE{APPEND} );
            $status = $STATE{$ERRORS{OK}};
          } elsif ( $status eq $STATE{$ERRORS{OFFLINE}} and $ignoreOffline eq 'T' ) {
            $objectPlugins->pluginValues ( { alert => "Result for $uKey ignored: $status" }, $TYPE{APPEND} );
            $status = $STATE{$ERRORS{OK}};
          } elsif ( $status eq $STATE{$ERRORS{'NO TEST'}} and $ignoreNoTest eq 'T' ) {
            $objectPlugins->pluginValues ( { alert => "Result for $uKey ignored: $status" }, $TYPE{APPEND} );
            $status = $STATE{$ERRORS{OK}};
          }

          $status{$status}++;

          if ( $status eq 'OK' ) { 
		    $cluster{OK}++; 
          } else {
            $cluster{non_OK}++;

            if ( $status =~ /^(?:WARNING|CRITICAL)$/ ) {
		      $cluster{$status}++;
            } else {
              $cluster{UNKNOWN}++;
            }
          }

          if ( $method eq 'weight' ) {
            if ( $status eq 'OK' ) { 
              $weight{OK} += $uKeys{$uKey}{weight};
            } elsif ( $status eq 'WARNING' ) {
              $weight{WARNING}  += $uKeys{$uKey}{weight};
            } else {
              $weight{CRITICAL} += $uKeys{$uKey}{weight};
            }
          }
        }
      } else {
        $status{'NO DATA'}++;
        $objectPlugins->pluginValues ( { stateValue => $ERRORS{'UNKNOWN'}, alert => "No result for '$_uKey'" }, $TYPE{APPEND} );



( run in 0.562 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )