(plugin) cloud::aws::cloudwatch - Unified AWS Host Discovery (#3805)

* Unified AWS Host Discovery

* Unified AWS Host Discovery

* + wip AWS disco unify

* + wip proposal

* + cosmetic

* Unified AWS Host Discovery

* Unified AWS Host Discovery

* Unified AWS Host Discovery

Co-authored-by: itoussies <centos@ip-10-25-11-66.eu-west-1.compute.internal>
Co-authored-by: itoussies <root@ip-10-25-11-66.eu-west-1.compute.internal>
Co-authored-by: Simon Bomm <sbomm@centreon.com>
This commit is contained in:
itoussies 2022-08-04 15:49:31 +02:00 committed by GitHub
parent cb35907d1f
commit e3040adf03
19 changed files with 381 additions and 184 deletions

View File

@ -87,6 +87,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -58,6 +58,7 @@ sub run {
foreach my $backup_vault (@{$backup_vaults->{BackupVaultList}}) {
my %backup_vault;
$backup_vault{type} = 'backup_vault';
$backup_vault{name} = $backup_vault->{BackupVaultName};
push @disco_data, \%backup_vault;
}
@ -78,6 +79,8 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);

View File

@ -30,24 +30,33 @@ sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
"service:s@" => { name => 'service' },
"prettify" => { name => 'prettify' },
"prettify" => { name => 'prettify' }
});
$self->{services} = {
VPC => $self->can('discover_vpc'),
EC2 => $self->can('discover_ec2'),
RDS => $self->can('discover_rds'),
ELB => $self->can('discover_elb'),
VPN => $self->can('discover_vpn'),
KINESIS => $self->can('discover_kinesis_stream'),
DYNAMODB => $self->can('discover_dynamodb_table'),
APIGATEWAY => $self->can('discover_api'),
S3 => $self->can('discover_s3_bucket')
APIGATEWAY => $self->can('discover_api'),
BACKUP_VAULT => $self->can('discover_backup_vault'),
DYNAMODB => $self->can('discover_dynamodb_table'),
EBS => $self->can('discover_ebs'),
EC2 => $self->can('discover_ec2'),
EFS => $self->can('discover_efs'),
ELB_APP => $self->can('discover_elb_app'),
ELB_CLASSIC => $self->can('discover_elb_classic'),
ELB_NETWORK => $self->can('discover_elb_network'),
FSX => $self->can('discover_fsx'),
KINESIS => $self->can('discover_kinesis_stream'),
LAMBDA => $self->can('discover_lambda'),
RDS => $self->can('discover_rds'),
S3_BUCKET => $self->can('discover_s3_bucket'),
SNS => $self->can('discover_sns'),
SPOT_FLEET_REQUEST => $self->can('discover_spotfleetrequest'),
SQS => $self->can('discover_sqs'),
VPC => $self->can('discover_vpc'),
VPN => $self->can('discover_vpn')
};
return $self;
}
@ -55,15 +64,11 @@ sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{service}) || $self->{option_results}->{service} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --service option.");
$self->{output}->option_exit();
}
}
sub discover_vpc {
my (%options) = @_;
my @disco_data;
my $vpcs = $options{custom}->discovery(region => $options{region},
@ -83,92 +88,10 @@ sub discover_vpc {
}
push @disco_data, \%vpc;
}
return @disco_data;
}
sub discover_ec2 {
my (%options) = @_;
my @disco_data;
my %asgs;
my $instances = $options{custom}->discovery(region => $options{region},
service => 'ec2', command => 'describe-instances');
foreach my $reservation (@{$instances->{Reservations}}) {
foreach my $instance (@{$reservation->{Instances}}) {
next if (!defined($instance->{InstanceId}));
my %asg;
$asg{type} = "asg";
my %ec2;
$ec2{type} = "ec2";
$ec2{id} = $instance->{InstanceId};
$ec2{state} = $instance->{State}->{Name};
$ec2{key_name} = $instance->{KeyName};
$ec2{private_ip} = $instance->{PrivateIpAddress};
$ec2{private_dns_name} = $instance->{PrivateDnsName};
$ec2{public_dns_name} = $instance->{PublicDnsName};
$ec2{instance_type} = $instance->{InstanceType};
$ec2{vpc_id} = $instance->{VpcId};
foreach my $tag (@{$instance->{Tags}}) {
if ($tag->{Key} eq "aws:autoscaling:groupName" && defined($tag->{Value})) {
$ec2{asg} = $tag->{Value};
next if (defined($asgs{$tag->{Value}}));
$asg{name} = $tag->{Value};
$asgs{$tag->{Value}} = 1;
}
if ($tag->{Key} eq "Name" && defined($tag->{Value})) {
$ec2{name} = $tag->{Value};
}
push @{$ec2{tags}}, { key => $tag->{Key}, value => $tag->{Value} };
}
push @disco_data, \%ec2;
push @disco_data, \%asg if (defined($asg{name}) && $asg{name} ne '');
}
}
return @disco_data;
}
sub discover_rds {
my (%options) = @_;
my @disco_data;
my $db_instances = $options{custom}->discovery(region => $options{region},
service => 'rds', command => 'describe-db-instances');
foreach my $db_instance (@{$db_instances->{DBInstances}}) {
next if (!defined($db_instance->{DbiResourceId}));
my %rds;
$rds{type} = "rds";
$rds{id} = $db_instance->{DbiResourceId};
$rds{name} = $db_instance->{DBInstanceIdentifier};
$rds{status} = $db_instance->{DBInstanceStatus};
$rds{storage_type} = $db_instance->{StorageType};
$rds{instance_class} = $db_instance->{DBInstanceClass};
$rds{availability_zone} = $db_instance->{AvailabilityZone};
$rds{vpc_id} = $db_instance->{DBSubnetGroup}->{VpcId};
push @disco_data, \%rds;
}
return @disco_data;
}
sub discover_elb {
my (%options) = @_;
my @disco_data;
my $load_balancers = $options{custom}->discovery(region => $options{region},
service => 'elb', command => 'describe-load-balancers');
foreach my $load_balancer (@{$load_balancers->{LoadBalancerDescriptions}}) {
next if (!defined($load_balancer->{LoadBalancerName}));
my %elb;
$elb{type} = "elb";
$elb{name} = $load_balancer->{LoadBalancerName};
$elb{dns_name} = $load_balancer->{DNSName};
$elb{availability_zones} = $load_balancer->{AvailabilityZones};
$elb{vpc_id} = $load_balancer->{VPCId};
push @disco_data, \%elb;
}
return @disco_data;
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
<<<<<<< HEAD
=======
return \@disco_data, \@disco_keys;
}
sub discover_vpn {
@ -194,44 +117,9 @@ sub discover_vpn {
}
push @disco_data, \%vpn;
}
return @disco_data;
}
sub discover_kinesis_stream {
my (%options) = @_;
my @disco_data;
my $streams = $options{custom}->discovery(region => $options{region},
service => 'kinesis', command => 'list-streams');
foreach my $stream (@{$streams->{StreamNames}}) {
my %stream;
$stream{type} = "kinesis_stream";
$stream{name} = $stream;
push @disco_data, \%stream;
}
return @disco_data;
}
sub discover_s3_bucket {
my (%options) = @_;
my @disco_data;
my $buckets = $options{custom}->discovery(region => $options{region},
service => 's3api', command => 'list-buckets');
foreach my $bucket (@{$buckets->{Buckets}}) {
my %bucket;
$bucket{type} = "s3_bucket";
$bucket{name} = $bucket->{Name};
$bucket{creation_date} = $bucket->{CreationDate};
push @disco_data, \%bucket;
}
return @disco_data;
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
>>>>>>> 4cdbb870b663049dadf4b390fce8d526bf63c9ef
return \@disco_data, \@disco_keys;
}
sub discover_dynamodb_table {
@ -241,52 +129,203 @@ sub discover_dynamodb_table {
my $tables = $options{custom}->discovery(region => $options{region},
service => 'dynamodb', command => 'list-tables');
foreach my $table (@{$tables->{TableNames}}) {
my %table;
$table{type} = "dynamodb_table";
$table{name} = $table;
push @disco_data, \%table;
}
return @disco_data;
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_api {
my (%options) = @_;
my @disco_data;
use cloud::aws::apigateway::mode::discovery;
my @disco_data = cloud::aws::apigateway::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0];
return \@disco_data, \@disco_keys;
}
my $apis = $options{custom}->discovery(region => $options{region},
service => 'apigateway', command => 'get-rest-apis');
sub discover_backup_vault {
my (%options) = @_;
foreach my $api (@{$apis->{items}}) {
my %api;
$api{id} = $api->{id};
$api{name} = $api->{name};
$api{description} = $api->{description};
$api{version} = $api->{version};
foreach my $type (@{$api->{endpointConfiguration}->{types}}) {
push @{$api{types}}, $type;
}
use cloud::aws::backup::mode::discovery;
my @disco_data = cloud::aws::backup::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
push @disco_data, \%api;
}
sub discover_ebs {
my (%options) = @_;
return @disco_data;
use cloud::aws::ebs::mode::discovery;
my @disco_data = cloud::aws::ebs::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_ec2 {
my (%options) = @_;
use cloud::aws::ec2::mode::discovery;
my @disco_data = cloud::aws::ec2::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_efs {
my (%options) = @_;
use cloud::aws::efs::mode::discovery;
my @disco_data = cloud::aws::efs::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_elb_app {
my (%options) = @_;
use cloud::aws::elb::application::mode::discovery;
my @disco_data = cloud::aws::elb::application::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_elb_classic {
my (%options) = @_;
use cloud::aws::elb::classic::mode::discovery;
my @disco_data = cloud::aws::elb::classic::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_elb_network {
my (%options) = @_;
use cloud::aws::elb::network::mode::discovery;
my @disco_data = cloud::aws::elb::network::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_fsx {
my (%options) = @_;
use cloud::aws::fsx::mode::discovery;
my @disco_data = cloud::aws::fsx::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_kinesis_stream {
my (%options) = @_;
use cloud::aws::kinesis::mode::discovery;
my @disco_data = cloud::aws::kinesis::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_lambda {
my (%options) = @_;
use cloud::aws::lambda::mode::discovery;
my @disco_data = cloud::aws::lambda::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_rds {
my (%options) = @_;
use cloud::aws::rds::mode::discovery;
my @disco_data = cloud::aws::rds::mode::discovery->run(custom => $options{custom}, discover => 1);
next if (\@disco_data == 0);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_s3_bucket {
my (%options) = @_;
use cloud::aws::s3::mode::discovery;
my @disco_data = cloud::aws::s3::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_sns {
my (%options) = @_;
use cloud::aws::sns::mode::discovery;
my @disco_data = cloud::aws::sns::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_spotfleetrequest {
my (%options) = @_;
use cloud::aws::ec2::mode::discoveryspotfleetrequests;
my @disco_data = cloud::aws::ec2::mode::discoveryspotfleetrequests->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
return \@disco_data, \@disco_keys;
}
sub discover_sqs {
my (%options) = @_;
use cloud::aws::sqs::mode::discovery;
my @disco_data = cloud::aws::sqs::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0] if @disco_data != 0;
<<<<<<< HEAD
return \@disco_data, \@disco_keys;
}
sub discover_vpn {
my (%options) = @_;
use cloud::aws::vpn::mode::discovery;
my @disco_data = cloud::aws::vpn::mode::discovery->run(custom => $options{custom}, discover => 1);
my @disco_keys = keys $disco_data[0];
=======
>>>>>>> 4cdbb870b663049dadf4b390fce8d526bf63c9ef
return \@disco_data, \@disco_keys;
}
sub run {
my ($self, %options) = @_;
my $disco_data_ori;
my $disco_data_keys;
my @disco_data;
my $disco_stats;
my %asset_attr_keys;
<<<<<<< HEAD
$disco_stats->{start_time} = time();
=======
>>>>>>> 4cdbb870b663049dadf4b390fce8d526bf63c9ef
$disco_stats->{start_time} = time();
foreach my $service (@{$self->{option_results}->{service}}) {
push @disco_data, $self->{services}->{uc($service)}->(custom => $options{custom},
region => $self->{option_results}->{region}) if (defined($self->{services}->{uc($service)}));
foreach my $service (keys %{$self->{services}}) {
($disco_data_ori, $disco_data_keys) = $self->{services}->{$service}->(custom => $options{custom});
$asset_attr_keys{$_}++ for (@{$disco_data_keys});
push @disco_data, @{$disco_data_ori} if @{$disco_data_ori};
}
foreach my $discovered_item (@disco_data) {
foreach my $key (keys %asset_attr_keys) {
if (!defined($discovered_item->{$key})) {
$discovered_item->{$key} = "NOT_APPLICABLE";
}
}
}
$disco_stats->{end_time} = time();
@ -305,7 +344,7 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();
@ -321,12 +360,6 @@ Resources discovery.
=over 8
=item B<--service>
Choose the service from which discover
resources (Can be: 'VPC', 'EC2', 'RDS',
'ELB', 'VPN') (Mandatory).
=item B<--prettify>
Prettify JSON output.

View File

@ -66,7 +66,8 @@ sub run {
}
$volume{id} = $volume->{VolumeId};
$volume{name} = join(",", @name_tags);
$volume{type} = $volume->{VolumeType};
$volume{type} = 'ebs';
$volume{volumeType} = $volume->{VolumeType};
$volume{state} = $volume->{State};
$volume{snapshotid} = $volume->{SnapshotId};
$volume{encrypted} = $volume->{Encrypted};
@ -90,6 +91,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -87,10 +87,17 @@ sub run {
}
push @{$ec2{tags}}, { key => $tag->{Key}, value => $tag->{Value} };
}
push @disco_data, \%ec2 unless (defined($self->{option_results}->{filter_type})
&& $ec2{type} !~ /$self->{option_results}->{filter_type}/);
push @disco_data, \%asg unless ((defined($self->{option_results}->{filter_type})
&& $asg{type} !~ /$self->{option_results}->{filter_type}/) || !defined($asg{name}) || $asg{name} eq '');
if (defined($options{discover})) {
push @disco_data, \%ec2;
push @disco_data, \%asg if defined($asg{name});
} else {
push @disco_data, \%ec2 unless (defined($self->{option_results}->{filter_type})
&& $ec2{type} !~ /$self->{option_results}->{filter_type}/);
push @disco_data, \%asg unless ((defined($self->{option_results}->{filter_type})
&& $asg{type} !~ /$self->{option_results}->{filter_type}/) || !defined($asg{name}) || $asg{name} eq '');
}
}
}
@ -110,7 +117,9 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -61,12 +61,18 @@ sub run {
foreach my $fleet_request (@{$spot_fleet_requests->{SpotFleetRequestConfigs}}) {
my %sfr;
$sfr{type} = 'spot_fleet_request';
$sfr{state} = $fleet_request->{SpotFleetRequestState};
$sfr{id} = $fleet_request->{SpotFleetRequestId};
$sfr{activity_status} = $fleet_request->{ActivityStatus};
push @disco_data, \%sfr unless (defined($self->{option_results}->{filter_state})
&& $sfr{state} !~ /$self->{option_results}->{filter_state}/);
if (defined($options{discover})) {
push @disco_data, \%sfr;
} else {
push @disco_data, \%sfr unless (defined($self->{option_results}->{filter_state})
&& $sfr{state} !~ /$self->{option_results}->{filter_state}/);
}
}
$disco_stats->{end_time} = time();
@ -85,6 +91,8 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);

View File

@ -87,6 +87,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -86,7 +86,9 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -86,6 +86,8 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);

View File

@ -86,7 +86,9 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -88,6 +88,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -80,6 +80,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -58,6 +58,7 @@ sub run {
foreach my $function (@{$functions->{Functions}}) {
my %function;
$function{type} = 'lambda';
$function{name} = $function->{FunctionName};
push @disco_data, \%function;
}
@ -78,7 +79,9 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -92,7 +92,9 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -81,6 +81,8 @@ sub run {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -55,9 +55,10 @@ sub run {
service => 'sns',
command => 'list-topics'
);
foreach my $topic (@{$topics->{Topics}}) {
my %topic;
$topic{type} = 'sns';
$topic{name} = $topic->{TopicArn};
$topic{name} =~ s/(.*)\://g;
push @disco_data, \%topic;
@ -79,7 +80,8 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -58,6 +58,7 @@ sub run {
foreach my $queue (@{$queues->{QueueUrls}}) {
my %queue;
$queue{type} = 'sqs';
$queue{name} = $queue;
$queue{name} =~ s/(.*)\///g;
push @disco_data, \%queue;
@ -79,6 +80,8 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);

View File

@ -0,0 +1,114 @@
#
# Copyright 2022 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::vpn::mode::discovery;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
"prettify" => { name => 'prettify' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub run {
my ($self, %options) = @_;
my @disco_data;
my $disco_stats;
$disco_stats->{start_time} = time();
my $vpns = $options{custom}->discovery(region => $options{region},
service => 'ec2', command => 'describe-vpn-connections');
foreach my $connection (@{$vpns->{VpnConnections}}) {
next if (!defined($connection->{VpnConnectionId}));
my %vpn;
$vpn{type} = "vpn";
$vpn{id} = $connection->{VpnConnectionId};
$vpn{connection_type} = $connection->{Type};
$vpn{state} = $connection->{State};
$vpn{category} = $connection->{Category};
foreach my $tag (@{$connection->{Tags}}) {
if ($tag->{Key} eq "Name" && defined($tag->{Value})) {
$vpn{name} = $tag->{Value};
}
push @{$vpn{tags}}, { key => $tag->{Key}, value => $tag->{Value} };
}
push @disco_data, \%vpn;
}
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = @disco_data;
$disco_stats->{results} = \@disco_data;
my $encoded_data;
eval {
if (defined($self->{option_results}->{prettify})) {
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
} else {
$encoded_data = JSON::XS->new->utf8->encode($disco_stats);
}
};
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
return @disco_data if (defined($options{discover}));
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Amazon VPN discovery.
=over 8
=item B<--prettify>
Prettify JSON output.
=back
=cut

View File

@ -31,6 +31,7 @@ sub new {
$self->{version} = '1.0';
%{ $self->{modes} } = (
'discovery' => 'cloud::aws::vpn::mode::discovery',
'traffic' => 'cloud::aws::vpn::mode::traffic',
'list-vpn' => 'cloud::aws::vpn::mode::listvpn'
);