From 13f6b0b31d1d0e3df5d035caa5e42caeda7726e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sabati=C3=A9?= Date: Fri, 18 Dec 2015 16:09:19 +0100 Subject: [PATCH] list.pm: added exclude state and exclude service --- .gitignore | 1 + cloud/aws/mode/cloudwatch.pm | 12 +++++------ cloud/aws/mode/list.pm | 42 ++++++++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 3a4edf690..3b1550de5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .project +.sync-config.cson diff --git a/cloud/aws/mode/cloudwatch.pm b/cloud/aws/mode/cloudwatch.pm index 9278d44f0..42b308596 100644 --- a/cloud/aws/mode/cloudwatch.pm +++ b/cloud/aws/mode/cloudwatch.pm @@ -30,12 +30,12 @@ use JSON; use Module::Load; my $CloudwatchMetrics = { - cpu => "cloud::aws::mode::metrics::ec2instancecpu", - traffic => "cloud::aws::mode::metrics::ec2instancenetwork", - cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage", - cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance", - bucketsize => "cloud::aws::mode::metrics::s3bucketsize", - rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu", + cpu => "cloud::aws::mode::metrics::ec2instancecpu", + traffic => "cloud::aws::mode::metrics::ec2instancenetwork", + cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage", + cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance", + bucketsize => "cloud::aws::mode::metrics::s3bucketsize", + rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu", }; my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount"; diff --git a/cloud/aws/mode/list.pm b/cloud/aws/mode/list.pm index 7099611a0..a732bee56 100644 --- a/cloud/aws/mode/list.pm +++ b/cloud/aws/mode/list.pm @@ -27,7 +27,7 @@ use JSON; my $AWSServices = 'EC2,S3,RDS'; my @Disco_service_tab = ('EC2', 'RDS'); -my @EC2_instance_states = ['running', 'stopped']; +my $EC2_instance_states = 'running,stopped'; my $awsapi; sub new @@ -39,7 +39,9 @@ sub new $options{options}->add_options( arguments => { "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} = {}; @@ -55,6 +57,13 @@ sub api_request { my ($self, %options) = @_; @{$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}}) { $self->{result}->{count}->{$service} = 0; if ($service eq 'EC2') { @@ -79,12 +88,19 @@ sub EC2 }; # 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} = { 'DryRun' => JSON::false, 'Filters' => [ { 'Name' => 'instance-state-name', - 'Values' => @EC2_instance_states, + 'Values' => [@ec2_statestab], } ], }; @@ -103,7 +119,7 @@ sub EC2 $self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} = { State => $instance->{Instances}[0]->{State}->{Name}, - Name => $instance->{Instances}[0]->{Name} + Name => $instance->{Instances}[0]->{Name} }; $self->{result}->{count}->{'EC2'}++; @@ -132,8 +148,7 @@ sub S3 } # Compute data - foreach my $bucket (@buckets) - { + foreach my $bucket (@buckets) { $self->{result}->{'S3'}->{$bucket->{Name}} = {'Creation date' => $bucket->{CreationDate}}; $self->{result}->{count}->{'S3'}++; @@ -196,10 +211,7 @@ sub run foreach my $device (keys %{$self->{result}->{$service}}) { my $output = $device . " ["; foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) { - $output = - $output - . $value . " = " - . $self->{result}->{$service}->{$device}->{$value} . ", "; + $output = $output . $value . " = " . $self->{result}->{$service}->{$device}->{$value} . ", "; } $output =~ s/, $//; $output = $output . "]"; @@ -227,10 +239,18 @@ List your EC2, RDS instance and S3 buckets (optional) List one particular service. -=item B<--exclude> +=item B<--exclude-service> (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 =cut