diff --git a/centreon-plugins/apps/backup/netapp/snapcenter/restapi/custom/api.pm b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/custom/api.pm new file mode 100644 index 000000000..c9a9cf170 --- /dev/null +++ b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/custom/api.pm @@ -0,0 +1,270 @@ +# +# 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::backup::netapp::snapcenter::restapi::custom::api; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use Digest::MD5 qw(md5_hex); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + 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 => { + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port'}, + 'proto:s' => { name => 'proto' }, + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'api-version:s' => { name => 'api_version' }, + 'timeout:s' => { name => 'timeout' }, + }); + } + + $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); + $self->{cache} = centreon::plugins::statefile->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} : undef; + $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} : 30; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : undef; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : undef; + $self->{api_version} = (defined($self->{option_results}->{api_version}) && $self->{option_results}->{api_version} ne '') ? $self->{option_results}->{api_version} : '4.1.1'; + + 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_username}) || $self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_password}) || $self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + $self->{cache}->check_options(option_results => $self->{option_results}); + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json;charset=UTF-8'); + $self->{http}->add_header(key => 'Accept', value => 'application/json;charset=UTF-8'); + if (defined($self->{token})) { + $self->{http}->add_header(key => 'Token', value => $self->{token}); + } + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub json_decode { + my ($self, %options) = @_; + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($options{content}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub clean_token { + my ($self, %options) = @_; + + my $datas = { last_timestamp => time() }; + $options{statefile}->write(data => $datas); + $self->{token} = undef; +} + +sub authenticate { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'netapp_snapcenter_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username})); + my $token = $options{statefile}->get(name => 'token'); + + if ($has_cache_file == 0 || !defined($token)) { + my $login = { UserOperationContext => { User => { Name => $self->{api_username}, Passphrase => $self->{api_password} } } }; + my $post_json = JSON::XS->new->utf8->encode($login); + + my $content = $self->{http}->request( + method => 'POST', + url_path => '/api/' . $self->{api_version} . '/auth/login?TokenNeverExpires=false', + query_form_post => $post_json, + warning_status => '', unknown_status => '', critical_status => '', + ); + if ($self->{http}->get_code() != 200) { + $self->{output}->add_option_msg(short_msg => "Login error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded = $self->json_decode(content => $content); + + if (defined($decoded) && defined($decoded->{User}->{Token})) { + $token = $decoded->{User}->{Token}; + } else { + $self->{output}->add_option_msg(short_msg => "Error retrieving token"); + $self->{output}->option_exit(); + } + + $self->{http}->add_header(key => 'Token', value => $token); + my $datas = { last_timestamp => time(), token => $token }; + $options{statefile}->write(data => $datas); + } + + $self->{token} = $token; +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + if (!defined($self->{token})) { + $self->authenticate(statefile => $self->{cache}); + } + my $content = $self->{http}->request( + url_path => '/api/' . $self->{api_version} . '/' . $options{endpoint}, + warning_status => '', unknown_status => '', critical_status => '' + ); + + # Maybe there is an issue with the token. So we retry. + if ($self->{http}->get_code() != 200) { + $self->clean_token(statefile => $self->{cache}); + $self->authenticate(statefile => $self->{cache}); + $content = $self->{http}->request( + url_path => '/api/' . $self->{api_version} . '/' . $options{endpoint}, + warning_status => '', unknown_status => '', critical_status => '' + ); + } + + my $decoded = $self->json_decode(content => $content); + if (!defined($decoded)) { + $self->{output}->add_option_msg(short_msg => "Error while retrieving data (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +1; + +__END__ + +=head1 NAME + +Netapp SnapCenter Rest API + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +Set hostname or IP of vsca. + +=item B<--port> + +Set port (Default: '443'). + +=item B<--proto> + +Specify https if needed (Default: 'https'). + +=item B<--api-username> + +Set username. + +=item B<--api-password> + +Set password. + +=item B<--timeout> + +Threshold for HTTP timeout (Default: '30'). + +=back + +=cut diff --git a/centreon-plugins/apps/backup/netapp/snapcenter/restapi/mode/jobs.pm b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/mode/jobs.pm new file mode 100644 index 000000000..420ff135e --- /dev/null +++ b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/mode/jobs.pm @@ -0,0 +1,277 @@ +# +# 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::backup::netapp::snapcenter::restapi::mode::jobs; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf('status : %s [type: %s] [end time: %s]: %s', + $self->{result_values}->{status}, + $self->{result_values}->{type}, + $self->{result_values}->{end_time}, + $self->{result_values}->{error}, + ); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'jobs', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { nlabel => 'alerts.problems.current.count', min => 0 }, + group => [ { name => 'job', , cb_prefix_output => 'prefix_job_output', skipped_code => { -11 => 1 } } ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total', nlabel => 'jobs.total.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total jobs : %s', + perfdatas => [ + { label => 'total', value => 'total_absolute', template => '%s', min => 0 }, + ], + } + }, + ]; + + $self->{maps_counters}->{job} = [ + { label => 'status', threshold => 0, set => { + key_values => [ + { name => 'status' }, { name => 'display' }, + { name => 'type' }, { name => 'error' }, + { name => 'elapsed_time' }, { name => 'end_time' } + ], + 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, + } + }, + ]; +} + +sub prefix_job_output { + my ($self, %options) = @_; + + return "job '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'filter-type:s' => { name => 'filter_type' }, + 'filter-start-time:s' => { name => 'filter_start_time' }, + 'filter-end-time:s' => { name => 'filter_end_time', default => 86400 }, + 'unknown-status:s' => { name => 'unknown_status', default => '' }, + 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /warning/i' }, + 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /failed/i' }, + 'timezone:s' => { name => 'timezone' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + centreon::plugins::misc::mymodule_load( + output => $self->{output}, module => 'DateTime', + error_msg => "Cannot load module 'DateTime'." + ); + + $self->change_macros(macros => [ + 'warning_status', 'critical_status', 'unknown_status', + ]); + + if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') { + $ENV{TZ} = $self->{option_results}->{timezone}; + } +} + +sub date2ts { + my ($self, %options) = @_; + + return undef if (!defined($options{date}) || $options{date} eq ''); + if ($options{date} !~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2}):(\d{2})\s+(AM|PM)/i) { + $self->{output}->add_option_msg(short_msg => "unknown date format: $options{date}"); + $self->{output}->option_exit(); + } + + my $hour = $4; + $hour += 12 if ($7 eq 'PM'); + my $dt = DateTime->new( + year => $3, + month => $1, + day => $2, + hour => $4, + minute => $5, + second => $6 + ); + return $dt->epoch(); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->request_api(endpoint => 'jobs'); + + my $map_status = { + -1 => 'None', 0 => 'running', 1 => 'warning', 2 => 'failed', 3 => 'completed', + 4 => 'retry', 5 => 'queued', 6 => 'validating', + 7 => 'completedWithVerificationPending', 8 => 'canceled', + 9 => 'completedWithRMANCatalogingPending', 10 => 'completedWithVerificationAndRMANCatalogingPending', + 11 => 'completedPrimaryBackup', 12 => 'transferredBackupToVault', + 13 => 'cancelling', + }; + my $map_type = { + -1 => 'None', 0 => 'backup', 1 => 'restore', 2 => 'replication', 3 => 'retention', + 4 => 'verification', 5 => 'plug-in installation', 6 => 'clone', + 7 => 'delete clone', 8 => 'clone life cycle', + 9 => 'ressource group', 10 => 'host', 11 => 'policy', 12 => 'discovery', + 13 => 'attach policy', 14 => 'detach policy', 15 => 'storage connection', + 16 => 'license check', 17 => 'mount backup', 18 => 'unmount backup', + 19 => 'register mount', 20 => 'get mount', 21 => 'delete mount', + 22 => 'provision', 23 => 'maintenance', 24 => 'plugin', + 25 => 'remote plugin uninstallation', 26 => 'protect snapCenter repository', + 27 => 'configure resources', 28 => 'catalog backup', 29 => 'uncatalog backup', + 30 => 'resource', 32 => 'apply protection', 33 => 'catalog', 34 => 'plugin modify', + 35 => 'repository management', 36 => 'remove protection', 37 => 'clone split', + 38 => 'server management', 39 => 'import protection', 40 => 'guest file restore', + 41 => 'extend protection', 42 => 'purge jobs', 43 => 'assign assets', + }; + + $self->{global} = { total => 0 }; + $self->{jobs}->{global} = { job => {} }; + + my $current_time = time(); + foreach (@{$results->{Results}}) { + my $type = defined($_->{Type}) && defined($map_type->{$_->{Type}}) ? $map_type->{$_->{Type}} : 'unknown'; + my $status = defined($_->{Status}) && defined($map_status->{$_->{Status}}) ? $map_status->{$_->{Status}} : 'unknown'; + + my $start_ts = $self->date2ts(date => $_->{StartTime}); + my $end_ts = $self->date2ts(date => $_->{EndTime}); + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $_->{Name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping job '" . $_->{Id} . "': no matching filter type.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && + $type !~ /$self->{option_results}->{filter_type}/) { + $self->{output}->output_add(long_msg => "skipping job '" . $_->{Id} . "': no matching filter type.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_end_time}) && $self->{option_results}->{filter_end_time} =~ /[0-9]+/ && + (!defined($end_ts) || $end_ts < ($current_time - $self->{option_results}->{filter_end_time}))) { + $self->{output}->output_add(long_msg => "skipping job '" . $_->{Id} . "': end time too old.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_start_time}) && $self->{option_results}->{filter_start_time} =~ /[0-9]+/ && + (!defined($start_ts) || $start_ts < ($current_time - $self->{option_results}->{filter_start_time}))) { + $self->{output}->output_add(long_msg => "skipping job '" . $_->{Id} . "': start time too old.", debug => 1); + next; + } + + my $error = defined($_->{Error}) && $_->{Error} ne '' ? $_->{Error} : 'no error'; + $error =~ s/[\n\|]/ -- /msg; + $error =~ s/[\r]//msg; + my $elapsed_time = defined($start_ts) ? $current_time - $start_ts : -1; + $self->{jobs}->{global}->{job}->{$_->{Id}} = { + display => $_->{Name}, + elapsed_time => $elapsed_time, + status => $status, + type => $type, + end_time => defined($_->{EndTime}) && $_->{EndTime} ne '' ? $_->{EndTime} : -1, + error => $error, + }; + + $self->{global}->{total}++; + } +} + +1; + +__END__ + +=head1 MODE + +Check jobs status. + +=over 8 + +=item B<--filter-name> + +Filter job name (can be a regexp). + +=item B<--filter-type> + +Filter job type (can be a regexp). + +=item B<--filter-start-time> + +Filter job with start time greater than current time less value in seconds. + +=item B<--filter-end-time> + +Filter job with end time greater than current time less value in seconds (Default: 86400). + +=item B<--timezone> + +Set timezone (If not set, we use current server execution timezone). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{display}, %{status}, %{type} + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /warning/i'). +Can used special variables like: %{display}, %{status}, %{type} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /failed/i'). +Can used special variables like: %{display}, %{status}, %{type} + +=item B<--warning-total> + +Set warning threshold for total jobs. + +=item B<--critical-total> + +Set critical threshold for total jobs. + +=back + +=cut diff --git a/centreon-plugins/apps/backup/netapp/snapcenter/restapi/plugin.pm b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/plugin.pm new file mode 100644 index 000000000..23634f20b --- /dev/null +++ b/centreon-plugins/apps/backup/netapp/snapcenter/restapi/plugin.pm @@ -0,0 +1,54 @@ +# +# 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::backup::netapp::snapcenter::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} = '1.0'; + %{$self->{modes}} = ( + 'jobs' => 'apps::backup::netapp::snapcenter::restapi::mode::jobs', + ); + + $self->{custom_modes}{api} = 'apps::backup::netapp::snapcenter::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Netapp SnapCenter through HTTP/REST API. + +=over 8 + +=back + +=cut