Debian Codegraph @

src/network/perle/ids/snmp/mode/alarms.pm

raw
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::perle::ids::snmp::mode::alarms;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use POSIX;
28 use centreon::plugins::misc;
29 use centreon::plugins::statefile;
30 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
31
32 sub custom_status_output {
33 my ($self, %options) = @_;
34
35 my $msg = sprintf("alarm [severity: %s] [source: %s] [text: %s] %s", $self->{result_values}->{severity},
36 $self->{result_values}->{source}, $self->{result_values}->{text}, $self->{result_values}->{generation_time});
37 return $msg;
38 }
39
40 sub custom_status_calc {
41 my ($self, %options) = @_;
42
43 $self->{result_values}->{source} = $options{new_datas}->{$self->{instance} . '_perleEntityAlarmSourceName'};
44 $self->{result_values}->{text} = $options{new_datas}->{$self->{instance} . '_perleEntityAlarmDescr'};
45 $self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_perleEntityAlarmSeverity'};
46 $self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'};
47 $self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_perleEntityAlarmTimeStamp'};
48 return 0;
49 }
50
51 sub set_counters {
52 my ($self, %options) = @_;
53
54 $self->{maps_counters_type} = [
55 { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 },
56 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
57 }
58 ];
59
60 $self->{maps_counters}->{alarm} = [
61 { label => 'status', threshold => 0, set => {
62 key_values => [ { name => 'perleEntityAlarmDescr' }, { name => 'perleEntityAlarmSeverity' }, { name => 'perleEntityAlarmTimeStamp' },
63 { name => 'since' }, { name => 'perleEntityAlarmSourceName' }, { name => 'perleEntityAlarmType' } ],
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 'filter-msg:s' => { name => 'filter_msg' },
80 'warning-status:s' => { name => 'warning_status', default => '%{severity} =~ /minor/i' },
81 'critical-status:s' => { name => 'critical_status', default => '%{severity} =~ /critical|major/i' },
82 'memory' => { name => 'memory' },
83 });
84
85 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
86 return $self;
87 }
88
89 sub check_options {
90 my ($self, %options) = @_;
91 $self->SUPER::check_options(%options);
92
93 $self->change_macros(macros => ['warning_status', 'critical_status']);
94 if (defined($self->{option_results}->{memory})) {
95 $self->{statefile_cache}->check_options(%options);
96 }
97 }
98
99 my %map_severity = (
100 1 => 'critical',
101 2 => 'major',
102 3 => 'minor',
103 4 => 'info',
104 );
105
106 my $mapping = {
107 perleEntityAlarmSourceName => { oid => '.1.3.6.1.4.1.1966.22.11.1.1.1.3' },
108 perleEntityAlarmSeverity => { oid => '.1.3.6.1.4.1.1966.22.11.1.1.1.4', map => \%map_severity },
109 perleEntityAlarmDescr => { oid => '.1.3.6.1.4.1.1966.22.11.1.1.1.5' },
110 perleEntityAlarmTimeStamp => { oid => '.1.3.6.1.4.1.1966.22.11.1.1.1.6' },
111 };
112 my $oid_perleEntityAlarmEntry = '.1.3.6.1.4.1.1966.22.11.1.1.1';
113
114 sub manage_selection {
115 my ($self, %options) = @_;
116
117 $self->{alarms}->{global} = { alarm => {} };
118 my $snmp_result = $options{snmp}->get_table(
119 oid => $oid_perleEntityAlarmEntry,
120 start => $mapping->{perleEntityAlarmSourceName}->{oid},
121 end => $mapping->{perleEntityAlarmDescr}->{oid}
122 );
123
124 my $last_time;
125 if (defined($self->{option_results}->{memory})) {
126 $self->{statefile_cache}->read(statefile => "cache_perle_ids_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port(). '_' . $self->{mode});
127 $last_time = $self->{statefile_cache}->get(name => 'last_time');
128 }
129
130 my ($i, $current_time) = (1, time());
131 foreach my $oid (keys %{$snmp_result}) {
132 next if ($oid !~ /^$mapping->{perleEntityAlarmSeverity}->{oid}\.(.*)$/);
133 my $instance = $1;
134 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
135
136 my $create_time = $result->{perleEntityAlarmTimeStamp};
137 $result->{perleEntityAlarmTimeStamp} = strftime("%d/%m/%Y %H:%M:%S", localtime($result->{perleEntityAlarmTimeStamp}));
138 if (!defined($create_time)) {
139 $self->{output}->output_add(
140 severity => 'UNKNOWN',
141 short_msg => "Can't get date '" . $create_time . "'"
142 );
143 next;
144 }
145
146 next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time);
147 if (defined($self->{option_results}->{filter_msg}) && $self->{option_results}->{filter_msg} ne '' &&
148 $result->{perleEntityAlarmDescr} !~ /$self->{option_results}->{filter_msg}/) {
149 $self->{output}->output_add(long_msg => "skipping '" . $result->{perleEntityAlarmDescr} . "': no matching filter.", debug => 1);
150 next;
151 }
152
153 my $diff_time = $current_time - $create_time;
154 $self->{alarms}->{global}->{alarm}->{$i} = { %$result, since => $diff_time };
155 $i++;
156 }
157
158 if (defined($self->{option_results}->{memory})) {
159 $self->{statefile_cache}->write(data => { last_time => $current_time });
160 }
161 }
162
163 1;
164
165 __END__
166
167 =head1 MODE
168
169 Check alarms.
170
171 =over 8
172
173 =item B<--filter-msg>
174
175 Filter by message (can be a regexp).
176
177 =item B<--warning-status>
178
179 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor/i')
180 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
181
182 =item B<--critical-status>
183
184 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /critical|major/i').
185 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
186
187 =item B<--memory>
188
189 Only check new alarms.
190
191 =back
192
193 =cut