| 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::fortinet::fortigate::snmp::mode::vpn; |
| 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 centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); |
| 29 |
|
| 30 |
sub custom_state_output { |
| 31 |
my ($self, %options) = @_; |
| 32 |
|
| 33 |
return sprintf("state is '%s'", $self->{result_values}->{state}); |
| 34 |
} |
| 35 |
|
| 36 |
sub set_counters { |
| 37 |
my ($self, %options) = @_; |
| 38 |
|
| 39 |
$self->{maps_counters_type} = [ |
| 40 |
{ name => 'vd', type => 3, cb_prefix_output => 'prefix_vd_output', cb_long_output => 'vd_long_output', indent_long_output => ' ', message_multiple => 'All virtual domains are OK', |
| 41 |
group => [ |
| 42 |
{ name => 'global', type => 0, skipped_code => { -10 => 1 } }, |
| 43 |
{ name => 'vpn', display_long => 1, cb_prefix_output => 'prefix_vpn_output', message_multiple => 'All vpn are ok', type => 1, skipped_code => { -10 => 1 } } |
| 44 |
] |
| 45 |
} |
| 46 |
]; |
| 47 |
|
| 48 |
$self->{maps_counters}->{global} = [ |
| 49 |
{ label => 'users', nlabel => 'vpn.users.logged.count', set => { |
| 50 |
key_values => [ { name => 'users' } ], |
| 51 |
output_template => 'Logged users: %s', |
| 52 |
perfdatas => [ |
| 53 |
{ label => 'users', template => '%d', min => 0, unit => 'users', label_extra_instance => 1 } |
| 54 |
] |
| 55 |
} |
| 56 |
}, |
| 57 |
{ label => 'sessions', nlabel => 'vpn.websessions.active.count', set => { |
| 58 |
key_values => [ { name => 'sessions' }], |
| 59 |
output_template => 'Active web sessions: %s', |
| 60 |
perfdatas => [ |
| 61 |
{ label => 'sessions', template => '%d', min => 0, unit => 'sessions', label_extra_instance => 1 } |
| 62 |
] |
| 63 |
} |
| 64 |
}, |
| 65 |
{ label => 'tunnels', nlabel => 'vpn.tunnels.active.count', set => { |
| 66 |
key_values => [ { name => 'tunnels' } ], |
| 67 |
output_template => 'Active tunnels: %s', |
| 68 |
perfdatas => [ |
| 69 |
{ label => 'active_tunnels', template => '%d', min => 0, unit => 'tunnels', label_extra_instance => 1 } |
| 70 |
] |
| 71 |
} |
| 72 |
}, |
| 73 |
{ label => 'ipsec-tunnels-count', nlabel => 'vpn.ipsec.tunnels.state.count', set => { |
| 74 |
key_values => [ { name => 'ipsec_tunnels_count' } ], |
| 75 |
output_template => 'IPSec tunnels state up: %s', |
| 76 |
perfdatas => [ |
| 77 |
{ label => 'ipsec-tunnels-count', template => '%d', min => 0, unit => 'tunnels', label_extra_instance => 1 } |
| 78 |
] |
| 79 |
} |
| 80 |
} |
| 81 |
]; |
| 82 |
|
| 83 |
$self->{maps_counters}->{vpn} = [ |
| 84 |
{ label => 'status', threshold => 0, set => { |
| 85 |
key_values => [ { name => 'state' }, { name => 'display' } ], |
| 86 |
closure_custom_output => \&custom_state_output, |
| 87 |
closure_custom_perfdata => sub { return 0; }, |
| 88 |
closure_custom_threshold_check => \&catalog_status_threshold |
| 89 |
} |
| 90 |
}, |
| 91 |
{ label => 'traffic-in', nlabel => 'vpn.traffic.in.bitspersecond', set => { |
| 92 |
key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ], |
| 93 |
output_template => 'Traffic in: %s %s/s', |
| 94 |
output_change_bytes => 2, |
| 95 |
perfdatas => [ |
| 96 |
{ label => 'traffic_in', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1 } |
| 97 |
] |
| 98 |
} |
| 99 |
}, |
| 100 |
{ label => 'traffic-out', nlabel => 'vpn.traffic.out.bitspersecond', set => { |
| 101 |
key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ], |
| 102 |
output_template => 'Traffic out: %s %s/s', |
| 103 |
output_change_bytes => 2, |
| 104 |
perfdatas => [ |
| 105 |
{ label => 'traffic_out', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1 } |
| 106 |
] |
| 107 |
} |
| 108 |
} |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
sub prefix_vd_output { |
| 113 |
my ($self, %options) = @_; |
| 114 |
|
| 115 |
return "Virtual domain '" . $options{instance_value}->{display} . "' "; |
| 116 |
} |
| 117 |
|
| 118 |
sub vd_long_output { |
| 119 |
my ($self, %options) = @_; |
| 120 |
|
| 121 |
return "checking virtual domain '" . $options{instance_value}->{display} . "'"; |
| 122 |
} |
| 123 |
|
| 124 |
sub prefix_vpn_output { |
| 125 |
my ($self, %options) = @_; |
| 126 |
|
| 127 |
return "Link '" . $options{instance_value}->{display} . "' "; |
| 128 |
} |
| 129 |
|
| 130 |
sub new { |
| 131 |
my ($class, %options) = @_; |
| 132 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); |
| 133 |
bless $self, $class; |
| 134 |
|
| 135 |
$options{options}->add_options(arguments => { |
| 136 |
'filter-vpn:s' => { name => 'filter_vpn' }, |
| 137 |
'filter-vdomain:s' => { name => 'filter_vdomain' }, |
| 138 |
'warning-status:s' => { name => 'warning_status', default => '' }, |
| 139 |
'critical-status:s' => { name => 'critical_status', default => '%{state} eq "down"' } |
| 140 |
}); |
| 141 |
|
| 142 |
return $self; |
| 143 |
} |
| 144 |
|
| 145 |
sub check_options { |
| 146 |
my ($self, %options) = @_; |
| 147 |
$self->SUPER::check_options(%options); |
| 148 |
|
| 149 |
$self->change_macros(macros => ['warning_status', 'critical_status']); |
| 150 |
} |
| 151 |
|
| 152 |
my $map_status = { 1 => 'down', 2 => 'up' }; |
| 153 |
|
| 154 |
my $mapping = { |
| 155 |
fgVpnTunEntPhase2Name => { oid => '.1.3.6.1.4.1.12356.101.12.2.2.1.3' }, |
| 156 |
fgVpnTunEntInOctets => { oid => '.1.3.6.1.4.1.12356.101.12.2.2.1.18' }, |
| 157 |
fgVpnTunEntOutOctets => { oid => '.1.3.6.1.4.1.12356.101.12.2.2.1.19' }, |
| 158 |
fgVpnTunEntStatus => { oid => '.1.3.6.1.4.1.12356.101.12.2.2.1.20', map => $map_status }, |
| 159 |
fgVpnTunEntVdom => { oid => '.1.3.6.1.4.1.12356.101.12.2.2.1.21' } |
| 160 |
}; |
| 161 |
|
| 162 |
my $mapping2 = { |
| 163 |
fgVpnSslStatsLoginUsers => { oid => '.1.3.6.1.4.1.12356.101.12.2.3.1.2' }, |
| 164 |
fgVpnSslStatsActiveWebSessions => { oid => '.1.3.6.1.4.1.12356.101.12.2.3.1.4' }, |
| 165 |
fgVpnSslStatsActiveTunnels => { oid => '.1.3.6.1.4.1.12356.101.12.2.3.1.6' } |
| 166 |
}; |
| 167 |
|
| 168 |
my $oid_fgVpnTunTable = '.1.3.6.1.4.1.12356.101.12.2.2.1'; |
| 169 |
my $oid_fgVpnSslStatsTable = '.1.3.6.1.4.1.12356.101.12.2.3'; |
| 170 |
my $oid_fgVdEntName = '.1.3.6.1.4.1.12356.101.3.2.1.1.2'; |
| 171 |
|
| 172 |
sub manage_selection { |
| 173 |
my ($self, %options) = @_; |
| 174 |
|
| 175 |
$self->{cache_name} = 'fortigate_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' . |
| 176 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . |
| 177 |
(defined($self->{option_results}->{filter_vpn}) ? md5_hex($self->{option_results}->{filter_vpn}) : md5_hex('all')) . '_' . |
| 178 |
(defined($self->{option_results}->{filter_vdomain}) ? md5_hex($self->{option_results}->{filter_vdomain}) : md5_hex('all')); |
| 179 |
|
| 180 |
my $snmp_result = $options{snmp}->get_multiple_table( |
| 181 |
oids => [ |
| 182 |
{ oid => $oid_fgVdEntName }, |
| 183 |
{ oid => $oid_fgVpnSslStatsTable } |
| 184 |
], |
| 185 |
nothing_quit => 1 |
| 186 |
); |
| 187 |
my $snmp_result2 = $options{snmp}->get_multiple_table( |
| 188 |
oids => [ |
| 189 |
{ oid => $mapping->{fgVpnTunEntPhase2Name}->{oid} }, |
| 190 |
{ oid => $oid_fgVpnTunTable, start => $mapping->{fgVpnTunEntInOctets}->{oid} } |
| 191 |
], |
| 192 |
return_type => 1 |
| 193 |
); |
| 194 |
|
| 195 |
$self->{vd} = {}; |
| 196 |
my $duplicated = {}; |
| 197 |
my $ipsec_tunnels_counter = 0; |
| 198 |
foreach my $oid (keys %{$snmp_result->{ $oid_fgVdEntName }}) { |
| 199 |
$oid =~ /^$oid_fgVdEntName\.(.*)$/; |
| 200 |
my $vdom_instance = $1; |
| 201 |
my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_fgVpnSslStatsTable}, instance => $vdom_instance); |
| 202 |
my $vdomain_name = $snmp_result->{$oid_fgVdEntName}->{$oid_fgVdEntName . '.' . $vdom_instance}; |
| 203 |
|
| 204 |
if (defined($self->{option_results}->{filter_vdomain}) && $self->{option_results}->{filter_vdomain} ne '' && |
| 205 |
$vdomain_name !~ /$self->{option_results}->{filter_vdomain}/) { |
| 206 |
$self->{output}->output_add(long_msg => "skipping '" . $vdomain_name . "': no matching filter.", debug => 1); |
| 207 |
next; |
| 208 |
} |
| 209 |
|
| 210 |
$self->{vd}->{$vdomain_name} = { |
| 211 |
display => $vdomain_name, |
| 212 |
global => { |
| 213 |
users => $result->{fgVpnSslStatsLoginUsers}, |
| 214 |
tunnels => $result->{fgVpnSslStatsActiveTunnels}, |
| 215 |
sessions => $result->{fgVpnSslStatsActiveWebSessions}, |
| 216 |
ipsec_tunnels_count => $ipsec_tunnels_counter |
| 217 |
}, |
| 218 |
vpn => {}, |
| 219 |
}; |
| 220 |
|
| 221 |
foreach (keys %$snmp_result2) { |
| 222 |
next if (! /^$mapping->{fgVpnTunEntVdom}->{oid}\.(.*)$/ || |
| 223 |
$snmp_result2->{$_} != $vdom_instance |
| 224 |
); |
| 225 |
my $instance = $1; |
| 226 |
$result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result2, instance => $instance); |
| 227 |
|
| 228 |
if (defined($self->{option_results}->{filter_vpn}) && $self->{option_results}->{filter_vpn} ne '' && |
| 229 |
$result->{fgVpnTunEntPhase2Name} !~ /$self->{option_results}->{filter_vpn}/) { |
| 230 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{fgVpnTunEntPhase2Name} . "': no matching filter.", debug => 1); |
| 231 |
next; |
| 232 |
} |
| 233 |
|
| 234 |
my $name = $result->{fgVpnTunEntPhase2Name}; |
| 235 |
$name .= '.' . $instance if (defined($duplicated->{$name})); |
| 236 |
if (defined($self->{vd}->{$vdomain_name}->{vpn}->{$name})) { |
| 237 |
$duplicated->{$name} = 1; |
| 238 |
$self->{vd}->{$vdomain_name}->{vpn}->{$name . '.' . $self->{vd}->{$vdomain_name}->{vpn}->{$name}->{instance}} = $self->{vd}->{$vdomain_name}->{vpn}->{$name}; |
| 239 |
$self->{vd}->{$vdomain_name}->{vpn}->{$name . '.' . $self->{vd}->{$vdomain_name}->{vpn}->{$name}->{instance}}->{display} = $name . '.' . $self->{vd}->{$vdomain_name}->{vpn}->{$name}->{instance}; |
| 240 |
delete $self->{vd}->{$vdomain_name}->{vpn}->{$name}; |
| 241 |
$name = $result->{fgVpnTunEntPhase2Name} . '.' . $instance; |
| 242 |
} |
| 243 |
|
| 244 |
$self->{vd}->{$vdomain_name}->{vpn}->{$name} = { |
| 245 |
display => $name, |
| 246 |
instance => $instance, |
| 247 |
state => $result->{fgVpnTunEntStatus}, |
| 248 |
traffic_in => $result->{fgVpnTunEntInOctets} * 8, |
| 249 |
traffic_out => $result->{fgVpnTunEntOutOctets} * 8 |
| 250 |
}; |
| 251 |
# count tunnels in state up |
| 252 |
if ($self->{vd}->{$vdomain_name}->{vpn}->{$name}->{state} eq "up") { |
| 253 |
$ipsec_tunnels_counter++; |
| 254 |
}; |
| 255 |
} |
| 256 |
$self->{vd}->{$vdomain_name}->{global}->{ipsec_tunnels_count} = $ipsec_tunnels_counter; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
1; |
| 261 |
|
| 262 |
__END__ |
| 263 |
|
| 264 |
=head1 MODE |
| 265 |
|
| 266 |
Check Vdomain statistics and VPN state and traffic |
| 267 |
|
| 268 |
=over 8 |
| 269 |
|
| 270 |
=item B<--filter-*> |
| 271 |
|
| 272 |
Filter name with regexp. Can be ('vdomain', 'vpn') |
| 273 |
|
| 274 |
=item B<--warning-*> |
| 275 |
|
| 276 |
Warning on counters. Can be ('users', 'sessions', 'tunnels', 'traffic-in', 'traffic-out', 'ipsec-tunnels-count') |
| 277 |
|
| 278 |
=item B<--critical-*> |
| 279 |
|
| 280 |
Critical on counters. Can be ('users', 'sessions', 'tunnels', 'traffic-in', 'traffic-out', 'ipsec-tunnels-count')) |
| 281 |
|
| 282 |
=item B<--warning-status> |
| 283 |
|
| 284 |
Define the conditions to match for the status to be WARNING. Use "%{state}" as a special variable. |
| 285 |
Useful to be notified when tunnel is up "%{state} eq 'up'" |
| 286 |
|
| 287 |
=item B<--critical-status> |
| 288 |
|
| 289 |
Define the conditions to match for the status to be CRITICAL. Use "%{state}" as a special variable. |
| 290 |
Useful to be notified when tunnel is up "%{state} eq 'up'" |
| 291 |
|
| 292 |
=back |
| 293 |
|
| 294 |
=cut |