Debian Codegraph @

src/hardware/server/cisco/ucs/snmp/mode/faults.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 hardware::server::cisco::ucs::snmp::mode::faults;
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 POSIX;
30 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
31
32 my $map_severity = {
33 0 => 'cleared', 1 => 'info', 2 => 'condition',
34 3 => 'warning', 4 => 'minor', 5 => 'major',
35 6 => 'critical'
36 };
37
38 sub custom_status_output {
39 my ($self, %options) = @_;
40
41 return sprintf(
42 'fault [severity: %s] [dn: %s] [description: %s] %s',
43 $self->{result_values}->{severity},
44 $self->{result_values}->{dn},
45 $self->{result_values}->{description},
46 scalar(localtime($self->{result_values}->{created}))
47 );
48 }
49
50 sub prefix_global_output {
51 my ($self, %options) = @_;
52
53 return 'Faults ';
54 }
55
56 sub set_counters {
57 my ($self, %options) = @_;
58
59 $self->{maps_counters_type} = [
60 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', },
61 { name => 'faults', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { nlabel => 'faults.problems.current.count', min => 0 },
62 group => [ { name => 'fault', skipped_code => { -11 => 1 } } ]
63 }
64 ];
65
66 $self->{maps_counters}->{global} = [
67 { label => 'faults-total', nlabel => 'faults.total.count', display_ok => 0, set => {
68 key_values => [ { name => 'total' } ],
69 output_template => 'total: %s',
70 perfdatas => [
71 { template => '%s', min => 0 }
72 ]
73 }
74 }
75 ];
76
77 foreach (values %$map_severity) {
78 push @{$self->{maps_counters}->{global}},
79 { label => 'faults-' . $_, nlabel => 'faults.' . $_ . '.count', display_ok => 0, set => {
80 key_values => [ { name => $_ }, { name => 'total' } ],
81 output_template => $_ . ': %s',
82 perfdatas => [
83 { template => '%s', min => 0, max => 'total' }
84 ]
85 }
86 };
87 }
88
89 $self->{maps_counters}->{fault} = [
90 {
91 label => 'status',
92 type => 2,
93 warning_default => '%{severity} =~ /minor|warning/',
94 critical_default => '%{severity} =~ /major|critical/',
95 set => {
96 key_values => [
97 { name => 'dn' }, { name => 'severity' },
98 { name => 'description' }, { name => 'created' }
99 ],
100 closure_custom_output => $self->can('custom_status_output'),
101 closure_custom_perfdata => sub { return 0; },
102 closure_custom_threshold_check => \&catalog_status_threshold_ng
103 }
104 }
105 ];
106 }
107
108 sub new {
109 my ($class, %options) = @_;
110 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
111 bless $self, $class;
112
113 $options{options}->add_options(arguments => {
114 'filter-message:s' => { name => 'filter_message' },
115 'retention:s' => { name => 'retention' },
116 'memory' => { name => 'memory' }
117 });
118
119 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
120 return $self;
121 }
122
123 sub check_options {
124 my ($self, %options) = @_;
125 $self->SUPER::check_options(%options);
126
127 if (defined($self->{option_results}->{memory})) {
128 $self->{statefile_cache}->check_options(%options);
129 }
130 }
131
132 sub get_timestamp {
133 my ($self, %options) = @_;
134
135 my $currentTmsp = 0;
136 my $value = $options{value};
137 if ($value =~ /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/) {
138 $currentTmsp = mktime($6, $5, $4, $3, $2 - 1, $1 - 1900);
139 } else {
140 $value = unpack('H*', $value);
141 $value =~ /^([0-9a-z]{4})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})/;
142 $currentTmsp = mktime(hex($6), hex($5), hex($4), hex($3), hex($2) - 1, hex($1) - 1900);
143 }
144
145 return $currentTmsp;
146 }
147
148 my $mapping = {
149 dn => { oid => '.1.3.6.1.4.1.9.9.719.1.1.1.1.2' }, # cucsFaultDn
150 created => { oid => '.1.3.6.1.4.1.9.9.719.1.1.1.1.10' }, # cucsFaultCreationTime
151 description => { oid => '.1.3.6.1.4.1.9.9.719.1.1.1.1.11' }, # cucsFaultDescription
152 severity => { oid => '.1.3.6.1.4.1.9.9.719.1.1.1.1.20', map => $map_severity } # cucsFaultSeverity
153 };
154
155 sub manage_selection {
156 my ($self, %options) = @_;
157
158 my $datas = {};
159 my ($start, $last_instance);
160 if (defined($self->{option_results}->{memory})) {
161 $self->{statefile_cache}->read(statefile => 'cache_ciscoucs_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode});
162 $start = $self->{statefile_cache}->get(name => 'start');
163 $start = $start - 1 if (defined($start));
164 }
165
166 my $snmp_result = $options{snmp}->get_multiple_table(
167 oids => [ map({ oid => $_->{oid}, start => $_->{oid} . (defined($start) ? '.' . $start : '') }, values(%$mapping)) ],
168 return_type => 1
169 );
170
171 $self->{global} = {
172 total => 0, cleared => 0, info => 0, condition => 0,
173 warning => 0, minor => 0, major => 0, critical => 0
174 };
175 $self->{faults} = { global => { fault => {} } };
176 my ($i, $current_time) = (1, time());
177 foreach my $oid ($options{snmp}->oid_lex_sort(keys %$snmp_result)) {
178 next if ($oid !~ /^$mapping->{dn}->{oid}\.(\d+)$/);
179 my $instance = $1;
180 if (defined($self->{option_results}->{memory})) {
181 $last_instance = $instance;
182 next if (defined($start) && ($start + 1) >= $instance); # we skip last one from previous check)
183 }
184
185 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
186 $result->{description} = centreon::plugins::misc::trim($result->{description});
187 $result->{created} = $self->get_timestamp(value => $result->{created});
188 if (defined($self->{option_results}->{retention})) {
189 next if ($current_time - $result->{created} > $self->{option_results}->{retention});
190 }
191
192 $self->{global}->{total}++;
193 next if (defined($self->{option_results}->{filter_message}) && $self->{option_results}->{filter_message} ne '' && $result->{description} !~ /$self->{option_results}->{filter_message}/);
194
195 $self->{faults}->{global}->{fault}->{$i} = $result;
196 $self->{global}->{ $result->{severity} }++;
197 $i++;
198 }
199
200 if (defined($self->{option_results}->{memory})) {
201 $datas->{start} = $last_instance;
202 $self->{statefile_cache}->write(data => $datas);
203 }
204 }
205
206 1;
207
208 __END__
209
210 =head1 MODE
211
212 Check faults.
213
214 =over 8
215
216 =item B<--warning-status>
217
218 Define the conditions to match for the status to be WARNING (default: '%{severity} =~ /minor|warning/')
219 You can use the following variables: %{severity}, %{description}, %{dn}
220
221 =item B<--critical-status>
222
223 Define the conditions to match for the status to be CRITICAL (default: '%{severity} =~ /major|critical/').
224 You can use the following variables: %{severity}, %{description}, %{dn}
225
226 =item B<--memory>
227
228 Only check new fault.
229
230 =item B<--filter-message>
231
232 Filter on event message. (default: none)
233
234 =item B<--retention>
235
236 Event older (current time - retention time) is not checked (in seconds).
237
238 =back
239
240 =cut