From e98771a033f1437068ddd3683465c49f9dc6b42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sabati=C3=A9?= Date: Fri, 6 Nov 2015 11:53:54 +0100 Subject: [PATCH] ! --- cloud/aws/mode/cloudwatch.pm | 186 +++++++++++++++++------------------ cloud/aws/mode/list.pm | 171 ++++++++++++++++---------------- 2 files changed, 172 insertions(+), 185 deletions(-) diff --git a/cloud/aws/mode/cloudwatch.pm b/cloud/aws/mode/cloudwatch.pm index 43c5fef6e..d5dbe724f 100644 --- a/cloud/aws/mode/cloudwatch.pm +++ b/cloud/aws/mode/cloudwatch.pm @@ -31,12 +31,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", + '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", + 'bucketsize' => "cloud::aws::mode::metrics::s3bucketsize", + 'rdscpu' => "cloud::aws::mode::metrics::rdsinstancecpu", }; my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount"; @@ -47,24 +47,25 @@ my $apiRequest = { 'subcommand' => 'get-metric-statistics', }; -sub new { - my ( $class, %options ) = @_; - my $self = $class->SUPER::new( package => __PACKAGE__, %options ); +sub new +{ + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; $self->{version} = '0.1'; $options{options}->add_options( arguments => { - "metric:s" => { name => 'metric' }, - "period:s" => { name => 'period', default => 300 }, - "starttime:s" => { name => 'starttime' }, - "endtime:s" => { name => 'endtime' }, - "statistics:s" => { name => 'statistics', default => 'Average' }, - "exclude-statistics:s" => { name => 'exclude-statistics' }, - "object:s" => { name => 'object' }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, + "metric:s" => {name => 'metric'}, + "period:s" => {name => 'period', default => 300}, + "starttime:s" => {name => 'starttime'}, + "endtime:s" => {name => 'endtime'}, + "statistics:s" => {name => 'statistics', default => 'Average'}, + "exclude-statistics:s" => {name => 'exclude-statistics'}, + "object:s" => {name => 'object'}, + "warning:s" => {name => 'warning'}, + "critical:s" => {name => 'critical'}, } ); $self->{result} = {}; @@ -72,30 +73,26 @@ sub new { return $self; } -sub check_options { - my ( $self, %options ) = @_; +sub check_options +{ + my ($self, %options) = @_; $self->SUPER::init(%options); $self->{option_results}->{def_endtime} = $def_endtime; - if ( ( $self->{perfdata}->threshold_validate( label => 'warning' ,value => $self->{option_results}->{warning} ) ) == 0 ) + if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output} - ->add_option_msg( short_msg => "Wrong warning threshold '" - . $self->{option_results}->{warning} - . "'." ); + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); $self->{output}->option_exit(); } - if ( ( $self->{perfdata}->threshold_validate( label => 'critical' ,value => $self->{option_results}->{critical} ) ) == 0 ) + if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output} - ->add_option_msg( short_msg => "Wrong critical threshold '" - . $self->{option_results}->{critical} - . "'." ); + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); $self->{output}->option_exit(); } - if ( !defined( $self->{option_results}->{metric} ) ) { + if (!defined($self->{option_results}->{metric})) + { $self->{output}->add_option_msg( severity => 'UNKNOWN', short_msg => "Please give a metric to watch (cpu, disk, ...)." @@ -103,7 +100,8 @@ sub check_options { $self->{output}->option_exit(); } - if ( !defined( $self->{option_results}->{object} ) ) { + if (!defined($self->{option_results}->{object})) + { $self->{output}->add_option_msg( severity => 'UNKNOWN', short_msg => "Please give the object to request (instanceid, ...)." @@ -111,27 +109,29 @@ sub check_options { $self->{output}->option_exit(); } - if ( !defined( $self->{option_results}->{endtime} ) ) { - $self->{option_results}->{endtime} = - strftime( "%FT%H:%M:%S.000Z", - gmtime( $self->{option_results}->{def_endtime} ) ); + if (!defined($self->{option_results}->{endtime})) + { + $self->{option_results}->{endtime} = strftime("%FT%H:%M:%S.000Z", gmtime($self->{option_results}->{def_endtime})); } - if ( !defined( $self->{option_results}->{starttime} ) ) { - $self->{option_results}->{starttime} = - strftime( "%FT%H:%M:%S.000Z", - gmtime( $self->{option_results}->{def_endtime} - 600 ) ); + if (!defined($self->{option_results}->{starttime})) + { + $self->{option_results}->{starttime} = strftime("%FT%H:%M:%S.000Z", gmtime($self->{option_results}->{def_endtime} - 600)); } # Getting some parameters # statistics - if ( $self->{option_results}->{statistics} eq 'all' ) { - @{ $self->{option_results}->{statisticstab} } = split( /,/, $StatisticsType ); + if ($self->{option_results}->{statistics} eq 'all') + { + @{$self->{option_results}->{statisticstab}} = split(/,/, $StatisticsType); } - else { - @{ $self->{option_results}->{statisticstab} } = split( /,/, $self->{option_results}->{statistics} ); - foreach my $curstate ( @{ $self->{option_results}->{statisticstab} } ) { - if ( !grep { /^$curstate$/ } split( /,/, $StatisticsType ) ) { + else + { + @{$self->{option_results}->{statisticstab}} = split(/,/, $self->{option_results}->{statistics}); + foreach my $curstate (@{$self->{option_results}->{statisticstab}}) + { + if (!grep { /^$curstate$/ } split(/,/, $StatisticsType)) + { $self->{output}->add_option_msg( severity => 'UNKNOWN', short_msg => "The statistic $curstate doesn't exist." @@ -142,37 +142,43 @@ sub check_options { } # exclusions - if (defined($self->{option_results}->{'exclude-statistics'})){ - my @excludetab = split(/,/, $self->{option_results}->{'exclude-statistics'}); - my %array1 = map { $_ => 1 } @excludetab; - @{$self->{option_results}->{statisticstab}} = grep { not $array1{$_} } @{$self->{option_results}->{statisticstab}}; + if (defined($self->{option_results}->{'exclude-statistics'})) + { + my @excludetab = split(/,/, $self->{option_results}->{'exclude-statistics'}); + my %array1 = map { $_ => 1 } @excludetab; + @{$self->{option_results}->{statisticstab}} = grep { not $array1{$_} } @{$self->{option_results}->{statisticstab}}; } - + # Force Average statistic - if ( ! grep $_ eq 'Average', @{$self->{option_results}->{statisticstab}} ) { - my $statistics = join(',',@{ $self->{option_results}->{statisticstab} }); - if ( ! $statistics eq '' ) { + if (!grep $_ eq 'Average', @{$self->{option_results}->{statisticstab}}) + { + my $statistics = join(',', @{$self->{option_results}->{statisticstab}}); + if (!$statistics eq '') + { $statistics = $statistics . ',Average'; } - else { + else + { $statistics = 'Average'; } - @{ $self->{option_results}->{statisticstab} } = split( /,/, $statistics ); + @{$self->{option_results}->{statisticstab}} = split(/,/, $statistics); } } -sub manage_selection { - my ( $self, $metric ) = @_; +sub manage_selection +{ + my ($self, $metric) = @_; my @result; my @Dimensions = ( - { - 'Value' => $self->{option_results}->{object}, - 'Name' => $metric->{ObjectName} - } - ); + { + 'Value' => $self->{option_results}->{object}, + 'Name' => $metric->{ObjectName} + } + ); - if ( defined( $metric->{ExtraDimensions} ) ) { + if (defined($metric->{ExtraDimensions})) + { push @Dimensions, $metric->{ExtraDimensions}; } @@ -188,63 +194,47 @@ sub manage_selection { }; } -sub run { - my ( $self, %options ) = @_; +sub run +{ + my ($self, %options) = @_; - my ( $msg, $exit_code, $awsapi ); + my ($msg, $exit_code, $awsapi); - if ( defined( $CloudwatchMetrics->{ $self->{option_results}->{metric} } ) ) + if (defined($CloudwatchMetrics->{$self->{option_results}->{metric}})) { - load $CloudwatchMetrics->{ $self->{option_results}->{metric} },qw/cloudwatchCheck/; + load $CloudwatchMetrics->{$self->{option_results}->{metric}}, qw/cloudwatchCheck/; cloudwatchCheck($self); } - else { - $self->{output} - ->add_option_msg( short_msg => "Wrong option. Cannot find metric '" - . $self->{option_results}->{metric} - . "'." ); + else + { + $self->{output}->add_option_msg(short_msg => "Wrong option. Cannot find metric '" . $self->{option_results}->{metric} . "'."); $self->{output}->option_exit(); } - foreach my $metric ( @{ $self->{metric} } ) { + foreach my $metric (@{$self->{metric}}) + { $self->manage_selection($metric); $awsapi = $options{custom}; $self->{command_return} = $awsapi->execReq($apiRequest); $self->{output}->perfdata_add( - label => sprintf( - $metric->{Labels}->{PerfData}, - unit => $metric->{Labels}->{Unit} - ), - value => sprintf( - $metric->{Labels}->{Value}, - $self->{command_return}->{Datapoints}[0]->{Average} - ), - warning => $self->{perfdata}->get_perfdata_for_output( label => 'warning' ), - critical => $self->{perfdata}->get_perfdata_for_output( label => 'critical' ), + label => sprintf($metric->{Labels}->{PerfData}, unit => $metric->{Labels}->{Unit}), + value => sprintf($metric->{Labels}->{Value}, $self->{command_return}->{Datapoints}[0]->{Average}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + #min => 0, #max => 100 ); $exit_code = $self->{perfdata}->threshold_check( value => $self->{command_return}->{Datapoints}[0]->{Average}, - threshold => [ - { label => 'critical', 'exit_litteral' => 'critical' }, - { label => 'warning', exit_litteral => 'warning' } - ] + threshold => [{label => 'critical', 'exit_litteral' => 'critical'}, {label => 'warning', exit_litteral => 'warning'}] ); - $self->{output}->output_add( - long_msg => sprintf( - $metric->{Labels}->{LongOutput}, - $self->{command_return}->{Datapoints}[0]->{Average} - ) - ); + $self->{output}->output_add(long_msg => sprintf($metric->{Labels}->{LongOutput}, $self->{command_return}->{Datapoints}[0]->{Average})); $self->{output}->output_add( severity => $exit_code, - short_msg => sprintf( - $metric->{Labels}->{ShortOutput}, - $self->{command_return}->{Datapoints}[0]->{Average} - ) + short_msg => sprintf($metric->{Labels}->{ShortOutput}, $self->{command_return}->{Datapoints}[0]->{Average}) ); } diff --git a/cloud/aws/mode/list.pm b/cloud/aws/mode/list.pm index a1eb79835..5de4eeacf 100644 --- a/cloud/aws/mode/list.pm +++ b/cloud/aws/mode/list.pm @@ -17,71 +17,72 @@ # 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 Data::Dumper; use JSON; -my $AWSServices = 'EC2,S3,RDS'; -my @Disco_service_tab = ('EC2', 'RDS'); -my @EC2_instance_states = [ 'running', 'stopped' ]; - +my $AWSServices = 'EC2,S3,RDS'; +my @Disco_service_tab = ('EC2', 'RDS'); +my @EC2_instance_states = ['running', 'stopped']; my $awsapi; -sub new { - my ( $class, %options ) = @_; - my $self = $class->SUPER::new( package => __PACKAGE__, %options ); +sub new +{ + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $self->{version} = '0.1'; - $options{options}->add_options( arguments => { - "service:s" => { name => 'service', default => $AWSServices }, - "exclude:s" => { name => 'exclude' }, + "service:s" => {name => 'service', default => $AWSServices}, + "exclude:s" => {name => 'exclude'}, } ); $self->{result} = {}; return $self; } -sub check_options { - my ( $self, %options ) = @_; +sub check_options +{ + my ($self, %options) = @_; $self->SUPER::init(%options); } -sub api_request { - my ( $self, %options ) = @_; - - @{ $self->{option_results}->{servicetab} } = - split( /,/, $self->{option_results}->{service} ); - foreach my $service ( @{ $self->{option_results}->{servicetab} } ) { +sub api_request +{ + my ($self, %options) = @_; + @{$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' ) { + if ($service eq 'EC2') + { $self->EC2(%options); } - elsif ( $service eq 'S3' ) { + elsif ($service eq 'S3') + { $self->S3(%options); } - elsif ( $service eq 'RDS' ) { + elsif ($service eq 'RDS') + { $self->RDS(%options); } - else { - $self->{output}->add_option_msg( - short_msg => "Service $service doesn't exists" ); + else + { + $self->{output}->add_option_msg(short_msg => "Service $service doesn't exists"); $self->{output}->option_exit(); } } } -sub EC2 { - my ( $self, %options ) = @_; - +sub EC2 +{ + my ($self, %options) = @_; my $apiRequest = { 'command' => 'ec2', 'subcommand' => 'describe-instances', @@ -103,26 +104,27 @@ sub EC2 { $self->{command_return} = $awsapi->execReq($apiRequest); # Compute data - foreach my $instance ( @{ $self->{command_return}->{Reservations} } ) { - foreach my $tags ( @{ $instance->{Instances}[0]->{Tags} } ) { - if ( $tags->{Key} eq 'Name' ) { + foreach my $instance (@{$self->{command_return}->{Reservations}}) + { + foreach my $tags (@{$instance->{Instances}[0]->{Tags}}) + { + if ($tags->{Key} eq 'Name') + { $instance->{Instances}[0]->{Name} = $tags->{Value}; } } - $self->{result}->{'EC2'}->{ $instance->{Instances}[0]->{InstanceId} } = - { + $self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} = { 'State' => $instance->{Instances}[0]->{State}->{Name}, 'Name' => $instance->{Instances}[0]->{Name} - }; - + }; $self->{result}->{count}->{'EC2'}++; } } -sub S3 { - my ( $self, %options ) = @_; - my ( @buckets, @return ) = (); - +sub S3 +{ + my ($self, %options) = @_; + my (@buckets, @return) = (); my $apiRequest = { 'command' => 's3', 'subcommand' => 'ls', @@ -134,23 +136,25 @@ sub S3 { $self->{command_return} = $awsapi->execReq($apiRequest); # Exec command - foreach my $line ( @{ $self->{command_return} } ) { - my ( $date, $time, $name ) = split( / /, $line ); + foreach my $line (@{$self->{command_return}}) + { + my ($date, $time, $name) = split(/ /, $line); my $creationdate = $date . " " . $time; - push( @buckets, { Name => $name, CreationDate => $creationdate } ); + push(@buckets, {Name => $name, CreationDate => $creationdate}); } # Compute data - foreach my $bucket (@buckets) { - $self->{result}->{'S3'}->{ $bucket->{Name} } = - { 'Creation date' => $bucket->{CreationDate} }; + foreach my $bucket (@buckets) + { + $self->{result}->{'S3'}->{$bucket->{Name}} = + {'Creation date' => $bucket->{CreationDate}}; $self->{result}->{count}->{'S3'}++; } } -sub RDS { - my ( $self, %options ) = @_; - +sub RDS +{ + my ($self, %options) = @_; my $apiRequest = { 'command' => 'rds', 'subcommand' => 'describe-db-instances', @@ -161,8 +165,9 @@ sub RDS { $self->{command_return} = $awsapi->execReq($apiRequest); # Compute data - foreach my $dbinstance ( @{ $self->{command_return}->{DBInstances} } ) { - $self->{result}->{'RDS'}->{ $dbinstance->{DBInstanceIdentifier} } = { + foreach my $dbinstance (@{$self->{command_return}->{DBInstances}}) + { + $self->{result}->{'RDS'}->{$dbinstance->{DBInstanceIdentifier}} = { 'State' => $dbinstance->{DBInstanceStatus}, 'Name' => $dbinstance->{DBInstanceIdentifier} }; @@ -170,59 +175,53 @@ sub RDS { } } -sub disco_format { - my ( $self, %options ) = @_; - - my $names = [ 'name', 'id', 'state', 'service' ]; - $self->{output}->add_disco_format( elements => $names ); +sub disco_format +{ + my ($self, %options) = @_; + my $names = ['name', 'id', 'state', 'service']; + $self->{output}->add_disco_format(elements => $names); } -sub disco_show { - my ( $self, %options ) = @_; - +sub disco_show +{ + my ($self, %options) = @_; $self->api_request(%options); - - foreach my $service ( @Disco_service_tab ) { - foreach my $device ( keys %{ $self->{result}->{$service} } ) { + foreach my $service (@Disco_service_tab) + { + foreach my $device (keys %{$self->{result}->{$service}}) + { $self->{output}->add_disco_entry( - name => $self->{result}->{$service}->{$device}->{Name}, - id => $device, - state => $self->{result}->{$service}->{$device}->{State}, + name => $self->{result}->{$service}->{$device}->{Name}, + id => $device, + state => $self->{result}->{$service}->{$device}->{State}, service => $service, ); } } } -sub run { - my ( $self, %options ) = @_; - +sub run +{ + my ($self, %options) = @_; $self->api_request(%options); # Send formated data to Centreon - foreach my $service ( @{ $self->{option_results}->{servicetab} } ) { - $self->{output} - ->output_add( long_msg => sprintf( "AWS service: %s", $service ) ); - foreach my $device ( keys %{ $self->{result}->{$service} } ) { + foreach my $service (@{$self->{option_results}->{servicetab}}) + { + $self->{output}->output_add(long_msg => sprintf("AWS service: %s", $service)); + foreach my $device (keys %{$self->{result}->{$service}}) + { my $output = $device . " ["; - foreach my $value ( - sort( keys %{ $self->{result}->{$service}->{$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 . "]"; - $self->{output}->output_add( long_msg => $output ); + $self->{output}->output_add(long_msg => $output); } - $self->{output}->output_add( - short_msg => sprintf( "%s: %s", - $service, $self->{result}->{count}->{$service} ) - ); + $self->{output}->output_add(short_msg => sprintf("%s: %s", $service, $self->{result}->{count}->{$service})); } - $self->{output}->display( nolabel => 1, force_ignore_perfdata => 1, @@ -230,9 +229,7 @@ sub run { ); $self->{output}->exit(); } - 1; - __END__ =head1 MODE