| 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 network::aruba::orchestrator::restapi::mode::alarms; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use DateTime; |
| 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 |
"alarm [severity: %s] [name: %s] [hostname: %s] %s", |
| 35 |
$self->{result_values}->{severity}, |
| 36 |
$self->{result_values}->{name}, |
| 37 |
$self->{result_values}->{hostname}, |
| 38 |
$self->{result_values}->{timeraised} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_global_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return 'Alarms 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 alarm detected', format_output => '%s alarms detected', display_counter_problem => { nlabel => 'alerms.problems.current.count', min => 0 }, |
| 54 |
group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] |
| 55 |
} |
| 56 |
]; |
| 57 |
|
| 58 |
$self->{maps_counters}->{global} = []; |
| 59 |
foreach ('minor', 'warning', 'major', 'critical') { |
| 60 |
push @{$self->{maps_counters}->{global}}, { |
| 61 |
label => 'severity-' . $_, nlabel => 'alarms.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|warning/i', |
| 76 |
critical_default => '%{severity} =~ /major|critical/i', |
| 77 |
set => { |
| 78 |
key_values => [ |
| 79 |
{ name => 'name' }, { name => 'hostname' }, |
| 80 |
{ name => 'severity' }, { name => 'timeraised' } |
| 81 |
], |
| 82 |
closure_custom_output => $self->can('custom_status_output'), |
| 83 |
closure_custom_perfdata => sub { return 0; }, |
| 84 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 85 |
} |
| 86 |
} |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
sub new { |
| 91 |
my ($class, %options) = @_; |
| 92 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 93 |
bless $self, $class; |
| 94 |
|
| 95 |
$options{options}->add_options(arguments => { |
| 96 |
'filter-hostname:s' => { name => 'filter_hostname' }, |
| 97 |
'timezone:s' => { name => 'timezone' } |
| 98 |
}); |
| 99 |
|
| 100 |
return $self; |
| 101 |
} |
| 102 |
|
| 103 |
sub check_options { |
| 104 |
my ($self, %options) = @_; |
| 105 |
$self->SUPER::check_options(%options); |
| 106 |
|
| 107 |
$self->{option_results}->{timezone} = 'UTC' if (!defined($self->{option_results}->{timezone}) || $self->{option_results}->{timezone} eq ''); |
| 108 |
} |
| 109 |
|
| 110 |
sub manage_selection { |
| 111 |
my ($self, %options) = @_; |
| 112 |
|
| 113 |
$self->{global} = { |
| 114 |
minor => 0, warning => 0, major => 0, critical => 0 |
| 115 |
}; |
| 116 |
$self->{alarms}->{global} = { alarm => {} }; |
| 117 |
|
| 118 |
my $appliances = $options{custom}->request_api(endpoint => '/appliance'); |
| 119 |
my $post = { ids => [] }; |
| 120 |
|
| 121 |
foreach my $appliance (@$appliances) { |
| 122 |
next if (defined($self->{option_results}->{filter_hostname}) && $self->{option_results}->{filter_hostname} ne '' && |
| 123 |
$appliance->{hostName} !~ /$self->{option_results}->{filter_hostname}/); |
| 124 |
|
| 125 |
push @{$post->{ids}}, $appliance->{id}; |
| 126 |
} |
| 127 |
|
| 128 |
if (scalar(@{$post->{ids}}) <= 0) { |
| 129 |
$self->{output}->add_option_msg(short_msg => 'no appliances selected'); |
| 130 |
$self->{output}->option_exit(); |
| 131 |
} |
| 132 |
|
| 133 |
my $results = $options{custom}->request_api( |
| 134 |
method => 'POST', |
| 135 |
endpoint => '/alarm/appliance', |
| 136 |
get_param => ['view=active'], |
| 137 |
query_form_post => $post |
| 138 |
); |
| 139 |
|
| 140 |
foreach my $entry (@$results) { |
| 141 |
my $dt = DateTime->from_epoch(epoch => $entry->{timeOccurredInMills} / 1000, time_zone => $self->{option_results}->{timezone}); |
| 142 |
my $timeraised = sprintf( |
| 143 |
'%02d-%02d-%02dT%02d:%02d:%02d (%s)', $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second, $self->{option_results}->{timezone} |
| 144 |
); |
| 145 |
|
| 146 |
$self->{global}->{ lc($entry->{severity}) }++; |
| 147 |
$self->{alarms}->{global}->{alarm}->{ $entry->{id} } = { |
| 148 |
hostname => $entry->{hostName}, |
| 149 |
name => $entry->{name}, |
| 150 |
severity => lc($entry->{severity}), |
| 151 |
timeraised => $timeraised |
| 152 |
}; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
1; |
| 157 |
|
| 158 |
__END__ |
| 159 |
|
| 160 |
=head1 MODE |
| 161 |
|
| 162 |
Check alarms. |
| 163 |
|
| 164 |
=over 8 |
| 165 |
|
| 166 |
=item B<--filter-hostname> |
| 167 |
|
| 168 |
Filter alarms by hostname (can be a regexp). |
| 169 |
|
| 170 |
=item B<--timezone> |
| 171 |
|
| 172 |
Set timezone for creation time (default is 'UTC'). |
| 173 |
|
| 174 |
=item B<--warning-status> |
| 175 |
|
| 176 |
Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/i') |
| 177 |
You can use the following variables: %{severity}, %{hostname}, %{name}, %{timeraised} |
| 178 |
|
| 179 |
=item B<--critical-status> |
| 180 |
|
| 181 |
Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /major|critical/i'). |
| 182 |
You can use the following variables: %{severity}, %{hostname}, %{name}, %{timeraised} |
| 183 |
|
| 184 |
=item B<--warning-*> B<--critical-*> |
| 185 |
|
| 186 |
Thresholds. |
| 187 |
Can be: 'severity-minor', 'severity-warning', 'severity-major', 'severity-critical'. |
| 188 |
|
| 189 |
=back |
| 190 |
|
| 191 |
=cut |