diff --git a/cloud/aws/custom/paws.pm b/cloud/aws/custom/paws.pm index a796337fb..94f8e16b0 100644 --- a/cloud/aws/custom/paws.pm +++ b/cloud/aws/custom/paws.pm @@ -23,7 +23,6 @@ package cloud::aws::custom::paws; use strict; use warnings; use Paws; -use Paws::Net::LWPCaller; use DateTime; sub new { @@ -45,6 +44,7 @@ sub new { 'aws-secret-key:s' => { name => 'aws_secret_key' }, 'aws-access-key:s' => { name => 'aws_access_key' }, 'aws-session-token:s' => { name => 'aws_session_token' }, + 'aws-role-arn:s' => { name => 'aws_role_arn' }, 'region:s' => { name => 'region' }, 'timeframe:s' => { name => 'timeframe' }, 'period:s' => { name => 'period' }, @@ -102,11 +102,6 @@ sub set_defaults { sub check_options { my ($self, %options) = @_; - if (defined($self->{option_results}->{proxyurl}) && $self->{option_results}->{proxyurl} ne '') { - $ENV{HTTP_PROXY} = $self->{option_results}->{proxyurl}; - $ENV{HTTPS_PROXY} = $self->{option_results}->{proxyurl}; - } - if (defined($self->{option_results}->{aws_secret_key}) && $self->{option_results}->{aws_secret_key} ne '') { $ENV{AWS_SECRET_KEY} = $self->{option_results}->{aws_secret_key}; } @@ -131,6 +126,30 @@ sub check_options { } } + my $config = {}; + if (defined($self->{option_results}->{proxyurl}) && $self->{option_results}->{proxyurl} ne '') { + $ENV{HTTP_PROXY} = $self->{option_results}->{proxyurl}; + $ENV{HTTPS_PROXY} = $self->{option_results}->{proxyurl}; + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => 'Paws::Net::LWPCaller', + error_msg => "Cannot load module 'Paws::Net::LWPCaller'." + ); + $config->{caller} = new Paws::Net::LWPCaller(); + } + if (defined($self->{option_results}->{aws_role_arn}) && $self->{option_results}->{aws_role_arn} ne '') { + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => 'Paws::Credential::AssumeRole', + error_msg => "Cannot load module 'Paws::Credential::AssumeRole'." + ); + $config->{credentials} = Paws::Credential::AssumeRole->new( + RoleArn => $self->{option_results}->{aws_role_arn}, + RoleSessionName => 'centreon-plugins' + ); + } + $self->{paws} = Paws->new(config => $config); + return 0; } @@ -139,8 +158,7 @@ sub cloudwatch_get_metrics { my $metric_results = {}; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $cw = Paws->service('CloudWatch', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $cw = $self->{paws}->service('CloudWatch', region => $self->{option_results}->{region}); my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601; my $end_time = DateTime->now->iso8601; @@ -197,8 +215,7 @@ sub cloudwatch_get_alarms { my $alarm_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $cw = Paws->service('CloudWatch', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $cw = $self->{paws}->service('CloudWatch', region => $self->{option_results}->{region}); my $alarms = $cw->DescribeAlarms(); foreach my $alarm (@{$alarms->{MetricAlarms}}) { push @$alarm_results, { @@ -222,8 +239,7 @@ sub cloudwatch_list_metrics { my $metric_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $cw = Paws->service('CloudWatch', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $cw = $self->{paws}->service('CloudWatch', region => $self->{option_results}->{region}); my %cw_options = (); $cw_options{Namespace} = $options{namespace} if (defined($options{namespace})); $cw_options{MetricName} = $options{metric} if (defined($options{metric})); @@ -257,8 +273,7 @@ sub cloudwatchlogs_describe_log_groups { my $log_groups_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $cw = Paws->service('CloudWatchLogs', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $cw = $self->{paws}->service('CloudWatchLogs', region => $self->{option_results}->{region}); my %cw_options = (); while ((my $list_log_groups = $cw->DescribeLogGroups(%cw_options))) { foreach (@{$list_log_groups->{logGroups}}) { @@ -282,8 +297,7 @@ sub cloudwatchlogs_filter_log_events { my $log_groups_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $cw = Paws->service('CloudWatchLogs', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $cw = $self->{paws}->service('CloudWatchLogs', region => $self->{option_results}->{region}); my %cw_options = (); $cw_options{StartTime} = $options{start_time} if (defined($options{start_time})); $cw_options{LogStreamNames} = [@{$options{LogStreamNames}}] if (defined($options{LogStreamNames})); @@ -309,8 +323,7 @@ sub ebs_list_volumes { my $volume_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $ebsvolume = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $ebsvolume = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $ebsvolume_requests = $ebsvolume->DescribeVolumes(DryRun => 0); foreach my $request (@{$ebsvolume_requests->{Volumes}}) { my @name_tags; @@ -340,8 +353,7 @@ sub ec2_get_instances_status { my $instance_results = {}; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $ec2 = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $ec2 = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $instances = $ec2->DescribeInstanceStatus(DryRun => 0, IncludeAllInstances => 1); foreach (@{$instances->{InstanceStatuses}}) { @@ -364,8 +376,7 @@ sub ec2spot_get_active_instances { my $instance_results = {}; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $ec2 = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $ec2 = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $instances = $ec2->DescribeSpotFleetInstances('SpotFleetRequestId' => $options{spot_fleet_request_id}, DryRun => 0, IncludeAllInstances => 1); foreach (@{$instances->{ActiveInstances}}) { @@ -388,8 +399,7 @@ sub ec2spot_list_fleet_requests { my $resource_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $ec2spot = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $ec2spot = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $spot_fleet_requests = $ec2spot->DescribeSpotFleetRequests(DryRun => 0); foreach (@{$spot_fleet_requests->{SpotFleetRequestConfigs}}) { @@ -413,8 +423,7 @@ sub ec2_list_resources { my $resource_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $ec2 = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $ec2 = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $list_instances = $ec2->DescribeInstances(DryRun => 0); foreach my $reservation (@{$list_instances->{Reservations}}) { @@ -457,8 +466,7 @@ sub asg_get_resources { my $autoscaling_groups = {}; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $asg = Paws->service('AutoScaling', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $asg = $self->{paws}->service('AutoScaling', region => $self->{option_results}->{region}); $autoscaling_groups = $asg->DescribeAutoScalingGroups(); }; if ($@) { @@ -474,8 +482,7 @@ sub rds_get_instances_status { my $instance_results = {}; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $rds = Paws->service('RDS', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $rds = $self->{paws}->service('RDS', region => $self->{option_results}->{region}); my $instances = $rds->DescribeDBInstances(); foreach (@{$instances->{DBInstances}}) { $instance_results->{$_->{DBInstanceIdentifier}} = { state => $_->{DBInstanceStatus} }; @@ -494,8 +501,7 @@ sub rds_list_instances { my $instance_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $rds = Paws->service('RDS', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $rds = $self->{paws}->service('RDS', region => $self->{option_results}->{region}); my $list_instances = $rds->DescribeDBInstances(); foreach my $instance (@{$list_instances->{DBInstances}}) { @@ -522,8 +528,7 @@ sub rds_list_clusters { my $cluster_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $rds = Paws->service('RDS', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $rds = $self->{paws}->service('RDS', region => $self->{option_results}->{region}); my $list_clusters = $rds->DescribeDBClusters(); foreach my $cluster (@{$list_clusters->{DBClusters}}) { @@ -548,8 +553,7 @@ sub vpn_list_connections { my ($self, %options) = @_; my $connections_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $vpn = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $vpn = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $list_vpn = $vpn->DescribeVpnConnections(); foreach my $connection (@{$list_vpn->{VpnConnections}}) { my @name_tags; @@ -578,8 +582,7 @@ sub health_describe_events { my $event_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $health = Paws->service('Health', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $health = $self->{paws}->service('Health', region => $self->{option_results}->{region}); my $health_options = { Filter => {} }; foreach ((['service', 'Services'], ['region', 'Regions'], ['entity_value', 'EntityValues'], ['event_status', 'EventStatusCodes'], ['event_category', 'EventTypeCategories'])) { next if (!defined($options{ 'filter_' . $_->[0] })); @@ -617,8 +620,7 @@ sub health_describe_affected_entities { my $entities_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $health = Paws->service('Health', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $health = $self->{paws}->service('Health', region => $self->{option_results}->{region}); while (my @events = splice(@{$options{filter_event_arns}}, 0, 10)) { my $health_options = { Filter => { EventArns => \@events } }; @@ -651,8 +653,7 @@ sub sqs_list_queues { my ($self, %options) = @_; my $queues_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $queues = Paws->service('SQS', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $queues = $self->{paws}->service('SQS', region => $self->{option_results}->{region}); my $list_queues = $queues->ListQueues(); foreach my $queue (@{$list_queues->{QueueUrls}}) { push @{$queues_results}, $queue; @@ -670,8 +671,7 @@ sub sns_list_topics { my ($self, %options) = @_; my $topics_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $topics = Paws->service('SNS', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $topics = $self->{paws}->service('SNS', region => $self->{option_results}->{region}); my $raw_results = $topics->ListTopics(); foreach my $topic (@{$raw_results->{Topics}}) { push @{$topics_results}, { name => $topic->{TopicArn} }; @@ -690,8 +690,7 @@ sub tgw_list_gateways { my ($self, %options) = @_; my $gateway_results = []; eval { - my $lwp_caller = new Paws::Net::LWPCaller(); - my $gateways = Paws->service('EC2', caller => $lwp_caller, region => $self->{option_results}->{region}); + my $gateways = $self->{paws}->service('EC2', region => $self->{option_results}->{region}); my $raw_results = $gateways->DescribeTransitGateways(); foreach my $gateway (@{$raw_results->{TransitGateways}}) { push @{$gateway_results}, { id => $gateway->{TransitGatewayId}, name => $gateway->{Description} }; @@ -734,6 +733,10 @@ Set AWS access key. Set AWS session token. +=item B<--aws-role-arn> + +Set arn of the role to be assumed. + =item B<--region> Set the region name (Required).