diff --git a/centreon-plugins/apps/protocols/tcp/mode/responsetime.pm b/centreon-plugins/apps/protocols/tcp/mode/responsetime.pm index 6097928e3..3a6c35bfc 100644 --- a/centreon-plugins/apps/protocols/tcp/mode/responsetime.pm +++ b/centreon-plugins/apps/protocols/tcp/mode/responsetime.pm @@ -20,24 +20,75 @@ package apps::protocols::tcp::mode::responsetime; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; use Time::HiRes qw(gettimeofday tv_interval); use IO::Socket::SSL; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf( + 'Connection status on port %s is %s', + $self->{result_values}->{port}, + $self->{result_values}->{status} + ); + if ($self->{result_values}->{status} ne 'ok') { + $msg .= ': ' . $self->{result_values}->{error_message}; + } + return $msg; +} + +sub custom_time_output { + my ($self, %options) = @_; + + return sprintf( + "Response time on port %s is %.3fs", + $self->{result_values}->{port}, + $self->{result_values}->{response_time} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'status', type => 2, critical_default => '%{status} eq "failed"', display_ok => 0, set => { + key_values => [ { name => 'status' }, { name => 'port' }, { name => 'error_message' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'time', nlabel => 'tcp.response.time.seconds', set => { + key_values => [ { name => 'response_time' }, { name => 'port' } ], + closure_custom_output => $self->can('custom_time_output'), + perfdatas => [ + { template => '%s', min => 0, unit => 's' } + ] + } + } + ]; +} sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; $options{options}->add_options(arguments => { 'hostname:s' => { name => 'hostname' }, 'port:s' => { name => 'port', }, - 'warning:s' => { name => 'warning' }, - 'critical:s' => { name => 'critical' }, - 'timeout:s' => { name => 'timeout', default => '3' }, + 'warning:s' => { name => 'warning', redirect => 'warning-tcp-response-time-seconds' }, + 'critical:s' => { name => 'critical', redirect => 'critical-tcp-response-time-seconds' }, + 'timeout:s' => { name => 'timeout', default => 3 }, 'ssl' => { name => 'ssl' } }); @@ -46,79 +97,56 @@ sub new { sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the hostname option"); + $self->{output}->add_option_msg(short_msg => 'Please set the hostname option'); $self->{output}->option_exit(); } if (!defined($self->{option_results}->{port})) { - $self->{output}->add_option_msg(short_msg => "Please set the port option"); + $self->{output}->add_option_msg(short_msg => 'Please set the port option'); $self->{output}->option_exit(); } } -sub run { +sub manage_selection { my ($self, %options) = @_; - my ($connection, $timing0, $timeelapsed); + my $connection; + my $timing0 = [gettimeofday]; if (defined($self->{option_results}->{ssl})) { - $timing0 = [gettimeofday]; $connection = IO::Socket::SSL->new( PeerAddr => $self->{option_results}->{hostname}, PeerPort => $self->{option_results}->{port}, Timeout => $self->{option_results}->{timeout}, ); - $timeelapsed = tv_interval ($timing0, [gettimeofday]); } else { - $timing0 = [gettimeofday]; $connection = IO::Socket::INET->new( PeerAddr => $self->{option_results}->{hostname}, PeerPort => $self->{option_results}->{port}, Timeout => $self->{option_results}->{timeout}, ); - $timeelapsed = tv_interval ($timing0, [gettimeofday]); } + my $timeelapsed = tv_interval($timing0, [gettimeofday]); + $self->{global} = { + port => $self->{option_results}->{port}, + status => 'ok', + response_time => $timeelapsed, + error_message => '' + }; + if (!defined($connection)) { - if (!defined($!) || ($! eq '')) { - $self->{output}->output_add( - severity => 'CRITICAL', - short_msg => sprintf("Connection failed on port %s : SSL error", $self->{option_results}->{port}) - ); - } else { - $self->{output}->output_add( - severity => 'CRITICAL', - short_msg => sprintf("Connection failed on port %s : %s", $self->{option_results}->{port}, $!) - ); + $self->{global}->{status} = 'failed'; + my $append = ''; + if (defined($!) && $! ne '') { + $self->{global}->{error_message} = "error=$!"; + $append = ', '; } - } else { - my $exit = $self->{perfdata}->threshold_check(value => $timeelapsed, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add( - severity => $exit, - short_msg => sprintf("Response time on port %s is %.3fs", $self->{option_results}->{port}, $timeelapsed)); - $self->{output}->perfdata_add( - label => 'time', - value => sprintf('%.3f', $timeelapsed), - unit => 's', - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0 - ); - - close($connection); + $self->{global}->{error_message} .= "${append}ssl_error=$SSL_ERROR" if (defined($SSL_ERROR)); } - - $self->{output}->display(); - $self->{output}->exit(); + + close($connection) if (defined($connection)); } 1; @@ -148,11 +176,26 @@ Use SSL connection. Connection timeout in seconds (Default: 3) -=item B<--warning> +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{port}, %{error_message} + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{port}, %{error_message} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} eq "failed"'). +Can used special variables like: %{status}, %{port}, %{error_message} + +=item B<--warning-time> Threshold warning in seconds -=item B<--critical> +=item B<--critical-time> Threshold critical in seconds