Debian Codegraph @

src/network/viptela/snmp/mode/gretunnels.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::viptela::snmp::mode::gretunnels;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28 use Digest::MD5 qw(md5_hex);
29 use Socket;
30
31 sub custom_packets_perfdata {
32 my ($self) = @_;
33
34 $self->{output}->perfdata_add(
35 nlabel => $self->{nlabel},
36 instances => [
37 $self->{result_values}->{sourceIp},
38 $self->{result_values}->{destIp}
39 ],
40 value => $self->{result_values}->{ $self->{key_values}->[0]->{name} },
41 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
42 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
43 min => 0
44 );
45 }
46
47 sub tunnel_long_output {
48 my ($self, %options) = @_;
49
50 return sprintf(
51 "checking gre tunnel [src: %s] [dst: %s]",
52 $options{instance_value}->{sourceIp},
53 $options{instance_value}->{destIp}
54 );
55 }
56
57 sub prefix_tunnel_output {
58 my ($self, %options) = @_;
59
60 return sprintf(
61 "gre tunnel [src: %s] [dst: %s] ",
62 $options{instance_value}->{sourceIp},
63 $options{instance_value}->{destIp}
64 );
65 }
66
67 sub prefix_global_output {
68 my ($self, %options) = @_;
69
70 return 'Number of gre tunnels ';
71 }
72
73 sub prefix_packet_output {
74 my ($self, %options) = @_;
75
76 return 'packets ';
77 }
78
79 sub set_counters {
80 my ($self, %options) = @_;
81
82 $self->{maps_counters_type} = [
83 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
84 { name => 'tunnels', type => 3, cb_prefix_output => 'prefix_tunnel_output', cb_long_output => 'tunnel_long_output',
85 indent_long_output => ' ', message_multiple => 'All gre tunnels are ok',
86 group => [
87 { name => 'status', type => 0, skipped_code => { -10 => 1 } },
88 { name => 'packet', type => 0, cb_prefix_output => 'prefix_packet_output', skipped_code => { -10 => 1 } },
89 ]
90 }
91 ];
92
93 $self->{maps_counters}->{global} = [
94 { label => 'gretunnels-detected', display_ok => 0, nlabel => 'gre_tunnels.detected.count', set => {
95 key_values => [ { name => 'detected' } ],
96 output_template => 'detected: %s',
97 perfdatas => [
98 { template => '%s', min => 0 }
99 ]
100 }
101 },
102 { label => 'gretunnels-up', display_ok => 0, nlabel => 'gre_tunnels.operational.up.count', set => {
103 key_values => [ { name => 'oper_up' } ],
104 output_template => 'up: %s',
105 perfdatas => [
106 { template => '%s', min => 0 }
107 ]
108 }
109 },
110 { label => 'gretunnels-down', display_ok => 0, nlabel => 'gre_tunnels.operational.down.count', set => {
111 key_values => [ { name => 'oper_down' } ],
112 output_template => 'down: %s',
113 perfdatas => [
114 { template => '%s', min => 0 }
115 ]
116 }
117 },
118 { label => 'gretunnels-invalid', display_ok => 0, nlabel => 'gre_tunnels.operational.invalid.count', set => {
119 key_values => [ { name => 'oper_invalid' } ],
120 output_template => 'invalid: %s',
121 perfdatas => [
122 { template => '%s', min => 0 }
123 ]
124 }
125 }
126 ];
127
128 $self->{maps_counters}->{status} = [
129 {
130 label => 'status',
131 type => 2,
132 critical_default => '%{adminState} eq "up" and %{operState} ne "up"',
133 set => {
134 key_values => [
135 { name => 'adminState' }, { name => 'operState' },
136 { name => 'sourceIp' }, { name => 'destIp' }
137 ],
138 output_template => "status: %s",
139 closure_custom_perfdata => sub { return 0; },
140 closure_custom_threshold_check => \&catalog_status_threshold_ng
141 }
142 }
143 ];
144
145 $self->{maps_counters}->{packet} = [
146 { label => 'gretunnel-packets-in', nlabel => 'gre_tunnel.packets.in.count', set => {
147 key_values => [ { name => 'packetsIn', diff => 1 }, { name => 'sourceIp' }, { name => 'destIp' } ],
148 output_template => 'in: %s',
149 closure_custom_perfdata => $self->can('custom_packets_perfdata')
150 }
151 },
152 { label => 'gretunnel-packets-out', nlabel => 'gre_tunnel.packets.out.count', set => {
153 key_values => [ { name => 'packetsOut', diff => 1 }, { name => 'sourceIp' }, { name => 'destIp' } ],
154 output_template => 'out: %s',
155 closure_custom_perfdata => $self->can('custom_packets_perfdata')
156 }
157 }
158 ];
159 }
160
161 sub new {
162 my ($class, %options) = @_;
163 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
164 bless $self, $class;
165
166 $options{options}->add_options(arguments => {
167 'filter-src-ip:s' => { name => 'filter_src_ip' },
168 'filter-dest-ip:s' => { name => 'filter_dest_ip' }
169 });
170
171 return $self;
172 }
173
174 my $map_status = {
175 0 => 'down', 1 => 'up', 2 => 'invalid'
176 };
177
178 my $mapping_name = {
179 sourceIp => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.3' }, # tunnelGreKeepalivesSourceIp
180 destIp => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.4' } # tunnelGreKeepalivesDestIp
181 };
182 my $mapping = {
183 adminState => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.5', map => $map_status }, # tunnelGreKeepalivesAdminState
184 operState => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.6', map => $map_status }, # tunnelGreKeepalivesOperState
185 packetsOut => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.10' }, # tunnelGreKeepalivesTxPackets
186 packetsIn => { oid => '.1.3.6.1.4.1.41916.4.5.2.1.11' } # tunnelGreKeepalivesRxPackets
187 };
188
189 sub manage_selection {
190 my ($self, %options) = @_;
191
192 if ($options{snmp}->is_snmpv1()) {
193 $self->{output}->add_option_msg(short_msg => 'Need to use SNMP v2c or v3.');
194 $self->{output}->option_exit();
195 }
196
197 $self->{global} = { detected => 0, oper_up => 0, oper_down => 0, oper_invalid => 0 };
198 $self->{tunnels} = {};
199
200 my $oid_tunnelTable = '.1.3.6.1.4.1.41916.4.5.2'; # tunnelGreKeepalivesTable
201 my $snmp_result = $options{snmp}->get_table(
202 oid => $oid_tunnelTable,
203 start => $mapping_name->{sourceIp}->{oid},
204 end => $mapping_name->{destIp}->{oid},
205 nothing_quit => 1
206 );
207 foreach my $oid (keys %$snmp_result) {
208 next if ($oid !~ /^$mapping_name->{sourceIp}->{oid}\.(.*)$/);
209 my $instance = $1;
210 my $result = $options{snmp}->map_instance(mapping => $mapping_name, results => $snmp_result, instance => $instance);
211 $result->{sourceIp} = inet_ntoa($result->{sourceIp});
212 $result->{destIp} = inet_ntoa($result->{destIp});
213
214 next if (defined($self->{option_results}->{filter_src_ip}) && $self->{option_results}->{filter_src_ip} ne '' &&
215 $result->{sourceIp} !~ /$self->{option_results}->{filter_src_ip}/);
216 next if (defined($self->{option_results}->{filter_dest_ip}) && $self->{option_results}->{filter_dest_ip} ne '' &&
217 $result->{destIp} !~ /$self->{option_results}->{filter_dest_ip}/);
218
219 $self->{tunnels}->{$instance} = {
220 %$result,
221 status => $result,
222 packet => $result
223 };
224 }
225
226 return if (scalar(keys %{$self->{tunnels}}) <= 0);
227
228 $options{snmp}->load(
229 oids => [
230 map($_->{oid}, values(%$mapping))
231 ],
232 instances => [ map($_, keys %{$self->{tunnels}}) ],
233 instance_regexp => '^(.*)$'
234 );
235 $snmp_result = $options{snmp}->get_leef();
236
237 foreach (keys %{$self->{tunnels}}) {
238 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
239
240 $self->{tunnels}->{$_}->{status}->{adminState} = $result->{adminState};
241 $self->{tunnels}->{$_}->{status}->{operState} = $result->{operState};
242
243 $self->{tunnels}->{$_}->{packet}->{packetsOut} = $result->{packetsOut};
244 $self->{tunnels}->{$_}->{packet}->{packetsIn} = $result->{packetsIn};
245
246 $self->{global}->{detected}++;
247 $self->{global}->{ 'oper_' . $result->{operState} }++;
248 }
249
250 $self->{cache_name} = 'viptela_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
251 md5_hex(
252 (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : 'all') .
253 (defined($self->{option_results}->{filter_src_ip}) ? $self->{option_results}->{filter_src_ip} : 'all') .
254 (defined($self->{option_results}->{filter_dest_ip}) ? $self->{option_results}->{filter_dest_ip} : 'all')
255 );
256 }
257
258 1;
259
260 __END__
261
262 =head1 MODE
263
264 Check GRE tunnels.
265
266 =over 8
267
268 =item B<--filter-src-ip>
269
270 Filter tunnels by source ip address.
271
272 =item B<--filter-dest-ip>
273
274 Filter tunnels by destination ip address.
275
276 =item B<--unknown-status>
277
278 Define the conditions to match for the status to be UNKNOWN.
279 You can use the following variables: %{adminState}, %{operState}, %{sourceIp}, %{destIp}
280
281 =item B<--warning-status>
282
283 Define the conditions to match for the status to be WARNING.
284 You can use the following variables: %{adminState}, %{operState}, %{sourceIp}, %{destIp}
285
286 =item B<--critical-status>
287
288 Define the conditions to match for the status to be CRITICAL (default: '%{adminState} eq "up" and %{operState} ne "up"').
289 You can use the following variables: %{adminState}, %{operState}, %{sourceIp}, %{destIp}
290
291 =item B<--warning-*> B<--critical-*>
292
293 Thresholds.
294 Can be: 'gretunnels-detected', 'gretunnels-up', 'gretunnels-down', 'gretunnels-invalid',
295 'gretunnel-packets-in', 'gretunnel-packets-out'.
296
297 =back
298
299 =cut