| 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 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 hardware::devices::polycom::dma::snmp::mode::alerts; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
use centreon::plugins::statefile; |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); |
| 30 |
|
| 31 |
sub custom_status_output { |
| 32 |
my ($self, %options) = @_; |
| 33 |
|
| 34 |
return sprintf( |
| 35 |
"alarm [code: %s] [severity: %s] [description: %s] %s", $self->{result_values}->{code}, |
| 36 |
$self->{result_values}->{severity}, $self->{result_values}->{description}, $self->{result_values}->{timestamp} |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
sub custom_status_calc { |
| 41 |
my ($self, %options) = @_; |
| 42 |
|
| 43 |
$self->{result_values}->{code} = $options{new_datas}->{$self->{instance} . '_alActAlertCode'}; |
| 44 |
$self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_alActAlertSeverity'}; |
| 45 |
$self->{result_values}->{description} = $options{new_datas}->{$self->{instance} . '_alActAlertDescription'}; |
| 46 |
$self->{result_values}->{timestamp} = $options{new_datas}->{$self->{instance} . '_alActAlertTimestamp'}; |
| 47 |
$self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'}; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
sub set_counters { |
| 52 |
my ($self, %options) = @_; |
| 53 |
|
| 54 |
$self->{maps_counters_type} = [ |
| 55 |
{ name => 'alerts', type => 2, message_multiple => 'No problem detected', display_counter_problem => { label => 'dma.alerts.total.count', min => 0 }, |
| 56 |
group => [ { name => 'alert', skipped_code => { -11 => 1 } } ] |
| 57 |
} |
| 58 |
]; |
| 59 |
|
| 60 |
$self->{maps_counters}->{alert} = [ |
| 61 |
{ label => 'status', threshold => 0, set => { |
| 62 |
key_values => [ { name => 'alActAlertCode' }, { name => 'alActAlertSeverity' }, |
| 63 |
{ name => 'since' }, { name => 'alActAlertDescription' }, { name => 'alActAlertTimestamp' } ], |
| 64 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 65 |
closure_custom_output => $self->can('custom_status_output'), |
| 66 |
closure_custom_perfdata => sub { return 0; }, |
| 67 |
closure_custom_threshold_check => \&catalog_status_threshold |
| 68 |
} |
| 69 |
} |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
sub new { |
| 74 |
my ($class, %options) = @_; |
| 75 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 76 |
bless $self, $class; |
| 77 |
|
| 78 |
$options{options}->add_options(arguments => { |
| 79 |
'filter-msg:s' => { name => 'filter_msg' }, |
| 80 |
'warning-status:s' => { name => 'warning_status', default => '%{severity} =~ /warn/i' }, |
| 81 |
'critical-status:s' => { name => 'critical_status', default => '%{severity} =~ /severe/i' }, |
| 82 |
'memory' => { name => 'memory' } |
| 83 |
}); |
| 84 |
|
| 85 |
centreon::plugins::misc::mymodule_load( |
| 86 |
output => $self->{output}, |
| 87 |
module => 'Date::Parse', |
| 88 |
error_msg => "Cannot load module 'Date::Parse'." |
| 89 |
); |
| 90 |
|
| 91 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 92 |
|
| 93 |
return $self; |
| 94 |
} |
| 95 |
|
| 96 |
sub check_options { |
| 97 |
my ($self, %options) = @_; |
| 98 |
$self->SUPER::check_options(%options); |
| 99 |
|
| 100 |
$self->change_macros(macros => ['warning_status', 'critical_status']); |
| 101 |
if (defined($self->{option_results}->{memory})) { |
| 102 |
$self->{statefile_cache}->check_options(%options); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
my %map_severity = (0 => 'warn', 1 => 'severe', 2 => 'critical'); |
| 107 |
|
| 108 |
my $mapping = { |
| 109 |
alActAlertTimestamp => { oid => '.1.3.6.1.4.1.13885.13.2.4.1.2.1.3' }, |
| 110 |
alActAlertCode => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.4' }, |
| 111 |
alActAlertSeverity => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.6' }, |
| 112 |
alActAlertDescription => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.13', map => \%map_severity } |
| 113 |
}; |
| 114 |
|
| 115 |
my $oid_alActiveAlertsEntry = '.1.3.6.1.4.1.13885.13.2.4.1.2.1'; |
| 116 |
|
| 117 |
sub manage_selection { |
| 118 |
my ($self, %options) = @_; |
| 119 |
|
| 120 |
$self->{alerts}->{global} = { alarm => {} }; |
| 121 |
|
| 122 |
my $alert_result = $options{snmp}->get_table( |
| 123 |
oid => $oid_alActiveAlertsEntry, |
| 124 |
nothing_quit => 0 |
| 125 |
); |
| 126 |
|
| 127 |
my $last_time; |
| 128 |
if (defined($self->{option_results}->{memory})) { |
| 129 |
$self->{statefile_cache}->read(statefile => 'cache_polycom_dma_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port(). '_' . $self->{mode}); |
| 130 |
$last_time = $self->{statefile_cache}->get(name => 'last_time'); |
| 131 |
} |
| 132 |
|
| 133 |
my ($i, $current_time) = (1, time()); |
| 134 |
foreach my $oid (keys %{$alert_result}) { |
| 135 |
next if ($oid !~ /^$mapping->{alActAlertSeverity}->{oid}\.(.*)$/); |
| 136 |
my $instance = $1; |
| 137 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $alert_result, instance => $instance); |
| 138 |
|
| 139 |
my $create_time = Date::Parse::str2time($result->{msgGenerationTime}); |
| 140 |
if (!defined($create_time)) { |
| 141 |
$self->{output}->output_add( |
| 142 |
severity => 'UNKNOWN', |
| 143 |
short_msg => "Can't Parse date '" . $result->{msgGenerationTime} . "'" |
| 144 |
); |
| 145 |
next; |
| 146 |
} |
| 147 |
|
| 148 |
next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time); |
| 149 |
if (defined($self->{option_results}->{filter_msg}) && $self->{option_results}->{filter_msg} ne '' && |
| 150 |
$result->{filter_severity} !~ /$self->{option_results}->{filter_msg}/) { |
| 151 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{filter_severity} . "': no matching filter.", debug => 1); |
| 152 |
next; |
| 153 |
} |
| 154 |
if (defined($self->{option_results}->{filter_severity}) && $self->{option_results}->{filter_severity} ne '' && |
| 155 |
$result->{alActAlertSeverity} !~ /$self->{option_results}->{filter_severity}/) { |
| 156 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{alActAlertSeverity} . "': no matching filter.", debug => 1); |
| 157 |
next; |
| 158 |
} |
| 159 |
|
| 160 |
my $diff_time = $current_time - $create_time; |
| 161 |
|
| 162 |
$self->{alerts}->{global}->{alert}->{$i} = { %$result, since => $diff_time }; |
| 163 |
$i++; |
| 164 |
} |
| 165 |
|
| 166 |
if (defined($self->{option_results}->{memory})) { |
| 167 |
$self->{statefile_cache}->write(data => { last_time => $current_time }); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
1; |
| 172 |
|
| 173 |
__END__ |
| 174 |
|
| 175 |
=head1 MODE |
| 176 |
|
| 177 |
Check alerts. |
| 178 |
|
| 179 |
=over 8 |
| 180 |
|
| 181 |
=item B<--filter-msg> |
| 182 |
|
| 183 |
Filter by message (can be a regexp). |
| 184 |
|
| 185 |
=item B<--warning-status> |
| 186 |
|
| 187 |
Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /warn/i') |
| 188 |
Can use special variables like: %{severity}, %{text}, %{code}, %{since} |
| 189 |
|
| 190 |
=item B<--critical-status> |
| 191 |
|
| 192 |
Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /severe/i'). |
| 193 |
Can use special variables like: %{severity}, %{text}, %{source}, %{since} |
| 194 |
|
| 195 |
=item B<--memory> |
| 196 |
|
| 197 |
Only check new alarms. |
| 198 |
|
| 199 |
=back |
| 200 |
|
| 201 |
=cut |