Debian Codegraph @

src/network/rad/airmux/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::rad::airmux::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} . '_radwllMilOduAgnLastEventsIfIndex'};
44 $self->{result_values}->{text} = $options{new_datas}->{$self->{instance} . '_radwllMilOduAgnLastEventsText'};
45 $self->{result_values}->{severity} = $options{new_datas}->{$self->{instance} . '_radwllMilOduAgnLastEventsSeverity'};
46 $self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'};
47 $self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_radwllMilOduAgnLastEventsTimeT'};
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 => 'radwllMilOduAgnLastEventsIfIndex' }, { name => 'radwllMilOduAgnLastEventsText' },
63 { name => 'since' }, { name => 'radwllMilOduAgnLastEventsSeverity' }, { name => 'radwllMilOduAgnLastEventsTimeT' } ],
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|warning/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 = (1 => 'event', 2 => 'normal', 4 => 'warning', 8 => 'minor',
100 16 => 'major', 32 => 'critical');
101
102 my $mapping = {
103 radwllMilOduAgnLastEventsIfIndex => { oid => '.1.3.6.1.4.1.4458.1000.1.7.4.2.1.3' },
104 radwllMilOduAgnLastEventsText => { oid => '.1.3.6.1.4.1.4458.1000.1.7.4.2.1.5' },
105 radwllMilOduAgnLastEventsTimeT => { oid => '.1.3.6.1.4.1.4458.1000.1.7.4.2.1.4' }, #timestamp in second
106 radwllMilOduAgnLastEventsSeverity => { oid => '.1.3.6.1.4.1.4458.1000.1.7.4.2.1.2', map => \%map_severity },
107 };
108
109 sub manage_selection {
110 my ($self, %options) = @_;
111
112 $self->{alarms}->{global} = { alarm => {} };
113 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
114 { oid => $mapping->{radwllMilOduAgnLastEventsIfIndex}->{oid} },
115 { oid => $mapping->{radwllMilOduAgnLastEventsText}->{oid} },
116 { oid => $mapping->{radwllMilOduAgnLastEventsTimeT}->{oid} },
117 { oid => $mapping->{radwllMilOduAgnLastEventsSeverity}->{oid} },
118 ], return_type => 1);
119
120 my $last_time;
121 if (defined($self->{option_results}->{memory})) {
122 $self->{statefile_cache}->read(statefile => "cache_rad_airmux_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port(). '_' . $self->{mode});
123 $last_time = $self->{statefile_cache}->get(name => 'last_time');
124 }
125
126 my ($i, $current_time) = (1, time());
127 foreach my $oid (keys %{$snmp_result}) {
128 next if ($oid !~ /^$mapping->{radwllMilOduAgnLastEventsSeverity}->{oid}\.(.*)$/);
129 my $instance = $1;
130 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
131
132 my $create_time = $result->{radwllMilOduAgnLastEventsTimeT};
133 $result->{radwllMilOduAgnLastEventsTimeT} = strftime("%d/%m/%Y %H:%M:%S", localtime($result->{radwllMilOduAgnLastEventsTimeT}));
134 if (!defined($create_time)) {
135 $self->{output}->output_add(severity => 'UNKNOWN',
136 short_msg => "Can't get date '" . $result->{radwllMilOduAgnLastEventsTimeT} . "'");
137 next;
138 }
139
140 next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time);
141 if (defined($self->{option_results}->{filter_msg}) && $self->{option_results}->{filter_msg} ne '' &&
142 $result->{radwllMilOduAgnLastEventsText} !~ /$self->{option_results}->{filter_msg}/) {
143 $self->{output}->output_add(long_msg => "skipping '" . $result->{radwllMilOduAgnLastEventsText} . "': no matching filter.", debug => 1);
144 next;
145 }
146
147 my $diff_time = $current_time - $create_time;
148 $self->{alarms}->{global}->{alarm}->{$i} = { %$result, since => $diff_time };
149 $i++;
150 }
151
152 if (defined($self->{option_results}->{memory})) {
153 $self->{statefile_cache}->write(data => { last_time => $current_time });
154 }
155 }
156
157 1;
158
159 __END__
160
161 =head1 MODE
162
163 Check alarms.
164
165 =over 8
166
167 =item B<--filter-msg>
168
169 Filter by message (can be a regexp).
170
171 =item B<--warning-status>
172
173 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/i')
174 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
175
176 =item B<--critical-status>
177
178 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /critical|major/i').
179 You can use the following variables: %{severity}, %{text}, %{source}, %{since}
180
181 =item B<--memory>
182
183 Only check new alarms.
184
185 =back
186
187 =cut