2015-09-15 17:01:49 +02:00
|
|
|
#
|
|
|
|
# Copyright 2015 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::mode::list;
|
|
|
|
|
|
|
|
use base qw(centreon::plugins::mode);
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use centreon::plugins::misc;
|
|
|
|
use JSON;
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
my $AWSServices = 'EC2,S3,RDS';
|
|
|
|
my @Disco_service_tab = ('EC2', 'RDS');
|
|
|
|
my @EC2_instance_states = ['running', 'stopped'];
|
2015-09-15 17:01:49 +02:00
|
|
|
my $awsapi;
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my ($class, %options) = @_;
|
|
|
|
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
2015-09-15 17:01:49 +02:00
|
|
|
bless $self, $class;
|
|
|
|
$self->{version} = '0.1';
|
|
|
|
$options{options}->add_options(
|
|
|
|
arguments => {
|
2015-11-06 11:53:54 +01:00
|
|
|
"service:s" => {name => 'service', default => $AWSServices},
|
|
|
|
"exclude:s" => {name => 'exclude'},
|
2015-09-15 17:01:49 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{result} = {};
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_options {
|
2015-11-06 11:53:54 +01:00
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->SUPER::init(%options);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub api_request {
|
2015-11-06 11:53:54 +01:00
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
|
2015-09-30 13:56:12 +02:00
|
|
|
@{$self->{option_results}->{servicetab}} = split( /,/, $self->{option_results}->{service} );
|
|
|
|
foreach my $service (@{$self->{option_results}->{servicetab}}) {
|
|
|
|
$self->{result}->{count}->{$service} = 0;
|
|
|
|
if ($service eq 'EC2') {
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->EC2(%options);
|
2015-09-30 13:56:12 +02:00
|
|
|
} elsif ($service eq 'S3') {
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->S3(%options);
|
2015-09-30 13:56:12 +02:00
|
|
|
} elsif ($service eq 'RDS') {
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->RDS(%options);
|
2015-09-30 13:56:12 +02:00
|
|
|
} else {
|
|
|
|
$self->{output}->add_option_msg(short_msg => "Service $service doesn't exists" );
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->{output}->option_exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub EC2
|
|
|
|
{
|
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
my $apiRequest = {
|
|
|
|
'command' => 'ec2',
|
|
|
|
'subcommand' => 'describe-instances',
|
|
|
|
};
|
|
|
|
|
|
|
|
# Building JSON
|
|
|
|
$apiRequest->{json} = {
|
|
|
|
'DryRun' => JSON::false,
|
|
|
|
'Filters' => [
|
|
|
|
{
|
|
|
|
'Name' => 'instance-state-name',
|
|
|
|
'Values' => @EC2_instance_states,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
# Requesting API
|
|
|
|
$awsapi = $options{custom};
|
|
|
|
$self->{command_return} = $awsapi->execReq($apiRequest);
|
|
|
|
|
|
|
|
# Compute data
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $instance (@{$self->{command_return}->{Reservations}}) {
|
|
|
|
foreach my $tags (@{$instance->{Instances}[0]->{Tags}}) {
|
|
|
|
if ($tags->{Key} eq 'Name') {
|
2015-09-15 17:01:49 +02:00
|
|
|
$instance->{Instances}[0]->{Name} = $tags->{Value};
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 13:56:12 +02:00
|
|
|
$self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} =
|
2015-09-15 17:01:49 +02:00
|
|
|
{
|
2015-09-30 13:56:12 +02:00
|
|
|
State => $instance->{Instances}[0]->{State}->{Name},
|
|
|
|
Name => $instance->{Instances}[0]->{Name}
|
2015-09-15 17:01:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$self->{result}->{count}->{'EC2'}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub S3
|
|
|
|
{
|
|
|
|
my ($self, %options) = @_;
|
|
|
|
my (@buckets, @return) = ();
|
2015-09-15 17:01:49 +02:00
|
|
|
my $apiRequest = {
|
|
|
|
'command' => 's3',
|
|
|
|
'subcommand' => 'ls',
|
|
|
|
'output' => 'text'
|
|
|
|
};
|
|
|
|
|
|
|
|
# Requesting API
|
|
|
|
$awsapi = $options{custom};
|
|
|
|
$self->{command_return} = $awsapi->execReq($apiRequest);
|
|
|
|
|
|
|
|
# Exec command
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $line (@{$self->{command_return}}) {
|
|
|
|
my ($date, $time, $name) = split / /, $line;
|
2015-09-15 17:01:49 +02:00
|
|
|
my $creationdate = $date . " " . $time;
|
2015-09-30 13:56:12 +02:00
|
|
|
push(@buckets, { Name => $name, CreationDate => $creationdate });
|
2015-09-15 17:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Compute data
|
2015-11-06 11:53:54 +01:00
|
|
|
foreach my $bucket (@buckets)
|
|
|
|
{
|
|
|
|
$self->{result}->{'S3'}->{$bucket->{Name}} =
|
|
|
|
{'Creation date' => $bucket->{CreationDate}};
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->{result}->{count}->{'S3'}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub RDS
|
|
|
|
{
|
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
my $apiRequest = {
|
|
|
|
'command' => 'rds',
|
|
|
|
'subcommand' => 'describe-db-instances',
|
|
|
|
};
|
|
|
|
|
|
|
|
# Requesting API
|
|
|
|
$awsapi = $options{custom};
|
|
|
|
$self->{command_return} = $awsapi->execReq($apiRequest);
|
|
|
|
|
|
|
|
# Compute data
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $dbinstance (@{$self->{command_return}->{DBInstances}}) {
|
2015-11-06 11:53:54 +01:00
|
|
|
$self->{result}->{'RDS'}->{$dbinstance->{DBInstanceIdentifier}} = {
|
2015-09-30 13:56:12 +02:00
|
|
|
State => $dbinstance->{DBInstanceStatus},
|
|
|
|
Name => $dbinstance->{DBInstanceIdentifier}
|
2015-09-15 17:01:49 +02:00
|
|
|
};
|
2015-09-30 13:56:12 +02:00
|
|
|
$self->{result}->{count}->{RDS}++;
|
2015-09-15 17:01:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub disco_format {
|
2015-11-06 11:53:54 +01:00
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
|
|
|
|
my $names = [ 'name', 'id', 'state', 'service' ];
|
|
|
|
$self->{output}->add_disco_format( elements => $names );
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub disco_show
|
|
|
|
{
|
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->api_request(%options);
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $service (@Disco_service_tab) {
|
|
|
|
foreach my $device (keys %{$self->{result}->{$service}}) {
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->{output}->add_disco_entry(
|
2015-11-06 11:53:54 +01:00
|
|
|
name => $self->{result}->{$service}->{$device}->{Name},
|
|
|
|
id => $device,
|
|
|
|
state => $self->{result}->{$service}->{$device}->{State},
|
2015-09-15 17:01:49 +02:00
|
|
|
service => $service,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:53:54 +01:00
|
|
|
sub run
|
|
|
|
{
|
|
|
|
my ($self, %options) = @_;
|
2015-09-15 17:01:49 +02:00
|
|
|
$self->api_request(%options);
|
|
|
|
|
|
|
|
# Send formated data to Centreon
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $service (@{$self->{option_results}->{servicetab}}) {
|
2015-11-06 11:53:54 +01:00
|
|
|
$self->{output}->output_add(long_msg => sprintf("AWS service: %s", $service));
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $device (keys %{$self->{result}->{$service}}) {
|
2015-09-15 17:01:49 +02:00
|
|
|
my $output = $device . " [";
|
2015-09-30 13:56:12 +02:00
|
|
|
foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) {
|
2015-09-15 17:01:49 +02:00
|
|
|
$output =
|
|
|
|
$output
|
|
|
|
. $value . " = "
|
|
|
|
. $self->{result}->{$service}->{$device}->{$value} . ", ";
|
|
|
|
}
|
|
|
|
$output =~ s/, $//;
|
|
|
|
$output = $output . "]";
|
2015-11-06 11:53:54 +01:00
|
|
|
$self->{output}->output_add(long_msg => $output);
|
2015-09-15 17:01:49 +02:00
|
|
|
}
|
2015-11-06 11:53:54 +01:00
|
|
|
$self->{output}->output_add(short_msg => sprintf("%s: %s", $service, $self->{result}->{count}->{$service}));
|
2015-09-15 17:01:49 +02:00
|
|
|
}
|
|
|
|
$self->{output}->display(
|
|
|
|
nolabel => 1,
|
|
|
|
force_ignore_perfdata => 1,
|
|
|
|
force_long_output => 1
|
|
|
|
);
|
|
|
|
$self->{output}->exit();
|
|
|
|
}
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 MODE
|
|
|
|
|
|
|
|
List your EC2, RDS instance and S3 buckets
|
|
|
|
|
|
|
|
=over 8
|
|
|
|
|
|
|
|
=item B<--service>
|
|
|
|
|
|
|
|
(optional) List one particular service.
|
|
|
|
|
|
|
|
=item B<--exclude>
|
|
|
|
|
|
|
|
(optional) Service to exclude from the scan.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=cut
|