diff --git a/network/cisco/ssms/restapi/custom/api.pm b/network/cisco/ssms/restapi/custom/api.pm index eb44615a2..9c21489c0 100644 --- a/network/cisco/ssms/restapi/custom/api.pm +++ b/network/cisco/ssms/restapi/custom/api.pm @@ -215,19 +215,14 @@ sub request_api { return $decoded; } -sub get_licenses { +sub get_alerts { my ($self, %options) = @_; - #my $plop = do { - # local $/ = undef; - # if (!open my $fh, "<", '/tmp/licenses.pretty') { - # $self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{$_} : $!"); - # $self->{output}->option_exit(); - # } - # <$fh>; - #}; - #$plop = JSON::XS->new->utf8->decode($plop); - #return $plop; + return $self->request_api(endpoint => '/api/v1/accounts/' . $options{account} . '/alerts'); +} + +sub get_licenses { + my ($self, %options) = @_; return $self->request_api(endpoint => '/api/v1/accounts/' . $options{account} . '/licenses'); } diff --git a/network/cisco/ssms/restapi/mode/alerts.pm b/network/cisco/ssms/restapi/mode/alerts.pm new file mode 100644 index 000000000..f8083ef08 --- /dev/null +++ b/network/cisco/ssms/restapi/mode/alerts.pm @@ -0,0 +1,196 @@ +# +# Copyright 2020 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 network::cisco::ssms::restapi::mode::alerts; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); + +sub custom_alert_output { + my ($self, %options) = @_; + + return sprintf( + '[message type: %s] [severity: %s] [source: %s]', + $self->{result_values}->{message_type}, + $self->{result_values}->{severity}, + $self->{result_values}->{source} ne '' ? $self->{result_values}->{source} : '-' + ); +} + +sub account_long_output { + my ($self, %options) = @_; + + return "checking account '" . $options{instance_value}->{display} . "'"; +} + +sub prefix_account_output { + my ($self, %options) = @_; + + return "account '" . $options{instance_value}->{display} . "' "; +} + +sub prefix_alerts_global_output { + my ($self, %options) = @_; + + return 'alerts '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'accounts', type => 3, cb_prefix_output => 'prefix_account_output', cb_long_output => 'account_long_output', indent_long_output => ' ', message_multiple => 'All accounts are ok', + group => [ + { name => 'alerts_global', type => 0, cb_prefix_output => 'prefix_alerts_global_output' }, + { name => 'alerts', type => 1, display_short => 0 } + ] + } + ]; + + $self->{maps_counters}->{alerts_global} = [ + { label => 'alerts-minor', nlabel => 'account.alerts.minor.count', set => { + key_values => [ { name => 'minor' } ], + output_template => 'minor: %s', + perfdatas => [ + { template => '%d', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => 'alerts-major', nlabel => 'account.alerts.major.count', set => { + key_values => [ { name => 'major' } ], + output_template => 'major: %s', + perfdatas => [ + { template => '%d', min => 0, label_extra_instance => 1 } + ] + } + } + ]; + + $self->{maps_counters}->{alerts} = [ + { label => 'alert-status', threshold => 0, set => { + key_values => [ { name => 'severity' }, { name => 'message_type' }, { name => 'source' } ], + closure_custom_output => $self->can('custom_alert_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => sub { return 'ok'; } + } + } + ]; +} + +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 => { + 'account:s@' => { name => 'account' }, + 'filter-message-type:s' => { name => 'filter_message_type' }, + 'display-alerts' => { name => 'display_alerts' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{account_names} = []; + if (defined($self->{option_results}->{account})) { + foreach my $account (@{$self->{option_results}->{account}}) { + push @{$self->{account_names}}, $account if ($account ne ''); + } + } + if (scalar(@{$self->{account_names}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'need to specify --account option.'); + $self->{output}->option_exit(); + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{accounts} = {}; + foreach my $account (@{$self->{account_names}}) { + my $results = $options{custom}->get_alerts( + account => $account + ); + + next if (!defined($results->{alerts})); + + $self->{accounts}->{$account} = { + display => $account, + alerts_global => { minor => 0, major => 0 }, + alerts => {} + }; + my $i = 0; + foreach (@{$results->{alerts}}) { + next if (defined($self->{option_results}->{filter_message_type}) && $self->{option_results}->{filter_message_type} ne '' && + $_->{messageType} !~ /$self->{option_results}->{filter_message_type}/); + + $self->{accounts}->{$account}->{alerts_global}->{ lc($_->{severity}) }++; + + next if (!defined($self->{option_results}->{display_alerts})); + + $self->{accounts}->{$account}->{alerts}->{$i} = { + message_type => $_->{messageType}, + severity => lc($_->{severity}), + source => $_->{source} + }; + + $i++; + } + } +} + +1; + +__END__ + +=head1 MODE + +Check alerts. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='minor' + +=item B<--account> + +Check account name (Required. Multiple option). + +=item B<--filter-message-type> + +Filter alerts by message type (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + +=back + +=cut diff --git a/network/cisco/ssms/restapi/mode/licenses.pm b/network/cisco/ssms/restapi/mode/licenses.pm index 1cea04425..2053232ca 100644 --- a/network/cisco/ssms/restapi/mode/licenses.pm +++ b/network/cisco/ssms/restapi/mode/licenses.pm @@ -72,7 +72,7 @@ sub set_counters { $self->{maps_counters_type} = [ { name => 'accounts', type => 3, cb_prefix_output => 'prefix_account_output', cb_long_output => 'account_long_output', indent_long_output => ' ', message_multiple => 'All accounts are ok', group => [ - { name => 'licenses', display_long => 1, cb_prefix_output => 'prefix_license_output', message_multiple => 'licenses are ok', type => 1, skipped_code => { -10 => 1 } } + { name => 'licenses', display_long => 1, cb_prefix_output => 'prefix_license_output', message_multiple => 'licenses are ok', type => 1, skipped_code => { -10 => 1 } } ] } ]; diff --git a/network/cisco/ssms/restapi/plugin.pm b/network/cisco/ssms/restapi/plugin.pm index 373484aaf..608e2371b 100644 --- a/network/cisco/ssms/restapi/plugin.pm +++ b/network/cisco/ssms/restapi/plugin.pm @@ -31,6 +31,7 @@ sub new { $self->{version} = '0.1'; $self->{modes} = { + 'alerts' => 'network::cisco::ssms::restapi::mode::alerts', 'licenses' => 'network::cisco::ssms::restapi::mode::licenses' };