| 1 |
# |
| 2 |
# Copyright 2018 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package storage::dell::powerstore::restapi::mode::alerts; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::statefile; |
| 28 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 29 |
|
| 30 |
sub custom_status_output { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
return sprintf( |
| 34 |
"alert [severity: %s] [name: %s] [resource: %s] %s", |
| 35 |
$self->{result_values}->{severity}, |
| 36 |
$self->{result_values}->{name}, |
| 37 |
$self->{result_values}->{resource}, |
| 38 |
$self->{result_values}->{timeraised} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_global_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return 'Alerts severity '; |
| 46 |
} |
| 47 |
|
| 48 |
sub set_counters { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
$self->{maps_counters_type} = [ |
| 52 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 53 |
{ name => 'alarms', type => 2, message_multiple => '0 alerts detected', format_output => '%s alerts detected', display_counter_problem => { nlabel => 'alerts.problems.current.count', min => 0 }, |
| 54 |
group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] |
| 55 |
} |
| 56 |
]; |
| 57 |
|
| 58 |
$self->{maps_counters}->{global} = []; |
| 59 |
foreach ('none', 'info', 'minor', 'major', 'critical') { |
| 60 |
push @{$self->{maps_counters}->{global}}, { |
| 61 |
label => 'severity-' . $_, nlabel => 'alerts.severity.' . $_ . '.count', display_ok => 0, set => { |
| 62 |
key_values => [ { name => $_ } ], |
| 63 |
output_template => $_ . ': %s', |
| 64 |
perfdatas => [ |
| 65 |
{ template => '%s', min => 0 } |
| 66 |
] |
| 67 |
} |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
$self->{maps_counters}->{alarm} = [ |
| 72 |
{ |
| 73 |
label => 'status', |
| 74 |
type => 2, |
| 75 |
warning_default => '%{severity} =~ /minor/i', |
| 76 |
critical_default => '%{severity} =~ /major|critical/i', |
| 77 |
set => { |
| 78 |
key_values => [ |
| 79 |
{ name => 'resource' }, { name => 'name' }, |
| 80 |
{ name => 'severity' }, { name => 'timeraised' }, |
| 81 |
{ name => 'acknowledged' } |
| 82 |
], |
| 83 |
closure_custom_output => $self->can('custom_status_output'), |
| 84 |
closure_custom_perfdata => sub { return 0; }, |
| 85 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 86 |
} |
| 87 |
} |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
sub new { |
| 92 |
my ($class, %options) = @_; |
| 93 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 94 |
bless $self, $class; |
| 95 |
|
| 96 |
$options{options}->add_options(arguments => { |
| 97 |
'filter-name:s' => { name => 'filter_name' }, |
| 98 |
'memory' => { name => 'memory' } |
| 99 |
}); |
| 100 |
|
| 101 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 102 |
return $self; |
| 103 |
} |
| 104 |
|
| 105 |
sub check_options { |
| 106 |
my ($self, %options) = @_; |
| 107 |
$self->SUPER::check_options(%options); |
| 108 |
|
| 109 |
if (defined($self->{option_results}->{memory})) { |
| 110 |
$self->{statefile_cache}->check_options(%options); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
sub manage_selection { |
| 115 |
my ($self, %options) = @_; |
| 116 |
|
| 117 |
$self->{global} = { |
| 118 |
'none' => 0, 'info' => 0, 'minor' => 0, 'major' => 0, 'critical' => 0 |
| 119 |
}; |
| 120 |
$self->{alarms}->{global} = { alarm => {} }; |
| 121 |
my $results = $options{custom}->request_api( |
| 122 |
endpoint => '/api/rest/alert', |
| 123 |
get_param => ['select=*'] |
| 124 |
); |
| 125 |
|
| 126 |
my $alerts_mem; |
| 127 |
if (defined($self->{option_results}->{memory})) { |
| 128 |
$self->{statefile_cache}->read(statefile => 'dell_powerstore_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port(). '_' . $self->{mode}); |
| 129 |
$alerts_mem = $self->{statefile_cache}->get(name => 'alerts'); |
| 130 |
} |
| 131 |
|
| 132 |
foreach my $entry (@$results) { |
| 133 |
next if ($entry->{state} eq 'CLEARED'); |
| 134 |
|
| 135 |
if (defined($self->{option_results}->{memory})) { |
| 136 |
if (defined($alerts_mem) && defined($alerts_mem->{ $entry->{id} })) { |
| 137 |
next; |
| 138 |
} |
| 139 |
$alerts_mem->{ $entry->{id} } = 1; |
| 140 |
} |
| 141 |
|
| 142 |
my $name = '-'; |
| 143 |
if (defined($entry->{name})) { |
| 144 |
$name = $entry->{name}; |
| 145 |
} elsif (defined($entry->{events}->[0]->{name})) { |
| 146 |
$name = $entry->{events}->[0]->{name}; |
| 147 |
} |
| 148 |
|
| 149 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 150 |
$name !~ /$self->{option_results}->{filter_name}/) { |
| 151 |
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); |
| 152 |
next; |
| 153 |
} |
| 154 |
|
| 155 |
$self->{global}->{ lc($entry->{severity}) }++; |
| 156 |
$self->{alarms}->{global}->{alarm}->{ $entry->{id} } = { |
| 157 |
resource => $entry->{resource_name}, |
| 158 |
name => $name, |
| 159 |
severity => lc($entry->{severity}), |
| 160 |
timeraised => $entry->{generated_timestamp}, |
| 161 |
acknowledged => ($entry->{is_acknowledged} =~ /True|1/i) ? 'yes' : 'no' |
| 162 |
}; |
| 163 |
} |
| 164 |
|
| 165 |
if (defined($self->{option_results}->{memory})) { |
| 166 |
$self->{statefile_cache}->write(data => { alerts => $alerts_mem }); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
1; |
| 171 |
|
| 172 |
__END__ |
| 173 |
|
| 174 |
=head1 MODE |
| 175 |
|
| 176 |
Check alerts. |
| 177 |
|
| 178 |
=over 8 |
| 179 |
|
| 180 |
=item B<--filter-name> |
| 181 |
|
| 182 |
Filter alerts by name (can be a regexp). |
| 183 |
|
| 184 |
=item B<--warning-status> |
| 185 |
|
| 186 |
Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor/i') |
| 187 |
You can use the following variables: %{severity}, %{resource}, %{name}, %{timeraised}, %{acknowledged} |
| 188 |
|
| 189 |
=item B<--critical-status> |
| 190 |
|
| 191 |
Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /major|critical/i'). |
| 192 |
You can use the following variables: %{severity}, %{resource}, %{name}, %{timeraised}, %{acknowledged} |
| 193 |
|
| 194 |
=item B<--warning-*> B<--critical-*> |
| 195 |
|
| 196 |
Thresholds. |
| 197 |
Can be: 'severity-none', 'severity-info', 'severity-minor', 'severity-major', 'severity-critical'. |
| 198 |
|
| 199 |
=item B<--memory> |
| 200 |
|
| 201 |
Only check new alarms. |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=cut |