| 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::video::appeartv::snmp::mode::alarms; |
| 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 |
my $msg = sprintf("alarm [severity: %s] [source: %s] [text: %s] %s", $self->{result_values}->{severity}, |
| 35 |
$self->{result_values}->{source}, $self->{result_values}->{text}, $self->{result_values}->{generation_time}); |
| 36 |
return $msg; |
| 37 |
} |
| 38 |
|
| 39 |
sub custom_status_calc { |
| 40 |
my ($self, %options) = @_; |
| 41 |
|
| 42 |
$self->{result_values}->{source} = $options{new_datas}->{$self->{instance} . '_msgSourceName'}; |
| 43 |
$self->{result_values}->{text} = $options{new_datas}->{$self->{instance} . '_msgText'}; |
| 44 |
$self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_msgSeverity'}; |
| 45 |
$self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'}; |
| 46 |
$self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_msgGenerationTime'}; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
sub set_counters { |
| 51 |
my ($self, %options) = @_; |
| 52 |
|
| 53 |
$self->{maps_counters_type} = [ |
| 54 |
{ name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 }, |
| 55 |
group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] |
| 56 |
} |
| 57 |
]; |
| 58 |
|
| 59 |
$self->{maps_counters}->{alarm} = [ |
| 60 |
{ label => 'status', |
| 61 |
threshold => 0, |
| 62 |
set => { |
| 63 |
key_values => [ { name => 'msgSourceName' }, { name => 'msgText' }, { name => 'since' }, { name => 'msgSeverity' }, { name => 'msgGenerationTime' } ], |
| 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); |
| 76 |
bless $self, $class; |
| 77 |
|
| 78 |
$options{options}->add_options(arguments => |
| 79 |
{ |
| 80 |
"filter-msg:s" => { name => 'filter_msg' }, |
| 81 |
"warning-status:s" => { name => 'warning_status', default => '%{severity} =~ /minor|warning/i' }, |
| 82 |
"critical-status:s" => { name => 'critical_status', default => '%{severity} =~ /critical|major/i' }, |
| 83 |
"memory" => { name => 'memory' }, |
| 84 |
}); |
| 85 |
|
| 86 |
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', |
| 87 |
error_msg => "Cannot load module 'Date::Parse'."); |
| 88 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 89 |
return $self; |
| 90 |
} |
| 91 |
|
| 92 |
sub check_options { |
| 93 |
my ($self, %options) = @_; |
| 94 |
$self->SUPER::check_options(%options); |
| 95 |
|
| 96 |
$self->change_macros(macros => [ 'warning_status', 'critical_status' ]); |
| 97 |
if (defined($self->{option_results}->{memory})) { |
| 98 |
$self->{statefile_cache}->check_options(%options); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
my %map_severity = (0 => 'indeterminate', |
| 103 |
1 => 'critical', |
| 104 |
2 => 'major', |
| 105 |
3 => 'minor', |
| 106 |
4 => 'warning', |
| 107 |
5 => 'cleared', |
| 108 |
6 => 'notify'); |
| 109 |
|
| 110 |
my $mapping = { |
| 111 |
msgSourceName => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.3' }, |
| 112 |
msgText => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.4' }, |
| 113 |
msgGenerationTime => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.6' }, # 2016-12-16 14:49:18 |
| 114 |
msgSeverity => { oid => '.1.3.6.1.4.1.23916.3.1.4.1.13', map => \%map_severity }, |
| 115 |
}; |
| 116 |
|
| 117 |
sub manage_selection { |
| 118 |
my ($self, %options) = @_; |
| 119 |
|
| 120 |
$self->{alarms}->{global} = { alarm => {} }; |
| 121 |
my $snmp_result = $options{snmp}->get_multiple_table(oids => [ |
| 122 |
{ oid => $mapping->{msgSourceName}->{oid} }, |
| 123 |
{ oid => $mapping->{msgText}->{oid} }, |
| 124 |
{ oid => $mapping->{msgGenerationTime}->{oid} }, |
| 125 |
{ oid => $mapping->{msgSeverity}->{oid} }, |
| 126 |
], |
| 127 |
return_type => 1); |
| 128 |
|
| 129 |
my $last_time; |
| 130 |
if (defined($self->{option_results}->{memory})) { |
| 131 |
$self->{statefile_cache}->read(statefile => "cache_appeartv_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode}); |
| 132 |
$last_time = $self->{statefile_cache}->get(name => 'last_time'); |
| 133 |
} |
| 134 |
|
| 135 |
my ($i, $current_time) = (1, time()); |
| 136 |
foreach my $oid (keys %{$snmp_result}) { |
| 137 |
next if ($oid !~ /^$mapping->{msgSeverity}->{oid}\.(.*)$/); |
| 138 |
my $instance = $1; |
| 139 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); |
| 140 |
|
| 141 |
my $create_time = Date::Parse::str2time($result->{msgGenerationTime}); |
| 142 |
if (!defined($create_time)) { |
| 143 |
$self->{output}->output_add(severity => 'UNKNOWN', |
| 144 |
short_msg => "Can't Parse date '" . $result->{msgGenerationTime} . "'"); |
| 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->{msgText} !~ /$self->{option_results}->{filter_msg}/) { |
| 151 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{msgText} . "': no matching filter.", debug => 1); |
| 152 |
next; |
| 153 |
} |
| 154 |
|
| 155 |
my $diff_time = $current_time - $create_time; |
| 156 |
|
| 157 |
$self->{alarms}->{global}->{alarm}->{$i} = { %$result, since => $diff_time }; |
| 158 |
$i++; |
| 159 |
} |
| 160 |
|
| 161 |
if (defined($self->{option_results}->{memory})) { |
| 162 |
$self->{statefile_cache}->write(data => { last_time => $current_time }); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
1; |
| 167 |
|
| 168 |
__END__ |
| 169 |
|
| 170 |
=head1 MODE |
| 171 |
|
| 172 |
Check alarms. |
| 173 |
|
| 174 |
=over 8 |
| 175 |
|
| 176 |
=item C<--filter-msg> |
| 177 |
|
| 178 |
Filter by message (can be a regexp). |
| 179 |
|
| 180 |
=item C<--warning-status> |
| 181 |
|
| 182 |
Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/i') |
| 183 |
You can use the following variables: %{severity}, %{text}, %{source}, %{since} |
| 184 |
|
| 185 |
=item C<--critical-status> |
| 186 |
|
| 187 |
Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /critical|major/i'). |
| 188 |
You can use the following variables: %{severity}, %{text}, %{source}, %{since} |
| 189 |
|
| 190 |
=item C<--memory> |
| 191 |
|
| 192 |
Only check new alarms. |
| 193 |
|
| 194 |
=back |
| 195 |
|
| 196 |
=cut |