Debian Codegraph @

src/network/oracle/infiniband/snmp/mode/infinibandusage.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::oracle::infiniband::snmp::mode::infinibandusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
29
30 sub custom_status_threshold {
31 my ($self, %options) = @_;
32
33 $self->{instance_mode}->{last_status} = 0;
34 if ($self->{result_values}->{status} ne 'down') {
35 $self->{instance_mode}->{last_status} = 1;
36 }
37 return catalog_status_threshold_ng($self, %options);
38 }
39
40 sub custom_status_output {
41 my ($self, %options) = @_;
42
43 return 'Status : ' . $self->{result_values}->{status};
44 }
45
46 sub custom_ib_perfdata {
47 my ($self, %options) = @_;
48
49 my ($warning, $critical);
50 if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
51 $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
52 $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
53 } elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
54 $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
55 $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
56 }
57
58 $self->{output}->perfdata_add(
59 label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s',
60 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
61 value => sprintf("%.2f", $self->{result_values}->{traffic}),
62 warning => $warning,
63 critical => $critical,
64 min => 0, max => $self->{result_values}->{speed}
65 );
66 }
67
68 sub custom_ib_threshold {
69 my ($self, %options) = @_;
70
71 my $exit = 'ok';
72 if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
73 $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
74 } elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
75 $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
76 }
77 return $exit;
78 }
79
80 sub custom_ib_output {
81 my ($self, %options) = @_;
82
83 my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1);
84 my ($total_value, $total_unit);
85 if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) {
86 ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1);
87 }
88
89 return sprintf(
90 "Traffic %s : %s/s (%s on %s)",
91 ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
92 defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-',
93 defined($total_value) ? $total_value . $total_unit : '-'
94 );
95 }
96
97 sub custom_ib_calc {
98 my ($self, %options) = @_;
99
100 return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0);
101
102 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
103 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
104 my $diff_traffic = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}} - $options{old_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}};
105 $self->{result_values}->{traffic} = $diff_traffic / $options{delta_time};
106 if ($options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} > 0) {
107 $self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} * 1000 * 1000;
108 $self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / $self->{result_values}->{speed};
109 } elsif (defined($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}}) && $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} =~ /[0-9]/) {
110 $self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / ($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000);
111 $self->{result_values}->{speed} = $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000;
112 }
113 return 0;
114 }
115
116 sub set_counters {
117 my ($self, %options) = @_;
118
119 $self->{maps_counters_type} = [
120 { name => 'ib', type => 1, cb_prefix_output => 'prefix_ib_output', message_multiple => 'All infiniband interfaces are ok', cb_init => 'skip_empty_ib', skipped_code => { -10 => 1 } },
121 { name => 'ibgw', type => 1, cb_prefix_output => 'prefix_ibgw_output', message_multiple => 'All gateway infiniband interfaces are ok', cb_init => 'skip_empty_ibgw', skipped_code => { -10 => 1 } },
122 ];
123
124 $self->{maps_counters}->{ib} = [
125 { label => 'ib-status', type => 2, critical_default => '%{status} !~ /active/i', set => {
126 key_values => [ { name => 'status' }, { name => 'display' } ],
127 closure_custom_output => $self->can('custom_status_output'),
128 closure_custom_perfdata => sub { return 0; },
129 closure_custom_threshold_check => $self->can('custom_status_threshold')
130 }
131 },
132 { label => 'in', set => {
133 key_values => [ { name => 'in', diff => 1 }, { name => 'display' }, { name => 'speed_in' } ],
134 closure_custom_calc => $self->can('custom_ib_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
135 closure_custom_output => $self->can('custom_ib_output'),
136 closure_custom_perfdata => $self->can('custom_ib_perfdata'),
137 closure_custom_threshold_check => $self->can('custom_ib_threshold')
138 }
139 },
140 { label => 'out', set => {
141 key_values => [ { name => 'out', diff => 1 }, { name => 'display' }, { name => 'speed_out' } ],
142 closure_custom_calc => $self->can('custom_ib_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
143 closure_custom_output => $self->can('custom_ib_output'),
144 closure_custom_perfdata => $self->can('custom_ib_perfdata'),
145 closure_custom_threshold_check => $self->can('custom_ib_threshold')
146 }
147 }
148 ];
149
150 $self->{maps_counters}->{ibgw} = [
151 { label => 'ibgw-status', type => 2, critical_default => '%{status} !~ /up/i', set => {
152 key_values => [ { name => 'status' }, { name => 'display' } ],
153 closure_custom_output => $self->can('custom_status_output'),
154 closure_custom_perfdata => sub { return 0; },
155 closure_custom_threshold_check => $self->can('custom_status_threshold')
156 }
157 },
158 { label => 'in', set => {
159 key_values => [ { name => 'in', diff => 1 }, { name => 'display' }, { name => 'speed_in' } ],
160 closure_custom_calc => $self->can('custom_ib_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
161 closure_custom_output => $self->can('custom_ib_output'),
162 closure_custom_perfdata => $self->can('custom_ib_perfdata'),
163 closure_custom_threshold_check => $self->can('custom_ib_threshold')
164 }
165 },
166 { label => 'out', set => {
167 key_values => [ { name => 'out', diff => 1 }, { name => 'display' }, { name => 'speed_out' } ],
168 closure_custom_calc => $self->can('custom_ib_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
169 closure_custom_output => $self->can('custom_ib_output'),
170 closure_custom_perfdata => $self->can('custom_ib_perfdata'),
171 closure_custom_threshold_check => $self->can('custom_ib_threshold')
172 }
173 }
174 ];
175 }
176
177 sub new {
178 my ($class, %options) = @_;
179 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
180 bless $self, $class;
181
182 $options{options}->add_options(arguments => {
183 'filter-ib-name:s' => { name => 'filter_ib_name' },
184 'filter-ibgw-name:s' => { name => 'filter_ibgw_name' },
185 'speed-in:s' => { name => 'speed_in' },
186 'speed-out:s' => { name => 'speed_out' },
187 'units-traffic:s' => { name => 'units_traffic', default => '%' }
188 });
189
190 return $self;
191 }
192
193 sub prefix_ib_output {
194 my ($self, %options) = @_;
195
196 return "Infiniband '" . $options{instance_value}->{display} . "' ";
197 }
198
199 sub prefix_ibgw_output {
200 my ($self, %options) = @_;
201
202 return "Infiniband gateway '" . $options{instance_value}->{display} . "' ";
203 }
204
205 sub skip_empty_ib {
206 my ($self, %options) = @_;
207
208 scalar(keys %{$self->{ib}}) > 0 ? return(0) : return(1);
209 }
210
211 sub skip_empty_ibgw {
212 my ($self, %options) = @_;
213
214 scalar(keys %{$self->{ibgw}}) > 0 ? return(0) : return(1);
215 }
216
217 my %map_link_state = (1 => 'down', 2 => 'init', 3 => 'armed', 4 => 'active', 5 => 'other');
218 my %map_gw_link_state = (0 => 'down', 1 => 'up');
219 my %map_link_speed = (
220 1 => 5000, # sdr-2point5Gbps
221 2 => 5000, # ddr-5Gbps',
222 4 => 10000, # qdr-10Gbps
223 5 => '', # other
224 );
225 my $mapping = {
226 ibSmaPortLinkState => { oid => '.1.3.6.1.4.1.42.2.135.2.2.5.1.1.6', map => \%map_link_state },
227 ibSmaPortLinkSpeedActive => { oid => '.1.3.6.1.4.1.42.2.135.2.2.5.1.1.10', map => \%map_link_speed },
228 };
229 my $mapping2 = {
230 ibPmaPortXmitData => { oid => '.1.3.6.1.4.1.42.2.135.2.6.1.2.1.3' },
231 ibPmaPortRcvData => { oid => '.1.3.6.1.4.1.42.2.135.2.6.1.2.1.4' },
232 ibPmaExtPortConnector => { oid => '.1.3.6.1.4.1.42.2.135.2.6.1.2.1.11' },
233 };
234 my $mapping3 = {
235 gwPortLongName => { oid => '.1.3.6.1.4.1.42.2.135.2.8.1.1.1.3' },
236 gwPortLinkState => { oid => '.1.3.6.1.4.1.42.2.135.2.8.1.1.1.5', map => \%map_gw_link_state },
237 };
238 my $mapping4 = {
239 gwEthRxBytes => { oid => '.1.3.6.1.4.1.42.2.135.2.8.1.2.1.4' },
240 gwEthTxBytes => { oid => '.1.3.6.1.4.1.42.2.135.2.8.1.2.1.13' },
241 };
242
243 my $oid_ibSmaPortInfoEntry = '.1.3.6.1.4.1.42.2.135.2.2.5.1.1';
244 my $oid_ibPmaExtPortCntrsEntry = '.1.3.6.1.4.1.42.2.135.2.6.1.2.1';
245 my $oid_gwPortStateEntry = '.1.3.6.1.4.1.42.2.135.2.8.1.1.1';
246 my $oid_gwEthPortCntrsEntry = '.1.3.6.1.4.1.42.2.135.2.8.1.2.1';
247
248 sub manage_selection {
249 my ($self, %options) = @_;
250
251 if ($options{snmp}->is_snmpv1()) {
252 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
253 $self->{output}->option_exit();
254 }
255
256 my $snmp_result = $options{snmp}->get_multiple_table(oids => [ { oid => $oid_ibSmaPortInfoEntry, end => $mapping->{ibSmaPortLinkSpeedActive}->{oid} },
257 { oid => $oid_ibPmaExtPortCntrsEntry },
258 { oid => $oid_gwPortStateEntry, end => $mapping3->{gwPortLinkState}->{oid} },
259 { oid => $oid_gwEthPortCntrsEntry },
260 ],
261 nothing_quit => 1);
262 $self->{ib} = {};
263 foreach my $oid (keys %{$snmp_result->{ $oid_ibSmaPortInfoEntry }}) {
264 next if ($oid !~ /^$mapping->{ibSmaPortLinkState}->{oid}\.(.*)$/);
265 my $instance = $1;
266 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{ $oid_ibSmaPortInfoEntry }, instance => $instance);
267 my $result2 = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{ $oid_ibPmaExtPortCntrsEntry }, instance => $instance);
268 $result2->{ibPmaExtPortConnector} =~ s/\x00//g;
269 if (defined($self->{option_results}->{filter_ib_name}) && $self->{option_results}->{filter_ib_name} ne '' &&
270 $result2->{ibPmaExtPortConnector} !~ /$self->{option_results}->{filter_ib_name}/) {
271 $self->{output}->output_add(long_msg => "Skipping '" . $result2->{ibPmaExtPortConnector} . "': no matching filter.", debug => 1);
272 next;
273 }
274
275 $self->{ib}->{'ib_' . $instance} = {
276 display => $result2->{ibPmaExtPortConnector},
277 status => $result->{ibSmaPortLinkState},
278 in => $result2->{ibPmaPortRcvData} * 4 * 8, out => $result2->{ibPmaPortXmitData} * 4 * 8,
279 speed_in => $result->{ibSmaPortLinkSpeedActive},
280 speed_out => $result->{ibSmaPortLinkSpeedActive}};
281 }
282
283 $self->{ibgw} = {};
284 foreach my $oid (keys %{$snmp_result->{ $oid_gwPortStateEntry }}) {
285 next if ($oid !~ /^$mapping3->{gwPortLinkState}->{oid}\.(.*)$/);
286 my $instance = $1;
287 my $result = $options{snmp}->map_instance(mapping => $mapping3, results => $snmp_result->{ $oid_gwPortStateEntry }, instance => $instance);
288 my $result2 = $options{snmp}->map_instance(mapping => $mapping4, results => $snmp_result->{ $oid_gwEthPortCntrsEntry }, instance => $instance);
289 $result->{gwPortLongName} =~ s/\x00//g;
290 if (defined($self->{option_results}->{filter_ibgw_name}) && $self->{option_results}->{filter_ibgw_name} ne '' &&
291 $result->{gwPortLongName} !~ /$self->{option_results}->{filter_ibgw_name}/) {
292 $self->{output}->output_add(long_msg => "Skipping '" . $result->{gwPortLongName} . "': no matching filter.", debug => 1);
293 next;
294 }
295
296 $self->{ibgw}->{'ibgw_' . $instance} = {
297 display => $result->{gwPortLongName},
298 status => $result->{gwPortLinkState},
299 in => $result2->{gwEthRxBytes} * 8, out => $result2->{gwEthTxBytes} * 8,
300 speed_in => 10000,
301 speed_out => 10000};
302 }
303
304 if (scalar(keys %{$self->{ibgw}}) <= 0 && scalar(keys %{$self->{ib}}) <= 0) {
305 $self->{output}->add_option_msg(short_msg => "No interfaces found.");
306 $self->{output}->option_exit();
307 }
308
309 $self->{cache_name} = "oracle_infiniband_" . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
310 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
311 (defined($self->{option_results}->{filter_ib_name}) ? md5_hex($self->{option_results}->{filter_ib_name}) : md5_hex('all')) . '_' .
312 (defined($self->{option_results}->{filter_ibgw_name}) ? md5_hex($self->{option_results}->{filter_ibgw_name}) : md5_hex('all'));
313 }
314
315 1;
316
317 __END__
318
319 =head1 MODE
320
321 Check infiniband interfaces usage.
322
323 =over 8
324
325 =item B<--filter-ib-name>
326
327 Filter by infiniband name (can be a regexp).
328
329 =item B<--filter-ibgw-name>
330
331 Filter by infiniband gateway name (can be a regexp).
332
333 =item B<--speed-in>
334
335 Set interface speed for incoming traffic (in Mb).
336
337 =item B<--speed-out>
338
339 Set interface speed for outgoing traffic (in Mb).
340
341 =item B<--units-traffic>
342
343 Units of thresholds for the traffic (default: '%') ('%', 'b/s').
344
345 =item B<--warning-ib-status>
346
347 Set warning threshold for ib status.
348 You can use the following variables: %{status}, %{display}
349
350 =item B<--critical-ib-status>
351
352 Set critical threshold for ib status (default: '%{status} !~ /up/i').
353 You can use the following variables: %{status}, %{display}
354
355 =item B<--warning-*>
356
357 Warning threshold.
358 Can be: 'in', 'out'.
359
360 =item B<--critical-*>
361
362 Critical threshold.
363 Can be: 'in', 'out'.
364
365 =back
366
367 =cut