Debian Codegraph @

src/network/watchguard/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 network::watchguard::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 prefix_global_output {
31 my ($self, %options) = @_;
32
33 return 'Total ';
34 }
35
36 sub prefix_tunnel_output {
37 my ($self, %options) = @_;
38
39 return "Tunnel '" . $options{instance_value}->{display} . "' ";
40 }
41
42 sub custom_traffic_calc {
43 my ($self, %options) = @_;
44
45 my ($checked, $total_bits) = (0, 0);
46 foreach (keys %{$options{new_datas}}) {
47 if (/^$self->{instance}_traffic_$options{extra_options}->{label_ref}_/) {
48 $checked |= 1;
49 my $new_bits = $options{new_datas}->{$_};
50 next if (!defined($options{old_datas}->{$_}));
51 my $old_bits = $options{old_datas}->{$_};
52
53 $checked |= 2;
54 my $diff_bits = $new_bits - $old_bits;
55 if ($diff_bits < 0) {
56 $total_bits += $new_bits;
57 } else {
58 $total_bits += $diff_bits;
59 }
60 }
61 }
62
63 if ($checked == 0) {
64 $self->{error_msg} = 'skipped (no value)';
65 return -10;
66 }
67 if ($checked == 1) {
68 $self->{error_msg} = 'buffer creation';
69 return -1;
70 }
71
72 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
73 $self->{result_values}->{traffic_per_second} = $total_bits / $options{delta_time};
74 return 0;
75 }
76
77
78 sub set_counters {
79 my ($self, %options) = @_;
80
81 $self->{maps_counters_type} = [
82 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
83 { name => 'tunnel', type => 1, cb_prefix_output => 'prefix_tunnel_output', message_multiple => 'All tunnels are ok', skipped_code => { -10 => 1 } }
84 ];
85
86 $self->{maps_counters}->{global} = [
87 { label => 'tunnels-total', nlabel => 'ipsec.tunnels.total.count', set => {
88 key_values => [ { name => 'total' } ],
89 output_template => 'tunnels: %s',
90 perfdatas => [
91 { template => '%s', min => 0 }
92 ]
93 }
94 }
95 ];
96
97 $self->{maps_counters}->{tunnel} = [
98 { label => 'tunnel-traffic-in', nlabel => 'ipsec.tunnel.traffic.in.bitspersecond', set => {
99 key_values => [],
100 manual_keys => 1,
101 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
102 output_template => 'traffic in: %s %s/s',
103 output_change_bytes => 2,
104 output_use => 'traffic_per_second',
105 threshold_use => 'traffic_per_second',
106 perfdatas => [
107 { template => '%s', value => 'traffic_per_second', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
108 ]
109 }
110 },
111 { label => 'tunnel-traffic-out', nlabel => 'ipsec.tunnel.traffic.out.bitspersecond', set => {
112 key_values => [],
113 manual_keys => 1,
114 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
115 output_template => 'traffic out: %s %s/s',
116 output_change_bytes => 2,
117 output_use => 'traffic_per_second',
118 threshold_use => 'traffic_per_second',
119 perfdatas => [
120 { template => '%s', value => 'traffic_per_second', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
121 ]
122 }
123 }
124 ];
125 }
126
127 sub new {
128 my ($class, %options) = @_;
129 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
130 bless $self, $class;
131
132 $options{options}->add_options(arguments => {
133 'filter-name:s' => { name => 'filter_name' }
134 });
135
136 return $self;
137 }
138
139 my $mapping = {
140 wgIpsecTunnelLocalAddr => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.2' },
141 wgIpsecTunnelPeerAddr => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.3' }
142 };
143
144 my $oid_wgIpsecTunnelEntry = '.1.3.6.1.4.1.3097.6.5.1.2.1';
145 my $mapping2 = {
146 selector_remote_ip_one => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.20' }, # wgIpsecTunnelSelectorRemoteIPOne
147 selector_remote_ip_two => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.21' }, # wgIpsecTunnelSelectorRemoteIPTwo
148 selector_local_ip_one => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.24' }, # wgIpsecTunnelSelectorLocalIPOne
149 selector_local_ip_two => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.25' }, # wgIpsecTunnelSelectorLocalIPTwo
150 traffic_in => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.28' }, # wgIpsecTunnelInKbytes
151 traffic_out => { oid => '.1.3.6.1.4.1.3097.6.5.1.2.1.29' } # wgIpsecTunnelOutKbytes
152 };
153
154 # We could use the following OIDs for global counters :
155 # wgIpsecEndpointPairTotalInAccKbytes - The total inbound IPSec traffic - 1.3.6.1.4.1.3097.5.1.2.3
156 # wgIpsecEndpointPairTotalOutAccKbytes - The total outbound IPSec traffic - 1.3.6.1.4.1.3097.5.1.2.4
157 # But we would then not satisfy filter options, let's then compute these ourselves.
158
159 sub manage_selection {
160 my ($self, %options) = @_;
161
162 my $snmp_result = $options{snmp}->get_table(
163 oid => $oid_wgIpsecTunnelEntry,
164 start => $mapping->{wgIpsecTunnelLocalAddr}->{oid},
165 end => $mapping->{wgIpsecTunnelPeerAddr}->{oid}
166 );
167
168 $self->{tunnel} = {};
169 $self->{global} = { total => 0 };
170 foreach (keys %$snmp_result) {
171 next if (!/$mapping->{wgIpsecTunnelLocalAddr}->{oid}\.(.*)/);
172 my $instance = $1;
173 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
174 my $name = $result->{wgIpsecTunnelLocalAddr} . ':' . $result->{wgIpsecTunnelPeerAddr};
175 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
176 $name !~ /$self->{option_results}->{filter_name}/) {
177 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter name.", debug => 1);
178 next;
179 }
180
181 $self->{tunnel}->{$instance} = { display => $name };
182 $self->{global}->{total}++;
183 }
184
185 if (scalar(keys %{$self->{tunnel}}) > 0) {
186 $options{snmp}->load(oids => [
187 map($_->{oid}, values(%$mapping2))
188 ],
189 instances => [keys %{$self->{tunnel}}],
190 instance_regexp => '^(.*)$'
191 );
192 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
193
194 # tunnel ID moved... so we use the display
195 # we can have tunnels with same display
196 my @instances = keys %{$self->{tunnel}};
197 foreach (@instances) {
198 my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => $_);
199 $result->{traffic_in} = defined($result->{traffic_in}) ? $result->{traffic_in} * 1024 * 8 : 0;
200 $result->{traffic_out} = defined($result->{traffic_out}) ? $result->{traffic_out} * 1024 * 8 : 0;
201
202 my $uuid = $self->{tunnel}->{$_}->{display} . '_' . $result->{selector_local_ip_one} . '_' . $result->{selector_local_ip_two} . '_' .
203 $result->{selector_remote_ip_one} . '_' . $result->{selector_remote_ip_two};
204 $self->{tunnel}->{ $self->{tunnel}->{$_}->{display} } = { display => $self->{tunnel}->{$_}->{display} }
205 if (!defined($self->{tunnel}->{ $self->{tunnel}->{$_}->{display} }));
206 $self->{tunnel}->{ $self->{tunnel}->{$_}->{display} }->{'traffic_in_' . $uuid} = $result->{traffic_in};
207 $self->{tunnel}->{ $self->{tunnel}->{$_}->{display} }->{'traffic_out_' . $uuid} = $result->{traffic_out};
208
209 delete $self->{tunnel}->{$_};
210 }
211 }
212
213 $self->{cache_name} = 'watchguard_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' .
214 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
215 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
216 }
217
218 1;
219
220 __END__
221
222 =head1 MODE
223
224 Check ipsec tunnels.
225
226 =over 8
227
228 =item B<--filter-name>
229
230 Filter name (can be a regexp).
231
232 =item B<--filter-counters>
233
234 Only display some counters (regexp can be used).
235 Example: --filter-counters='tunnels-total'
236
237 =item B<--warning-*> B<--critical-*>
238
239 Thresholds.
240 Can be: 'tunnels-total', 'tunnel-traffic-in', 'tunnel-traffic-out'.
241
242 =back
243
244 =cut