From 70a98a4da3b1a8c26d189484cf4c2f1b6bbfeaaa Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 13 Jan 2020 17:31:12 +0100 Subject: [PATCH] add certificates cces --- .../network/cisco/cces/restapi/custom/api.pm | 13 ++ .../cisco/cces/restapi/mode/certificates.pm | 133 ++++++++++++++++++ .../cisco/cces/restapi/mode/peripherals.pm | 2 +- .../network/cisco/cces/restapi/plugin.pm | 1 + 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 centreon-plugins/network/cisco/cces/restapi/mode/certificates.pm diff --git a/centreon-plugins/network/cisco/cces/restapi/custom/api.pm b/centreon-plugins/network/cisco/cces/restapi/custom/api.pm index 0e6dd9de2..d115a12ad 100644 --- a/centreon-plugins/network/cisco/cces/restapi/custom/api.pm +++ b/centreon-plugins/network/cisco/cces/restapi/custom/api.pm @@ -193,6 +193,19 @@ sub authenticate { sub request_api { my ($self, %options) = @_; + my $plop = do { + local $/ = undef; + if (!open my $fh, "<", '/tmp/plop4.txt') { + $self->{output}->add_option_msg(short_msg => "Could not open file"); + $self->{output}->option_exit(); + } + <$fh>; + }; + eval { + $plop = XMLin($plop, ForceArray => $options{ForceArray}, KeyAttr => []); + }; + return $plop; + $self->settings(); if (!defined($self->{session_cookie})) { $self->authenticate(statefile => $self->{cache}); diff --git a/centreon-plugins/network/cisco/cces/restapi/mode/certificates.pm b/centreon-plugins/network/cisco/cces/restapi/mode/certificates.pm new file mode 100644 index 000000000..0d2c3db40 --- /dev/null +++ b/centreon-plugins/network/cisco/cces/restapi/mode/certificates.pm @@ -0,0 +1,133 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::cisco::cces::restapi::mode::certificates; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::misc; + +sub custom_validity_output { + my ($self, %options) = @_; + + return sprintf( + 'expires in %s', + $self->{result_values}->{generation_time_absolute} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'certificates', type => 1, cb_prefix_output => 'prefix_certificate_output', message_multiple => 'All certificates are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{certificates} = [ + { label => 'certificate-expires', nlabel => 'system.certificate.expires.seconds', set => { + key_values => [ { name => 'validity_time' }, { name => 'generation_time' } ], + closure_custom_output => $self->can('custom_validity_output'), + perfdatas => [ + { value => 'validity_time_absolute', template => '%d', min => 0, unit => 's' }, + ], + } + } + ]; +} + +sub prefix_certificate_output { + my ($self, %options) = @_; + + return "Certificate '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + }); + + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'." + ); + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->request_api( + method => 'POST', + url_path => '/putxml', + query_form_post => '', + ForceArray => ['Details'] + ); + + $self->{certificates} = {}; + if (defined($result->{ServicesShowResult}->{Details})) { + foreach (@{$result->{ServicesShowResult}->{Details}}) { + my $end_date = Date::Parse::str2time($_->{notAfter}); + if (!defined($end_date)) { + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => "can't parse date '" . $_->{notAfter} . "'" + ); + next; + } + + $self->{certificates}->{$_->{item}} = { + display => $_->{SubjectName}, + validity_time => $end_date - time(), + generation_time => centreon::plugins::misc::change_seconds(value => $end_date - time()), + }; + } + } + + if (scalar(keys %{$self->{certificates}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No certificate found.'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check certificates validity. + +=over 8 + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'peripherals-connected'. + +=back + +=cut diff --git a/centreon-plugins/network/cisco/cces/restapi/mode/peripherals.pm b/centreon-plugins/network/cisco/cces/restapi/mode/peripherals.pm index b3f1771f7..3b2b76d81 100644 --- a/centreon-plugins/network/cisco/cces/restapi/mode/peripherals.pm +++ b/centreon-plugins/network/cisco/cces/restapi/mode/peripherals.pm @@ -80,7 +80,7 @@ sub manage_selection { if (defined($self->{option_results}->{filter_since}) && $self->{option_results}->{filter_since} =~ /\d+/) { my $last_seen = Date::Parse::str2time($_->{LastSeen}); if (!defined($last_seen)) { - $self->{manager}->{output}->output_add( + $self->{output}->output_add( severity => 'UNKNOWN', short_msg => "can't parse date '" . $_->{LastSeen} . "'" ); diff --git a/centreon-plugins/network/cisco/cces/restapi/plugin.pm b/centreon-plugins/network/cisco/cces/restapi/plugin.pm index 23827c612..fc4618257 100644 --- a/centreon-plugins/network/cisco/cces/restapi/plugin.pm +++ b/centreon-plugins/network/cisco/cces/restapi/plugin.pm @@ -35,6 +35,7 @@ sub new { 'components' => 'network::cisco::cces::restapi::mode::components', 'calls-summary' => 'network::cisco::cces::restapi::mode::callssummary', 'calls-rt' => 'network::cisco::cces::restapi::mode::callsrt', + 'certificates' => 'network::cisco::cces::restapi::mode::certificates', 'diagnostics' => 'network::cisco::cces::restapi::mode::diagnostics', 'peripherals' => 'network::cisco::cces::restapi::mode::peripherals', 'sessions' => 'network::cisco::cces::restapi::mode::sessions'