ASNMTAP
view release on metacpan or search on metacpan
lib/ASNMTAP/Asnmtap/Plugins.pm view on Meta::CPAN
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub timeout { &_checkAccObjRef ( $_[0] ); &_checkSubArgs1; $_[0]->{_timeout} = $_[1] if ( defined $_[1] ); $_[0]->{_timeout}; }
# Class accessor methods - - - - - - - - - - - - - - - - - - - - - - - -
sub setEndTime_and_getResponsTime {
&_checkAccObjRef ( $_[0] ); &_checkSubArgs1;
my ($endTimeSeconds, $endTimeMicroseconds) = gettimeofday();
$_[0]->{_pluginValues}->{endTime} = "$endTimeSeconds.$endTimeMicroseconds";
my ($startTimeSeconds, $startTimeMicroseconds) = split (/\./, $_[1]);
$startTimeMicroseconds = 0 unless ( defined $startTimeMicroseconds );
return ( defined $startTimeSeconds ? int ( ( ( $endTimeSeconds >= $startTimeSeconds ) ? tv_interval ( [$startTimeSeconds, $startTimeMicroseconds], [$endTimeSeconds, $endTimeMicroseconds] ) : tv_interval ( [$endTimeSeconds, $endTimeMicroseconds], [$...
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub write_debugfile {
&_checkAccObjRef ( $_[0] ); &_checkReadOnly2;
my $debugfile = $_[0]->{_getOptionsArgv}->{debugfile};
if ( defined $debugfile ) {
my $openAppend = ( defined $_[2] and $_[2] =~ /^1$/ ) ? 1 : 0;
my $rvOpen = open (DEBUGFILE, ($openAppend ? '>>' : '>') .$debugfile);
if ($rvOpen) {
print DEBUGFILE ${$_[1]}, "\n";
close(DEBUGFILE);
} else {
print ref ($_[0]) .": Cannot open $debugfile to print debug information\n";
}
}
}
# Utility methods - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub printUsage {
$_[0]->_getOptions () if ( exists $_[0]->{_getOptionsArgv}->{usage} );
$_[0]->SUPER::printUsage ($_[1]);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub printHelp {
$_[0]->_getOptions ();
$_[0]->SUPER::printHelp ();
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub call_system {
&_checkAccObjRef ( $_[0] ); &_checkReadOnly2;
return ( $ERRORS{DEPENDENT} ) unless ( defined $_[1] );
my ($status, $stdout, $stderr) = $_[0]->SUPER::call_system ( $_[1] );
if ( $status ) {
if ( $_[0]->{_getOptionsValues}->{debug} ) {
$_[0]->pluginValues ( { stateValue => $ERRORS{OK}, alert => $_[1] .': OK', result => ( defined $stdout ? $stdout : undef ) }, $TYPE{APPEND} );
} else {
$_[0]->pluginValues ( { stateValue => $ERRORS{OK}, result => ( defined $stdout ? $stdout : undef ) }, $TYPE{APPEND} );
}
return ( $ERRORS{OK} );
} else {
if ( $_[0]->{_getOptionsValues}->{debug} ) {
$_[0]->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, result => ( defined $stderr ? $stderr : undef ) }, $TYPE{APPEND} );
} else {
$_[0]->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => $_[1] .': '. $stderr, result => ( defined $stderr ? $stderr : undef ) }, $TYPE{APPEND} );
}
return ( $ERRORS{UNKNOWN} );
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub exit {
&_checkAccObjRef ( $_[0] ); &_checkSubArgs1;
exit $ERRORS { $_[0]->{_pluginValues}->{stateError} } if ( exists $_[0]->{_exit_} and $_[0]->{_exit_} == 1 );
# Timing Out Slow Plugins - part 2/2 - - - - - - - - - - - - - - - - -
if ( exists $_[0]->{_getOptionsArgv}->{timeout} ) {
my $remaining = alarm (0);
if ( $ALARM_OFF or $! =~ /\QASNMTAP::Asnmtap::Plugins::ALARM_OFF = 1\n\E/ or $@ =~ /\QASNMTAP::Asnmtap::Plugins::ALARM_OFF = 1\n\E/ ) {
$_[0]->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "TIMING OUT SLOW PLUGIN ($remaining)" }, $TYPE{APPEND} );
$remaining = 0;
}
$SIG{ALRM} = $_[0]->{_pluginValues}->{_handler_} ? $_[0]->{_pluginValues}->{_handler_} : 'DEFAULT';
if ( $_[0]->{_pluginValues}->{_alarm_} ) { # Previous alarm pending
my $alarm = $_[0]->{_pluginValues}->{_alarm_} - $_[0]->{_getOptionsArgv}->{timeout} + $remaining;
if ( $alarm > 0 ) { # Reset it, excluding the elapsed time
alarm ($alarm);
} else { # It should have gone off already, set it off
kill 'ALRM',$$;
}
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$_[0]->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'HELP, THERE IS A PROBLEM WITH THE PLUGIN' }, $TYPE{APPEND} ) if ( $_[0]->{_pluginValues}->{stateValue} == $ERRORS{DEPENDENT} or ( exists $_[0]->{_exit_} and $_[0]->{_exit_} == 2 ) );
$_[0]->appendPerformanceData('Status='. $_[0]->{_pluginValues}->{stateValue} .';1;2;0;3') if ( defined $_[1] && $_[1] =~ /^[1357]$/ );
my $duration = $_[0]->setEndTime_and_getResponsTime ($_[0]->{_pluginValues}->{startTime});
$_[0]->appendPerformanceData('Compilation='. ($_[0]->setEndTime_and_getResponsTime ($^T) - $duration) .'ms;;;0;') if ( $_[1] =~ /^[2367]$/ );
if ( $_[0]->{_getOptionsArgv}->{trendline} ) {
my $responseTimeSeconds = ( $duration / 1000 ); # convert to seconds
if ( $_[0]->{_getOptionsValues}->{debug} ) {
my ($startTimeEpoch, undef) = split (/\./, $_[0]->{_pluginValues}->{startTime});
my ($endTimeEpoch, undef) = split (/\./, $_[0]->{_pluginValues}->{endTime});
print "\nStart time : ", scalar(localtime($startTimeEpoch)), "\n";
print "End time : ", scalar(localtime($endTimeEpoch)), "\n";
print "Trendline : ". $_[0]->{_getOptionsArgv}->{trendline} ."\n";
print "Response time: $responseTimeSeconds\n";
}
if ( $_[0]->{_pluginValues}->{stateValue} ) {
$_[0]->appendPerformanceData('Trendline='. $responseTimeSeconds .'s;;;;');
} else {
( run in 1.403 second using v1.01-cache-2.11-cpan-99c4e6809bf )