Debian Codegraph @

src/hardware/devices/cisco/ces/restapi/mode/certificates.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 hardware::devices::cisco::ces::restapi::mode::certificates;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub custom_validity_output {
30 my ($self, %options) = @_;
31
32 return sprintf(
33 'expires in %s',
34 $self->{result_values}->{generation_time}
35 );
36 }
37
38 sub set_counters {
39 my ($self, %options) = @_;
40
41 $self->{maps_counters_type} = [
42 { name => 'certificates', type => 1, cb_prefix_output => 'prefix_certificate_output', message_multiple => 'All certificates are ok', skipped_code => { -10 => 1 } }
43 ];
44
45 $self->{maps_counters}->{certificates} = [
46 { label => 'certificate-expire', nlabel => 'system.certificate.expire.seconds', set => {
47 key_values => [ { name => 'validity_time' }, { name => 'generation_time' } ],
48 closure_custom_output => $self->can('custom_validity_output'),
49 perfdatas => [
50 { template => '%d', min => 0, unit => 's' }
51 ]
52 }
53 }
54 ];
55 }
56
57 sub prefix_certificate_output {
58 my ($self, %options) = @_;
59
60 return "Certificate '" . $options{instance_value}->{display} . "' ";
61 }
62
63 sub new {
64 my ($class, %options) = @_;
65 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
66 bless $self, $class;
67
68 $self->{version} = '1.0';
69 $options{options}->add_options(arguments => {
70 });
71
72 centreon::plugins::misc::mymodule_load(
73 output => $self->{output},
74 module => 'Date::Parse',
75 error_msg => "Cannot load module 'Date::Parse'."
76 );
77 return $self;
78 }
79
80 sub manage_selection {
81 my ($self, %options) = @_;
82
83 my $result = $options{custom}->request_api(
84 method => 'POST',
85 url_path => '/putxml',
86 query_form_post => '<Command><Security><Certificates><Services><Show/></Services></Certificates></Security></Command>',
87 ForceArray => ['Details']
88 );
89
90 $self->{certificates} = {};
91 if (defined($result->{ServicesShowResult}->{Details})) {
92 foreach (@{$result->{ServicesShowResult}->{Details}}) {
93 my $end_date = Date::Parse::str2time($_->{notAfter});
94 if (!defined($end_date)) {
95 $self->{output}->output_add(
96 severity => 'UNKNOWN',
97 short_msg => "can't parse date '" . $_->{notAfter} . "'"
98 );
99 next;
100 }
101
102 $self->{certificates}->{$_->{item}} = {
103 display => $_->{SubjectName},
104 validity_time => $end_date - time(),
105 generation_time => centreon::plugins::misc::change_seconds(value => $end_date - time())
106 };
107 }
108 }
109
110 if (scalar(keys %{$self->{certificates}}) <= 0) {
111 $self->{output}->add_option_msg(short_msg => 'No certificate found.');
112 $self->{output}->option_exit();
113 }
114 }
115
116 1;
117
118 __END__
119
120 =head1 MODE
121
122 Check certificates validity (since CE 9.2)
123
124 =over 8
125
126 =item B<--warning-*> B<--critical-*>
127
128 Thresholds.
129 Can be: 'certificate-expire'.
130
131 =back
132
133 =cut