fix(tomcat/web): applications mode - unsafe evaluation (#2849)

This commit is contained in:
qgarnier 2021-06-03 12:20:03 +02:00 committed by GitHub
parent 461a09a8a2
commit 8dc91d0d78
3 changed files with 48 additions and 56 deletions

View File

@ -24,14 +24,19 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use centreon::plugins::http; use centreon::plugins::http;
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'state: ' . $self->{result_values}->{state}; return 'state: ' . $self->{result_values}->{state};
return $msg; }
sub prefix_application_output {
my ($self, %options) = @_;
return "Application '" . $options{instance_value}->{display} . "' ";
} }
sub set_counters { sub set_counters {
@ -42,32 +47,24 @@ sub set_counters {
]; ];
$self->{maps_counters}->{application} = [ $self->{maps_counters}->{application} = [
{ label => 'status', threshold => 0, set => { { label => 'status', type => 2, unknown_default => '%{state} ne "running"', critical_default => '%{state} eq "stopped"', set => {
key_values => [ { name => 'state' }, { name => 'contextpath' }, { name => 'display' } ], key_values => [ { name => 'state' }, { name => 'contextpath' }, { name => 'display' } ],
closure_custom_calc => \&catalog_status_calc,
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 closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'sessions-active', nlabel => 'application.sessions.active.count', set => { { label => 'sessions-active', nlabel => 'application.sessions.active.count', set => {
key_values => [ { name => 'sessions' }, { name => 'display' } ], key_values => [ { name => 'sessions' }, { name => 'display' } ],
output_template => 'active sessions: %s', output_template => 'active sessions: %s',
perfdatas => [ perfdatas => [
{ value => 'sessions', template => '%s', { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'display' }
min => 0, label_extra_instance => 1, instance_use => 'display' }, ]
],
} }
}, }
]; ];
} }
sub prefix_application_output {
my ($self, %options) = @_;
return "Application '" . $options{instance_value}->{display} . "' ";
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
@ -85,12 +82,9 @@ sub new {
'urlpath:s' => { name => 'url_path', default => '/manager/text/list' }, 'urlpath:s' => { name => 'url_path', default => '/manager/text/list' },
'filter-name:s' => { name => 'filter_name' }, 'filter-name:s' => { name => 'filter_name' },
'filter-path:s' => { name => 'filter_path', }, 'filter-path:s' => { name => 'filter_path', },
'unknown-http-status:s' => { name => 'unknown_http_status', default => '%{http_code} < 200 or %{http_code} >= 300' }, 'unknown-http-status:s' => { name => 'unknown_http_status', default => '%{http_code} < 200 or %{http_code} >= 300' },
'warning-http-status:s' => { name => 'warning_http_status' }, 'warning-http-status:s' => { name => 'warning_http_status', default => '' },
'critical-http-status:s' => { name => 'critical_http_status' }, 'critical-http-status:s' => { name => 'critical_http_status', default => '' }
'unknown-status:s' => { name => 'unknown_status', default => '%{state} ne "running"' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{state} eq "stopped"' },
}); });
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);
@ -100,9 +94,8 @@ sub new {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
$self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']);
} }
sub manage_selection { sub manage_selection {
@ -111,7 +104,7 @@ sub manage_selection {
my $webcontent = $self->{http}->request( my $webcontent = $self->{http}->request(
unknown_status => $self->{option_results}->{unknown_http_status}, unknown_status => $self->{option_results}->{unknown_http_status},
warning_status => $self->{option_results}->{warning_http_status}, warning_status => $self->{option_results}->{warning_http_status},
critical_status => $self->{option_results}->{critical_http_status}, critical_status => $self->{option_results}->{critical_http_status}
); );
$self->{application} = {}; $self->{application} = {};
@ -130,7 +123,7 @@ sub manage_selection {
contextpath => $contextpath contextpath => $contextpath
}; };
} }
if (scalar(keys %{$self->{application}}) <= 0) { if (scalar(keys %{$self->{application}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No application found."); $self->{output}->add_option_msg(short_msg => "No application found.");
$self->{output}->option_exit(); $self->{output}->option_exit();

View File

@ -76,8 +76,8 @@ sub set_counters {
output_template => 'Threads Current : %s', output_template => 'Threads Current : %s',
perfdatas => [ perfdatas => [
{ label => 'threads_current', template => '%.2f', min => 0, max => 'maxThreads', { label => 'threads_current', template => '%.2f', min => 0, max => 'maxThreads',
label_extra_instance => 1 }, label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'threads-busy', nlabel => 'connector.threads.busy.count', set => { { label => 'threads-busy', nlabel => 'connector.threads.busy.count', set => {
@ -85,8 +85,8 @@ sub set_counters {
output_template => 'Threads Busy : %s', output_template => 'Threads Busy : %s',
perfdatas => [ perfdatas => [
{ label => 'threads_busy', template => '%.2f', min => 0, max => 'maxThreads', { label => 'threads_busy', template => '%.2f', min => 0, max => 'maxThreads',
label_extra_instance => 1 }, label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'traffic-in', nlabel => 'connector.traffic.in.bitspersecond', set => { { label => 'traffic-in', nlabel => 'connector.traffic.in.bitspersecond', set => {
@ -96,8 +96,8 @@ sub set_counters {
threshold_use => 'traffic', threshold_use => 'traffic',
perfdatas => [ perfdatas => [
{ label => 'traffic_in', value => 'traffic', template => '%.2f', min => 0, max => 'speed', { label => 'traffic_in', value => 'traffic', template => '%.2f', min => 0, max => 'speed',
unit => 'b/s', label_extra_instance => 1 }, unit => 'b/s', label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'traffic-in-prct', display_ok => 0, nlabel => 'connector.traffic.in.percent', set => { { label => 'traffic-in-prct', display_ok => 0, nlabel => 'connector.traffic.in.percent', set => {
@ -118,8 +118,8 @@ sub set_counters {
threshold_use => 'traffic', threshold_use => 'traffic',
perfdatas => [ perfdatas => [
{ label => 'traffic_out', value => 'traffic', template => '%.2f', min => 0, max => 'speed', { label => 'traffic_out', value => 'traffic', template => '%.2f', min => 0, max => 'speed',
unit => 'b/s', label_extra_instance => 1 }, unit => 'b/s', label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'traffic-out-prct', display_ok => 0, nlabel => 'connector.traffic.out.percent', set => { { label => 'traffic-out-prct', display_ok => 0, nlabel => 'connector.traffic.out.percent', set => {
@ -129,8 +129,8 @@ sub set_counters {
output_use => 'traffic_prct', threshold_use => 'traffic_prct', output_use => 'traffic_prct', threshold_use => 'traffic_prct',
perfdatas => [ perfdatas => [
{ label => 'traffic_out_prct', value => 'traffic_prct', template => '%.2f', min => 0, max => 100, { label => 'traffic_out_prct', value => 'traffic_prct', template => '%.2f', min => 0, max => 100,
unit => '%', label_extra_instance => 1 }, unit => '%', label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'requests-processingtime-total', nlabel => 'connector.requests.processingtime.total.milliseconds', set => { { label => 'requests-processingtime-total', nlabel => 'connector.requests.processingtime.total.milliseconds', set => {
@ -138,8 +138,8 @@ sub set_counters {
output_template => 'Requests Total Processing Time : %s ms', output_template => 'Requests Total Processing Time : %s ms',
perfdatas => [ perfdatas => [
{ label => 'requests_processingtime_total', template => '%s', min => 0, { label => 'requests_processingtime_total', template => '%s', min => 0,
unit => 'ms', label_extra_instance => 1 }, unit => 'ms', label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'requests-errors', nlabel => 'connector.requests.errors.count', set => { { label => 'requests-errors', nlabel => 'connector.requests.errors.count', set => {
@ -147,8 +147,8 @@ sub set_counters {
output_template => 'Requests Errors : %s', output_template => 'Requests Errors : %s',
perfdatas => [ perfdatas => [
{ label => 'requests_errors', template => '%s', min => 0, { label => 'requests_errors', template => '%s', min => 0,
label_extra_instance => 1 }, label_extra_instance => 1 }
], ]
} }
}, },
{ label => 'requests-total', nlabel => 'connector.requests.total.count', set => { { label => 'requests-total', nlabel => 'connector.requests.total.count', set => {
@ -156,10 +156,10 @@ sub set_counters {
output_template => 'Requests Total : %s', output_template => 'Requests Total : %s',
perfdatas => [ perfdatas => [
{ label => 'requests_total', template => '%s', min => 0, { label => 'requests_total', template => '%s', min => 0,
label_extra_instance => 1 }, label_extra_instance => 1 }
], ]
} }
}, }
]; ];
} }

View File

@ -30,13 +30,14 @@ use XML::XPath;
sub custom_memory_output { sub custom_memory_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf("Memory Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)", return sprintf(
"Memory Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)",
$self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
$self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
$self->{result_values}->{prct_used}, $self->{result_values}->{prct_used},
$self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
$self->{result_values}->{prct_free}); $self->{result_values}->{prct_free}
return $msg; );
} }
sub set_counters { sub set_counters {
@ -51,28 +52,26 @@ sub set_counters {
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_memory_output'), closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [ perfdatas => [
{ value => 'used', template => '%d', min => 0, max => 'total', { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
unit => 'B', cast_int => 1 }, ]
],
} }
}, },
{ label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => { { label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_memory_output'), closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [ perfdatas => [
{ value => 'free', template => '%d', min => 0, max => 'total', { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
unit => 'B', cast_int => 1 }, ]
],
} }
}, },
{ label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => { { label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
key_values => [ { name => 'prct_used' } ], key_values => [ { name => 'prct_used' } ],
output_template => 'Memory Used : %.2f %%', output_template => 'Memory Used : %.2f %%',
perfdatas => [ perfdatas => [
{ value => 'prct_used', template => '%.2f', min => 0, max => 100, unit => '%' }, { template => '%.2f', min => 0, max => 100, unit => '%' }
], ]
} }
}, }
]; ];
} }
@ -90,7 +89,7 @@ sub new {
'username:s' => { name => 'username' }, 'username:s' => { name => 'username' },
'password:s' => { name => 'password' }, 'password:s' => { name => 'password' },
'timeout:s' => { name => 'timeout' }, 'timeout:s' => { name => 'timeout' },
'urlpath:s' => { name => 'url_path', default => '/manager/status?XML=true' }, 'urlpath:s' => { name => 'url_path', default => '/manager/status?XML=true' }
}); });
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);