add mip plugin

This commit is contained in:
garnier-quentin 2019-12-04 11:25:46 +01:00
parent 93c720b32b
commit 2e881e663c
5 changed files with 696 additions and 9 deletions

View File

@ -0,0 +1,213 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::monitoring::mip::restapi::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'api-key:s' => { name => 'api_key' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'timeout:s' => { name => 'timeout' },
'unknown-http-status:s' => { name => 'unknown_http_status' },
'warning-http-status:s' => { name => 'warning_http_status' },
'critical-http-status:s' => { name => 'critical_http_status' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
$self->{http} = centreon::plugins::http->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
sub check_options {
my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{api_key} = (defined($self->{option_results}->{api_key})) ? $self->{option_results}->{api_key} : undef;
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
if (!defined($self->{hostname}) || $self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{api_key}) || $self->{api_key} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-key option.");
$self->{output}->option_exit();
}
return 0;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{timeout} = $self->{timeout};
}
sub settings {
my ($self, %options) = @_;
return if (defined($self->{settings_done}));
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}});
$self->{settings_done} = 1;
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
my $content = $self->{http}->request(
method => defined($options{method}) ? $options{method} : 'GET',
url_path => $options{url_path},
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status},
curl_backend_options => { header => ['MIP_API_KEY: ' . $self->{api_key}] },
lwp_backend_options => { header => [':MIP_API_KEY: ' . $self->{api_key}] },
);
if (!defined($content) || $content eq '') {
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
return $decoded;
}
1;
__END__
=head1 NAME
MIP Rest API
=head1 REST API OPTIONS
Maltem Insight Performance Rest API
=over 8
=item B<--hostname>
Set hostname.
=item B<--port>
Port used (Default: 443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--api-key>
MIP API Token.
=item B<--timeout>
Set timeout in seconds (Default: 10).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,114 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::monitoring::mip::restapi::mode::listscenarios;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $api_results = $options{custom}->request_api(
url_path => '/api/measures/?type=ASA_DESKTOP&engineActivated=true&fields=name,alias,displayName,frequency,scenario.name,scenario.application.name,timeout,agent.id,agent.name,activated,engineActivated&limit=-1'
);
my $results = {};
foreach (@{$api_results->{results}}) {
$results->{$_->{id}} = {
id => $_->{id},
name => $_->{name},
alias => $_->{alias},
display_name => $_->{displayName},
app_name => defined($_->{scenario}->{application}->{name}) ? $_->{scenario}->{application}->{name} : '-',
};
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $scenarios = $self->manage_selection(%options);
foreach (sort {lc $a->{name} cmp lc $b->{name}} values %$scenarios) {
$self->{output}->output_add(long_msg => sprintf(
'[name = %s][id = %s][alias = %s][display_name = %s][app_name = %s]',
$_->{name},
$_->{id},
$_->{alias},
$_->{display_name},
$_->{app_name}
));
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List scenarios:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['id', 'name', 'alias', 'display_name', 'app_name']);
}
sub disco_show {
my ($self, %options) = @_;
my $scenarios = $self->manage_selection(%options);
foreach (sort {lc $a->{name} cmp lc $b->{name}} values %$scenarios) {
$self->{output}->add_disco_entry(%$_);
}
}
1;
__END__
=head1 MODE
List scenarios.
=over 8
=back
=cut

View File

@ -0,0 +1,305 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::monitoring::mip::restapi::mode::scenarios;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex);
my $mapping_units = {
requests => { label => 'requests' },
bytes => { label => 'bytes', scale => 1, extra_unit => '' },
ms => { label => 'milliseconds' },
bps => { label => 'bitspersecond', scale => 1, extra_unit => '/s', network => 1 },
status => { label => 'count' },
};
sub custom_status_output {
my ($self, %options) = @_;
my $msg = 'status : ' . $self->{result_values}->{status};
return $msg;
}
sub custom_metric_output {
my ($self, %options) = @_;
my $msg;
if (defined($mapping_units->{ $self->{result_values}->{unit_absolute} }->{scale})) {
$msg = sprintf(
'value: %s %s%s',
$self->{perfdata}->change_bytes(
value => $self->{result_values}->{value_absolute},
network => $mapping_units->{ $self->{result_values}->{unit_absolute} }->{network}
),
$mapping_units->{ $self->{result_values}->{unit_absolute} }->{extra_unit}
);
} else {
$msg = sprintf(
'value: %s %s',
$self->{result_values}->{value_absolute},
$self->{result_values}->{unit_absolute}
);
}
return $msg;
}
sub custom_metric_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
unit => $self->{result_values}->{unit_absolute},
nlabel => 'scenario.metric.usage.' . $mapping_units->{ $self->{result_values}->{unit_absolute} }->{label},
instances => $self->{instance},
value => $self->{result_values}->{value_absolute},
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'scenario', type => 3, cb_prefix_output => 'prefix_scenario_output', cb_long_output => 'scenario_long_output', indent_long_output => ' ', message_multiple => 'All scenarios are ok',
group => [
{ name => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'metric', display_long => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => 'All metrics are ok', type => 1, skipped_code => { -10 => 1 } },
]
}
];
$self->{maps_counters}->{global} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'display' } ],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
];
$self->{maps_counters}->{metric} = [
{ label => 'metric', set => {
key_values => [ { name => 'value' }, { name => 'unit' }, { name => 'display' }, ],
closure_custom_output => $self->can('custom_metric_output'),
closure_custom_perfdata => $self->can('custom_metric_perfdata'),
closure_custom_threshold_check => sub { return 'ok'; }
}
},
];
}
sub scenario_long_output {
my ($self, %options) = @_;
return "checking scenario '" . $options{instance_value}->{display} . "'";
}
sub prefix_scenario_output {
my ($self, %options) = @_;
return "Scenario '" . $options{instance_value}->{display} . "' ";
}
sub prefix_metric_output {
my ($self, %options) = @_;
return "metric '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-id:s' => { name => 'filter_id' },
'filter-display-name:s' => { name => 'filter_display_name' },
'filter-name:s' => { name => 'filter_name' },
'filter-app-name:s' => { name => 'filter_app_name' },
'memory' => { name => 'memory' },
'display-instance:s' => { name => 'display_instance', default => '%{name}' },
'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /unknown/i' },
'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /warning/i' },
'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /critical/i' },
});
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->{option_results}->{display_instance} = '%{name}' if (!defined($self->{option_results}->{display_instance}) || $self->{option_results}->{display_instance} eq '');
$self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']);
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->check_options(%options);
}
}
sub manage_selection {
my ($self, %options) = @_;
# default time: &from=now-1h&to=now
my $results = $options{custom}->request_api(
url_path => '/api/measures/details?fields=displayName,frequency,timeout,scenario.name,scenario.application.name&scenario.name__neq=null&limit=-1'
);
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->read(
statefile => 'mip_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_id}) ? md5_hex($self->{option_results}->{filter_id}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_display_name}) ? md5_hex($self->{option_results}->{filter_display_name}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_app_name}) ? md5_hex($self->{option_results}->{filter_app_name}) : md5_hex('all'))
);
}
my $save_scenario_times = {};
$self->{scenario} = {};
foreach my $entry (@{$results->{results}}) {
my $mapping = {
id => $entry->{measure}->{id},
name => $entry->{measure}->{name},
display_name => $entry->{measure}->{displayName},
app_name => defined($entry->{measure}->{scenario}->{application}->{name}) ? $entry->{measure}->{scenario}->{application}->{name} : '-',
};
my $filter = 0;
foreach (keys %$mapping) {
if (defined($self->{option_results}->{'filter_' . $_}) && $self->{option_results}->{'filter_' . $_} ne '' &&
$mapping->{$_} !~ /$self->{option_results}->{'filter_' . $_}/) {
$filter = 1;
$self->{output}->output_add(long_msg => "skipping scenario id '" . $mapping->{id} . "': no matching filter '$_'.", debug => 1);
last;
}
}
next if ($filter == 1);
my $scenario_name = $self->{option_results}->{display_instance};
$scenario_name =~ s/%\{(.*?)\}/$mapping->{$1}/g;
$self->{scenario}->{$scenario_name} = {
display => $scenario_name,
global => {
display => $scenario_name,
},
metric => {},
};
my @sorted = sort(keys %{$entry->{results}});
my $last_time = pop(@sorted);
if (defined($self->{option_results}->{memory})) {
my $save_time = $self->{statefile_cache}->get(name => $mapping->{id});
$save_scenario_times->{$mapping->{id}} = $last_time;
if (defined($save_time) && $save_time eq $last_time) {
$self->{scenario}->{$scenario_name}->{global}->{status} = 'noNewValue';
next;
}
}
$self->{scenario}->{$scenario_name}->{global}->{status} = $entry->{results}->{$last_time}->{state}->{type};
foreach (keys %{$entry->{metricInfo}}) {
$self->{scenario}->{$scenario_name}->{metric}->{ $entry->{metricInfo}->{$_}->{label} } = {
display => $entry->{metricInfo}->{$_}->{label},
unit => $entry->{metricInfo}->{$_}->{unit},
value => $entry->{results}->{$last_time}->{metrics}->{$_}
};
}
}
if (scalar(keys %{$self->{scenario}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No scenario found");
$self->{output}->option_exit();
}
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->write(data => $save_scenario_times);
}
}
1;
__END__
=head1 MODE
Check scenarios.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^status$'
=item B<--filter-id>
Filter scenarios by id (can be a regexp).
=item B<--filter-display-name>
Filter scenarios by display name (can be a regexp).
=item B<--filter-name>
Filter scenarios by name (can be a regexp).
=item B<--filter-app-name>
Filter scenarios by applicationn name (can be a regexp).
=item B<--display-instance>
Set the scenario display value (Default: '%{name}').
Can used special variables like: %{name}, %{app_name}, %{display_name}, %{id}
=item B<--memory>
Only check new result entries for scenarios.
=item B<--unknown-status>
Set warning threshold for status (Default: '%{status} =~ /unknown/i').
Can used special variables like: %{status}, %{display}
=item B<--warning-status>
Set warning threshold for status (Default: '%{status} =~ /warning/i').
Can used special variables like: %{status}, %{display}
=item B<--critical-status>
Set critical threshold for status (Default: '%{status} =~ /critical/i').
Can used special variables like: %{status}, %{display}
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::monitoring::mip::restapi::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
%{ $self->{modes} } = (
'list-scenarios' => 'apps::monitoring::mip::restapi::mode::listscenarios',
'scenarios' => 'apps::monitoring::mip::restapi::mode::scenarios',
);
$self->{custom_modes}{api} = 'apps::monitoring::mip::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Maltem Insight Performance using Rest API.
=cut

