list.pm: added exclude state and exclude service

This commit is contained in:
David Sabatié 2015-12-18 16:09:19 +01:00
parent 336f5f6611
commit 13f6b0b31d
3 changed files with 38 additions and 17 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.project .project
.sync-config.cson

View File

@ -30,12 +30,12 @@ use JSON;
use Module::Load; use Module::Load;
my $CloudwatchMetrics = { my $CloudwatchMetrics = {
cpu => "cloud::aws::mode::metrics::ec2instancecpu", cpu => "cloud::aws::mode::metrics::ec2instancecpu",
traffic => "cloud::aws::mode::metrics::ec2instancenetwork", traffic => "cloud::aws::mode::metrics::ec2instancenetwork",
cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage", cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage",
cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance", cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance",
bucketsize => "cloud::aws::mode::metrics::s3bucketsize", bucketsize => "cloud::aws::mode::metrics::s3bucketsize",
rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu", rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu",
}; };
my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount"; my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount";

View File

@ -27,7 +27,7 @@ use JSON;
my $AWSServices = 'EC2,S3,RDS'; my $AWSServices = 'EC2,S3,RDS';
my @Disco_service_tab = ('EC2', 'RDS'); my @Disco_service_tab = ('EC2', 'RDS');
my @EC2_instance_states = ['running', 'stopped']; my $EC2_instance_states = 'running,stopped';
my $awsapi; my $awsapi;
sub new sub new
@ -39,7 +39,9 @@ sub new
$options{options}->add_options( $options{options}->add_options(
arguments => { arguments => {
"service:s" => {name => 'service', default => $AWSServices}, "service:s" => {name => 'service', default => $AWSServices},
"exclude:s" => {name => 'exclude'}, "exclude-service:s" => {name => 'exclude_service'},
"ec2-state:s"=>{name=> 'ec2_state', default => $EC2_instance_states},
"ec2-exclude-state:s" => {name => 'ec2_exclude_state'},
} }
); );
$self->{result} = {}; $self->{result} = {};
@ -55,6 +57,13 @@ sub api_request {
my ($self, %options) = @_; my ($self, %options) = @_;
@{$self->{option_results}->{servicetab}} = split( /,/, $self->{option_results}->{service} ); @{$self->{option_results}->{servicetab}} = split( /,/, $self->{option_results}->{service} );
# exclusions
if (defined($self->{option_results}->{exclude_service})) {
my @excludetab = split /,/, $self->{option_results}->{exclude_service};
my %array1 = map { $_ => 1 } @excludetab;
@{$self->{option_results}->{servicetab}} = grep { not $array1{$_} } @{$self->{option_results}->{servicetab}};
}
foreach my $service (@{$self->{option_results}->{servicetab}}) { foreach my $service (@{$self->{option_results}->{servicetab}}) {
$self->{result}->{count}->{$service} = 0; $self->{result}->{count}->{$service} = 0;
if ($service eq 'EC2') { if ($service eq 'EC2') {
@ -79,12 +88,19 @@ sub EC2
}; };
# Building JSON # Building JSON
my @ec2_statestab = split(/,/, $self->{option_results}->{ec2_state});
# exclusions
if (defined($self->{option_results}->{ec2_exclude_state})) {
my @excludetab = split /,/, $self->{option_results}->{ec2_exclude_state};
my %array1 = map { $_ => 1 } @excludetab;
@ec2_statestab = grep { not $array1{$_} } @ec2_statestab;
}
$apiRequest->{json} = { $apiRequest->{json} = {
'DryRun' => JSON::false, 'DryRun' => JSON::false,
'Filters' => [ 'Filters' => [
{ {
'Name' => 'instance-state-name', 'Name' => 'instance-state-name',
'Values' => @EC2_instance_states, 'Values' => [@ec2_statestab],
} }
], ],
}; };
@ -103,7 +119,7 @@ sub EC2
$self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} = $self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} =
{ {
State => $instance->{Instances}[0]->{State}->{Name}, State => $instance->{Instances}[0]->{State}->{Name},
Name => $instance->{Instances}[0]->{Name} Name => $instance->{Instances}[0]->{Name}
}; };
$self->{result}->{count}->{'EC2'}++; $self->{result}->{count}->{'EC2'}++;
@ -132,8 +148,7 @@ sub S3
} }
# Compute data # Compute data
foreach my $bucket (@buckets) foreach my $bucket (@buckets) {
{
$self->{result}->{'S3'}->{$bucket->{Name}} = $self->{result}->{'S3'}->{$bucket->{Name}} =
{'Creation date' => $bucket->{CreationDate}}; {'Creation date' => $bucket->{CreationDate}};
$self->{result}->{count}->{'S3'}++; $self->{result}->{count}->{'S3'}++;
@ -196,10 +211,7 @@ sub run
foreach my $device (keys %{$self->{result}->{$service}}) { foreach my $device (keys %{$self->{result}->{$service}}) {
my $output = $device . " ["; my $output = $device . " [";
foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) { foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) {
$output = $output = $output . $value . " = " . $self->{result}->{$service}->{$device}->{$value} . ", ";
$output
. $value . " = "
. $self->{result}->{$service}->{$device}->{$value} . ", ";
} }
$output =~ s/, $//; $output =~ s/, $//;
$output = $output . "]"; $output = $output . "]";
@ -227,10 +239,18 @@ List your EC2, RDS instance and S3 buckets
(optional) List one particular service. (optional) List one particular service.
=item B<--exclude> =item B<--exclude-service>
(optional) Service to exclude from the scan. (optional) Service to exclude from the scan.
=item B<--ec2-state>
(optional) State to request (default: 'running','stopped')
=item B<--ec2-exclude-state>
(optional) State to exclude from the scan.
=back =back
=cut =cut