From 4a06e02be66847b380f26c41fdfcd4f737c6dc80 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Thu, 14 Dec 2017 13:37:47 +0100 Subject: [PATCH] add cloudwatch list metrics mode --- cloud/aws/custom/awscli.pm | 32 ++++++ cloud/aws/custom/paws.pm | 33 ++++++ cloud/aws/mode/cloudwatchlistmetrics.pm | 127 ++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 cloud/aws/mode/cloudwatchlistmetrics.pm diff --git a/cloud/aws/custom/awscli.pm b/cloud/aws/custom/awscli.pm index 45e4b6fec..b56b4115e 100644 --- a/cloud/aws/custom/awscli.pm +++ b/cloud/aws/custom/awscli.pm @@ -205,6 +205,38 @@ sub cloudwatch_get_alarms { return $alarm_results; } +sub cloudwatch_list_metrics_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + my $cmd_options = "cloudwatch list-metrics --region $options{region} --output json"; + return $cmd_options; +} + +sub cloudwatch_list_metrics { + my ($self, %options) = @_; + + my $cmd_options = $self->cloudwatch_list_metrics_set_cmd(%options); + my ($response) = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + sudo => $self->{option_results}->{sudo}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $cmd_options + ); + my $list_metrics; + eval { + $list_metrics = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + return $list_metrics->{Metrics}; +} + 1; __END__ diff --git a/cloud/aws/custom/paws.pm b/cloud/aws/custom/paws.pm index e5d3da182..b93e4d2c3 100644 --- a/cloud/aws/custom/paws.pm +++ b/cloud/aws/custom/paws.pm @@ -170,6 +170,39 @@ sub cloudwatch_get_alarms { return $alarm_results; } +sub cloudwatch_list_metrics { + my ($self, %options) = @_; + + my $metric_results = []; + eval { + my $cw = Paws->service('CloudWatch', region => $options{region}); + my %options = (); + $options{Namespace} = $options{namespace} if (defined($options{namespace})); + while ((my $list_metrics = $cw->ListMetrics(%options))) { + foreach (@{$list_metrics->{Metrics}}) { + my $dimensions = []; + foreach my $dimension (@{$_->{Dimensions}}) { + push @$dimensions, { Name => $dimension->{Name}, Value => $dimension->{Value} }; + } + push @{$metric_results}, { + Namespace => $_->{Namespace}, + MetricName => $_->{MetricName}, + Dimensions => $dimensions, + }; + } + + last if (!defined($list_metrics->{NextToken})); + $options{NextToken} = $list_metrics->{NextToken}; + } + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "error: $@"); + $self->{output}->option_exit(); + } + + return $metric_results; +} + 1; __END__ diff --git a/cloud/aws/mode/cloudwatchlistmetrics.pm b/cloud/aws/mode/cloudwatchlistmetrics.pm new file mode 100644 index 000000000..b76d37802 --- /dev/null +++ b/cloud/aws/mode/cloudwatchlistmetrics.pm @@ -0,0 +1,127 @@ +# +# Copyright 2017 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::mode::cloudwatchlistmetrics; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "region:s" => { name => 'region' }, + "namespace:s" => { name => 'namespace' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{region}) || $self->{option_results}->{region} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --region option."); + $self->{output}->option_exit(); + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{metrics} = $options{custom}->cloudwatch_list_metrics(region => $self->{option_results}->{region}, namespace => $self->{option_results}->{namespace}); +} + +sub get_dimensions_str { + my ($self, %options) = @_; + + my $dimensions = ''; + my $append = ''; + foreach (@{$options{dimensions}}) { + $dimensions .= $append . "Name=$_->{Name},Value=$_->{Value}"; + $append = ','; + } + + return $dimensions; +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach (@{$self->{metrics}}) { + $self->{output}->output_add(long_msg => sprintf("[Namespace = %s][Dimensions = %s][Metric = %s]", + $_->{Namespace}, $self->get_dimensions_str(dimensions => $_->{Dimensions}), $_->{MetricName})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List metrics:'); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['namespace', 'metric', 'dimensions']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach (@{$self->{metrics}}) { + $self->{output}->add_disco_entry( + namespace => $_->{Namespace}, + metric => $_->{MetricName}, + dimensions => $self->get_dimensions_str(dimensions => $_->{Dimensions}), + ); + } +} + +1; + +__END__ + +=head1 MODE + +List cloudwatch metrics. + +=over 8 + +=item B<--region> + +Set the region name (Required). + +=item B<--namespace> + +Set cloudwatch namespace. + +=back + +=cut +