View File

@ -38,12 +38,18 @@ sub new {
$options{options}->add_help(package => __PACKAGE__, sections => 'HTTP GLOBAL OPTIONS');
}
centreon::plugins::misc::mymodule_load(output => $options{output}, module => 'centreon::plugins::backend::http::lwp',
error_msg => "Cannot load module 'centreon::plugins::backend::http::lwp'.");
centreon::plugins::misc::mymodule_load(
output => $options{output},
module => 'centreon::plugins::backend::http::lwp',
error_msg => "Cannot load module 'centreon::plugins::backend::http::lwp'."
);
$self->{backend_lwp} = centreon::plugins::backend::http::lwp->new(%options);
centreon::plugins::misc::mymodule_load(output => $options{output}, module => 'centreon::plugins::backend::http::curl',
error_msg => "Cannot load module 'centreon::plugins::backend::http::curl'.");
centreon::plugins::misc::mymodule_load(
output => $options{output},
module => 'centreon::plugins::backend::http::curl',
error_msg => "Cannot load module 'centreon::plugins::backend::http::curl'."
);
$self->{backend_curl} = centreon::plugins::backend::http::curl->new(%options);
$self->{output} = $options{output};
@ -87,10 +93,9 @@ sub check_options {
$self->{output}->option_exit();
}
if (defined($options{request}->{curl_backend_options}) &&
$self->{http_backend} eq 'curl') {
foreach (keys %{$options{request}->{curl_backend_options}}) {
$options{request}->{$_} = $options{request}->{curl_backend_options}->{$_};
if (defined($options{request}->{$self->{http_backend} . '_backend_options'})) {
foreach (keys %{$options{request}->{$self->{http_backend} . '_backend_options'}}) {
$options{request}->{$_} = $options{request}->{$self->{http_backend} . '_backend_options'}->{$_};
}
}
@ -116,7 +121,7 @@ sub check_options {
$options{request}->{headers} = {};
if (defined($options{request}->{header})) {
foreach (@{$options{request}->{header}}) {
if (/^(.*?):(.*)/) {
if (/^(:.+?|.+?):(.*)/) {
$options{request}->{headers}->{$1} = $2;
}
}