Debian Codegraph @

src/centreon/common/cisco/standard/snmp/mode/ipsectunnel.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 centreon::common::cisco::standard::snmp::mode::ipsectunnel;
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 Socket;
29
30 sub custom_traffic_perfdata {
31 my ($self, %options) = @_;
32
33 $self->{output}->perfdata_add(
34 label => 'traffic_' . lc($self->{result_values}->{label}), unit => 'b/s',
35 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
36 value => sprintf("%.2f", $self->{result_values}->{traffic_per_seconds}),
37 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
38 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
39 min => 0
40 );
41 }
42
43 sub custom_traffic_threshold {
44 my ($self, %options) = @_;
45
46 return $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_per_seconds}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
47 }
48
49 sub custom_traffic_output {
50 my ($self, %options) = @_;
51
52 my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic_per_seconds}, network => 1);
53 return sprintf(
54 'traffic %s: %s/s',
55 lc($self->{result_values}->{label}),
56 $traffic_value . $traffic_unit
57 );
58 }
59
60 sub custom_traffic_calc {
61 my ($self, %options) = @_;
62
63 if (!defined($options{old_datas}->{$self->{instance} . '_display'})) {
64 $self->{error_msg} = "Buffer creation";
65 return -1;
66 }
67
68 my $total_bytes = 0;
69 foreach (keys %{$options{new_datas}}) {
70 if (/$self->{instance}_cipSecTunHc$options{extra_options}->{label_ref}Octets_(\d+)/) {
71 my $new_bytes = $options{new_datas}->{$_};
72 next if (!defined($options{old_datas}->{$_}));
73 my $old_bytes = $options{old_datas}->{$_};
74
75 my $diff_bytes = $new_bytes - $old_bytes;
76 $total_bytes += (($diff_bytes < 0) ? $new_bytes : $diff_bytes);
77 } elsif (/$self->{instance}_cipSecTun$options{extra_options}->{label_ref}Octets_(\d+)/) {
78 my $new_bytes = $options{new_datas}->{$_};
79 my $new_wraps = $options{new_datas}->{$self->{instance} . '_cipSecTun' . $options{extra_options}->{label_ref} . 'OctWraps_' . $1};
80 next if (!defined($options{old_datas}->{$_}));
81 my ($old_bytes, $old_wraps) = ($options{old_datas}->{$_}, $options{old_datas}->{$self->{instance} . '_cipSecTun' . $options{extra_options}->{label_ref} . 'OctWraps_' . $1});
82
83 my $diff_bytes = $new_bytes - $old_bytes + (($new_wraps - $old_wraps) * (2**32));
84 $total_bytes += (($diff_bytes < 0) ? $new_bytes : $diff_bytes);
85 }
86 }
87
88 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
89 $self->{result_values}->{traffic_per_seconds} = ($total_bytes * 8) / $options{delta_time};
90 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
91
92 return 0;
93 }
94
95 sub custom_drop_perfdata {
96 my ($self, %options) = @_;
97
98 $self->{output}->perfdata_add(
99 label => 'drop_' . lc($self->{result_values}->{label}), unit => 'pkts/s',
100 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
101 value => sprintf("%.2f", $self->{result_values}->{pkts_per_seconds}),
102 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
103 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
104 min => 0
105 );
106 }
107
108 sub custom_drop_threshold {
109 my ($self, %options) = @_;
110
111 return $self->{perfdata}->threshold_check(value => $self->{result_values}->{pkts_per_seconds}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
112 }
113
114 sub custom_drop_output {
115 my ($self, %options) = @_;
116
117 return sprintf(
118 'drop %s: %s pkts/s',
119 lc($self->{result_values}->{label}), $self->{result_values}->{pkts_per_seconds}
120 );
121 }
122
123 sub custom_drop_calc {
124 my ($self, %options) = @_;
125
126 if (!defined($options{old_datas}->{$self->{instance} . '_display'})) {
127 $self->{error_msg} = "Buffer creation";
128 return -1;
129 }
130
131 my $total_pkts = 0;
132 foreach (keys %{$options{new_datas}}) {
133 if (/$self->{instance}_cipSecTun$options{extra_options}->{label_ref}DropPkts_(\d+)/) {
134 next if (!defined($options{old_datas}->{$_}));
135
136 my $diff_pkts = $options{new_datas}->{$_} - $options{old_datas}->{$_};
137 $total_pkts += (($diff_pkts < 0) ? $options{new_datas}->{$_} : $diff_pkts);
138 }
139 }
140
141 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
142 $self->{result_values}->{pkts_per_seconds} = $total_pkts / $options{delta_time};
143 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
144
145 return 0;
146 }
147
148 sub prefix_tunnel_output {
149 my ($self, %options) = @_;
150
151 return "Tunnel '" . $options{instance_value}->{display} . "' ";
152 }
153
154 sub set_counters {
155 my ($self, %options) = @_;
156
157 $self->{maps_counters_type} = [
158 { name => 'global', type => 0 },
159 { name => 'tunnel', type => 1, cb_prefix_output => 'prefix_tunnel_output', message_multiple => 'All tunnels are ok' }
160 ];
161
162 $self->{maps_counters}->{global} = [
163 { label => 'tunnels-total', set => {
164 key_values => [ { name => 'total' } ],
165 output_template => 'Total tunnels: %s',
166 perfdatas => [
167 { label => 'total_tunnels', template => '%s', min => 0 }
168 ]
169 }
170 }
171 ];
172
173 $self->{maps_counters}->{tunnel} = [
174 { label => 'traffic-in', set => {
175 key_values => [],
176 manual_keys => 1,
177 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'In' },
178 closure_custom_output => $self->can('custom_traffic_output'),
179 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
180 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
181 }
182 },
183 { label => 'traffic-out', set => {
184 key_values => [],
185 manual_keys => 1,
186 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'Out' },
187 closure_custom_output => $self->can('custom_traffic_output'),
188 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
189 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
190 }
191 },
192 { label => 'drop-in', set => {
193 key_values => [],
194 manual_keys => 1,
195 closure_custom_calc => $self->can('custom_drop_calc'), closure_custom_calc_extra_options => { label_ref => 'In' },
196 closure_custom_output => $self->can('custom_drop_output'),
197 closure_custom_perfdata => $self->can('custom_drop_perfdata'),
198 closure_custom_threshold_check => $self->can('custom_drop_threshold')
199 }
200 },
201 { label => 'drop-out', set => {
202 key_values => [],
203 manual_keys => 1,
204 closure_custom_calc => $self->can('custom_drop_calc'), closure_custom_calc_extra_options => { label_ref => 'Out' },
205 closure_custom_output => $self->can('custom_drop_output'),
206 closure_custom_perfdata => $self->can('custom_drop_perfdata'),
207 closure_custom_threshold_check => $self->can('custom_drop_threshold')
208 }
209 },
210 { label => 'sa-total', set => {
211 key_values => [ { name => 'sa' }, { name => 'display' } ],
212 output_template => 'total sa: %s',
213 perfdatas => [
214 { label => 'total_sa', template => '%s', min => 0, label_extra_instance => 1, instance_use => 'display' }
215 ]
216 }
217 }
218 ];
219 }
220
221 sub new {
222 my ($class, %options) = @_;
223 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
224 bless $self, $class;
225
226 $options{options}->add_options(arguments => {
227 'filter-name:s' => { name => 'filter_name' },
228 'filter-sa:s' => { name => 'filter_sa' }
229 });
230
231 return $self;
232 }
233
234 my $mapping = {
235 cikeTunLocalValue => { oid => '.1.3.6.1.4.1.9.9.171.1.2.3.1.3' },
236 cikeTunRemoteValue => { oid => '.1.3.6.1.4.1.9.9.171.1.2.3.1.7' }
237 };
238 my $oid_cikeTunActiveTime = '.1.3.6.1.4.1.9.9.171.1.2.3.1.16';
239
240 my $mapping2 = {
241 cipSecTunInOctets => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.26' },
242 cipSecTunHcInOctets => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.27' },
243 cipSecTunInOctWraps => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.28' }, # seems buggy
244 cipSecTunInDropPkts => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.33' },
245 cipSecTunOutOctets => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.39' },
246 cipSecTunHcOutOctets => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.40' }, # seems buggy
247 cipSecTunOutOctWraps => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.41' },
248 cipSecTunOutDropPkts => { oid => '.1.3.6.1.4.1.9.9.171.1.3.2.1.46' }
249 };
250 my $mapping3 = {
251 cipSecEndPtLocalAddr2 => { oid => '.1.3.6.1.4.1.9.9.171.1.3.3.1.5' },
252 cipSecEndPtRemoteAddr1 => { oid => '.1.3.6.1.4.1.9.9.171.1.3.3.1.10' },
253 cipSecEndPtRemoteAddr2 => { oid => '.1.3.6.1.4.1.9.9.171.1.3.3.1.11' }
254 };
255
256 my $oid_cipSecEndPtLocalAddr1 = '.1.3.6.1.4.1.9.9.171.1.3.3.1.4';
257 my $oid_cipSecTunIkeTunnelIndex = '.1.3.6.1.4.1.9.9.171.1.3.2.1.2';
258
259 sub manage_selection {
260 my ($self, %options) = @_;
261
262 $self->{cache_name} = 'cisco_ipsectunnel_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' .
263 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
264 (defined($self->{option_results}->{filter_sa}) ? md5_hex($self->{option_results}->{filter_sa}) : md5_hex('all')) . '_' .
265 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
266
267 $self->{global} = { total => 0 };
268 my $snmp_result = $options{snmp}->get_multiple_table(
269 oids => [
270 { oid => $mapping->{cikeTunLocalValue}->{oid} },
271 { oid => $mapping->{cikeTunRemoteValue}->{oid} }
272 ],
273 return_type => 1
274 );
275
276 # The MIB doesn't give IPSec tunnel type (site-to-site or dynamic client)
277 # You surely need to filter on SA. Dynamic client usually doesn't push local routes.
278 $self->{tunnel} = {};
279 my $ike_idx = {};
280 foreach (keys %$snmp_result) {
281 next if (!/$mapping->{cikeTunRemoteValue}->{oid}\.(\d+)/);
282
283 my $cike_tun_index = $1;
284 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $cike_tun_index);
285
286 my $name = $result->{cikeTunLocalValue} . '_' . $result->{cikeTunRemoteValue};
287 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
288 $name !~ /$self->{option_results}->{filter_name}/) {
289 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter name.", debug => 1);
290 next;
291 }
292
293 $ike_idx->{$cike_tun_index} = 1;
294 $self->{tunnel}->{$name} = { display => $name, sa => 0, ike_tun_idx => $cike_tun_index };
295 }
296
297 $snmp_result = $options{snmp}->get_multiple_table(
298 oids => [
299 { oid => $oid_cipSecTunIkeTunnelIndex },
300 { oid => $oid_cipSecEndPtLocalAddr1 }
301 ]
302 );
303 my $sectun_idx_instances = {};
304 my $sectun_idx_select = {};
305 foreach (keys %{$snmp_result->{$oid_cipSecTunIkeTunnelIndex}}) {
306 next if (!defined($ike_idx->{ $snmp_result->{$oid_cipSecTunIkeTunnelIndex}->{$_} }));
307
308 /^$oid_cipSecTunIkeTunnelIndex\.(\d+)/;
309 if (!defined($sectun_idx_instances->{ $snmp_result->{$oid_cipSecTunIkeTunnelIndex}->{$_} })) {
310 $sectun_idx_instances->{ $snmp_result->{$oid_cipSecTunIkeTunnelIndex}->{$_} } = [];
311 }
312 $sectun_idx_select->{$1} = 1;
313 push @{$sectun_idx_instances->{ $snmp_result->{$oid_cipSecTunIkeTunnelIndex}->{$_} }}, $1;
314 }
315
316 foreach my $name (keys %{$self->{tunnel}}) {
317 delete $self->{tunnel}->{$name} if (!defined($sectun_idx_instances->{ $self->{tunnel}->{$name}->{ike_tun_idx} }));
318 }
319
320 return if (scalar(keys %{$self->{tunnel}}) <= 0);
321
322 my $sec_endpoint_idx_select = {};
323 my $sec_endpoint_idx_instances = [];
324 foreach (keys %{$snmp_result->{$oid_cipSecEndPtLocalAddr1}}) {
325 /(\d+)\.(\d+)$/;
326 next if (!defined($sectun_idx_select->{$1}));
327 push @$sec_endpoint_idx_instances, $1 . '.' . $2;
328 $sec_endpoint_idx_select->{$1} = [] if (!defined($sec_endpoint_idx_select->{$1}));
329 $sec_endpoint_idx_select->{$1} = [$2, $snmp_result->{$oid_cipSecEndPtLocalAddr1}->{$_}];
330 }
331
332 $options{snmp}->load(
333 oids => [ map($_->{oid}, values(%$mapping2)) ],
334 instances => [ map(@$_, values(%$sectun_idx_instances)) ],
335 instance_regexp => '^(.*)$'
336 );
337 $options{snmp}->load(
338 oids => [$oid_cikeTunActiveTime],
339 instances => [ map($_->{ike_tun_idx}, values(%{$self->{tunnel}})) ],
340 instance_regexp => '^(.*)$'
341 );
342 $options{snmp}->load(
343 oids => [ map($_->{oid}, values(%$mapping3)) ],
344 instances => $sec_endpoint_idx_instances,
345 instance_regexp => '^(.*)$'
346 );
347 $snmp_result = $options{snmp}->get_leef();
348
349 foreach my $name (keys %{$self->{tunnel}}) {
350 foreach my $cip_sec_tun_idx (@{$sectun_idx_instances->{ $self->{tunnel}->{$name}->{ike_tun_idx} }}) {
351 my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => $cip_sec_tun_idx);
352
353 my $sa_name = '';
354 if (defined($sec_endpoint_idx_select->{$cip_sec_tun_idx})) {
355 my $result3 = $options{snmp}->map_instance(mapping => $mapping3, results => $snmp_result, instance => $cip_sec_tun_idx . '.' . $sec_endpoint_idx_select->{$cip_sec_tun_idx}->[0]);
356 $sa_name = inet_ntoa($sec_endpoint_idx_select->{$cip_sec_tun_idx}->[1]) . ':' . inet_ntoa($result3->{cipSecEndPtLocalAddr2}) . '_' . inet_ntoa($result3->{cipSecEndPtRemoteAddr1}) . ':' . inet_ntoa($result3->{cipSecEndPtRemoteAddr2});
357 }
358
359 if (defined($self->{option_results}->{filter_sa}) && $self->{option_results}->{filter_sa} ne '' &&
360 $sa_name !~ /$self->{option_results}->{filter_sa}/) {
361 $self->{output}->output_add(long_msg => "skipping '" . $sa_name . "': no matching filter sa.", debug => 1);
362 next;
363 }
364
365 if (defined($result->{cipSecTunHcInOctets}) && defined($result->{cipSecTunHcOutOctets})) {
366 delete $result->{cipSecTunInOctets};
367 delete $result->{cipSecTunInOctWraps};
368 delete $result->{cipSecTunOutOctets};
369 delete $result->{cipSecTunOutOctWraps};
370 }
371 foreach my $oid_name (keys %$mapping2) {
372 $self->{tunnel}->{$name}->{ $oid_name . '_' . $cip_sec_tun_idx } = $result->{$oid_name} if (defined($result->{$oid_name}));
373 }
374 $self->{tunnel}->{$name}->{cikeTunActiveTime} = $snmp_result->{ $oid_cikeTunActiveTime . '.' . $self->{tunnel}->{$name}->{ike_tun_idx} };
375 $self->{tunnel}->{$name}->{sa}++;
376 }
377 }
378
379 foreach my $name (keys %{$self->{tunnel}}) {
380 delete $self->{tunnel}->{$name} if ($self->{tunnel}->{$name}->{sa} == 0);
381 }
382
383 $self->{global} = { total => scalar(keys %{$self->{tunnel}}) };
384 }
385
386 1;
387
388 __END__
389
390 =head1 MODE
391
392 Check IPsec tunnels.
393
394 =over 8
395
396 =item B<--filter-name>
397
398 Filter name (can be a regexp).
399 Example (format localaddr_remoteaddr):
400
401 =item B<--filter-sa>
402
403 Filter IPSec Security Associations (can be a regexp).
404 Example (format localaddr:localmask_remoteaddr:remotemask):
405
406 =item B<--filter-counters>
407
408 Only display some counters (regexp can be used).
409 Example: --filter-counters='^(tunnels-total)$'
410
411 =item B<--warning-*>
412
413 Warning threshold.
414 Can be: 'tunnels-total', 'traffic-in',
415 'traffic-out', 'drop-in', 'drop-out', 'sa-total'.
416
417 =item B<--critical-*>
418
419 Critical threshold.
420 Can be: 'tunnels-total', 'traffic-in',
421 'traffic-out', 'drop-in', 'drop-out', 'sa-total'.
422
423 =back
424
425 =cut