| 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::cisco::meraki::cloudcontroller::restapi::mode::vpntunnels; |
| 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 |
|
| 30 |
sub custom_device_status_output { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
return 'status: ' . $self->{result_values}->{deviceStatus} . ' [mode: ' . $self->{result_values}->{deviceMode} . ']'; |
| 34 |
} |
| 35 |
|
| 36 |
sub custom_vpn_status_output { |
| 37 |
my ($self, %options) = @_; |
| 38 |
|
| 39 |
return 'status: ' . $self->{result_values}->{vpnStatus}; |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_vpn_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return "vpn tunnel '" . $options{instance_value}->{vpnName} . "' [type: " . $options{instance_value}->{vpnType} . "] "; |
| 46 |
} |
| 47 |
|
| 48 |
sub prefix_global_output { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
return 'Number of VPNS '; |
| 52 |
} |
| 53 |
|
| 54 |
sub device_long_output { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
return sprintf( |
| 58 |
"checking device '%s'", |
| 59 |
$options{instance_value}->{serial} |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
sub prefix_device_output { |
| 64 |
my ($self, %options) = @_; |
| 65 |
|
| 66 |
return sprintf( |
| 67 |
"device '%s' ", |
| 68 |
$options{instance_value}->{serial} |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
sub set_counters { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
$self->{maps_counters_type} = [ |
| 76 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, |
| 77 |
{ name => 'devices', type => 3, cb_prefix_output => 'prefix_device_output', cb_long_output => 'device_long_output', indent_long_output => ' ', message_multiple => 'All devices are ok', |
| 78 |
group => [ |
| 79 |
{ name => 'status', type => 0, skipped_code => { -10 => 1 } }, |
| 80 |
{ name => 'vpns', type => 1, display_long => 1, cb_prefix_output => 'prefix_vpn_output', message_multiple => 'All VPNs are ok', skipped_code => { -10 => 1 } } |
| 81 |
] |
| 82 |
} |
| 83 |
]; |
| 84 |
|
| 85 |
$self->{maps_counters}->{global} = [ |
| 86 |
{ label => 'total-unreachable', nlabel => 'vpn.tunnels.unreachable.count', display_ok => 0, set => { |
| 87 |
key_values => [ { name => 'unreachable' }, { name => 'total' } ], |
| 88 |
output_template => 'unreachable: %s', |
| 89 |
perfdatas => [ |
| 90 |
{ template => '%s', min => 0, max => 'total' } |
| 91 |
] |
| 92 |
} |
| 93 |
} |
| 94 |
]; |
| 95 |
|
| 96 |
$self->{maps_counters}->{status} = [ |
| 97 |
{ |
| 98 |
label => 'device-status', |
| 99 |
type => 2, |
| 100 |
unknown_default => '%{status} =~ /offline/i', |
| 101 |
set => { |
| 102 |
key_values => [ { name => 'deviceStatus' }, { name => 'deviceMode' }, { name => 'deviceSerial' } ], |
| 103 |
closure_custom_output => $self->can('custom_device_status_output'), |
| 104 |
closure_custom_perfdata => sub { return 0; }, |
| 105 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 106 |
} |
| 107 |
} |
| 108 |
]; |
| 109 |
|
| 110 |
$self->{maps_counters}->{vpns} = [ |
| 111 |
{ |
| 112 |
label => 'vpn-status', |
| 113 |
type => 2, |
| 114 |
critical_default => '%{deviceStatus} =~ /online/i and %{vpnStatus} =~ /unreachable/i', |
| 115 |
set => { |
| 116 |
key_values => [ |
| 117 |
{ name => 'vpnStatus' }, { name => 'vpnName' }, { name => 'vpnType' }, |
| 118 |
{ name => 'deviceStatus' }, { name => 'deviceSerial' } |
| 119 |
], |
| 120 |
closure_custom_output => $self->can('custom_vpn_status_output'), |
| 121 |
closure_custom_perfdata => sub { return 0; }, |
| 122 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 123 |
} |
| 124 |
} |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
sub new { |
| 129 |
my ($class, %options) = @_; |
| 130 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 131 |
bless $self, $class; |
| 132 |
|
| 133 |
$options{options}->add_options(arguments => { |
| 134 |
'filter-network-name:s' => { name => 'filter_network_name' }, |
| 135 |
'filter-organization-name:s' => { name => 'filter_organization_name' }, |
| 136 |
'filter-organization-id:s' => { name => 'filter_organization_id' }, |
| 137 |
'filter-device-serial:s' => { name => 'filter_device_serial' }, |
| 138 |
'filter-vpn-type:s' => { name => 'filter_vpn_type' }, |
| 139 |
'filter-vpn-name:s' => { name => 'filter_vpn_name' } |
| 140 |
}); |
| 141 |
|
| 142 |
return $self; |
| 143 |
} |
| 144 |
|
| 145 |
sub manage_selection { |
| 146 |
my ($self, %options) = @_; |
| 147 |
|
| 148 |
my $datas = $options{custom}->get_datas(skipDevices => 1, skipDevicesStatus => 1, skipNetworks => 1); |
| 149 |
|
| 150 |
$self->{global} = { unreachable => 0 }; |
| 151 |
$self->{devices} = {}; |
| 152 |
foreach my $id (keys %{$datas->{vpn_tunnels_status}}) { |
| 153 |
next if (defined($self->{option_results}->{filter_network_name}) && $self->{option_results}->{filter_network_name} ne '' && |
| 154 |
$datas->{vpn_tunnels_status}->{$id}->{networkName} !~ /$self->{option_results}->{filter_network_name}/); |
| 155 |
next if (defined($self->{option_results}->{filter_device_serial}) && $self->{option_results}->{filter_device_serial} ne '' && |
| 156 |
$id !~ /$self->{option_results}->{filter_device_serial}/); |
| 157 |
|
| 158 |
next if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' && |
| 159 |
$datas->{vpn_tunnels_status}->{$id}->{organizationId} !~ /$self->{option_results}->{filter_organization_id}/); |
| 160 |
next if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' && |
| 161 |
$datas->{orgs}->{ $datas->{vpn_tunnels_status}->{$id}->{organizationId} }->{name} !~ /$self->{option_results}->{filter_organization_name}/); |
| 162 |
|
| 163 |
$self->{devices}->{$id} = { |
| 164 |
serial => $id, |
| 165 |
status => { |
| 166 |
deviceSerial => $id, |
| 167 |
deviceStatus => $datas->{vpn_tunnels_status}->{$id}->{deviceStatus}, |
| 168 |
deviceMode => $datas->{vpn_tunnels_status}->{$id}->{vpnMode} |
| 169 |
}, |
| 170 |
vpns => {} |
| 171 |
}; |
| 172 |
|
| 173 |
foreach (@{$datas->{vpn_tunnels_status}->{$id}->{merakiVpnPeers}}) { |
| 174 |
my $type = 'meraki'; |
| 175 |
next if (defined($self->{option_results}->{filter_vpn_type}) && $self->{option_results}->{filter_vpn_type} ne '' && |
| 176 |
$type !~ /$self->{option_results}->{filter_vpn_type}/); |
| 177 |
next if (defined($self->{option_results}->{filter_vpn_name}) && $self->{option_results}->{filter_vpn_name} ne '' && |
| 178 |
$_->{networkName} !~ /$self->{option_results}->{filter_vpn_name}/); |
| 179 |
|
| 180 |
$self->{devices}->{$id}->{vpns}->{ $_->{networkName} } = { |
| 181 |
deviceSerial => $id, |
| 182 |
deviceStatus => $datas->{vpn_tunnels_status}->{$id}->{deviceStatus}, |
| 183 |
vpnType => $type, |
| 184 |
vpnName => $_->{networkName}, |
| 185 |
vpnStatus => $_->{reachability} |
| 186 |
}; |
| 187 |
|
| 188 |
$self->{global}->{total}++; |
| 189 |
$self->{global}->{ lc($_->{reachability}) }++ |
| 190 |
if (defined($self->{global}->{ lc($_->{reachability}) })); |
| 191 |
} |
| 192 |
|
| 193 |
foreach (@{$datas->{vpn_tunnels_status}->{$id}->{thirdPartyVpnPeers}}) { |
| 194 |
my $type = 'thirdParty'; |
| 195 |
next if (defined($self->{option_results}->{filter_vpn_type}) && $self->{option_results}->{filter_vpn_type} ne '' && |
| 196 |
$type !~ /$self->{option_results}->{filter_vpn_type}/); |
| 197 |
next if (defined($self->{option_results}->{filter_vpn_name}) && $self->{option_results}->{filter_vpn_name} ne '' && |
| 198 |
$_->{name} !~ /$self->{option_results}->{filter_vpn_name}/); |
| 199 |
|
| 200 |
$self->{devices}->{$id}->{vpns}->{ $_->{name} } = { |
| 201 |
deviceSerial => $id, |
| 202 |
deviceStatus => $datas->{vpn_tunnels_status}->{$id}->{deviceStatus}, |
| 203 |
vpnType => $type, |
| 204 |
vpnName => $_->{name}, |
| 205 |
vpnStatus => $_->{reachability} |
| 206 |
}; |
| 207 |
|
| 208 |
$self->{global}->{total}++; |
| 209 |
$self->{global}->{ lc($_->{reachability}) }++ |
| 210 |
if (defined($self->{global}->{ lc($_->{reachability}) })); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
# we remove entries if there is a --filter-vpn-[type|name] and no --filter-device-serial |
| 215 |
if ((!defined($self->{option_results}->{filter_device_serial}) || $self->{option_results}->{filter_device_serial} eq '') && |
| 216 |
((defined($self->{option_results}->{filter_vpn_type}) && $self->{option_results}->{filter_vpn_type} ne '') || |
| 217 |
(defined($self->{option_results}->{filter_vpn_name}) && $self->{option_results}->{filter_vpn_name} ne '')) |
| 218 |
) { |
| 219 |
foreach my $id (keys %{$self->{devices}}) { |
| 220 |
delete $self->{devices}->{$id} if (scalar(keys %{$self->{devices}->{$id}->{vpns}}) <= 0); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
1; |
| 226 |
|
| 227 |
__END__ |
| 228 |
|
| 229 |
=head1 MODE |
| 230 |
|
| 231 |
Check VPN tunnels. |
| 232 |
|
| 233 |
=over 8 |
| 234 |
|
| 235 |
=item B<--filter-network-name> |
| 236 |
|
| 237 |
Filter VPN tunnels by network name (can be a regexp). |
| 238 |
|
| 239 |
=item B<--filter-organization-id> |
| 240 |
|
| 241 |
Filter VPN tunnels by organization ID (can be a regexp). |
| 242 |
|
| 243 |
=item B<--filter-organization-name> |
| 244 |
|
| 245 |
Filter VPN tunnels by organization name (can be a regexp). |
| 246 |
|
| 247 |
=item B<--filter-device-serial> |
| 248 |
|
| 249 |
Filter VPN tunnels by device serial (can be a regexp). |
| 250 |
|
| 251 |
=item B<--filter-vpn-type> |
| 252 |
|
| 253 |
Filter VPN tunnels by VPN type (can be a regexp). |
| 254 |
|
| 255 |
=item B<--filter-vpn-name> |
| 256 |
|
| 257 |
Filter VPN tunnels by VPN name (can be a regexp). |
| 258 |
|
| 259 |
=item B<--unknown-device-status> |
| 260 |
|
| 261 |
Define the conditions to match for the status to be UNKNOWN (default: '%{deviceStatus} =~ /offline/i'). |
| 262 |
You can use the following variables: %{deviceStatus}, %{deviceSerial}, %{deviceMode} |
| 263 |
|
| 264 |
=item B<--warning-device-status> |
| 265 |
|
| 266 |
Define the conditions to match for the status to be WARNING. |
| 267 |
You can use the following variables: %{deviceStatus}, %{deviceSerial}, %{deviceMode} |
| 268 |
|
| 269 |
=item B<--critical-device-status> |
| 270 |
|
| 271 |
Define the conditions to match for the status to be CRITICAL. |
| 272 |
You can use the following variables: %{deviceStatus}, %{deviceSerial} |
| 273 |
|
| 274 |
=item B<--unknown-vpn-status> |
| 275 |
|
| 276 |
Define the conditions to match for the status to be UNKNOWN. |
| 277 |
You can use the following variables: %{vpnStatus}, %{vpnName}, %{vpnType}, %{deviceStatus}, %{deviceSerial} |
| 278 |
|
| 279 |
=item B<--warning-vpn-status> |
| 280 |
|
| 281 |
Define the conditions to match for the status to be WARNING. |
| 282 |
You can use the following variables: %{vpnStatus}, %{vpnName}, %{vpnType}, %{deviceStatus}, %{deviceSerial} |
| 283 |
|
| 284 |
=item B<--critical-vpn-status> |
| 285 |
|
| 286 |
Define the conditions to match for the status to be CRITICAL (default: '%{deviceStatus} =~ /online/i and %{vpnStatus} =~ /unreachable/i'). |
| 287 |
You can use the following variables: %{vpnStatus}, %{vpnName}, %{vpnType}, %{deviceStatus}, %{deviceSerial} |
| 288 |
|
| 289 |
=item B<--warning-*> B<--critical-*> |
| 290 |
|
| 291 |
Thresholds. |
| 292 |
Can be: 'total-unreachable'. |
| 293 |
|
| 294 |
=back |
| 295 |
|
| 296 |
=cut |