(plugin) apps::protocols::tcp - force perfdata v2 (#3669)

This commit is contained in:
qgarnier 2022-05-19 09:08:30 +02:00 committed by GitHub
parent e2d66391c6
commit 465e02cbcb
3 changed files with 43 additions and 27 deletions

View File

@ -32,7 +32,8 @@ sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf( my $msg = sprintf(
'Connection status on port %s is %s', "Connection status '%s' on port %s is %s",
$self->{result_values}->{hostname},
$self->{result_values}->{port}, $self->{result_values}->{port},
$self->{result_values}->{status} $self->{result_values}->{status}
); );
@ -46,7 +47,8 @@ sub custom_time_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return sprintf( return sprintf(
"Response time on port %s is %.3fs", "Response time '%s' on port %s is %.3fs",
$self->{result_values}->{hostname},
$self->{result_values}->{port}, $self->{result_values}->{port},
$self->{result_values}->{response_time} $self->{result_values}->{response_time}
); );
@ -61,14 +63,14 @@ sub set_counters {
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'status', type => 2, critical_default => '%{status} eq "failed"', display_ok => 0, set => { { label => 'status', type => 2, critical_default => '%{status} eq "failed"', display_ok => 0, set => {
key_values => [ { name => 'status' }, { name => 'port' }, { name => 'error_message' } ], key_values => [ { name => 'status' }, { name => 'hostname' }, { name => 'port' }, { name => 'error_message' } ],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'time', nlabel => 'tcp.response.time.seconds', set => { { label => 'time', nlabel => 'tcp.response.time.seconds', set => {
key_values => [ { name => 'response_time' }, { name => 'port' } ], key_values => [ { name => 'response_time' }, { name => 'hostname' }, { name => 'port' } ],
closure_custom_output => $self->can('custom_time_output'), closure_custom_output => $self->can('custom_time_output'),
perfdatas => [ perfdatas => [
{ template => '%s', min => 0, unit => 's' } { template => '%s', min => 0, unit => 's' }
@ -85,7 +87,7 @@ sub new {
$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', redirect => 'warning-tcp-response-time-seconds' }, 'warning:s' => { name => 'warning', redirect => 'warning-tcp-response-time-seconds' },
'critical:s' => { name => 'critical', redirect => 'critical-tcp-response-time-seconds' }, 'critical:s' => { name => 'critical', redirect => 'critical-tcp-response-time-seconds' },
'timeout:s' => { name => 'timeout', default => 3 }, 'timeout:s' => { name => 'timeout', default => 3 },
@ -103,7 +105,7 @@ sub check_options {
$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->{option_results}->{port} !~ /(\d+)/) {
$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();
} }
@ -118,18 +120,19 @@ sub manage_selection {
$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}
); );
} else { } else {
$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}
); );
} }
my $timeelapsed = tv_interval($timing0, [gettimeofday]); my $timeelapsed = tv_interval($timing0, [gettimeofday]);
$self->{global} = { $self->{global} = {
hostname => $self->{option_results}->{hostname},
port => $self->{option_results}->{port}, port => $self->{option_results}->{port},
status => 'ok', status => 'ok',
response_time => $timeelapsed, response_time => $timeelapsed,

View File

@ -30,44 +30,48 @@ use IO::Socket::INET;
sub prefix_output { sub prefix_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return 'TCP port ' . $self->{option_results}->{port} . ' '; return sprintf(
"TCP '%s' port %s ",
$options{instance_value}->{hostname},
$options{instance_value}->{port}
);
} }
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' } { name => 'global', type => 0, cb_prefix_output => 'prefix_output' }
]; ];
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'rta', nlabel => 'roundtrip.time.average.milliseconds', set => { { label => 'rta', nlabel => 'tcp.roundtrip.time.average.milliseconds', set => {
key_values => [ { name => 'rta' } ], key_values => [ { name => 'rta' } ],
output_template => 'rta %.3fms', output_template => 'rta %.3fms',
perfdatas => [ perfdatas => [
{ label => 'rta', template => '%.3f', min => 0, unit => 'ms' } { template => '%.3f', min => 0, unit => 'ms' }
] ]
} }
}, },
{ label => 'rtmax', nlabel => 'roundtrip.time.maximum.milliseconds', display_ok => 0, set => { { label => 'rtmax', nlabel => 'tcp.roundtrip.time.maximum.milliseconds', display_ok => 0, set => {
key_values => [ { name => 'rtmax' } ], key_values => [ { name => 'rtmax' } ],
perfdatas => [ perfdatas => [
{ label => 'rtmax', template => '%.3f', min => 0, unit => 'ms' } { template => '%.3f', min => 0, unit => 'ms' }
] ]
} }
}, },
{ label => 'rtmin', nlabel => 'roundtrip.time.minimum.milliseconds', display_ok => 0, set => { { label => 'rtmin', nlabel => 'tcp.roundtrip.time.minimum.milliseconds', display_ok => 0, set => {
key_values => [ { name => 'rtmin' } ], key_values => [ { name => 'rtmin' } ],
perfdatas => [ perfdatas => [
{ label => 'rtmin', template => '%.3f', min => 0, unit => 'ms' } { template => '%.3f', min => 0, unit => 'ms' }
] ]
} }
}, },
{ label => 'pl', nlabel => 'packets.loss.percentage', set => { { label => 'pl', nlabel => 'tcp.packets.loss.percentage', set => {
key_values => [ { name => 'pl' } ], key_values => [ { name => 'pl' } ],
output_template => 'lost %s%%', output_template => 'lost %s%%',
perfdatas => [ perfdatas => [
{ label => 'pl', template => '%s', min => 0, max => 100, unit => '%' } { template => '%s', min => 0, max => 100, unit => '%' }
] ]
} }
} }
@ -76,14 +80,14 @@ sub set_counters {
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' },
'timeout:s' => { name => 'timeout', default => 5}, 'timeout:s' => { name => 'timeout', default => 5 },
'packets:s' => { name => 'packets', default => 5} 'packets:s' => { name => 'packets', default => 5 }
}); });
return $self; return $self;
@ -93,7 +97,11 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
if (!length($self->{option_results}->{port})) { if (!defined($self->{option_results}->{hostname})) {
$self->{output}->add_option_msg(short_msg => 'Please set the hostname option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{port}) || $self->{option_results}->{port} !~ /(\d+)/) {
$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();
} }
@ -126,6 +134,8 @@ sub manage_selection {
} }
$self->{global} = { $self->{global} = {
hostname => $self->{option_results}->{hostname},
port => $self->{option_results}->{port},
rta => ($self->{option_results}->{packets} > $total_packet_lost) ? $total_time_elapsed * 1000 / ($self->{option_results}->{packets} - $total_packet_lost) : 0, rta => ($self->{option_results}->{packets} > $total_packet_lost) ? $total_time_elapsed * 1000 / ($self->{option_results}->{packets} - $total_packet_lost) : 0,
rtmax => $max_time_elapsed * 1000, rtmax => $max_time_elapsed * 1000,
rtmin => $min_time_elapsed * 1000, rtmin => $min_time_elapsed * 1000,
@ -148,6 +158,10 @@ Check TCP port response time.
Only display some counters (regexp can be used). Only display some counters (regexp can be used).
Example : --filter-counters='rta' Example : --filter-counters='rta'
=item B<--hostname>
IP Addr/FQDN of the host
=item B<--port> =item B<--port>
Port used Port used

View File

@ -29,11 +29,10 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$self->{version} = '0.1'; $self->{modes} = {
%{$self->{modes}} = ( 'connection-status' => 'apps::protocols::tcp::mode::connectionstatus',
'connection-status' => 'apps::protocols::tcp::mode::connectionstatus', 'response-time' => 'apps::protocols::tcp::mode::responsetime'
'response-time' => 'apps::protocols::tcp::mode::responsetime' };
);
return $self; return $self;
} }