Debian Codegraph @

src/apps/thales/mistral/vs9/restapi/mode/devices.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 apps::thales::mistral::vs9::restapi::mode::devices;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5;
28 use DateTime;
29 use POSIX;
30 use centreon::plugins::misc;
31 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
32
33 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
34 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
35
36 sub custom_certificate_status_output {
37 my ($self, %options) = @_;
38
39 return sprintf(
40 'revoked: %s',
41 $self->{result_values}->{revoked}
42 );
43 }
44
45 sub custom_certificate_expires_perfdata {
46 my ($self, %options) = @_;
47
48 $self->{output}->perfdata_add(
49 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{time_certificate_unit} },
50 unit => $self->{instance_mode}->{option_results}->{time_certificate_unit},
51 instances => [
52 $self->{result_values}->{sn},
53 $self->{result_values}->{certSn},
54 $self->{result_values}->{subjectCommonName},
55 $self->{result_values}->{issuerCommonName}
56 ],
57 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_certificate_unit} }),
58 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
59 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
60 min => 0
61 );
62 }
63
64 sub custom_certificate_expires_threshold {
65 my ($self, %options) = @_;
66
67 return $self->{perfdata}->threshold_check(
68 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_certificate_unit} }),
69 threshold => [
70 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
71 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
72 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
73 ]
74 );
75 }
76
77 sub custom_time_offset_output {
78 my ($self, %options) = @_;
79
80 return sprintf(
81 'time offset %d second(s): %s',
82 $self->{result_values}->{offset},
83 $self->{result_values}->{date}
84 );
85 }
86
87 sub custom_uptime_output {
88 my ($self, %options) = @_;
89
90 return sprintf(
91 'uptime: %s',
92 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{uptime}, start => 'd')
93 );
94 }
95
96 sub custom_uptime_perfdata {
97 my ($self, %options) = @_;
98
99 $self->{output}->perfdata_add(
100 nlabel => 'system.uptime.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{time_uptime_unit} },
101 unit => $self->{instance_mode}->{option_results}->{time_uptime_unit},
102 instances => $self->{result_values}->{sn},
103 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_uptime_unit} }),
104 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
105 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
106 min => 0
107 );
108 }
109
110 sub custom_uptime_threshold {
111 my ($self, %options) = @_;
112
113 return $self->{perfdata}->threshold_check(
114 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_uptime_unit} }),
115 threshold => [
116 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
117 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
118 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
119 ]
120 );
121 }
122
123 sub custom_mistral_version_output {
124 my ($self, %options) = @_;
125
126 return sprintf(
127 'firmware version: %s (current) %s (other), configurationId: %s',
128 $self->{result_values}->{firmwareCurrentVersion},
129 $self->{result_values}->{firmwareOtherVersion},
130 $self->{result_values}->{configurationId}
131 );
132 }
133
134 sub custom_system_version_output {
135 my ($self, %options) = @_;
136
137 return sprintf(
138 'system os: %s %s',
139 $self->{result_values}->{osName},
140 $self->{result_values}->{osRelease}
141 );
142 }
143
144 sub custom_traffic_perfdata {
145 my ($self, %options) = @_;
146
147 my ($warning, $critical);
148 if ($self->{instance_mode}->{option_results}->{traffic_unit} eq 'percent_delta' && defined($self->{result_values}->{speed})) {
149 $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
150 $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
151 } elsif ($self->{instance_mode}->{option_results}->{traffic_unit} =~ /bps|counter/) {
152 $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
153 $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
154 }
155
156 if ($self->{instance_mode}->{option_results}->{traffic_unit} eq 'counter') {
157 my $nlabel = $self->{nlabel};
158 $nlabel =~ s/bitspersecond/bits/;
159 $self->{output}->perfdata_add(
160 nlabel => $nlabel,
161 unit => 'b',
162 instances => [$self->{result_values}->{sn}, $self->{result_values}->{name}],
163 value => $self->{result_values}->{traffic_counter},
164 warning => $warning,
165 critical => $critical,
166 min => 0
167 );
168 } else {
169 $self->{output}->perfdata_add(
170 nlabel => $self->{nlabel},
171 instances => [$self->{result_values}->{sn}, $self->{result_values}->{name}],
172 value => sprintf('%.2f', $self->{result_values}->{traffic_per_seconds}),
173 warning => $warning,
174 critical => $critical,
175 min => 0, max => $self->{result_values}->{speed}
176 );
177 }
178 }
179
180 sub custom_traffic_threshold {
181 my ($self, %options) = @_;
182
183 my $exit = 'ok';
184 if ($self->{instance_mode}->{option_results}->{traffic_unit} eq 'percent_delta' && defined($self->{result_values}->{speed})) {
185 $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
186 } elsif ($self->{instance_mode}->{option_results}->{traffic_unit} eq 'bps') {
187 $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_per_seconds}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
188 } elsif ($self->{instance_mode}->{option_results}->{traffic_unit} eq 'counter') {
189 $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_counter}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
190 }
191 return $exit;
192 }
193
194 sub custom_traffic_output {
195 my ($self, %options) = @_;
196
197 my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic_per_seconds}, network => 1);
198 return sprintf(
199 'traffic %s: %s/s (%s)',
200 $self->{result_values}->{label}, $traffic_value . $traffic_unit,
201 defined($self->{result_values}->{traffic_prct}) ? sprintf('%.2f%%', $self->{result_values}->{traffic_prct}) : '-'
202 );
203 }
204
205 sub custom_traffic_calc {
206 my ($self, %options) = @_;
207
208 $self->{result_values}->{traffic_per_seconds} = ($options{new_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} } - $options{old_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} }) / $options{delta_time};
209 $self->{result_values}->{traffic_counter} = $options{new_datas}->{ $self->{instance} . '_' . $options{extra_options}->{label_ref} };
210
211 $self->{result_values}->{traffic_per_seconds} = sprintf('%d', $self->{result_values}->{traffic_per_seconds});
212
213 if (defined($options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}}) &&
214 $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}} ne '' &&
215 $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}} > 0) {
216 $self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic_per_seconds} * 100 / $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}};
217 $self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_speed_' . $options{extra_options}->{label_ref}};
218 }
219
220 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
221 $self->{result_values}->{sn} = $options{new_datas}->{$self->{instance} . '_sn'};
222 $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
223 return 0;
224 }
225
226 sub custom_connection_perfdata {
227 my ($self, %options) = @_;
228
229 $self->{output}->perfdata_add(
230 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{time_connection_unit} },
231 unit => $self->{instance_mode}->{option_results}->{time_connection_unit},
232 instances => $self->{result_values}->{sn},
233 value => floor($self->{result_values}->{connection_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_connection_unit} }),
234 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
235 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
236 min => 0
237 );
238 }
239
240 sub custom_connection_threshold {
241 my ($self, %options) = @_;
242
243 return $self->{perfdata}->threshold_check(
244 value => floor($self->{result_values}->{connection_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_connection_unit} }),
245 threshold => [
246 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
247 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
248 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
249 ]
250 );
251 }
252
253 sub device_long_output {
254 my ($self, %options) = @_;
255
256 return sprintf(
257 "checking device '%s'",
258 $options{instance_value}->{sn}
259 );
260 }
261
262 sub prefix_device_output {
263 my ($self, %options) = @_;
264
265 return sprintf(
266 "device '%s' ",
267 $options{instance_value}->{sn}
268 );
269 }
270
271 sub prefix_global_output {
272 my ($self, %options) = @_;
273
274 return 'Number of devices ';
275 }
276
277 sub prefix_interface_output {
278 my ($self, %options) = @_;
279
280 return sprintf(
281 "interface '%s' ",
282 $options{instance_value}->{name}
283 );
284 }
285
286 sub prefix_autotest_output {
287 my ($self, %options) = @_;
288
289 return sprintf(
290 "autotest '%s' ",
291 $options{instance_value}->{name}
292 );
293 }
294
295 sub prefix_certificate_output {
296 my ($self, %options) = @_;
297
298 return sprintf(
299 "certificate '%s' [subject: %s, issuer: %s, usages: %s] ",
300 $options{instance_value}->{certSn},
301 $options{instance_value}->{subjectCommonName},
302 $options{instance_value}->{issuerCommonName},
303 $options{instance_value}->{usages}
304 );
305 }
306
307 sub prefix_ike_sa_output {
308 my ($self, %options) = @_;
309
310 return sprintf(
311 "vpn ike sa '%s' ",
312 $options{instance_value}->{name}
313 );
314 }
315
316 sub prefix_sa_output {
317 my ($self, %options) = @_;
318
319 return sprintf(
320 "vpn sa '%s' ",
321 $options{instance_value}->{name}
322 );
323 }
324
325 sub set_counters {
326 my ($self, %options) = @_;
327
328 $self->{maps_counters_type} = [
329 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
330 {
331 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',
332 group => [
333 { name => 'system', type => 0, skipped_code => { -10 => 1 } },
334 { name => 'connection', type => 0, skipped_code => { -10 => 1 } },
335 { name => 'mistral', type => 0, skipped_code => { -10 => 1 } },
336 { name => 'autotests', type => 1, cb_prefix_output => 'prefix_autotest_output', message_multiple => 'autotests are ok', display_long => 1, skipped_code => { -10 => 1 } },
337 { name => 'interfaces', type => 1, cb_prefix_output => 'prefix_interface_output', message_multiple => 'interfaces are ok', display_long => 1, skipped_code => { -10 => 1 } },
338 { name => 'certificates', type => 1, cb_prefix_output => 'prefix_certificate_output', message_multiple => 'certificates are ok', display_long => 1, skipped_code => { -10 => 1 } },
339 { name => 'ike_service', type => 0, skipped_code => { -10 => 1 } },
340 { name => 'ike_sa', type => 1, cb_prefix_output => 'prefix_ike_sa_output', message_multiple => 'ike sa are ok', display_long => 1, skipped_code => { -10 => 1 } },
341 { name => 'sa', type => 1, cb_prefix_output => 'prefix_sa_output', message_multiple => 'sa are ok', display_long => 1, skipped_code => { -10 => 1 } },
342 ]
343 }
344 ];
345
346 $self->{maps_counters}->{global} = [
347 { label => 'devices-detected', display_ok => 0, nlabel => 'devices.detected.count', set => {
348 key_values => [ { name => 'detected' } ],
349 output_template => 'detected: %s',
350 perfdatas => [
351 { template => '%s', min => 0 }
352 ]
353 }
354 }
355 ];
356
357 $self->{maps_counters}->{connection} = [
358 {
359 label => 'connection-status',
360 type => 2,
361 unknown_default => '%{connectionStatus} =~ /unknown/i',
362 warning_default => '%{connectionStatus} =~ /disconnected|unpaired/i',
363 set => {
364 key_values => [ { name => 'connectionStatus' }, { name => 'sn' } ],
365 output_template => "connection status: %s",
366 closure_custom_perfdata => sub { return 0; },
367 closure_custom_threshold_check => \&catalog_status_threshold_ng
368 }
369 },
370 { label => 'connection-last-time', nlabel => 'device.connection.last.time', set => {
371 key_values => [ { name => 'connection_seconds' }, { name => 'connection_human' }, { name => 'sn' } ],
372 output_template => 'last connection: %s',
373 output_use => 'connection_human',
374 closure_custom_perfdata => $self->can('custom_connection_perfdata'),
375 closure_custom_threshold_check => $self->can('custom_connection_threshold')
376 }
377 }
378 ];
379
380 $self->{maps_counters}->{mistral} = [
381 {
382 label => 'mistral-version',
383 type => 2,
384 set => {
385 key_values => [ { name => 'firmwareCurrentVersion' }, { name => 'firmwareOtherVersion' }, { name => 'configurationId' } ],
386 closure_custom_output => $self->can('custom_mistral_version_output'),
387 closure_custom_perfdata => sub { return 0; },
388 closure_custom_threshold_check => \&catalog_status_threshold_ng
389 }
390 },
391 {
392 label => 'operating-state',
393 type => 2,
394 critical_default => '%{operatingState} !~ /operating/i',
395 set => {
396 key_values => [ { name => 'operatingState' }, { name => 'sn' } ],
397 output_template => "operating state: %s",
398 closure_custom_perfdata => sub { return 0; },
399 closure_custom_threshold_check => \&catalog_status_threshold_ng
400 }
401 },
402 { label => 'temperature', nlabel => 'device.temperature.celsius', set => {
403 key_values => [ { name => 'temperature' }, { name => 'sn' } ],
404 output_template => 'temperature: %.2f C',
405 closure_custom_perfdata => sub {
406 my ($self, %options) = @_;
407
408 $self->{output}->perfdata_add(
409 nlabel => $self->{nlabel},
410 unit => 'C',
411 instances => $self->{result_values}->{sn},
412 value => $self->{result_values}->{temperature},
413 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
414 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
415 );
416 }
417 }
418 }
419 ];
420
421 $self->{maps_counters}->{autotests} = [
422 {
423 label => 'autotest-state',
424 type => 2,
425 critical_default => '%{state} !~ /success/i',
426 set => {
427 key_values => [ { name => 'state' }, { name => 'name' }, { name => 'sn' } ],
428 output_template => "state: %s",
429 closure_custom_perfdata => sub { return 0; },
430 closure_custom_threshold_check => \&catalog_status_threshold_ng
431 }
432 }
433 ];
434
435 $self->{maps_counters}->{system} = [
436 {
437 label => 'system-version',
438 type => 2,
439 set => {
440 key_values => [ { name => 'osName' }, { name => 'osRelease' } ],
441 closure_custom_output => $self->can('custom_system_version_output'),
442 closure_custom_perfdata => sub { return 0; },
443 closure_custom_threshold_check => \&catalog_status_threshold_ng
444 }
445 },
446 { label => 'system-uptime', set => {
447 key_values => [ { name => 'uptime' }, { name => 'sn' } ],
448 closure_custom_output => $self->can('custom_uptime_output'),
449 closure_custom_perfdata => $self->can('custom_uptime_perfdata'),
450 closure_custom_threshold_check => $self->can('custom_uptime_threshold')
451 }
452 },
453 { label => 'system-time-offset', nlabel => 'system.time.offset.seconds', set => {
454 key_values => [ { name => 'offset' }, { name => 'date' }, { name => 'sn' } ],
455 closure_custom_output => $self->can('custom_time_offset_output'),
456 closure_custom_perfdata => sub {
457 my ($self, %options) = @_;
458
459 $self->{output}->perfdata_add(
460 nlabel => $self->{nlabel},
461 unit => 's',
462 instances => $self->{result_values}->{sn},
463 value => $self->{result_values}->{offset},
464 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
465 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
466 );
467 }
468 }
469 }
470 ];
471
472 $self->{maps_counters}->{interfaces} = [
473 {
474 label => 'interface-status',
475 type => 2,
476 warning_default => '%{operatingStatus} !~ /up/i',
477 set => {
478 key_values => [ { name => 'operatingStatus' }, { name => 'name' }, { name => 'sn' } ],
479 output_template => "operating status: %s",
480 closure_custom_perfdata => sub { return 0; },
481 closure_custom_threshold_check => \&catalog_status_threshold_ng
482 }
483 },
484 { label => 'interface-traffic-in', nlabel => 'interface.traffic.in.bitspersecond', set => {
485 key_values => [ { name => 'in', diff => 1 }, { name => 'speed_in' }, { name => 'name' }, { name => 'sn' } ],
486 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' },
487 closure_custom_output => $self->can('custom_traffic_output'),
488 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
489 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
490 }
491 },
492 { label => 'interface-traffic-out', nlabel => 'interface.traffic.out.bitspersecond', set => {
493 key_values => [ { name => 'out', diff => 1 }, { name => 'speed_out' }, { name => 'name' }, { name => 'sn' } ],
494 closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' },
495 closure_custom_output => $self->can('custom_traffic_output'),
496 closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
497 closure_custom_threshold_check => $self->can('custom_traffic_threshold')
498 }
499 }
500 ];
501
502 $self->{maps_counters}->{certificates} = [
503 {
504 label => 'certificate-status',
505 type => 2,
506 set => {
507 key_values => [
508 { name => 'revoked' },
509 { name => 'certSn' }, { name => 'subjectCommonName' }, { name => 'issuerCommonName' },
510 { name => 'sn' }
511 ],
512 closure_custom_output => $self->can('custom_certificate_status_output'),
513 closure_custom_perfdata => sub { return 0; },
514 closure_custom_threshold_check => \&catalog_status_threshold_ng
515 }
516 },
517 { label => 'certificate-expires', nlabel => 'certificate.expires', set => {
518 key_values => [
519 { name => 'expires_seconds' }, { name => 'expires_human' },
520 { name => 'certSn' }, { name => 'subjectCommonName' }, { name => 'issuerCommonName' },
521 { name => 'sn' }
522 ],
523 output_template => 'expires in %s',
524 output_use => 'expires_human',
525 closure_custom_perfdata => $self->can('custom_certificate_expires_perfdata'),
526 closure_custom_threshold_check => $self->can('custom_certificate_expires_threshold')
527 }
528 }
529 ];
530
531 $self->{maps_counters}->{ike_service} = [
532 {
533 label => 'vpn-ike-service-state',
534 type => 2,
535 critical_default => '%{state} =~ /stopped/i',
536 set => {
537 key_values => [ { name => 'state' }, { name => 'sn' } ],
538 output_template => 'vpn ike service state: %s',
539 closure_custom_perfdata => sub { return 0; },
540 closure_custom_threshold_check => \&catalog_status_threshold_ng
541 }
542 }
543 ];
544
545 $self->{maps_counters}->{ike_sa} = [
546 {
547 label => 'vpn-ike-sa-state',
548 type => 2,
549 critical_default => '%{state} =~ /down/i',
550 set => {
551 key_values => [ { name => 'state' }, { name => 'name' }, { name => 'sn' } ],
552 output_template => 'vpn state: %s',
553 closure_custom_perfdata => sub { return 0; },
554 closure_custom_threshold_check => \&catalog_status_threshold_ng
555 }
556 }
557 ];
558
559 $self->{maps_counters}->{sa} = [
560 {
561 label => 'vpn-sa-state',
562 type => 2,
563 critical_default => '%{state} =~ /down/i',
564 set => {
565 key_values => [ { name => 'state' }, { name => 'name' }, { name => 'sn' } ],
566 output_template => 'state: %s',
567 closure_custom_perfdata => sub { return 0; },
568 closure_custom_threshold_check => \&catalog_status_threshold_ng
569 }
570 },
571 { label => 'vpn-sa-traffic', nlabel => 'vpn.sa.traffic.bitspersecond', set => {
572 key_values => [ { name => 'traffic', per_second => 1 }, { name => 'name' }, { name => 'sn' } ],
573 output_template => 'traffic: %s %s/s',
574 output_change_bytes => 2,
575 closure_custom_perfdata => sub {
576 my ($self, %options) = @_;
577
578 $self->{output}->perfdata_add(
579 nlabel => $self->{nlabel},
580 unit => 'b/s',
581 instances => [$self->{result_values}->{sn}, $self->{result_values}->{name}],
582 value => $self->{result_values}->{traffic},
583 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
584 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
585 min => 0
586 );
587 }
588 }
589 }
590 ];
591 }
592
593 sub new {
594 my ($class, %options) = @_;
595 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
596 bless $self, $class;
597
598 $options{options}->add_options(arguments => {
599 'filter-id:s' => { name => 'filter_id' },
600 'filter-sn:s' => { name => 'filter_sn' },
601 'filter-cert-revoked' => { name => 'filter_cert_revoked' },
602 'add-interfaces' => { name => 'add_interfaces' },
603 'add-status' => { name => 'add_status' },
604 'add-system' => { name => 'add_system' },
605 'add-mistral' => { name => 'add_mistral' },
606 'add-certificates' => { name => 'add_certificates' },
607 'add-tunnels' => { name => 'add_tunnels' },
608 'time-connection-unit:s' => { name => 'time_connection_unit', default => 's' },
609 'time-uptime-unit:s' => { name => 'time_uptime_unit', default => 's' },
610 'time-certificate-unit:s' => { name => 'time_certificate_unit', default => 's' },
611 'traffic-unit:s' => { name => 'traffic_unit', default => 'percent_delta' },
612 'speed:s' => { name => 'speed' },
613 'ntp-hostname:s' => { name => 'ntp_hostname' },
614 'ntp-port:s' => { name => 'ntp_port', default => 123 }
615 });
616
617 return $self;
618 }
619
620 sub check_options {
621 my ($self, %options) = @_;
622 $self->SUPER::check_options(%options);
623
624 $self->{checking} = '';
625 my $selected = 0;
626 foreach ('status', 'interfaces', 'tunnels', 'mistral', 'system', 'certificates') {
627 if (defined($self->{option_results}->{'add_' . $_})) {
628 $selected = 1;
629 $self->{checking} .= $_;
630 }
631 }
632 if ($selected == 0) {
633 $self->{option_results}->{add_status} = 1;
634 }
635
636 if ($self->{option_results}->{time_connection_unit} eq '' || !defined($unitdiv->{$self->{option_results}->{time_connection_unit}})) {
637 $self->{option_results}->{time_connection_unit} = 's';
638 }
639 if ($self->{option_results}->{time_uptime_unit} eq '' || !defined($unitdiv->{$self->{option_results}->{time_uptime_unit}})) {
640 $self->{option_results}->{time_uptime_unit} = 's';
641 }
642 if ($self->{option_results}->{time_certificate_unit} eq '' || !defined($unitdiv->{$self->{option_results}->{time_certificate_unit}})) {
643 $self->{option_results}->{time_certificate_unit} = 's';
644 }
645
646 if (defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '') {
647 if ($self->{option_results}->{speed} !~ /^[0-9]+(\.[0-9]+){0,1}$/) {
648 $self->{output}->add_option_msg(short_msg => "Speed must be a positive number '" . $self->{option_results}->{speed} . "' (can be a float also)");
649 $self->{output}->option_exit();
650 } else {
651 $self->{option_results}->{speed} *= 1000000;
652 }
653 }
654
655 $self->{option_results}->{traffic_unit} = 'percent_delta'
656 if (!defined($self->{option_results}->{traffic_unit}) ||
657 $self->{option_results}->{traffic_unit} eq '' ||
658 $self->{option_results}->{traffic_unit} eq '%');
659 if ($self->{option_results}->{traffic_unit} !~ /^(?:percent|percent_delta|bps|counter)$/) {
660 $self->{output}->add_option_msg(short_msg => 'Wrong option --traffic-unit');
661 $self->{output}->option_exit();
662 }
663
664 if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
665 centreon::plugins::misc::mymodule_load(
666 output => $self->{output}, module => 'Net::NTP',
667 error_msg => "Cannot load module 'Net::NTP'."
668 );
669 }
670 }
671
672 sub add_interfaces {
673 my ($self, %options) = @_;
674
675 $self->{devices}->{ $options{device}->{id} }->{interfaces} = {};
676
677 my $interfaces = $options{custom}->request_api(endpoint => '/ssIpsecGwHws/' . $options{device}->{id} . '/interfacesStatistics');
678 foreach my $interface (@{$interfaces->{listInterfaces}}) {
679 $self->{devices}->{ $options{device}->{id} }->{interfaces}->{ $interface->{name} } = {
680 name => $interface->{name},
681 sn => $options{device}->{serialNumber},
682 operatingStatus => $interface->{operatingStatus},
683 in => $interface->{interfaceStats}->{inOctets} * 8,
684 out => $interface->{interfaceStats}->{outOctets} * 8,
685 speed_in => defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '' ? $self->{option_results}->{speed} : $interface->{speed},
686 speed_out => defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '' ? $self->{option_results}->{speed} : $interface->{speed}
687 };
688 }
689 }
690
691 sub add_system {
692 my ($self, %options) = @_;
693
694 my $system = $options{custom}->request_api(endpoint => '/ssIpsecGwHws/' . $options{device}->{id} . '/systemStateStatistics');
695 $self->{devices}->{ $options{device}->{id} }->{system} = {
696 sn => $options{device}->{serialNumber},
697 osName => $system->{platform}->{osName},
698 osRelease => $system->{platform}->{osRelease}
699 };
700
701 if ($system->{clock}->{bootDatetime} =~ /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+([+-].*)$/) {
702 my $dt = DateTime->new(
703 year => $1,
704 month => $2,
705 day => $3,
706 hour => $4,
707 minute => $5,
708 second => $6,
709 time_zone => $7
710 );
711 $self->{devices}->{ $options{device}->{id} }->{system}->{uptime} = time() - $dt->epoch();
712 }
713
714 if ($system->{clock}->{currentDatetime} =~ /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+([+-].*)$/) {
715 my $ref_time;
716 if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
717 my %ntp;
718
719 eval {
720 %ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port});
721 };
722 if ($@) {
723 $self->{output}->add_option_msg(short_msg => "Couldn't connect to ntp server: " . $@);
724 $self->{output}->option_exit();
725 }
726
727 $ref_time = $ntp{'Transmit Timestamp'};
728 } else {
729 $ref_time = time();
730 }
731
732 my $timezone = $7;
733 my $dt = DateTime->new(
734 year => $1,
735 month => $2,
736 day => $3,
737 hour => $4,
738 minute => $5,
739 second => $6,
740 time_zone => $timezone
741 );
742 my $remote_date_formated = sprintf(
743 'local time: %02d-%02d-%02dT%02d:%02d:%02d (%s)',
744 $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second, $timezone
745 );
746
747 my $offset = $dt->epoch() - $ref_time;
748
749 $self->{devices}->{ $options{device}->{id} }->{system}->{offset} = sprintf('%d', $offset);
750 $self->{devices}->{ $options{device}->{id} }->{system}->{date} = $remote_date_formated;
751 }
752 }
753
754 sub add_mistral {
755 my ($self, %options) = @_;
756
757 my $mistral = $options{custom}->request_api(endpoint => '/ssIpsecGwHws/' . $options{device}->{id} . '/mistralStateStatistics');
758 $self->{devices}->{ $options{device}->{id} }->{mistral} = {
759 sn => $options{device}->{serialNumber},
760 firmwareCurrentVersion => $mistral->{firmwareCurrent}->{version},
761 firmwareOtherVersion => $mistral->{firmwareOther}->{version},
762 configurationId => $mistral->{configurationId},
763 operatingState => lc($mistral->{operatingState}),
764 temperature => $mistral->{temperature}
765 };
766
767 $self->{devices}->{ $options{device}->{id} }->{autotests} = {};
768 foreach (@{$mistral->{autotestStates}}) {
769 $self->{devices}->{ $options{device}->{id} }->{autotests}->{ name => $_->{name} } = {
770 sn => $options{device}->{serialNumber},
771 name => $_->{name},
772 state => lc($_->{state})
773 };
774 }
775 }
776
777 sub add_certificates {
778 my ($self, %options) = @_;
779
780 $self->{devices}->{ $options{device}->{id} }->{certificates} = {};
781 foreach my $cert (@{$options{device}->{certificates}}) {
782 if ($cert->{validityPeriodEnd} =~ /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+([+-].*)$/) {
783 my $dt = DateTime->new(
784 year => $1,
785 month => $2,
786 day => $3,
787 hour => $4,
788 minute => $5,
789 second => $6,
790 time_zone => $7
791 );
792
793 my $revoked = $cert->{revoked} =~ /true|1/i ? 'yes' : 'no';
794 next if (defined($self->{option_results}->{filter_cert_revoked}) && $revoked eq 'yes');
795
796 $self->{devices}->{ $options{device}->{id} }->{certificates}->{ $cert->{gwCertificateName} } = {
797 sn => $options{device}->{serialNumber},
798 certSn => $cert->{serialNumber},
799 subjectCommonName => $cert->{subjectCommonName},
800 issuerCommonName => defined($cert->{issuerCommonName}) ? $cert->{issuerCommonName} : '',
801 usages => join(' ', @{$cert->{usages}}),
802 revoked => $revoked,
803 expires_seconds => $dt->epoch() - time()
804 };
805 $self->{devices}->{ $options{device}->{id} }->{certificates}->{ $cert->{gwCertificateName} }->{expires_seconds} = 0
806 if ($self->{devices}->{ $options{device}->{id} }->{certificates}->{ $cert->{gwCertificateName} }->{expires_seconds} < 0);
807 $self->{devices}->{ $options{device}->{id} }->{certificates}->{ $cert->{gwCertificateName} }->{expires_human} = centreon::plugins::misc::change_seconds(
808 value => $self->{devices}->{ $options{device}->{id} }->{certificates}->{ $cert->{gwCertificateName} }->{expires_seconds}
809 );
810 }
811 }
812 }
813
814 sub add_tunnels {
815 my ($self, %options) = @_;
816
817 my $tunnels = $options{custom}->request_api(endpoint => '/ssIpsecGwHws/' . $options{device}->{id} . '/vpnStatistics');
818 $self->{devices}->{ $options{device}->{id} }->{ike_service} = {
819 sn => $options{device}->{serialNumber},
820 state => lc($tunnels->{ikev2State}->{serviceState})
821 };
822
823 $self->{devices}->{ $options{device}->{id} }->{ike_sa} = {};
824 foreach (@{$tunnels->{ikev2State}->{ikeSaState}}) {
825 $self->{devices}->{ $options{device}->{id} }->{ike_sa}->{ $_->{name} } = {
826 sn => $options{device}->{serialNumber},
827 name => $_->{name},
828 state => lc($_->{saState})
829 };
830 }
831
832 $self->{devices}->{ $options{device}->{id} }->{sa} = {};
833 foreach my $sa (@{$tunnels->{sadState}->{listSaName}}) {
834 foreach my $saState (@{$sa->{listSaState}}) {
835 my $name = $sa->{name};
836
837 foreach my $spd (@{$tunnels->{spdState}->{listSpState}}) {
838 if ($saState->{spi} eq $spd->{spi}) {
839 $name .= '.' . lc($spd->{direction}) . '(src:' . $spd->{sourceTs}->{ipAddress} . ',dst:' . $spd->{destinationTs}->{ipAddress} . ')';
840 last;
841 }
842 }
843
844 $self->{devices}->{ $options{device}->{id} }->{sa}->{$name} = {
845 sn => $options{device}->{serialNumber},
846 name => $name,
847 state => lc($saState->{state}),
848 traffic => $saState->{lifetime}->{byteCurrent} * 1000
849 };
850 }
851 }
852 }
853
854 sub manage_selection {
855 my ($self, %options) = @_;
856
857 my $inventory = $options{custom}->get_gateway_inventory();
858
859 $self->{global} = { detected => 0 };
860 $self->{devices} = {};
861 foreach my $device (@$inventory) {
862 next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
863 $device->{id} !~ /$self->{option_results}->{filter_id}/);
864 next if (defined($self->{option_results}->{filter_sn}) && $self->{option_results}->{filter_sn} ne '' &&
865 $device->{serialNumber} !~ /$self->{option_results}->{filter_sn}/);
866
867 $self->{global}->{detected}++;
868
869 $self->{devices}->{ $device->{id} } = {
870 sn => $device->{serialNumber},
871 name => defined($device->{name}) ? $device->{name} : ''
872 };
873 if (defined($self->{option_results}->{add_status})) {
874 $self->{devices}->{ $device->{id} }->{connection} = {
875 sn => $device->{serialNumber},
876 connectionStatus => defined($device->{status}) ? lc($device->{status}->{connectedStatus}) : 'unpaired'
877 };
878 if (defined($device->{status})) {
879 $self->{devices}->{ $device->{id} }->{connection}->{connection_seconds} = time() - ($device->{status}->{statusEpochMilli} / 1000);
880 $self->{devices}->{ $device->{id} }->{connection}->{connection_human} = centreon::plugins::misc::change_seconds(
881 value => $self->{devices}->{ $device->{id} }->{connection}->{connection_seconds}
882 );
883 }
884 }
885
886 $self->add_interfaces(custom => $options{custom}, device => $device)
887 if (defined($self->{option_results}->{add_interfaces}));
888
889 $self->add_system(custom => $options{custom}, device => $device)
890 if (defined($self->{option_results}->{add_system}));
891
892 $self->add_mistral(custom => $options{custom}, device => $device)
893 if (defined($self->{option_results}->{add_mistral}));
894
895 $self->add_certificates(custom => $options{custom}, device => $device)
896 if (defined($self->{option_results}->{add_certificates}));
897
898 $self->add_tunnels(custom => $options{custom}, device => $device)
899 if (defined($self->{option_results}->{add_tunnels}));
900 }
901
902 $self->{cache_name} = 'thales_mistral_' . $options{custom}->get_connection_info() . '_' . $self->{mode} . '_' .
903 Digest::MD5::md5_hex(
904 (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
905 (defined($self->{option_results}->{filter_id}) ? $self->{option_results}->{filter_id} : '') . '_' .
906 (defined($self->{option_results}->{filter_sn}) ? $self->{option_results}->{filter_sn} : '') . '_' .
907 $self->{checking}
908 );
909 }
910
911 1;
912
913 __END__
914
915 =head1 MODE
916
917 Check devices.
918
919 =over 8
920
921 =item B<--filter-id>
922
923 Filter devices by id.
924
925 =item B<--filter-sn>
926
927 Filter devices by serial number.
928
929 =item B<--filter-cert-revoked>
930
931 Skip revoked certificates.
932
933 =item B<--add-status>
934
935 Check connection status.
936
937 =item B<--add-interfaces>
938
939 Check interfaces.
940
941 =item B<--add-system>
942
943 Check system.
944
945 =item B<--add-mistral>
946
947 Check mistral (operating status, temperature, autotests).
948
949 =item B<--add-certificates>
950
951 Check certificates.
952
953 =item B<--add-tunnels>
954
955 Check tunnels.
956
957 =item B<--unknown-certificate-status>
958
959 Define the conditions to match for the status to be UNKNOWN.
960 You can use the following variables: %{revoked}, %{sn}, %{certSn}, %{subjectCommonName}, %{issuerCommonName}
961
962 =item B<--warning-certificate-status>
963
964 Define the conditions to match for the status to be WARNING.
965 You can use the following variables: %{revoked}, %{sn}, %{certSn}, %{subjectCommonName}, %{issuerCommonName}
966
967 =item B<--critical-certificate-status>
968
969 Define the conditions to match for the status to be CRITICAL.
970 You can use the following variables: %{revoked}, %{sn}, %{certSn}, %{subjectCommonName}, %{issuerCommonName}
971
972 =item B<--unknown-connection-status>
973
974 Define the conditions to match for the status to be UNKNOWN (default: '%{connectionStatus} =~ /unknown/i').
975 You can use the following variables: %{sn}, %{connectionStatus}
976
977 =item B<--warning-connection-status>
978
979 Define the conditions to match for the status to be WARNING (default: '%{connectionStatus} =~ /disconnected|unpaired/i').
980 You can use the following variables: %{sn}, %{connectionStatus}
981
982 =item B<--critical-connection-status>
983
984 Define the conditions to match for the status to be CRITICAL.
985 You can use the following variables: %{sn}, %{connectionStatus}
986
987 =item B<--unknown-operating-state>
988
989 Define the conditions to match for the status to be UNKNOWN.
990 You can use the following variables: %{sn}, %{operatingState}
991
992 =item B<--warning-operating-state>
993
994 Define the conditions to match for the status to be WARNING.
995 You can use the following variables: %{sn}, %{operatingState}
996
997 =item B<--critical-operating-state>
998
999 Define the conditions to match for the status to be CRITICAL (default: '%{operatingState} !~ /operating/i').
1000 You can use the following variables: %{sn}, %{operatingState}
1001
1002 =item B<--unknown-autotest-state>
1003
1004 Define the conditions to match for the status to be UNKNOWN.
1005 You can use the following variables: %{sn}, %{name}, %{state}
1006
1007 =item B<--warning-autotest-state>
1008
1009 Define the conditions to match for the status to be WARNING.
1010 You can use the following variables: %{sn}, %{name}, %{state}
1011
1012 =item B<--critical-autotest-state>
1013
1014 Define the conditions to match for the status to be CRITICAL (default: '%{state} !~ /success/i').
1015 You can use the following variables: %{sn}, %{name}, %{state}
1016
1017 =item B<--unknown-interface-status>
1018
1019 Define the conditions to match for the status to be UNKNOWN.
1020 You can use the following variables: %{sn}, %{name}, %{operatingStatus}
1021
1022 =item B<--warning-interface-status>
1023
1024 Define the conditions to match for the status to be WARNING.
1025 You can use the following variables: %{sn}, %{name}, %{operatingStatus}
1026
1027 =item B<--critical-interface-status>
1028
1029 Define the conditions to match for the status to be CRITICAL (default: '%{operatingStatus} !~ /up/i').
1030 You can use the following variables: %{sn}, %{name}, %{operatingStatus}
1031
1032 =item B<--unknown-vpn-ike-service-state>
1033
1034 Define the conditions to match for the status to be UNKNOWN.
1035 You can use the following variables: %{sn}, %{state}
1036
1037 =item B<--warning-vpn-ike-service-state>
1038
1039 Define the conditions to match for the status to be WARNING.
1040 You can use the following variables: %{sn}, %{state}
1041
1042 =item B<--critical-vpn-ike-service-state>
1043
1044 Define the conditions to match for the status to be CRITICAL (default: '%{state} =~ /stopped/i').
1045 You can use the following variables: %{sn}, %{state}
1046
1047 =item B<--unknown-vpn-ike-sa-state>
1048
1049 Define the conditions to match for the status to be UNKNOWN.
1050 You can use the following variables: %{sn}, %{name}, %{state}
1051
1052 =item B<--warning-vpn-ike-sa-state>
1053
1054 Define the conditions to match for the status to be WARNING.
1055 You can use the following variables: %{sn}, %{name}, %{state}
1056
1057 =item B<--critical-vpn-ike-sa-state>
1058
1059 Define the conditions to match for the status to be CRITICAL (default: '%{state} =~ /down/i').
1060 You can use the following variables: %{sn}, %{name}, %{state}
1061
1062 =item B<--unknown-vpn-sa-state>
1063
1064 Define the conditions to match for the status to be UNKNOWN.
1065 You can use the following variables: %{sn}, %{name}, %{state}
1066
1067 =item B<--warning-vpn-sa-state>
1068
1069 Define the conditions to match for the status to be WARNING.
1070 You can use the following variables: %{sn}, %{name}, %{state}
1071
1072 =item B<--critical-vpn-sa-state>
1073
1074 Define the conditions to match for the status to be CRITICAL (default: '%{state} =~ /down/i').
1075 You can use the following variables: %{sn}, %{name}, %{state}
1076
1077 =item B<--ntp-hostname>
1078
1079 Set the ntp hostname (if not set, localtime is used).
1080
1081 =item B<--ntp-port>
1082
1083 Set the ntp port (default: 123).
1084
1085 =item B<--time-connection-unit>
1086
1087 Select the time unit for connection threshold. May be 's' for seconds, 'm' for minutes,
1088 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
1089
1090 =item B<--time-uptime-unit>
1091
1092 Select the time unit for uptime threshold. May be 's' for seconds, 'm' for minutes,
1093 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
1094
1095 =item B<--time-certificate-unit>
1096
1097 Select the time unit for certificate threshold. May be 's' for seconds, 'm' for minutes,
1098 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
1099
1100 =item B<--traffic-unit>
1101
1102 Units of thresholds for the traffic (default: 'percent_delta') ('percent_delta', 'bps', 'counter').
1103
1104 =item B<--speed>
1105
1106 Set interface speed (in Mb).
1107
1108 =item B<--warning-*> B<--critical-*>
1109
1110 Thresholds.
1111 Can be: 'devices-detected', 'connection-last-time',
1112 'interface-traffic-in', 'interface-traffic-out',
1113 'system-uptime', 'system-time-offset', temperature',
1114 'certificate-expires', 'vpn-sa-traffic'.
1115
1116 =back
1117
1118 =cut