(plugin) cloud::aws::cloudfront - add discovery (#4143)

This commit is contained in:
qgarnier 2023-01-16 10:57:08 +00:00 committed by Kevin Duret
parent 4a893fbc47
commit bbe28a7931
5 changed files with 179 additions and 9 deletions

View File

@ -0,0 +1,111 @@
#
# Copyright 2022 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 cloud::aws::cloudfront::mode::discovery;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
"prettify" => { name => 'prettify' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub run {
my ($self, %options) = @_;
my @disco_data;
my $disco_stats;
$disco_stats->{start_time} = time();
my $instances = $options{custom}->cloudfront_list_distributions();
foreach my $cft_instance (@$instances) {
next if (!defined($cft_instance->{Id}));
my %cft;
$cft{id} = $cft_instance->{Id};
$cft{status} = $cft_instance->{Status};
$cft{domain_name} = $cft_instance->{DomainName};
$cft{aliases} = [];
if ($cft_instance->{Aliases}->{Quantity} > 0) {
$cft{aliases} = $cft_instance->{Aliases}->{Items};
}
push @disco_data, \%cft;
}
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = @disco_data;
$disco_stats->{results} = \@disco_data;
my $encoded_data;
eval {
if (defined($self->{option_results}->{prettify})) {
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
} else {
$encoded_data = JSON::XS->new->utf8->encode($disco_stats);
}
};
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
AWS ClouFront discovery.
=over 8
=item B<--prettify>
Prettify JSON output.
=back
=cut

View File

@ -29,15 +29,15 @@ sub new {
my $self = $class->SUPER::new( package => __PACKAGE__, %options );
bless $self, $class;
$self->{version} = '0.1';
%{ $self->{modes} } = (
'errors' => 'cloud::aws::cloudfront::mode::errors',
'requests' => 'cloud::aws::cloudfront::mode::requests',
'throughput' => 'cloud::aws::cloudfront::mode::throughput',
);
$self->{modes} = {
'discovery' => 'cloud::aws::cloudfront::mode::discovery',
'errors' => 'cloud::aws::cloudfront::mode::errors',
'requests' => 'cloud::aws::cloudfront::mode::requests',
'throughput' => 'cloud::aws::cloudfront::mode::throughput'
};
$self->{custom_modes}{paws} = 'cloud::aws::custom::paws';
$self->{custom_modes}{awscli} = 'cloud::aws::custom::awscli';
$self->{custom_modes}->{paws} = 'cloud::aws::custom::paws';
$self->{custom_modes}->{awscli} = 'cloud::aws::custom::awscli';
return $self;
}

View File

@ -39,6 +39,7 @@ sub new {
APIGATEWAY => $self->can('discover_api'),
BACKUP_VAULT => $self->can('discover_backup_vault'),
# DYNAMODB => $self->can('discover_dynamodb_table'),
CLOUDFRONT => $self->can('discover_cloudfront'),
EBS => $self->can('discover_ebs'),
EC2 => $self->can('discover_ec2'),
EFS => $self->can('discover_efs'),
@ -128,6 +129,15 @@ sub discover_backup_vault {
return \@disco_data, \@disco_keys;
}
sub discover_cloudfront {
my (%options) = @_;
use cloud::aws::cloudfront::mode::discovery;
my @disco_data = cloud::aws::cloudfront::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys %{$disco_data[0]} if (@disco_data != 0);
return \@disco_data, \@disco_keys;
}
sub discover_ebs {
my (%options) = @_;

View File

@ -875,6 +875,25 @@ sub tgw_list_gateways {
return $gateway_results;
}
sub cloudfront_list_distributions {
my ($self, %options) = @_;
my $cmd_options = $self->discovery_set_cmd(service => 'cloudfront', command => 'list-distributions');
my $raw_results = $self->execute(cmd_options => $cmd_options);
my $results = [];
foreach (@{$raw_results->{DistributionList}->{Items}}) {
push @$results, {
Id => $_->{Id},
Status => $_->{Status},
DomainName => $_->{DomainName},
Aliases => $_->{Aliases}
};
}
return $results;
}
1;
__END__

View File

@ -410,7 +410,8 @@ sub ec2spot_get_active_instances {
$instance_results->{$_->{InstanceId}} = {
health => $_->{InstanceHealth},
type => $_->{InstanceType},
request_id => $_->{SpotInstanceRequestId} };
request_id => $_->{SpotInstanceRequestId}
};
}
};
if ($@) {
@ -578,6 +579,7 @@ sub rds_list_clusters {
sub vpn_list_connections {
my ($self, %options) = @_;
my $connections_results = [];
eval {
my $vpn = $self->{paws}->service('EC2', region => $self->{option_results}->{region});
@ -696,6 +698,7 @@ sub sqs_list_queues {
sub sns_list_topics {
my ($self, %options) = @_;
my $topics_results = [];
eval {
my $topics = $self->{paws}->service('SNS', region => $self->{option_results}->{region});
@ -715,6 +718,7 @@ sub sns_list_topics {
sub tgw_list_gateways {
my ($self, %options) = @_;
my $gateway_results = [];
eval {
my $gateways = $self->{paws}->service('EC2', region => $self->{option_results}->{region});
@ -732,6 +736,32 @@ sub tgw_list_gateways {
return $gateway_results;
}
sub cloudfront_list_distributions {
my ($self, %options) = @_;
my $results = [];
eval {
my $cf = $self->{paws}->service('CloudFront', region => $self->{option_results}->{region});
my $listDistributionsResult = $cf->ListDistributions();
my $distributionList = $listDistributionsResult->DistributionList;
foreach (@{$distributionList->{Items}}) {
push @$results, {
Id => $_->{Id},
Status => $_->{Status},
DomainName => $_->{DomainName},
Aliases => $_->{Aliases}
};
}
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "error: $@");
$self->{output}->option_exit();
}
return $results;
}
1;
__END__