| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and cluster 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 hardware::devices::hms::netbiter::argos::restapi::mode::alarms; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Date::Parse; |
| 28 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 29 |
|
| 30 |
sub prefix_global_output { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
return 'Alarms: '; |
| 34 |
} |
| 35 |
|
| 36 |
sub prefix_alarms_output { |
| 37 |
my ($self, %options) = @_; |
| 38 |
return sprintf('[%s] Alarm name: "%s", Device name: "%s" ', $options{instance_value}->{timestamp}, $options{instance_value}->{display}, $options{instance_value}->{device_name}); |
| 39 |
} |
| 40 |
|
| 41 |
sub set_counters { |
| 42 |
my ($self, %options) = @_; |
| 43 |
|
| 44 |
$self->{maps_counters_type} = [ |
| 45 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, |
| 46 |
{ name => 'alarms', type => 1, cb_prefix_output => 'prefix_alarms_output' } |
| 47 |
]; |
| 48 |
|
| 49 |
$self->{maps_counters}->{global} = [ |
| 50 |
{ label => 'alarms-total', nlabel => 'alarms.total.count', set => { |
| 51 |
key_values => [ { name => 'total' } ], |
| 52 |
output_template => 'total current: %s', |
| 53 |
perfdatas => [ { template => '%d', min => 0 } ] |
| 54 |
} |
| 55 |
} |
| 56 |
]; |
| 57 |
|
| 58 |
$self->{maps_counters}->{alarms} = [ |
| 59 |
{ label => 'alarm-active', |
| 60 |
type => 2, |
| 61 |
critical_default => '%{active} =~ "true"', |
| 62 |
set => { |
| 63 |
key_values => [ { name => 'active' } ], |
| 64 |
output_template => 'active: %s', |
| 65 |
display_ok => 0, |
| 66 |
closure_custom_perfdata => sub { return 0; }, |
| 67 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 68 |
} |
| 69 |
}, |
| 70 |
{ label => 'alarm-acked', |
| 71 |
type => 2, |
| 72 |
warning_default => '%{acked} =~ "false"', |
| 73 |
set => { |
| 74 |
key_values => [ { name => 'acked' } ], |
| 75 |
output_template => 'acknowledged: %s', |
| 76 |
display_ok => 0, |
| 77 |
closure_custom_perfdata => sub { return 0; }, |
| 78 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 79 |
} |
| 80 |
}, |
| 81 |
{ label => 'alarm-duration', nlabel => 'alarm.duration.seconds', |
| 82 |
type => 1, |
| 83 |
set => { |
| 84 |
key_values => [ { name => 'duration' }, { name => 'display' } ], |
| 85 |
output_template => 'duration: %ds', |
| 86 |
display_ok => 0, |
| 87 |
perfdatas => [ { template => '%d', min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display' } ] |
| 88 |
} |
| 89 |
}, |
| 90 |
{ label => 'alarm-severity', |
| 91 |
type => 1, |
| 92 |
set => { |
| 93 |
key_values => [ { name => 'severity' } ], |
| 94 |
output_template => 'severity: %s', |
| 95 |
display_ok => 0, |
| 96 |
closure_custom_perfdata => sub { return 0; } |
| 97 |
} |
| 98 |
} |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
sub new { |
| 103 |
my ($class, %options) = @_; |
| 104 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 105 |
bless $self, $class; |
| 106 |
|
| 107 |
$options{options}->add_options(arguments => { |
| 108 |
'filter-acked' => { name => 'filter_acked' }, |
| 109 |
'filter-active' => { name => 'filter_active' }, |
| 110 |
'filter-severity:s' => { name => 'filter_severity' }, |
| 111 |
'system-id:s' => { name => 'system_id' } |
| 112 |
}); |
| 113 |
|
| 114 |
return $self; |
| 115 |
} |
| 116 |
|
| 117 |
sub check_options { |
| 118 |
my ($self, %options) = @_; |
| 119 |
$self->SUPER::check_options(%options); |
| 120 |
|
| 121 |
if (defined($self->{option_results}->{filter_severity}) && $self->{option_results}->{filter_severity} ne '') { |
| 122 |
if (lc($self->{option_results}->{filter_severity}) !~ '/critical|major|minor|warning|cleared/') { |
| 123 |
$self->{output}->add_option_msg(short_msg => 'Unknown severity "' . $self->{option_results}->{filter_severity} . '"'); |
| 124 |
$self->{output}->option_exit(); |
| 125 |
} |
| 126 |
$self->{severity} = lc($self->{option_results}->{filter_severity}); |
| 127 |
} |
| 128 |
if (!(defined($self->{option_results}->{system_id})) || $self->{option_results}->{system_id} eq '') { |
| 129 |
$self->{output}->add_option_msg(short_msg => "Need to specify --system-id option."); |
| 130 |
$self->{output}->option_exit(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
sub manage_selection { |
| 135 |
my ($self, %options) = @_; |
| 136 |
|
| 137 |
$self->{global}->{total} = 0; |
| 138 |
|
| 139 |
my $severity_mapping = { |
| 140 |
critical => '1', |
| 141 |
major => '2', |
| 142 |
minor => '3', |
| 143 |
warning => '4', |
| 144 |
cleared => '5' |
| 145 |
}; |
| 146 |
my $active_alarms = 'false'; |
| 147 |
my $url = '/system/' . $self->{option_results}->{system_id} . '/alarm'; |
| 148 |
my $get_params; |
| 149 |
|
| 150 |
if (defined($self->{severity})) { |
| 151 |
my $severity_level = $severity_mapping->{$self->{severity}}; |
| 152 |
push @$get_params, 'severity=' . $severity_level; |
| 153 |
} |
| 154 |
if (defined($self->{option_results}->{filter_acked})) { |
| 155 |
push @$get_params, 'acked=false'; |
| 156 |
} |
| 157 |
if (defined($self->{option_results}->{filter_active})) { |
| 158 |
$active_alarms = 'true'; |
| 159 |
} |
| 160 |
push @$get_params, 'active=' . $active_alarms; |
| 161 |
|
| 162 |
my $result = $options{custom}->request_api( |
| 163 |
request => $url, |
| 164 |
get_params => $get_params |
| 165 |
); |
| 166 |
|
| 167 |
foreach (@{$result}) { |
| 168 |
my $timestamp = str2time($_->{timestamp}, 'GMT'); |
| 169 |
$self->{alarms}->{$_->{id}} = { |
| 170 |
acked => ($_->{acked}) ? 'true' : 'false', |
| 171 |
active => ($_->{active}) ? 'true' : 'false', |
| 172 |
device_name => $_->{deviceName}, |
| 173 |
duration => time() - $timestamp, |
| 174 |
display => $_->{name}, |
| 175 |
id => $_->{id}, |
| 176 |
severity => $_->{severity}, |
| 177 |
timestamp => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($timestamp)) |
| 178 |
}; |
| 179 |
} |
| 180 |
|
| 181 |
$self->{global}->{total} = scalar (keys %{$self->{alarms}}); |
| 182 |
} |
| 183 |
|
| 184 |
1; |
| 185 |
|
| 186 |
__END__ |
| 187 |
|
| 188 |
=head1 MODE |
| 189 |
|
| 190 |
Check Netbiter alarms using Argos RestAPI. |
| 191 |
|
| 192 |
Example: |
| 193 |
perl centreon_plugins.pl --plugin=hardware::devices::hms::netbiter::argos::restapi::plugin --mode=alarms |
| 194 |
--access-key='ABCDEFG1234567890' --system-id='XYZ123' --filter-active --verbose |
| 195 |
|
| 196 |
More information on'https://apidocs.netbiter.net/?page=methods&show=getSystemAlarms'. |
| 197 |
|
| 198 |
=over 8 |
| 199 |
|
| 200 |
=item B<--system-id> |
| 201 |
|
| 202 |
Set the Netbiter Argos System ID (mandatory). |
| 203 |
|
| 204 |
=item B<--filter-acked> |
| 205 |
|
| 206 |
Hide acknowledged alarms. |
| 207 |
|
| 208 |
=item B<--filter-active> |
| 209 |
|
| 210 |
Only show active alarms. |
| 211 |
|
| 212 |
=item B<--filter-severity> |
| 213 |
|
| 214 |
Only show alarms with a given severity level. |
| 215 |
Can be: 'critical', 'major', 'minor', 'warning', 'cleared'. |
| 216 |
Only one value can be set (no multiple values). |
| 217 |
|
| 218 |
=item B<--warning-active-status> |
| 219 |
|
| 220 |
Set warning threshold for active status (default: ''). |
| 221 |
Typical syntax: --warning-active-status='%{active} =~ "true"' |
| 222 |
|
| 223 |
=item B<--critical-active-status> |
| 224 |
|
| 225 |
Set critical threshold for active status (default: '%{active} =~ "true"'). |
| 226 |
Typical syntax: --critical-active-status='%{active} =~ "true"' |
| 227 |
|
| 228 |
=item B<--warning-acked-status> |
| 229 |
|
| 230 |
Set warning threshold for acked status (default: '%{acked} =~ "false"'). |
| 231 |
Typical syntax: --warning-acked-status='%{acked} =~ "false"' |
| 232 |
|
| 233 |
=item B<--critical-acked-status> |
| 234 |
|
| 235 |
Set critical threshold for acked status (default: ''). |
| 236 |
Typical syntax: --critical-acked-status='%{acked} =~ "false"' |
| 237 |
|
| 238 |
=item B<--warning-*> B<--critical-*> |
| 239 |
|
| 240 |
Thresholds. |
| 241 |
Can be: 'warning-alarms-total' (count) 'critical-alarms-total' (count), |
| 242 |
'warning-alarm-duration' (s), 'critical-alarm-duration' (s), |
| 243 |
'warning-alarm-severity' (level from 0 to 5), critical-alarm-severity (level from 0 to 5). |
| 244 |
|
| 245 |
=back |
| 246 |
|
| 247 |
=cut |