Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/centreon/plugins/constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,23 @@ use constant {

# Only used with COUNTER_TYPE_MULTIPLE counters
COUNTER_MULTIPLE_INSTANCE => 0, # counter global to the instance
COUNTER_MULTIPLE_SUBINSTANCE => 1, # counter defined per subinstance
COUNTER_MULTIPLE_SUBINSTANCE => 1, # counter defined per sub instance

# Define the nature of a counter ( numeric or text )
COUNTER_KIND_METRIC => 1, # numeric counter with thesholds and perfdata
COUNTER_KIND_TEXT => 2, # text counter with status check and no perfdata

# Unit conversion constants
CONVERT_NONE => 0, # No conversion
CONVERT_STORAGE => 1, # Convert units using factor 1024
CONVERT_NETWORK => 2, # Convert units using factore 1000
Comment thread
scresto31 marked this conversation as resolved.

MSG_JSON_DECODE_ERROR => 'Cannot decode response (add --debug option to display returned content)'
};

our %EXPORT_TAGS = (
values => [ qw(NO_VALUE BUFFER_CREATION RUN_OK NOT_PROCESSED) ],
unit_conversion => [ qw(CONVERT_NONE CONVERT_STORAGE CONVERT_NETWORK) ],
counter_types => [ qw(COUNTER_TYPE_GLOBAL COUNTER_TYPE_INSTANCE COUNTER_TYPE_GROUP COUNTER_TYPE_MULTIPLE) ],
counter_multiple_types => [ qw(COUNTER_MULTIPLE_INSTANCE COUNTER_MULTIPLE_SUBINSTANCE) ],
counter_kinds => [ qw(COUNTER_KIND_METRIC COUNTER_KIND_TEXT) ],
Expand Down Expand Up @@ -134,7 +140,7 @@ Counter global to the instance.

=item B<COUNTER_MULTIPLE_SUBINSTANCE (1)>

Counter defined per subinstance.
Counter defined per sub instance.

=back

Expand Down
15 changes: 9 additions & 6 deletions src/centreon/plugins/values.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2024 Centreon (http://www.centreon.com/)
# Copyright 2026-Present Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
Expand All @@ -23,7 +23,7 @@ package centreon::plugins::values;
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::plugins::constants qw(:values);
use centreon::plugins::constants qw(:values :unit_conversion);

# Warning message with sprintf and too much arguments.
# Really annoying. Need to disable that warning
Expand Down Expand Up @@ -158,14 +158,15 @@ sub output {

if (defined($name)) {
$value = $self->{result_values}->{$name};
if ($self->{output_change_bytes} == 1) {
if ($self->{output_change_bytes} == CONVERT_STORAGE) {
($value, $unit) = $self->{perfdata}->change_bytes(value => $value);
} elsif ($self->{output_change_bytes} == 2) {
} elsif ($self->{output_change_bytes} == CONVERT_NETWORK) {
($value, $unit) = $self->{perfdata}->change_bytes(value => $value, network => 1);
}
}

return sprintf($self->{output_template}, $value, $unit);
my $output_template = $self->{output_template} =~ s!%\{([A-Za-z0-9_]+)\}! $self->{result_values}->{$1} // '' !ger;
return sprintf($output_template, $value, $unit);
}

sub use_instances {
Expand Down Expand Up @@ -193,7 +194,9 @@ sub perfdata {
my $cast_int = (defined($perf->{cast_int}) && $perf->{cast_int} == 1) ? 1 : 0;
my $template = '%s';

$template = $perf->{template} if (defined($perf->{template}));
$template = $perf->{template}=~ s!%\{([A-Za-z0-9_]+)\}! $self->{result_values}->{$1} // '' !ger
if defined $perf->{template};

$label = $perf->{label} if (defined($perf->{label}));
if (defined($perf->{min})) {
$min = ($perf->{min} =~ /[^0-9.-]/) ? $self->{result_values}->{$perf->{min}} : $perf->{min};
Expand Down
50 changes: 25 additions & 25 deletions src/network/paloalto/api/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sub new {
'username:s' => { name => 'username', default => '' },
'password:s' => { name => 'password', default => '' },
'timeout:s' => { name => 'timeout', default => 30 },
'target:s' => { name => 'target', default => '' },
'unknown-http-status:s' => { name => 'unknown_http_status', default => '%{http_code} < 200 or %{http_code} >= 300' },
'warning-http-status:s' => { name => 'warning_http_status', default => '' },
'critical-http-status:s' => { name => 'critical_http_status', default => '' }
Expand All @@ -78,17 +79,10 @@ sub set_defaults {}
sub check_options {
my ($self, %options) = @_;

$self->{hostname} = $self->{option_results}->{hostname};
$self->{port} = $self->{option_results}->{port};
$self->{proto} = $self->{option_results}->{proto};
$self->{auth_type} = $self->{option_results}->{auth_type};
$self->{api_key} = $self->{option_results}->{api_key};
$self->{username} = $self->{option_results}->{username};
$self->{password} = $self->{option_results}->{password};
$self->{timeout} = $self->{option_results}->{timeout};
$self->{unknown_http_status} = $self->{option_results}->{unknown_http_status};
$self->{warning_http_status} = $self->{option_results}->{warning_http_status};
$self->{critical_http_status} = $self->{option_results}->{critical_http_status};
$self->{$_} = $self->{option_results}->{$_} foreach qw/hostname port proto timeout
auth_type api_key username password
target
unknown_http_status warning_http_status critical_http_status/;

$self->{output}->option_exit(short_msg => "Need to specify --hostname option.")
if $self->{hostname} eq '';
Expand Down Expand Up @@ -135,9 +129,9 @@ sub generate_api_key {
my $content = $self->{http}->request(
url_path => '/api/',
method => 'POST',
get_param => ['type=keygen'],
get_param => [ 'type=keygen' ],
query_form_post => 'user=' . uri_escape($self->{username}) . '&password=' . uri_escape($self->{password}),
header => ['Content-Type: application/x-www-form-urlencoded'],
header => [ 'Content-Type: application/x-www-form-urlencoded' ],
unknown_status => '',
warning_status => '',
critical_status => ''
Expand Down Expand Up @@ -191,12 +185,17 @@ sub _build_auth_header {
sub _http_request {
my ($self, %options) = @_;

my %params = (
'type' => $options{type}
);
$params{'cmd'} = $options{cmd} if $options{cmd};
$params{'action'} = $options{action} if $options{action};
$params{'xpath'} = $options{xpath} if $options{xpath};
$params{'target'} = $self->{target} if $self->{target};

return $self->{http}->request(
url_path => '/api/',
get_params => {
'type' => $options{type},
'cmd' => $options{cmd}
},
get_params => \%params,
header => [
$self->_build_auth_header(),
'Accept: application/xml'
Expand All @@ -215,20 +214,17 @@ sub _parse_xml {
$self->{output}->option_exit( short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']")
if is_empty($content);

$self->{output}->option_exit(short_msg => "Cannot find XML response in API reply.")
unless $content =~ /(<response status=["'](.*?)["']>.*<\/response>)/ms;

my ($xml, $status) = ($1, $2);
$self->{output}->option_exit(short_msg => "API response status: $status")
unless $status eq 'success';

my $result;
eval {
$result = XMLin($xml, ForceArray => $options{ForceArray} // [], KeyAttr => []);
$result = XMLin($content, ForceArray => $options{ForceArray} // [], KeyAttr => []);
};

$self->{output}->option_exit(short_msg => "Cannot decode XML response: $@")
if $@;

$self->{output}->option_exit(short_msg => "API response status: ".value_of($result, "->{status}", "UNKNOWN"))
unless ref $result eq 'HASH' && $result->{status} && $result->{status} eq 'success';

return $result->{result};
}

Expand Down Expand Up @@ -304,6 +300,10 @@ Also used with --auth-type=api-key to auto-generate or regenerate the API key.

Password.

=item B<--target>

Firewall serial number to monitor. Only applicable when the hostname points to Panorama.

=item B<--timeout>

HTTP request timeout in seconds (default: 30).
Expand Down
Loading
Loading