Debian Codegraph @

src/centreon/common/cisco/standard/snmp/mode/bgp.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::bgp;
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 Socket;
29 use Digest::MD5;
30
31 sub custom_status_output {
32 my ($self, %options) = @_;
33
34 return sprintf(
35 'state: %s [admin status: %s]',
36 $self->{result_values}->{state},
37 $self->{result_values}->{adminStatus}
38 );
39 }
40
41 sub prefix_peer_output {
42 my ($self, %options) = @_;
43
44 return sprintf(
45 "Peer [localAddr: %s, remoteAddr: %s, remoteAs: %s] ",
46 $options{instance_value}->{localAddr},
47 $options{instance_value}->{remoteAddr},
48 $options{instance_value}->{remoteAs}
49 );
50 }
51
52 sub prefix_global_output {
53 my ($self, %options) = @_;
54
55 return 'number of peers ';
56 }
57
58 sub set_counters {
59 my ($self, %options) = @_;
60
61 $self->{maps_counters_type} = [
62 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
63 { name => 'peers', type => 1, cb_prefix_output => 'prefix_peer_output', message_multiple => 'All BGP peers are ok' }
64 ];
65
66 $self->{maps_counters}->{global} = [
67 { label => 'peers-detected', nlabel => 'bgp.peers.detected.count', set => {
68 key_values => [ { name => 'detected' } ],
69 output_template => 'detected: %s',
70 perfdatas => [
71 { template => '%s', min => 0 }
72 ]
73 }
74 }
75 ];
76
77 $self->{maps_counters}->{peers} = [
78 { label => 'status', type => 2, critical_default => '%{adminStatus} =~ /start/ && %{state} !~ /established/', set => {
79 key_values => [
80 { name => 'adminStatus' }, { name => 'state' },
81 { name => 'localAddr' }, { name => 'remoteAddr' }, { name => 'remoteAs' }
82 ],
83 closure_custom_output => $self->can('custom_status_output'),
84 closure_custom_perfdata => sub { return 0; },
85 closure_custom_threshold_check => \&catalog_status_threshold_ng
86 }
87 },
88 { label => 'peer-update-last', nlabel => 'bgp.peer.update.last.seconds', set => {
89 key_values => [ { name => 'last_update' } ],
90 output_template => 'last update: %s s',
91 perfdatas => [
92 { template => '%s', unit => 's', min => 0, label_extra_instance => 1 }
93 ]
94 }
95 },
96 { label => 'peer-prefixes-accepted', nlabel => 'bgp.peer.prefixes.accepted.count', set => {
97 key_values => [ { name => 'acceptedPrefixes', diff => 1 } ],
98 output_template => 'prefixes accepted: %s',
99 perfdatas => [
100 { template => '%s', min => 0, label_extra_instance => 1 }
101 ]
102 }
103 },
104 { label => 'peer-prefixes-denied', nlabel => 'bgp.peer.prefixes.denied.count', set => {
105 key_values => [ { name => 'deniedPrefixes', diff => 1 } ],
106 output_template => 'prefixes denied: %s',
107 perfdatas => [
108 { template => '%s', min => 0, label_extra_instance => 1 }
109 ]
110 }
111 }
112 ];
113 }
114
115 sub new {
116 my ($class, %options) = @_;
117 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1);
118 bless $self, $class;
119
120 $options{options}->add_options(arguments => {
121 'filter-remote-addr:s' => { name => 'filter_remote_addr' },
122 'filter-remote-as:s' => { name => 'filter_remote_as' }
123 });
124
125 return $self;
126 }
127
128 my %map_peer_state = (
129 1 => 'idle',
130 2 => 'connect',
131 3 => 'active',
132 4 => 'opensent',
133 5 => 'openconfirm',
134 6 => 'established'
135 );
136
137 my %map_admin_state = (
138 1 => 'stop',
139 2 => 'start'
140 );
141
142 my $mapping = {
143 state => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.3', map => \%map_peer_state }, # cbgpPeer2State
144 adminStatus => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.4', map => \%map_admin_state }, # cbgpPeer2AdminStatus
145 localAddr => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.6' }, # cbgpPeer2LocalAddr
146 localPort => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.7' }, # cbgpPeer2LocalPort
147 remoteAs => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.11' }, # cbgpPeer2RemoteAs
148 inUpdateElapsedTime => { oid => '.1.3.6.1.4.1.9.9.187.1.2.5.1.27' } # cbgpPeer2InUpdateElapsedTime
149 };
150
151 sub get_ipv6 {
152 my ($self, %options) = @_;
153
154 my $ipv6 = '';
155 foreach my $val (split /\./, $options{value}) {
156 $ipv6 .= pack('C', $val);
157 }
158
159 return Socket::inet_ntop(Socket::AF_INET6, $ipv6);
160 }
161
162 sub manage_selection {
163 my ($self, %options) = @_;
164
165 $self->{cache_name} = 'cisco_standard_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
166 Digest::MD5::md5_hex(
167 (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
168 (defined($self->{option_results}->{filter_remote_addr}) ? $self->{option_results}->{filter_remote_addr} : '') . '_' .
169 (defined($self->{option_results}->{filter_remote_as}) ? $self->{option_results}->{filter_remote_ass} : '')
170 );
171
172 my $oid_remotePort = '.1.3.6.1.4.1.9.9.187.1.2.5.1.10'; # cbgpPeer2RemotePort
173 my $snmp_result = $options{snmp}->get_table(oid => $oid_remotePort, nothing_quit => 1);
174
175 $self->{global} = { detected => 0 };
176 $self->{peers} = {};
177 foreach my $oid (keys %$snmp_result) {
178 $oid =~ /^$oid_remotePort\.(\d+)\.(\d+)\.(.*)$/;
179 my ($type, $num, $remote_addr) = ($1, $2, $3);
180 my $remote;
181 if ($type == 1) {
182 $remote = $remote_addr . ':' . $snmp_result->{$oid};
183 } elsif ($type == 2) {
184 $remote = $self->get_ipv6(value => $remote_addr);
185 $remote = '[' . $remote . ']:' . $snmp_result->{$oid};
186 } else {
187 $self->{output}->add_option_msg(short_msg => 'unsupported address type');
188 $self->{output}->option_exit();
189 }
190
191 next if (defined($self->{option_results}->{filter_remote_addr}) && $self->{option_results}->{filter_remote_addr} ne '' &&
192 $remote !~ /$self->{option_results}->{filter_remote_addr}/);
193
194 $self->{global}->{detected}++;
195 $self->{peers}->{$remote} = { remoteAddr => $remote, instance => $type . '.' . $num . '.' . $remote_addr };
196 }
197
198 return if (scalar(keys %{$self->{peers}}) <= 0);
199
200 $options{snmp}->load(
201 oids => [ map($_->{oid}, values(%$mapping)) ],
202 instances => [ map($_->{instance}, values(%{$self->{peers}})) ],
203 instance_regexp => '^(.*)$'
204 );
205 $snmp_result = $options{snmp}->get_leef();
206
207 my $oid_acceptedPrefixes = '.1.3.6.1.4.1.9.9.187.1.2.8.1.1'; # cbgpPeer2AcceptedPrefixes
208 my $oid_deniedPrefixes = '.1.3.6.1.4.1.9.9.187.1.2.8.1.2'; # cbgpPeer2DeniedPrefixes
209 my $snmp_family = $options{snmp}->get_table(oid => '.1.3.6.1.4.1.9.9.187.1.2.8.1', end => $oid_deniedPrefixes); # cbgpPeer2AddrFamilyPrefixEntry
210
211 foreach (keys %{$self->{peers}}) {
212 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $self->{peers}->{$_}->{instance});
213
214 if (defined($self->{option_results}->{filter_remote_as}) && $self->{option_results}->{filter_remote_as} ne '' &&
215 $result->{remoteAs} !~ /$self->{option_results}->{filter_remote_as}/) {
216 delete $self->{peers}->{$_};
217 $self->{global}->{detected}--;
218 next;
219 }
220
221 my $local;
222 # it can be empty
223 if (length($result->{localAddr}) == 0) {
224 $local = '-';
225 } elsif (length($result->{localAddr}) == 4) {
226 $local = Socket::inet_ntop(Socket::AF_INET, $result->{localAddr}) . ':' . $result->{localPort};
227 } else {
228 $local = '[' . Socket::inet_ntop(Socket::AF_INET6, $result->{localAddr}) . ']:' . $result->{localPort};
229 }
230
231 $self->{peers}->{$_}->{state} = $result->{state};
232 $self->{peers}->{$_}->{adminStatus} = $result->{adminStatus};
233 $self->{peers}->{$_}->{remoteAs} = $result->{remoteAs};
234 $self->{peers}->{$_}->{localAddr} = $local;
235 $self->{peers}->{$_}->{last_update} = $result->{inUpdateElapsedTime};
236
237 foreach my $oid (keys %$snmp_family) {
238 next if ($oid !~ /^$oid_acceptedPrefixes\.$self->{peers}->{$_}->{instance}\.(.*)$/);
239
240 $self->{peers}->{$_}->{acceptedPrefixes} = $snmp_family->{$oid};
241 $self->{peers}->{$_}->{deniedPrefixes} = $snmp_family->{$oid_deniedPrefixes . '.' . $self->{peers}->{$_}->{instance} . '.' . $1};
242 }
243 }
244 }
245
246 1;
247
248 __END__
249
250 =head1 MODE
251
252 Check BGP.
253
254 =over 8
255
256 =item B<--filter-remote-as>
257
258 Filter based on remote AS number (regexp allowed)
259
260 =item B<--filter-remote-addr>
261
262 Filter based on IP of peers (regexp allowed)
263
264 =item B<--unknown-status>
265
266 Define the conditions to match for the status to be UNKNOWN.
267 You can use the following variables: %{adminStatus}, %{state}, %{localAddr}, %{remoteAddr}, %{remoteAs}
268
269 =item B<--warning-status>
270
271 Define the conditions to match for the status to be WARNING.
272 You can use the following variables: %{adminStatus}, %{state}, %{localAddr}, %{remoteAddr}, %{remoteAs}
273
274 =item B<--critical-status>
275
276 Define the conditions to match for the status to be CRITICAL (default: '%{adminStatus} =~ /start/ && %{state} !~ /established/').
277 You can use the following variables: %{adminStatus}, %{state}, %{localAddr}, %{remoteAddr}, %{remoteAs}
278
279 =item B<--warning-*> B<--critical-*>
280
281 Thresholds.
282 Can be: 'peers-detected', 'peer-update-last', 'peer-prefixes-accepted', 'peer-prefixes-denied'.
283
284 =back
285
286 =cut