From 41a2036860357959080f77cd33f4422fb20dd5e0 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Tue, 19 Dec 2017 13:52:24 +0100 Subject: [PATCH] new mode for pure storage restapi --- storage/purestorage/restapi/custom/api.pm | 24 ++- storage/purestorage/restapi/mode/alarms.pm | 218 +++++++++++++++++++++ storage/purestorage/restapi/plugin.pm | 1 + 3 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 storage/purestorage/restapi/mode/alarms.pm diff --git a/storage/purestorage/restapi/custom/api.pm b/storage/purestorage/restapi/custom/api.pm index 01389dc05..e7f3a59bf 100644 --- a/storage/purestorage/restapi/custom/api.pm +++ b/storage/purestorage/restapi/custom/api.pm @@ -42,11 +42,12 @@ sub new { if (!defined($options{noptions})) { $options{options}->add_options(arguments => { - "hostname:s" => { name => 'hostname', }, - "username:s" => { name => 'username', }, - "password:s" => { name => 'password', }, - "proxyurl:s" => { name => 'proxyurl', }, - "timeout:s" => { name => 'timeout', }, + "hostname:s" => { name => 'hostname' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "proxyurl:s" => { name => 'proxyurl' }, + "timeout:s" => { name => 'timeout' }, + "api-path:s" => { name => 'api_path' }, }); } $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); @@ -88,6 +89,7 @@ sub check_options { $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef; $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef; + $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/api/1.11'; if (!defined($self->{hostname})) { $self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); @@ -164,7 +166,7 @@ sub get_api_token { } $self->settings(); - my $decoded = $self->request_api(method => 'POST', url_path => '/api/1.11/auth/apitoken', query_form_post => $encoded); + my $decoded = $self->request_api(method => 'POST', url_path => $self->{api_path} . '/auth/apitoken', query_form_post => $encoded); if (!defined($decoded->{api_token})) { $self->{output}->add_option_msg(short_msg => "Cannot get api token"); $self->{output}->option_exit(); @@ -187,7 +189,7 @@ sub get_session { } $self->settings(); - my $decoded = $self->request_api(method => 'POST', url_path => '/api/1.11/auth/session', query_form_post => $encoded); + my $decoded = $self->request_api(method => 'POST', url_path => $self->{api_path} . '/auth/session', query_form_post => $encoded); my $headers = $self->{http}->get_header(); my $cookie = $headers->header('Set-Cookie'); if (!defined($cookie)) { @@ -214,14 +216,14 @@ sub get_object { } $self->settings(); - return $self->request_api(method => 'GET', url_path => '/api/1.11' . $options{path}); + return $self->request_api(method => 'GET', url_path => $self->{api_path} . $options{path}); } sub DESTROY { my $self = shift; if (defined($self->{session_id})) { - $self->request_api(method => 'DELETE', url_path => '/api/1.11/auth/session'); + $self->request_api(method => 'DELETE', url_path => $self->{api_path} . '/auth/session'); } } @@ -261,6 +263,10 @@ Proxy URL if any. Set HTTP timeout in seconds (Default: '10'). +=item B<--api-path> + +API base url path (Default: '/api/1.11'). + =back =head1 DESCRIPTION diff --git a/storage/purestorage/restapi/mode/alarms.pm b/storage/purestorage/restapi/mode/alarms.pm new file mode 100644 index 000000000..5fb82a8d6 --- /dev/null +++ b/storage/purestorage/restapi/mode/alarms.pm @@ -0,0 +1,218 @@ +# +# Copyright 2017 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 storage::purestorage::restapi::mode::alarms; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::misc; +use centreon::plugins::statefile; + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && + eval "$instance_mode->{option_results}->{critical_status}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && + eval "$instance_mode->{option_results}->{warning_status}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf("alarm [component: %s] [severity: %s] [category: %s] [event: %s] %s", $self->{result_values}->{component_name}, + $self->{result_values}->{severity}, $self->{result_values}->{category}, + $self->{result_values}->{event}, centreon::plugins::misc::change_seconds(value => $self->{result_values}->{opened})); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{category} = $options{new_datas}->{$self->{instance} . '_category'}; + $self->{result_values}->{code} = $options{new_datas}->{$self->{instance} . '_code'}; + $self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_severity'}; + $self->{result_values}->{component_name} = $options{new_datas}->{$self->{instance} . '_component_name'}; + $self->{result_values}->{opened} = $options{new_datas}->{$self->{instance} . '_opened'}; + $self->{result_values}->{event} = $options{new_datas}->{$self->{instance} . '_event'}; + return 0; +} + + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 }, + group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] + } + ]; + + $self->{maps_counters}->{alarm} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'category' }, { name => 'code' }, { name => 'severity' }, { name => 'opened' }, { name => 'event' }, { name => 'component_name' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-category:s" => { name => 'filter_category' }, + "warning-status:s" => { name => 'warning_status', default => '%{severity} =~ /warning/i' }, + "critical-status:s" => { name => 'critical_status', default => '%{severity} =~ /critical/i' }, + "memory" => { name => 'memory' }, + }); + + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'."); + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + $self->change_macros(); + + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->check_options(%options); + } +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_status', 'critical_status')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{alarms}->{global} = { alarm => {} }; + my $alarm_results = $options{custom}->get_object(path => '/message'); + + my $last_time; + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->read(statefile => 'cache_purestorage_' . $self->{mode} . '_' . $self->{option_results}->{region}); + $last_time = $self->{statefile_cache}->get(name => 'last_time'); + } + + #[ + # {"category": "hardware", "code": 39, "actual": "2", "opened": "2017-11-27T11:32:51Z", "component_type": "hardware", "event": "Fibre Channel link failure", "current_severity": "warning", "details": "", "expected": null, "id": 10088813, "component_name": "ct1.fc3"}, + # ... + # + + my ($i, $current_time) = (1, time()); + foreach my $alarm (@{$alarm_results}) { + my $create_time = Date::Parse::str2time($alarm->{opened}); + if (!defined($create_time)) { + $self->{manager}->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Can't Parse date '" . $alarm->{opened} . "'"); + next; + } + + next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time); + if (defined($self->{option_results}->{filter_category}) && $self->{option_results}->{filter_category} ne '' && + $alarm->{category} !~ /$self->{option_results}->{filter_category}/) { + $self->{output}->output_add(long_msg => "skipping '" . $alarm->{category} . "': no matching filter.", debug => 1); + next; + } + + my $diff_time = $current_time - $create_time; + + $self->{alarms}->{global}->{alarm}->{$i} = { + %$alarm, + opened => $diff_time, + }; + $i++; + } + + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->write(data => { last_time => $current_time }); + } +} + +1; + +__END__ + +=head1 MODE + +Check alarms. + +=over 8 + +=item B<--filter-category> + +Filter by category name (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{severity} =~ /warning/i') +Can used special variables like: %{category}, %{code}, %{severity}, %{opened}, %{event}, %{component_name} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{severity} =~ /critical/i'). +Can used special variables like: %{category}, %{code}, %{severity}, %{opened}, %{event}, %{component_name} + +=item B<--memory> + +Only check new alarms. + +=back + +=cut diff --git a/storage/purestorage/restapi/plugin.pm b/storage/purestorage/restapi/plugin.pm index d64b3364a..8a12a1b1b 100644 --- a/storage/purestorage/restapi/plugin.pm +++ b/storage/purestorage/restapi/plugin.pm @@ -31,6 +31,7 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( + 'alarms' => 'storage::purestorage::restapi::mode::alarms', 'hardware' => 'storage::purestorage::restapi::mode::hardware', 'list-volumes' => 'storage::purestorage::restapi::mode::listvolumes', 'volume-usage' => 'storage::purestorage::restapi::mode::volumeusage',