mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-07-21 12:45:10 +02:00
parent
6b694f56b4
commit
1642f53f6d
@ -20,24 +20,75 @@
|
|||||||
|
|
||||||
package apps::protocols::tcp::mode::responsetime;
|
package apps::protocols::tcp::mode::responsetime;
|
||||||
|
|
||||||
use base qw(centreon::plugins::mode);
|
use base qw(centreon::plugins::templates::counter);
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use Time::HiRes qw(gettimeofday tv_interval);
|
use Time::HiRes qw(gettimeofday tv_interval);
|
||||||
use IO::Socket::SSL;
|
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 {
|
sub new {
|
||||||
my ($class, %options) = @_;
|
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;
|
bless $self, $class;
|
||||||
|
|
||||||
$options{options}->add_options(arguments => {
|
$options{options}->add_options(arguments => {
|
||||||
'hostname:s' => { name => 'hostname' },
|
'hostname:s' => { name => 'hostname' },
|
||||||
'port:s' => { name => 'port', },
|
'port:s' => { name => 'port', },
|
||||||
'warning:s' => { name => 'warning' },
|
'warning:s' => { name => 'warning', redirect => 'warning-tcp-response-time-seconds' },
|
||||||
'critical:s' => { name => 'critical' },
|
'critical:s' => { name => 'critical', redirect => 'critical-tcp-response-time-seconds' },
|
||||||
'timeout:s' => { name => 'timeout', default => '3' },
|
'timeout:s' => { name => 'timeout', default => 3 },
|
||||||
'ssl' => { name => 'ssl' }
|
'ssl' => { name => 'ssl' }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -46,79 +97,56 @@ sub new {
|
|||||||
|
|
||||||
sub check_options {
|
sub check_options {
|
||||||
my ($self, %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})) {
|
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();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
if (!defined($self->{option_results}->{port})) {
|
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();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub run {
|
sub manage_selection {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
my ($connection, $timing0, $timeelapsed);
|
my $connection;
|
||||||
|
my $timing0 = [gettimeofday];
|
||||||
if (defined($self->{option_results}->{ssl})) {
|
if (defined($self->{option_results}->{ssl})) {
|
||||||
$timing0 = [gettimeofday];
|
|
||||||
$connection = IO::Socket::SSL->new(
|
$connection = IO::Socket::SSL->new(
|
||||||
PeerAddr => $self->{option_results}->{hostname},
|
PeerAddr => $self->{option_results}->{hostname},
|
||||||
PeerPort => $self->{option_results}->{port},
|
PeerPort => $self->{option_results}->{port},
|
||||||
Timeout => $self->{option_results}->{timeout},
|
Timeout => $self->{option_results}->{timeout},
|
||||||
);
|
);
|
||||||
$timeelapsed = tv_interval ($timing0, [gettimeofday]);
|
|
||||||
} else {
|
} else {
|
||||||
$timing0 = [gettimeofday];
|
|
||||||
$connection = IO::Socket::INET->new(
|
$connection = IO::Socket::INET->new(
|
||||||
PeerAddr => $self->{option_results}->{hostname},
|
PeerAddr => $self->{option_results}->{hostname},
|
||||||
PeerPort => $self->{option_results}->{port},
|
PeerPort => $self->{option_results}->{port},
|
||||||
Timeout => $self->{option_results}->{timeout},
|
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($connection)) {
|
||||||
if (!defined($!) || ($! eq '')) {
|
$self->{global}->{status} = 'failed';
|
||||||
$self->{output}->output_add(
|
my $append = '';
|
||||||
severity => 'CRITICAL',
|
if (defined($!) && $! ne '') {
|
||||||
short_msg => sprintf("Connection failed on port %s : SSL error", $self->{option_results}->{port})
|
$self->{global}->{error_message} = "error=$!";
|
||||||
);
|
$append = ', ';
|
||||||
} else {
|
|
||||||
$self->{output}->output_add(
|
|
||||||
severity => 'CRITICAL',
|
|
||||||
short_msg => sprintf("Connection failed on port %s : %s", $self->{option_results}->{port}, $!)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
$self->{global}->{error_message} .= "${append}ssl_error=$SSL_ERROR" if (defined($SSL_ERROR));
|
||||||
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->{output}->display();
|
close($connection) if (defined($connection));
|
||||||
$self->{output}->exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
@ -148,11 +176,26 @@ Use SSL connection.
|
|||||||
|
|
||||||
Connection timeout in seconds (Default: 3)
|
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
|
Threshold warning in seconds
|
||||||
|
|
||||||
=item B<--critical>
|
=item B<--critical-time>
|
||||||
|
|
||||||
Threshold critical in seconds
|
Threshold critical in seconds
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user