Debian Codegraph @

src/network/silverpeak/snmp/mode/alarms.pm

raw
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 network::silverpeak::snmp::mode::alarms;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28 use POSIX;
29 use centreon::plugins::misc;
30 use centreon::plugins::statefile;
31 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
32
33 sub custom_status_output {
34 my ($self, %options) = @_;
35
36 return sprintf(
37 "alarm [severity: %s] [source: %s] [text: %s] %s",
38 $self->{result_values}->{severity},
39 $self->{result_values}->{source},
40 $self->{result_values}->{text},
41 scalar(localtime($self->{result_values}->{generation_time}))
42 );
43 }
44
45 sub custom_status_calc {
46 my ($self, %options) = @_;
47
48 $self->{result_values}->{source} = $options{new_datas}->{$self->{instance} . '_spsActiveAlarmSource'};
49 $self->{result_values}->{text} = $options{new_datas}->{$self->{instance} . '_spsActiveAlarmDescr'};
50 $self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_spsActiveAlarmSeverity'};
51 $self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'};
52 $self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_spsActiveAlarmLogTime'};
53 return 0;
54 }
55
56 sub set_counters {
57 my ($self, %options) = @_;
58
59 $self->{maps_counters_type} = [
60 { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 },
61 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
62 }
63 ];
64
65 $self->{maps_counters}->{alarm} = [
66 {
67 label => 'status',
68 type => 2,
69 warning_default => '%{severity} =~ /minor|warning/i',
70 critical_default => '%{severity} =~ /critical|major/i',
71 set => {
72 key_values => [ { name => 'spsActiveAlarmSource' }, { name => 'spsActiveAlarmDescr' },
73 { name => 'since' }, { name => 'spsActiveAlarmSeverity' }, { name => 'spsActiveAlarmLogTime' } ],
74 closure_custom_calc => $self->can('custom_status_calc'),
75 closure_custom_output => $self->can('custom_status_output'),
76 closure_custom_perfdata => sub { return 0; },
77 closure_custom_threshold_check => \&catalog_status_threshold_ng
78 }
79 }
80 ];
81 }
82
83 sub new {
84 my ($class, %options) = @_;
85 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
86 bless $self, $class;
87
88 $options{options}->add_options(arguments => {
89 'filter-msg:s' => { name => 'filter_msg' },
90 'memory' => { name => 'memory' }
91 });
92
93 centreon::plugins::misc::mymodule_load(
94 output => $self->{output},
95 module => 'Date::Parse',
96 error_msg => "Cannot load module 'Date::Parse'."
97 );
98 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
99 return $self;
100 }
101
102 sub check_options {
103 my ($self, %options) = @_;
104 $self->SUPER::check_options(%options);
105
106 if (defined($self->{option_results}->{memory})) {
107 $self->{statefile_cache}->check_options(%options);
108 }
109 }
110
111 my %map_severity = (0 => 'info', 1 => 'warning', 2 => 'minor', 3 => 'major',
112 4 => 'critical', 5 => 'cleared', 6 => 'acknowledged', 7 => 'unacknowledged', 8 => 'indeterminate');
113
114 my $mapping = {
115 spsActiveAlarmSeverity => { oid => '.1.3.6.1.4.1.23867.3.1.1.2.1.1.3', map => \%map_severity },
116 spsActiveAlarmSource => { oid => '.1.3.6.1.4.1.23867.3.1.1.2.1.1.6' },
117 spsActiveAlarmDescr => { oid => '.1.3.6.1.4.1.23867.3.1.1.2.1.1.5' },
118 spsActiveAlarmLogTime => { oid => '.1.3.6.1.4.1.23867.3.1.1.2.1.1.11' } # timestamp
119 };
120
121 sub manage_selection {
122 my ($self, %options) = @_;
123
124 $self->{alarms}->{global} = { alarm => {} };
125 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
126 { oid => $mapping->{spsActiveAlarmSource}->{oid} },
127 { oid => $mapping->{spsActiveAlarmDescr}->{oid} },
128 { oid => $mapping->{spsActiveAlarmLogTime}->{oid} },
129 { oid => $mapping->{spsActiveAlarmSeverity}->{oid} },
130 ], return_type => 1);
131
132 my $last_time;
133 if (defined($self->{option_results}->{memory})) {
134 $self->{statefile_cache}->read(statefile => "cache_silverpeak_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port(). '_' . $self->{mode});
135 $last_time = $self->{statefile_cache}->get(name => 'last_time');
136 }
137
138 my ($i, $current_time) = (1, time());
139 foreach my $oid (keys %{$snmp_result}) {
140 next if ($oid !~ /^$mapping->{spsActiveAlarmSeverity}->{oid}\.(.*)$/);
141 my $instance = $1;
142 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
143
144 if (!defined($result->{spsActiveAlarmLogTime})) {
145 $self->{output}->output_add(
146 severity => 'UNKNOWN',
147 short_msg => "Can't get date '" . $result->{spsActiveAlarmLogTime} . "'"
148 );
149 next;
150 }
151
152 my $create_time = $result->{spsActiveAlarmLogTime};
153 if ($create_time !~ /^\d+$/) {
154 my @date = unpack('n C6 a C2', $result->{spsActiveAlarmLogTime});
155 my $tz;
156 if (defined($date[7])) {
157 $tz = sprintf('%s%02d%02d', $date[7], $date[8], $date[9]);
158 }
159 my $dt = DateTime->new(
160 year => $date[0],
161 month => $date[1],
162 day => $date[2],
163 hour => $date[3],
164 minute => $date[4],
165 second => $date[5],
166 time_zone => $tz
167 );
168 $create_time = $dt->epoch();
169 }
170
171 next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time);
172
173 if (defined($self->{option_results}->{filter_msg}) && $self->{option_results}->{filter_msg} ne '' &&
174 $result->{spsActiveAlarmDescr} !~ /$self->{option_results}->{filter_msg}/) {
175 $self->{output}->output_add(long_msg => "skipping '" . $result->{spsActiveAlarmDescr} . "': no matching filter.", debug => 1);
176 next;
177 }
178
179 my $diff_time = $current_time - $create_time;
180
181 $self->{alarms}->{global}->{alarm}->{$i} = $result;
182 $self->{alarms}->{global}->{alarm}->{$i}->{since} = $diff_time;
183 $self->{alarms}->{global}->{alarm}->{$i}->{spsActiveAlarmLogTime} = $create_time;
184 $i++;
185 }
186
187 if (defined($self->{option_results}->{memory})) {
188 $self->{statefile_cache}->write(data => { last_time => $current_time });
189 }
190 }
191
192 1;
193
194 __END__
195
196 =head1 MODE
197
198 Check alarms.
199
200 =over 8
201
202 =item B<--filter-msg>
203
204 Filter by message (can be a regexp).
205
206 =item B<--warning-status>
207
208 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/i')
209 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
210
211 =item B<--critical-status>
212
213 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /critical|major/i').
214 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
215
216 =item B<--memory>
217
218 Only check new alarms.
219
220 =back
221
222 =cut