Debian Codegraph @

src/centreon/common/fortinet/fortigate/snmp/mode/vdomusage.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::fortinet::fortigate::snmp::mode::vdomusage;
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 catalog_status_calc);
28 use Digest::MD5 qw(md5_hex);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return sprintf(
34 "operation mode is '%s'- HA cluster member state is '%s'",
35 $self->{result_values}->{op_mode},
36 $self->{result_values}->{ha_state}
37 );
38 }
39
40 sub custom_license_output {
41 my ($self, %options) = @_;
42
43 return sprintf(
44 'number of virtual domains used: %s/%s (%.2f%%)',
45 $self->{result_values}->{used},
46 $self->{result_values}->{total},
47 $self->{result_values}->{prct_used}
48 );
49 }
50
51 sub custom_traffic_calc {
52 my ($self, %options) = @_;
53
54 my ($checked, $total_bits) = (0, 0);
55 foreach (keys %{$options{new_datas}}) {
56 if (/$self->{instance}_traffic_$options{extra_options}->{label_ref}_(\d+)/) {
57 my $new_bits = $options{new_datas}->{$_};
58 next if (!defined($options{old_datas}->{$_}));
59 my $old_bits = $options{old_datas}->{$_};
60
61 $checked = 1;
62 my $diff_bits = $new_bits - $old_bits;
63 if ($diff_bits < 0) {
64 $total_bits += $new_bits;
65 } else {
66 $total_bits += $diff_bits;
67 }
68 }
69 }
70
71 if ($checked == 0) {
72 $self->{error_msg} = 'buffer creation';
73 return -1;
74 }
75
76 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
77 $self->{result_values}->{traffic_per_second} = $total_bits / $options{delta_time};
78 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
79 return 0;
80 }
81
82 sub vdom_long_output {
83 my ($self, %options) = @_;
84
85 return "checking virtual domain '" . $options{instance_value}->{display} . "'";
86 }
87
88 sub prefix_vdom_output {
89 my ($self, %options) = @_;
90
91 return "virtual domain '" . $options{instance_value}->{display} . "' ";
92 }
93
94 sub set_counters {
95 my ($self, %options) = @_;
96
97 $self->{maps_counters_type} = [
98 { name => 'global', type => 0 },
99 { name => 'vdom', type => 3, cb_prefix_output => 'prefix_vdom_output', cb_long_output => 'vdom_long_output',
100 indent_long_output => ' ', message_multiple => 'All virtual domains are ok',
101 group => [
102 { name => 'vdom_cpu', type => 0, skipped_code => { -10 => 1 } },
103 { name => 'vdom_memory', type => 0, skipped_code => { -10 => 1 } },
104 { name => 'vdom_session', type => 0, skipped_code => { -10 => 1 } },
105 { name => 'vdom_traffic', type => 0, skipped_code => { -10 => 1 } },
106 { name => 'vdom_policy', type => 0, skipped_code => { -10 => 1 } },
107 { name => 'vdom_status', type => 0, skipped_code => { -10 => 1 } }
108 ]
109 }
110 ];
111
112 $self->{maps_counters}->{global} = [
113 { label => 'license-usage', nlabel => 'virtualdomains.license.usage.count', set => {
114 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
115 closure_custom_output => $self->can('custom_license_output'),
116 perfdatas => [
117 { template => '%d', min => 0, max => 'total' }
118 ]
119 }
120 },
121 { label => 'license-free', nlabel => 'virtualdomains.license.free.count', display_ok => 0, set => {
122 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
123 closure_custom_output => $self->can('custom_license_output'),
124 perfdatas => [
125 { template => '%d', min => 0, max => 'total' }
126 ]
127 }
128 },
129 { label => 'license-usage-prct', nlabel => 'virtualdomains.license.usage.percentage', display_ok => 0, set => {
130 key_values => [ { name => 'prct_used' }, { name => 'free' }, { name => 'used' }, { name => 'prct_free' }, { name => 'total' } ],
131 closure_custom_output => $self->can('custom_license_output'),
132 perfdatas => [
133 { template => '%.2f', min => 0, max => 100, unit => '%' }
134 ]
135 }
136 }
137 ];
138
139 $self->{maps_counters}->{vdom_cpu} = [
140 { label => 'cpu-utilization', nlabel => 'virtualdomain.cpu.utilization.percentage', set => {
141 key_values => [ { name => 'cpu' }, { name => 'display' } ],
142 output_template => 'cpu usage: %.2f%%',
143 perfdatas => [
144 { template => '%.2f', unit => '%', min => 0, max => 100,
145 label_extra_instance => 1, instance_use => 'display' }
146 ]
147 }
148 }
149 ];
150
151 $self->{maps_counters}->{vdom_memory} = [
152 { label => 'memory-usage-prct', nlabel => 'virtualdomain.memory.usage.percentage', set => {
153 key_values => [ { name => 'prct_used' }, { name => 'display' } ],
154 output_template => 'memory used : %.2f %%',
155 perfdatas => [
156 { template => '%.2f', min => 0, max => 100,
157 unit => '%', label_extra_instance => 1, instance_use => 'display' }
158 ],
159 }
160 }
161 ];
162
163 $self->{maps_counters}->{vdom_policy} = [
164 { label => 'policies-active', nlabel => 'virtualdomain.policies.active.count', set => {
165 key_values => [ { name => 'active_policies' }, { name => 'display' } ],
166 output_template => 'active policies: %d',
167 perfdatas => [
168 { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' },
169 ]
170 }
171 }
172 ];
173
174 $self->{maps_counters}->{vdom_session} = [
175 { label => 'sessions-active', nlabel => 'virtualdomain.sessions.active.count', set => {
176 key_values => [ { name => 'active_sessions' }, { name => 'display' } ],
177 output_template => 'active sessions: %d',
178 perfdatas => [
179 { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' },
180 ]
181 }
182 },
183 { label => 'sessions-rate', nlabel => 'virtualdomain.sessions.rate.persecond', set => {
184 key_values => [ { name => 'session_rate' }, { name => 'display' } ],
185 output_template => 'session setup rate: %d/s',
186 perfdatas => [
187 { template => '%d', min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display' }
188 ]
189 }
190 }
191 ];
192
193 $self->{maps_counters}->{vdom_status} = [
194 { label => 'status', threshold => 0, set => {
195 key_values => [ { name => 'op_mode' }, { name => 'ha_state' }, { name => 'display' } ],
196 closure_custom_calc => \&catalog_status_calc,
197 closure_custom_output => $self->can('custom_status_output'),
198 closure_custom_perfdata => sub { return 0; },
199 closure_custom_threshold_check => \&catalog_status_threshold
200 }
201 }
202 ];
203
204 $self->{maps_counters}->{vdom_traffic} = [
205 { label => 'traffic-in', nlabel => 'virtualdomain.traffic.in.bitspersecond', set => {
206 key_values => [],
207 manual_keys => 1,
208 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
209 output_template => 'traffic in: %s%s/s',
210 output_use => 'traffic_per_second', threshold_use => 'traffic_per_second',
211 output_change_bytes => 2,
212 perfdatas => [
213 { value => 'traffic_per_second', template => '%s',
214 min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
215 ]
216 }
217 },
218 { label => 'traffic-out', nlabel => 'virtualdomain.traffic.out.bitspersecond', set => {
219 key_values => [],
220 manual_keys => 1,
221 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
222 output_template => 'traffic out: %s%s/s',
223 output_use => 'traffic_per_second', threshold_use => 'traffic_per_second',
224 output_change_bytes => 2,
225 perfdatas => [
226 { value => 'traffic_per_second', template => '%s',
227 min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
228 ]
229 }
230 }
231 ];
232 }
233
234 sub new {
235 my ($class, %options) = @_;
236 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
237 bless $self, $class;
238
239 $options{options}->add_options(arguments => {
240 'filter-vdomain:s' => { name => 'filter_vdomain' },
241 'add-traffic' => { name => 'add_traffic' },
242 'add-policy' => { name => 'add_policy' },
243 'policy-cache-time:s' => { name => 'policy_cache_time', default => 60 },
244 'warning-status:s' => { name => 'warning_status', default => '' },
245 'critical-status:s' => { name => 'critical_status', default => '' }
246 });
247
248 $self->{cache_policy} = centreon::plugins::statefile->new(%options);
249 return $self;
250 }
251
252 sub check_options {
253 my ($self, %options) = @_;
254 $self->SUPER::check_options(%options);
255
256 $self->change_macros(macros => ['warning_status', 'critical_status']);
257 $self->{cache_policy}->check_options(%options) if (defined($self->{option_results}->{add_policy}));
258 }
259
260 my $map_opmode = { 1 => 'nat', 2 => 'transparent' };
261 my $map_ha = { 1 => 'master', 2 => 'backup', 3 => 'standalone' };
262
263 my $mapping = {
264 fgVdNumber => { oid => '.1.3.6.1.4.1.12356.101.3.1.1' },
265 fgVdMaxVdoms => { oid => '.1.3.6.1.4.1.12356.101.3.1.2' }
266 };
267 my $mapping_vdom = {
268 fgVdEntOpMode => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.3', map => $map_opmode },
269 fgVdEntHaState => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.4', map => $map_ha },
270 fgVdEntCpuUsage => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.5' },
271 fgVdEntMemUsage => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.6' },
272 fgVdEntSesCount => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.7' },
273 fgVdEntSesRate => { oid => '.1.3.6.1.4.1.12356.101.3.2.1.1.8' }
274 };
275
276 my $oid_fgVdEntName = '.1.3.6.1.4.1.12356.101.3.2.1.1.2';
277 my $oid_fgVdInfo = '.1.3.6.1.4.1.12356.101.3.1';
278 my $oid_fgIntfEntVdom = '.1.3.6.1.4.1.12356.101.7.2.1.1.1';
279
280 sub add_traffic {
281 my ($self, %options) = @_;
282
283 my $traffic_in32 = '.1.3.6.1.2.1.2.2.1.10';
284 my $traffic_out32 = '.1.3.6.1.2.1.2.2.1.16';
285 my $traffic_in64 = '.1.3.6.1.2.1.31.1.1.1.6';
286 my $traffic_out64 = '.1.3.6.1.2.1.31.1.1.1.10';
287
288 my $snmp_result = $options{snmp}->get_table(oid => $oid_fgIntfEntVdom);
289 my $indexes = {};
290 foreach (keys %$snmp_result) {
291 /\.(\d+)$/;
292 my $ifindex = $1;
293 if (defined($self->{vdom}->{ $snmp_result->{$_} })) {
294 $indexes->{$ifindex} = $snmp_result->{$_};
295 }
296 }
297
298 my ($in_oid, $out_oid) = ($traffic_in32, $traffic_out32);
299 if (!$options{snmp}->is_snmpv1()) {
300 ($in_oid, $out_oid) = ($traffic_in64, $traffic_out64);
301 }
302 $options{snmp}->load(
303 oids => [ $in_oid, $out_oid ],
304 instances => [ keys %$indexes ],
305 instance_regexp => '^(.*)$'
306 );
307 $snmp_result = $options{snmp}->get_leef();
308
309 foreach (keys %$indexes) {
310 next if (!defined($snmp_result->{$in_oid . '.' . $_}));
311 $self->{vdom}->{ $indexes->{$_} }->{vdom_traffic} = { display => $self->{vdom}->{ $indexes->{$_} }->{display} }
312 if (!defined($self->{vdom}->{ $indexes->{$_} }->{vdom_traffic}));
313 $self->{vdom}->{ $indexes->{$_} }->{vdom_traffic}->{'traffic_in_' . $_} = $snmp_result->{$in_oid . '.' . $_} * 8;
314 $self->{vdom}->{ $indexes->{$_} }->{vdom_traffic}->{'traffic_out_' . $_} = $snmp_result->{$out_oid . '.' . $_} * 8;
315 }
316 }
317
318 sub add_policy {
319 my ($self, %options) = @_;
320
321 my $oid_fgFwPolID = '.1.3.6.1.4.1.12356.101.5.1.2.1.1.1';
322
323 my $has_cache_file = $self->{cache_policy}->read(statefile => 'fortinet_fortigate_policy_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port());
324 my $timestamp_cache = $self->{cache_policy}->get(name => 'last_timestamp');
325 my $snmp_result = $self->{cache_policy}->get(name => 'snmp_result');
326 if ($has_cache_file == 0 || !defined($timestamp_cache) || !defined($snmp_result) ||
327 ((time() - $timestamp_cache) > (($self->{option_results}->{policy_cache_time}) * 60))) {
328 $snmp_result = $options{snmp}->get_table(oid => $oid_fgFwPolID);
329 $self->{cache_policy}->write(data => { last_timestamp => time(), snmp_result => $snmp_result });
330 }
331
332 foreach (keys %$snmp_result) {
333 /^$oid_fgFwPolID\.(\d+)/;
334 $self->{vdom}->{$1}->{vdom_policy}->{active_policies}++
335 if (defined($self->{vdom}->{$1}));
336 }
337 }
338
339 sub manage_selection {
340 my ($self, %options) = @_;
341
342 my $snmp_result = $options{snmp}->get_multiple_table(
343 oids => [
344 { oid => $oid_fgVdInfo },
345 { oid => $oid_fgVdEntName }
346 ],
347 nothing_quit => 1
348 );
349
350 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_fgVdInfo}, instance => 0);
351 $self->{global} = {
352 used => $result->{fgVdNumber},
353 total => $result->{fgVdMaxVdoms},
354 free => $result->{fgVdMaxVdoms} - $result->{fgVdNumber},
355 prct_used => $result->{fgVdNumber} * 100 / $result->{fgVdMaxVdoms},
356 prct_free => 100 - ($result->{fgVdNumber} * 100 / $result->{fgVdMaxVdoms})
357 };
358
359 $self->{vdom} = {};
360 foreach (keys %{$snmp_result->{$oid_fgVdEntName}}) {
361 /\.(\d+)$/;
362 my $instance = $1;
363 my $name = $snmp_result->{$oid_fgVdEntName}->{$_};
364
365 if (defined($self->{option_results}->{filter_vdomain}) && $self->{option_results}->{filter_vdomain} ne '' &&
366 $name !~ /$self->{option_results}->{filter_vdomain}/) {
367 $self->{output}->output_add(long_msg => "skipping virtual domain '" . $name . "'.", debug => 1);
368 next;
369 }
370
371 $self->{vdom}->{$instance} = {
372 display => $name,
373 vdom_cpu => { display => $name },
374 vdom_memory => { display => $name },
375 vdom_session => { display => $name },
376 vdom_status => { display => $name }
377 };
378 $self->{vdom}->{$instance}->{vdom_policy} = { display => $name, active_policies => 0 }
379 if (defined($self->{option_results}->{add_policy}));
380 }
381
382 return if (scalar(keys %{$self->{vdom}}) <= 0);
383
384 $options{snmp}->load(
385 oids => [ map($_->{oid}, values(%$mapping_vdom)) ],
386 instances => [ keys %{$self->{vdom}} ],
387 instance_regexp => '^(.*)$'
388 );
389 $snmp_result = $options{snmp}->get_leef();
390 foreach (keys %{$self->{vdom}}) {
391 my $result = $options{snmp}->map_instance(mapping => $mapping_vdom, results => $snmp_result, instance => $_);
392 $self->{vdom}->{$_}->{vdom_cpu}->{cpu} = $result->{fgVdEntCpuUsage};
393 $self->{vdom}->{$_}->{vdom_memory}->{prct_used} = $result->{fgVdEntMemUsage};
394 $self->{vdom}->{$_}->{vdom_status}->{op_mode} = $result->{fgVdEntOpMode};
395 $self->{vdom}->{$_}->{vdom_status}->{ha_state} = $result->{fgVdEntHaState};
396 $self->{vdom}->{$_}->{vdom_session}->{active_sessions} = $result->{fgVdEntSesCount};
397 $self->{vdom}->{$_}->{vdom_session}->{session_rate} = $result->{fgVdEntSesRate};
398 }
399
400 $self->add_traffic(snmp => $options{snmp})
401 if (defined($self->{option_results}->{add_traffic}));
402 $self->add_policy(snmp => $options{snmp})
403 if (defined($self->{option_results}->{add_policy}));
404
405 $self->{cache_name} = 'fortinet_fortigate_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' .
406 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
407 (defined($self->{option_results}->{filter_vdomain}) ? md5_hex($self->{option_results}->{filter_vdomain}) : md5_hex('all'));
408 }
409
410 1;
411
412 __END__
413
414 =head1 MODE
415
416 Check virtual domains.
417
418 =over 8
419
420 =item B<--filter-vdomain>
421
422 Filter by virtual domain name (can be a regexp).
423
424 =item B<--add-traffic>
425
426 Add traffic usage by virtual domain.
427
428 =item B<--add-policy>
429
430 Add number of policies by virtual domain.
431
432 =item B<--policy-cache-time>
433
434 Time in minutes before reloading cache file (default: 60).
435
436 =item B<--warning-status>
437
438 Define the conditions to match for the status to be WARNING (default: '').
439 You can use the following variables: %{op_mode}, %{ha_state}
440
441 =item B<--critical-status>
442
443 Define the conditions to match for the status to be CRITICAL (default: '').
444 You can use the following variables: %{op_mode}, %{ha_state}
445
446 =item B<--warning-*> B<--critical-*>
447
448 Thresholds.
449 Can be: 'cpu-utilization', 'sessions-active', 'session-rate',
450 'memory-usage-prct', 'license-usage', 'license-free',
451 'license-usage-prct', 'traffic-in', 'traffic-out', 'policies-active'.
452
453 =back
454
455 =cut