| 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::mmccertificates; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use DateTime; |
| 28 |
use POSIX; |
| 29 |
use centreon::plugins::misc; |
| 30 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 31 |
|
| 32 |
my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 }; |
| 33 |
my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' }; |
| 34 |
|
| 35 |
sub custom_certificate_status_output { |
| 36 |
my ($self, %options) = @_; |
| 37 |
|
| 38 |
return sprintf( |
| 39 |
'active: %s [revoked: %s]', |
| 40 |
$self->{result_values}->{active}, |
| 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}->{subjectCommonName}, |
| 54 |
$self->{result_values}->{issuerCommonName} |
| 55 |
], |
| 56 |
value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_certificate_unit} }), |
| 57 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 58 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 59 |
min => 0 |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
sub custom_certificate_expires_threshold { |
| 64 |
my ($self, %options) = @_; |
| 65 |
|
| 66 |
return $self->{perfdata}->threshold_check( |
| 67 |
value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_certificate_unit} }), |
| 68 |
threshold => [ |
| 69 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 70 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }, |
| 71 |
{ label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' } |
| 72 |
] |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
sub prefix_certificate_output { |
| 77 |
my ($self, %options) = @_; |
| 78 |
|
| 79 |
return sprintf( |
| 80 |
"certificate '%s' [subject: %s, issuer: %s, usages: %s] ", |
| 81 |
$options{instance_value}->{sn}, |
| 82 |
$options{instance_value}->{subjectCommonName}, |
| 83 |
$options{instance_value}->{issuerCommonName}, |
| 84 |
$options{instance_value}->{usages} |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
sub set_counters { |
| 89 |
my ($self, %options) = @_; |
| 90 |
|
| 91 |
$self->{maps_counters_type} = [ |
| 92 |
{ name => 'certificates', type => 1, cb_prefix_output => 'prefix_certificate_output', message_multiple => 'all certificates are ok', skipped_code => { -10 => 1 } } |
| 93 |
]; |
| 94 |
|
| 95 |
$self->{maps_counters}->{certificates} = [ |
| 96 |
{ |
| 97 |
label => 'certificate-status', |
| 98 |
type => 2, |
| 99 |
set => { |
| 100 |
key_values => [ |
| 101 |
{ name => 'active' }, { name => 'revoked' }, |
| 102 |
{ name => 'sn' }, { name => 'subjectCommonName' }, { name => 'issuerCommonName' } |
| 103 |
], |
| 104 |
closure_custom_output => $self->can('custom_certificate_status_output'), |
| 105 |
closure_custom_perfdata => sub { return 0; }, |
| 106 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 107 |
} |
| 108 |
}, |
| 109 |
{ label => 'certificate-expires', nlabel => 'certificate.expires', set => { |
| 110 |
key_values => [ |
| 111 |
{ name => 'expires_seconds' }, { name => 'expires_human' }, |
| 112 |
{ name => 'sn' }, { name => 'subjectCommonName' }, { name => 'issuerCommonName' } |
| 113 |
], |
| 114 |
output_template => 'expires in %s', |
| 115 |
output_use => 'expires_human', |
| 116 |
closure_custom_perfdata => $self->can('custom_certificate_expires_perfdata'), |
| 117 |
closure_custom_threshold_check => $self->can('custom_certificate_expires_threshold') |
| 118 |
} |
| 119 |
} |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
sub new { |
| 124 |
my ($class, %options) = @_; |
| 125 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 126 |
bless $self, $class; |
| 127 |
|
| 128 |
$options{options}->add_options(arguments => { |
| 129 |
'time-certificate-unit:s' => { name => 'time_certificate_unit', default => 's' }, |
| 130 |
'filter-cert-inactive' => { name => 'filter_cert_inactive' }, |
| 131 |
'filter-cert-revoked' => { name => 'filter_cert_revoked' } |
| 132 |
}); |
| 133 |
|
| 134 |
return $self; |
| 135 |
} |
| 136 |
|
| 137 |
sub check_options { |
| 138 |
my ($self, %options) = @_; |
| 139 |
$self->SUPER::check_options(%options); |
| 140 |
|
| 141 |
if ($self->{option_results}->{time_certificate_unit} eq '' || !defined($unitdiv->{$self->{option_results}->{time_certificate_unit}})) { |
| 142 |
$self->{option_results}->{time_certificate_unit} = 's'; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
sub add_certificate { |
| 147 |
my ($self, %options) = @_; |
| 148 |
|
| 149 |
return if ($options{cert}->{validityPeriodEnd} !~ /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+([+-].*)$/); |
| 150 |
|
| 151 |
my $dt = DateTime->new( |
| 152 |
year => $1, |
| 153 |
month => $2, |
| 154 |
day => $3, |
| 155 |
hour => $4, |
| 156 |
minute => $5, |
| 157 |
second => $6, |
| 158 |
time_zone => $7 |
| 159 |
); |
| 160 |
|
| 161 |
my $revoked; |
| 162 |
if (defined($options{cert}->{revoked})) { |
| 163 |
$revoked = $options{cert}->{revoked} =~ /true|1/i ? 'yes' : 'no'; |
| 164 |
return if (defined($self->{option_results}->{filter_cert_revoked}) && $revoked eq 'yes'); |
| 165 |
} |
| 166 |
|
| 167 |
my $active; |
| 168 |
if (defined($options{cert}->{active})) { |
| 169 |
$active = $options{cert}->{active} =~ /true|1/i ? 'yes' : 'no'; |
| 170 |
return if (defined($self->{option_results}->{filter_cert_inactive}) && $active eq 'no'); |
| 171 |
} |
| 172 |
|
| 173 |
$self->{certificates}->{ $options{cert}->{serialNumber} } = { |
| 174 |
sn => $options{cert}->{serialNumber}, |
| 175 |
subjectCommonName => $options{cert}->{subjectCommonName}, |
| 176 |
issuerCommonName => defined($options{cert}->{issuerCommonName}) ? $options{cert}->{issuerCommonName} : '', |
| 177 |
usages => join(' ', @{$options{cert}->{usages}}), |
| 178 |
active => $active, |
| 179 |
revoked => $revoked, |
| 180 |
expires_seconds => $dt->epoch() - time() |
| 181 |
}; |
| 182 |
$self->{certificates}->{ $options{cert}->{serialNumber} }->{expires_seconds} = 0 |
| 183 |
if ($self->{certificates}->{ $options{cert}->{serialNumber} }->{expires_seconds} < 0); |
| 184 |
$self->{certificates}->{ $options{cert}->{serialNumber} }->{expires_human} = centreon::plugins::misc::change_seconds( |
| 185 |
value => $self->{certificates}->{ $options{cert}->{serialNumber} }->{expires_seconds} |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
sub manage_selection { |
| 190 |
my ($self, %options) = @_; |
| 191 |
|
| 192 |
my $certificates = $options{custom}->request_api( |
| 193 |
endpoint => '/certificateCas', |
| 194 |
get_param => ['projection=withSignedCertificates'] |
| 195 |
); |
| 196 |
|
| 197 |
$self->{certificates} = {}; |
| 198 |
foreach my $cert (@{$certificates->{content}}) { |
| 199 |
$self->add_certificate(cert => $cert); |
| 200 |
foreach my $mmc (@{$cert->{certificatesMmc}}) { |
| 201 |
$self->add_certificate(cert => $mmc); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
1; |
| 207 |
|
| 208 |
__END__ |
| 209 |
|
| 210 |
=head1 MODE |
| 211 |
|
| 212 |
Check certificates. |
| 213 |
|
| 214 |
=over 8 |
| 215 |
|
| 216 |
=item B<--time-certificate-unit> |
| 217 |
|
| 218 |
Select the time unit for certificate threshold. May be 's' for seconds, 'm' for minutes, |
| 219 |
'h' for hours, 'd' for days, 'w' for weeks. Default is seconds. |
| 220 |
|
| 221 |
=item B<--filter-cert-inactive> |
| 222 |
|
| 223 |
Skip inactive certificates. |
| 224 |
|
| 225 |
=item B<--filter-cert-revoked> |
| 226 |
|
| 227 |
Skip revoked certificates. |
| 228 |
|
| 229 |
=item B<--unknown-certificate-status> |
| 230 |
|
| 231 |
Define the conditions to match for the status to be UNKNOWN. |
| 232 |
You can use the following variables: %{active}, %{revoked}, %{sn}, %{subjectCommonName}, %{issuerCommonName} |
| 233 |
|
| 234 |
=item B<--warning-certificate-status> |
| 235 |
|
| 236 |
Define the conditions to match for the status to be WARNING. |
| 237 |
You can use the following variables: %{active}, %{revoked}, %{sn}, %{subjectCommonName}, %{issuerCommonName} |
| 238 |
|
| 239 |
=item B<--critical-certificate-status> |
| 240 |
|
| 241 |
Define the conditions to match for the status to be CRITICAL. |
| 242 |
You can use the following variables: %{active}, %{revoked}, %{sn}, %{subjectCommonName}, %{issuerCommonName} |
| 243 |
|
| 244 |
=item B<--warning-*> B<--critical-*> |
| 245 |
|
| 246 |
Thresholds. |
| 247 |
Can be: 'certificate-expires'. |
| 248 |
|
| 249 |
=back |
| 250 |
|
| 251 |
=cut |