From a4e59629a37b9d6178cf2d384320c52765818afe Mon Sep 17 00:00:00 2001 From: Thibault S <48209914+thibaults-centreon@users.noreply.github.com> Date: Tue, 12 May 2020 16:25:39 +0200 Subject: [PATCH] add(plugin): Amazon EBS (#1990) * add(plugin): Amazon EBS --- cloud/aws/custom/awscli.pm | 36 ++++ cloud/aws/custom/paws.pm | 38 +++- cloud/aws/ebs/mode/discovery.pm | 115 +++++++++++ cloud/aws/ebs/mode/listvolumes.pm | 99 ++++++++++ cloud/aws/ebs/mode/volumeio.pm | 281 +++++++++++++++++++++++++++ cloud/aws/ebs/mode/volumeiops.pm | 309 ++++++++++++++++++++++++++++++ cloud/aws/ebs/mode/volumetime.pm | 270 ++++++++++++++++++++++++++ cloud/aws/ebs/plugin.pm | 10 +- 8 files changed, 1152 insertions(+), 6 deletions(-) create mode 100644 cloud/aws/ebs/mode/discovery.pm create mode 100644 cloud/aws/ebs/mode/listvolumes.pm create mode 100644 cloud/aws/ebs/mode/volumeio.pm create mode 100644 cloud/aws/ebs/mode/volumeiops.pm create mode 100644 cloud/aws/ebs/mode/volumetime.pm diff --git a/cloud/aws/custom/awscli.pm b/cloud/aws/custom/awscli.pm index 7c920e8fe..f4f064bf7 100644 --- a/cloud/aws/custom/awscli.pm +++ b/cloud/aws/custom/awscli.pm @@ -341,6 +341,42 @@ sub cloudwatchlogs_filter_log_events { return $raw_results->{events}; } +sub ebs_list_volumes_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + + my $cmd_options = "ec2 describe-volumes --no-dry-run --region $options{region} --output json"; + $cmd_options .= " --endpoint-url $self->{endpoint_url}" if (defined($self->{endpoint_url}) && $self->{endpoint_url} ne ''); + + return $cmd_options; +} + +sub ebs_list_volumes { + my ($self, %options) = @_; + + my $cmd_options = $self->ebs_list_volumes_set_cmd(%options); + my $raw_results = $self->execute(cmd_options => $cmd_options); + + my $resource_results = []; + foreach my $volume_request (@{$raw_results->{Volumes}}) { + my @name_tags; + foreach my $tag (@{$volume_request->{Tags}}) { + if ($tag->{Key} eq "Name" && defined($tag->{Value})) { + push @name_tags, $tag->{Value}; + } + }; + push @{$resource_results}, { + VolumeId => $volume_request->{VolumeId}, + VolumeName => join(",", @name_tags), + VolumeType => $volume_request->{VolumeType}, + VolumeState => $volume_request->{State} + }; + } + + return $resource_results; +} + sub ec2_get_instances_status_set_cmd { my ($self, %options) = @_; diff --git a/cloud/aws/custom/paws.pm b/cloud/aws/custom/paws.pm index 06fcc5f29..8b433e7ab 100644 --- a/cloud/aws/custom/paws.pm +++ b/cloud/aws/custom/paws.pm @@ -25,6 +25,7 @@ use warnings; use Paws; use Paws::Net::LWPCaller; use DateTime; +use Data::Dumper; sub new { my ($class, %options) = @_; @@ -290,6 +291,37 @@ sub cloudwatchlogs_filter_log_events { return $log_groups_results; } +sub ebs_list_volumes { + my ($self, %options) = @_; + + my $volume_results = []; + eval { + my $lwp_caller = new Paws::Net::LWPCaller(); + my $ebsvolume = Paws->service('EC2', caller => $lwp_caller, region => $options{region}); + my $ebsvolume_requests = $ebsvolume->DescribeVolumes(DryRun => 0); + foreach my $request (@{$ebsvolume_requests->{Volumes}}) { + my @name_tags; + foreach my $tag (@{$request->{Tags}}) { + if ($tag->{Key} eq "Name" && defined($tag->{Value})) { + push @name_tags, $tag->{Value}; + } + }; + push @{$volume_results}, { + VolumeId => $request->{VolumeId}, + VolumeName => join(",", @name_tags), + VolumeType => $request->{VolumeType}, + VolumeState => $request->{State} + }; + } + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "error: $@"); + $self->{output}->option_exit(); + } + + return $volume_results; +} + sub ec2_get_instances_status { my ($self, %options) = @_; @@ -300,8 +332,10 @@ sub ec2_get_instances_status { my $instances = $ec2->DescribeInstanceStatus(DryRun => 0, IncludeAllInstances => 1); foreach (@{$instances->{InstanceStatuses}}) { - $instance_results->{$_->{InstanceId}} = { state => $_->{InstanceState}->{Name}, - status => $_->{InstanceStatus}->{Status} }; + $instance_results->{$_->{InstanceId}} = { + state => $_->{InstanceState}->{Name}, + status => $_->{InstanceStatus}->{Status} + }; } }; if ($@) { diff --git a/cloud/aws/ebs/mode/discovery.pm b/cloud/aws/ebs/mode/discovery.pm new file mode 100644 index 000000000..186e29ee0 --- /dev/null +++ b/cloud/aws/ebs/mode/discovery.pm @@ -0,0 +1,115 @@ +# +# Copyright 2020 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::ebs::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 $volumes = $options{custom}->discovery( + region => $self->{option_results}->{region}, + service => 'ec2', + command => 'describe-volumes' + ); + + foreach my $volume (@{$volumes->{Volumes}}) { + my %volume; + my @name_tags; + foreach my $tag (@{$volume->{Tags}}) { + if ($tag->{Key} eq "Name" && defined($tag->{Value})) { + push @name_tags, $tag->{Value}; + } + } + $volume{id} = $volume->{VolumeId}; + $volume{name} = join(",", @name_tags); + $volume{type} = $volume->{VolumeType}; + $volume{state} = $volume->{State}; + $volume{snapshotid} = $volume->{SnapshotId}; + $volume{encrypted} = $volume->{Encrypted}; + push @disco_data, \%volume; + } + + $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"}'; + } + + $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 EBS volumes discovery. + +=over 8 + +=item B<--prettify> + +Prettify JSON output. + +=back + +=cut diff --git a/cloud/aws/ebs/mode/listvolumes.pm b/cloud/aws/ebs/mode/listvolumes.pm new file mode 100644 index 000000000..63106d7cb --- /dev/null +++ b/cloud/aws/ebs/mode/listvolumes.pm @@ -0,0 +1,99 @@ +# +# Copyright 2020 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::ebs::mode::listvolumes; + +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; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{volume} = $options{custom}->ebs_list_volumes(region => $self->{option_results}->{region}); +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach (@{$self->{volume}}) { + $self->{output}->output_add( + long_msg => sprintf("[Name = %s][Id = %s][Type = %s][State = %s]", + $_->{VolumeName}, $_->{VolumeId}, $_->{VolumeType}, $_->{VolumeState} )); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List EBS volumes:' + ); + $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 => ['Name', 'Id', 'Type', 'State']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach (@{$self->{volume}}) { + $self->{output}->add_disco_entry( + VolumeName => $_->{VolumeName}, + VolumeId => $_->{VolumeId}, + VolumeType => $_->{VolumeType}, + VolumeState => $_->{VolumeState}, + ); + }; +} + +1; + +__END__ + +=head1 MODE + +List Amazon Elastic Block Store volumes. + +=over 8 + +=back + +=cut diff --git a/cloud/aws/ebs/mode/volumeio.pm b/cloud/aws/ebs/mode/volumeio.pm new file mode 100644 index 000000000..d58c02afd --- /dev/null +++ b/cloud/aws/ebs/mode/volumeio.pm @@ -0,0 +1,281 @@ +# +# Copyright 2020 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::ebs::mode::volumeio; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my %metrics_mapping = ( + 'VolumeReadBytes' => { + 'output' => 'Volume Read Bytes', + 'label' => 'volume-read-bytes', + 'nlabel' => { + 'absolute' => 'ebs.volume.bytes.read.bytes', + 'per_second' => 'ebs.volume.bytes.read.bytespersecond' + }, + 'unit' => 'B' + }, + 'VolumeWriteBytes' => { + 'output' => 'Volume Write Bytes', + 'label' => 'volume-write-bytes', + 'nlabel' => { + 'absolute' => 'ebs.volume.bytes.write.bytes', + 'per_second' => 'ebs.volume.bytes.write.bytespersecond' + }, + 'unit' => 'B' + } +); + + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}; + $self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + return 0; +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}, + threshold => [ + { label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label} , exit_litteral => 'critical' }, + { label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}, exit_litteral => 'warning' } + ] + ); + return $exit; +} + +sub custom_metric_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + instances => $self->{instance}, + label => $metrics_mapping{$self->{result_values}->{metric}}->{label}, + nlabel => defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $metrics_mapping{$self->{result_values}->{metric}}->{nlabel}->{per_second} : + $metrics_mapping{$self->{result_values}->{metric}}->{nlabel}->{absolute}, + unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $metrics_mapping{$self->{result_values}->{metric}}->{unit} . '/s' : + $metrics_mapping{$self->{result_values}->{metric}}->{unit}, + value => sprintf("%.2f", defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $self->{result_values}->{value_per_sec} : + $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + ); +} + +sub custom_metric_output { + my ($self, %options) = @_; + my $msg = ""; + + if (defined($self->{instance_mode}->{option_results}->{per_sec})) { + my ($value, $unit) = ($metrics_mapping{$self->{result_values}->{metric}}->{unit} eq 'B') ? + $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec}) : + ($self->{result_values}->{value_per_sec}, $metrics_mapping{$self->{result_values}->{metric}}->{unit}); + $msg = sprintf("%s: %.2f %s", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit . '/s'); + } else { + my ($value, $unit) = ($metrics_mapping{$self->{result_values}->{metric}}->{unit} eq 'B') ? + $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}) : + ($self->{result_values}->{value}, $metrics_mapping{$self->{result_values}->{metric}}->{unit}); + $msg = sprintf("%s: %.2f %s", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit); + } + return $msg; +} + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "'" . $options{instance_value}->{display} . "' "; +} + +sub prefix_statistics_output { + my ($self, %options) = @_; + + return "Statistic '" . $options{instance_value}->{display} . "' Metrics "; +} + +sub long_output { + my ($self, %options) = @_; + + return "AWS EBS Volume'" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metrics', type => 3, cb_prefix_output => 'prefix_metric_output', cb_long_output => 'long_output', + message_multiple => 'All EBS metrics are ok', indent_long_output => ' ', + group => [ + { name => 'statistics', display_long => 1, cb_prefix_output => 'prefix_statistics_output', + message_multiple => 'All metrics are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + foreach my $metric (keys %metrics_mapping) { + my $entry = { + label => $metrics_mapping{$metric}->{label}, + set => { + key_values => [ { name => $metric }, { name => 'timeframe' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric => $metric }, + closure_custom_output => $self->can('custom_metric_output'), + closure_custom_perfdata => $self->can('custom_metric_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold') + } + }; + push @{$self->{maps_counters}->{statistics}}, $entry; + } +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'volumeid:s@' => { name => 'volume_id' }, + 'per-sec' => { name => 'per_sec' }, + 'filter-metric:s' => { name => 'filter_metric' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{volume_id}) || $self->{option_results}->{volume_id} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --volumeid option."); + $self->{output}->option_exit(); + }; + + foreach my $instance (@{$self->{option_results}->{volume_id}}) { + if ($instance ne '') { + push @{$self->{aws_instance}}, $instance; + }; + } + + $self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600; + $self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60; + + $self->{aws_statistics} = ['Average']; + if (defined($self->{option_results}->{statistic})) { + $self->{aws_statistics} = []; + foreach my $stat (@{$self->{option_results}->{statistic}}) { + if ($stat ne '') { + push @{$self->{aws_statistics}}, ucfirst(lc($stat)); + } + } + }; + foreach my $metric (keys %metrics_mapping) { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + push @{$self->{aws_metrics}}, $metric; + }; +} + +sub manage_selection { + my ($self, %options) = @_; + + my %metric_results; + foreach my $instance (@{$self->{aws_instance}}) { + $metric_results{$instance} = $options{custom}->cloudwatch_get_metrics( + region => $self->{option_results}->{region}, + namespace => 'AWS/EBS', + dimensions => [ { Name => 'VolumeId', Value => $instance } ], + metrics => $self->{aws_metrics}, + statistics => $self->{aws_statistics}, + timeframe => $self->{aws_timeframe}, + period => $self->{aws_period}, + ); + + foreach my $metric (@{$self->{aws_metrics}}) { + foreach my $statistic (@{$self->{aws_statistics}}) { + next if (!defined($metric_results{$instance}->{$metric}->{lc($statistic)}) && + !defined($self->{option_results}->{zeroed})); + $self->{metrics}->{$instance}->{display} = $instance; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{display} = $statistic; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{timeframe} = $self->{aws_timeframe}; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{$metric} = + defined($metric_results{$instance}->{$metric}->{lc($statistic)}) ? + $metric_results{$instance}->{$metric}->{lc($statistic)} : 0; + } + } + } + + if (scalar(keys %{$self->{metrics}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Amazon Elastic Block Store volumes IO usage. + +Example: +perl centreon_plugins.pl --plugin=cloud::aws::ebs::plugin --custommode=awscli --mode=volumeio --region='eu-west-1' +--volumeid='vol-1234abcd' --warning-write-bytes='100000' --critical-write-bytes='200000' --warning --verbose + +See 'https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using_cloudwatch_ebs.html' for more information. + + +=over 8 + +=item B<--volumeid> + +Set the VolumeId (Required). + +=item B<--filter-metric> + +Filter on a specific metric. +Can be: VolumeReadBytes, VolumeWriteBytes + +=item B<--warning-$metric$> + +Warning thresholds ($metric$ can be: 'volume-read-bytes', 'volume-write-bytes'). + +=item B<--critical-$metric$> + +Critical thresholds ($metric$ can be: 'volume-read-bytes', 'volume-write-bytes'). + +=item B<--per-sec> + +Change the data to be unit/sec. + +=back + +=cut diff --git a/cloud/aws/ebs/mode/volumeiops.pm b/cloud/aws/ebs/mode/volumeiops.pm new file mode 100644 index 000000000..8207c81e0 --- /dev/null +++ b/cloud/aws/ebs/mode/volumeiops.pm @@ -0,0 +1,309 @@ +# +# Copyright 2020 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::ebs::mode::volumeiops; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my %metrics_mapping = ( + 'VolumeReadOps' => { + 'output' => 'IOPS Read', + 'label' => 'iops-read', + 'nlabel' => { + 'absolute' => 'ebs.volume.iops.read.count', + 'per_second' => 'ebs.volume.iops.read.countpersecond' + }, + 'unit' => '' + }, + 'VolumeWriteOps' => { + 'output' => 'IOPS Write', + 'label' => 'iops-write', + 'nlabel' => { + 'absolute' => 'ebs.volume.iops.write.count', + 'per_second' => 'ebs.volume.iops.write.countpersecond' + }, + 'unit' => '' + }, + 'VolumeThroughputPercentage' => { + 'output' => 'IOPS Throughput', + 'label' => 'iops-throughput', + 'nlabel' => { + 'absolute' => 'ebs.volume.iops.throughput.percentage', + 'per_second' => 'ebs.volume.iops.throughput.percentagepersecond' + }, + 'unit' => '%' + }, + 'VolumeConsumedReadWriteOps' => { + 'output' => 'IOPS Consumed', + 'label' => 'iops-consumed', + 'nlabel' => { + 'absolute' => 'ebs.volume.iops.consumed.count', + 'per_second' => 'ebs.volume.iops.consumed.countpersecond' + }, + 'unit' => '' + }, + 'VolumeQueueLength' => { + 'output' => 'IOPS Queue Length', + 'label' => 'iops-queue-length', + 'nlabel' => { + 'absolute' => 'ebs.volume.iops.queuelength.count', + 'per_second' => 'ebs.volume.iops.queuelength.countpersecond' + }, + 'unit' => '' + }, + +); + + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}; + $self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + return 0; +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}, + threshold => [ + { label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label} , exit_litteral => 'critical' }, + { label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}, exit_litteral => 'warning' } + ] + ); + return $exit; +} + +sub custom_metric_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + instances => $self->{instance}, + label => $metrics_mapping{$self->{result_values}->{metric}}->{label}, + nlabel => defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $metrics_mapping{$self->{result_values}->{metric}}->{nlabel}->{per_second} : + $metrics_mapping{$self->{result_values}->{metric}}->{nlabel}->{absolute}, + unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $metrics_mapping{$self->{result_values}->{metric}}->{unit} . '/s' : + $metrics_mapping{$self->{result_values}->{metric}}->{unit}, + value => sprintf("%.2f", defined($self->{instance_mode}->{option_results}->{per_sec}) ? + $self->{result_values}->{value_per_sec} : + $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + ); +} + +sub custom_metric_output { + my ($self, %options) = @_; + my $msg = ""; + + if (defined($self->{instance_mode}->{option_results}->{per_sec})) { + my ($value, $unit) = ($metrics_mapping{$self->{result_values}->{metric}}->{unit} eq 'B') ? + $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec}) : + ($self->{result_values}->{value_per_sec}, $metrics_mapping{$self->{result_values}->{metric}}->{unit}); + $msg = sprintf("%s: %.2f %s", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit . '/s'); + } else { + my ($value, $unit) = ($metrics_mapping{$self->{result_values}->{metric}}->{unit} eq 'B') ? + $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}) : + ($self->{result_values}->{value}, $metrics_mapping{$self->{result_values}->{metric}}->{unit}); + $msg = sprintf("%s: %.2f %s", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit); + } + return $msg; +} + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "'" . $options{instance_value}->{display} . "' "; +} + +sub prefix_statistics_output { + my ($self, %options) = @_; + + return "Statistic '" . $options{instance_value}->{display} . "' Metrics "; +} + +sub long_output { + my ($self, %options) = @_; + + return "AWS EBS Volume'" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metrics', type => 3, cb_prefix_output => 'prefix_metric_output', cb_long_output => 'long_output', + message_multiple => 'All EBS metrics are ok', indent_long_output => ' ', + group => [ + { name => 'statistics', display_long => 1, cb_prefix_output => 'prefix_statistics_output', + message_multiple => 'All metrics are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + foreach my $metric (keys %metrics_mapping) { + my $entry = { + label => $metrics_mapping{$metric}->{label}, + set => { + key_values => [ { name => $metric }, { name => 'timeframe' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric => $metric }, + closure_custom_output => $self->can('custom_metric_output'), + closure_custom_perfdata => $self->can('custom_metric_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold') + } + }; + push @{$self->{maps_counters}->{statistics}}, $entry; + } +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'volumeid:s@' => { name => 'volume_id' }, + 'per-sec' => { name => 'per_sec' }, + 'filter-metric:s' => { name => 'filter_metric' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{volume_id}) || $self->{option_results}->{volume_id} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --volumeid option."); + $self->{output}->option_exit(); + }; + + foreach my $instance (@{$self->{option_results}->{volume_id}}) { + if ($instance ne '') { + push @{$self->{aws_instance}}, $instance; + }; + } + + $self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600; + $self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60; + + $self->{aws_statistics} = ['Average']; + if (defined($self->{option_results}->{statistic})) { + $self->{aws_statistics} = []; + foreach my $stat (@{$self->{option_results}->{statistic}}) { + if ($stat ne '') { + push @{$self->{aws_statistics}}, ucfirst(lc($stat)); + } + } + }; + foreach my $metric (keys %metrics_mapping) { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + push @{$self->{aws_metrics}}, $metric; + }; +} + +sub manage_selection { + my ($self, %options) = @_; + + my %metric_results; + foreach my $instance (@{$self->{aws_instance}}) { + $metric_results{$instance} = $options{custom}->cloudwatch_get_metrics( + region => $self->{option_results}->{region}, + namespace => 'AWS/EBS', + dimensions => [ { Name => 'VolumeId', Value => $instance } ], + metrics => $self->{aws_metrics}, + statistics => $self->{aws_statistics}, + timeframe => $self->{aws_timeframe}, + period => $self->{aws_period}, + ); + + foreach my $metric (@{$self->{aws_metrics}}) { + foreach my $statistic (@{$self->{aws_statistics}}) { + next if (!defined($metric_results{$instance}->{$metric}->{lc($statistic)}) && + !defined($self->{option_results}->{zeroed})); + $self->{metrics}->{$instance}->{display} = $instance; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{display} = $statistic; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{timeframe} = $self->{aws_timeframe}; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{$metric} = + defined($metric_results{$instance}->{$metric}->{lc($statistic)}) ? + $metric_results{$instance}->{$metric}->{lc($statistic)} : 0; + } + } + } + + if (scalar(keys %{$self->{metrics}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Amazon Elastic Block Store volumes IOPS. + +Example: +perl centreon_plugins.pl --plugin=cloud::aws::ebs::plugin --custommode=awscli --mode=volumeiops --region='eu-west-1' +--volumeid='vol-1234abcd' --warning-iops-queue-length='100' --critical-iops-queue-length='200' --warning --verbose + +See 'https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using_cloudwatch_ebs.html' for more information. + + +=over 8 + +=item B<--volumeid> + +Set the VolumeId (Required). + +=item B<--filter-metric> + +Filter on a specific metric. +Can be: VolumeReadOps, VolumeWriteOps, VolumeThroughputPercentage, VolumeConsumedReadWriteOps + +=item B<--warning-$metric$> + +Warning thresholds ($metric$ can be: 'iops-read', 'iops-write', 'iops-throughput', 'iops-consumed-rw', 'iops-queue-length'). + +=item B<--critical-$metric$> + +Critical thresholds ($metric$ can be: 'iops-read', 'iops-write', 'iops-throughput', 'iops-consumed-rw', 'iops-queue-length'). + +=item B<--per-sec> + +Change the data to be unit/sec. + +=back + +=cut diff --git a/cloud/aws/ebs/mode/volumetime.pm b/cloud/aws/ebs/mode/volumetime.pm new file mode 100644 index 000000000..aea3122e9 --- /dev/null +++ b/cloud/aws/ebs/mode/volumetime.pm @@ -0,0 +1,270 @@ +# +# Copyright 2020 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::ebs::mode::volumetime; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my %metrics_mapping = ( + 'VolumeTotalReadTime' => { + 'output' => 'Total Read Time', + 'label' => 'total-read-time', + 'nlabel' => { 'absolute' => 'ebs.volume.totalread.time.second' }, + 'unit' => 's' + }, + 'VolumeTotalWriteTime' => { + 'output' => 'Total Write Time', + 'label' => 'total-write-time', + 'nlabel' => { 'absolute' => 'ebs.volume.totalwrite.time.second' }, + 'unit' => 's' + }, + 'VolumeIdleTime' => { + 'output' => 'Idle Time', + 'label' => 'idle-time', + 'nlabel' => { 'absolute' => 'ebs.volume.idle.time.second' }, + 'unit' => 's', + 'display_percent' => 1 + } +); + + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'}; + $self->{result_values}->{period} = $options{new_datas}->{$self->{instance} . '_period'}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + return 0; +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => $self->{result_values}->{value}, + threshold => [ + { label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label} , exit_litteral => 'critical' }, + { label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}, exit_litteral => 'warning' } + ] + ); + return $exit; +} + +sub custom_metric_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + instances => $self->{instance}, + label => $metrics_mapping{$self->{result_values}->{metric}}->{label}, + nlabel => $metrics_mapping{$self->{result_values}->{metric}}->{nlabel}->{absolute}, + unit => $metrics_mapping{$self->{result_values}->{metric}}->{unit}, + value => $self->{result_values}->{value}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $metrics_mapping{$self->{result_values}->{metric}}->{label}), + ); +} + +sub custom_metric_output { + my ($self, %options) = @_; + my $msg = ""; + + my ($value, $unit) = ($metrics_mapping{$self->{result_values}->{metric}}->{unit} eq 'B') ? + $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}) : + ($self->{result_values}->{value}, $metrics_mapping{$self->{result_values}->{metric}}->{unit}); + if (defined($metrics_mapping{$self->{result_values}->{metric}}->{display_percent})) { + my $percent = ($self->{result_values}->{value} / $self->{result_values}->{period} * 100); + $msg = sprintf("%s: %.2f%s (%.2f%)", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit, $percent); + } else { + $msg = sprintf("%s: %.2f%s", $metrics_mapping{$self->{result_values}->{metric}}->{output}, $value, $unit); + }; + return $msg; +} + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "'" . $options{instance_value}->{display} . "' "; +} + +sub prefix_statistics_output { + my ($self, %options) = @_; + + return "Statistic '" . $options{instance_value}->{display} . "' Metrics "; +} + +sub long_output { + my ($self, %options) = @_; + + return "AWS EBS Volume'" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metrics', type => 3, cb_prefix_output => 'prefix_metric_output', cb_long_output => 'long_output', + message_multiple => 'All EBS metrics are ok', indent_long_output => ' ', + group => [ + { name => 'statistics', display_long => 1, cb_prefix_output => 'prefix_statistics_output', + message_multiple => 'All metrics are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + foreach my $metric (keys %metrics_mapping) { + my $entry = { + label => $metrics_mapping{$metric}->{label}, + set => { + key_values => [ { name => $metric }, { name => 'timeframe' }, { name => 'display' }, { name => 'period'} ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric => $metric }, + closure_custom_output => $self->can('custom_metric_output'), + closure_custom_perfdata => $self->can('custom_metric_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold') + } + }; + push @{$self->{maps_counters}->{statistics}}, $entry; + } +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'volumeid:s@' => { name => 'volume_id' }, + 'filter-metric:s' => { name => 'filter_metric' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{volume_id}) || $self->{option_results}->{volume_id} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --volumeid option."); + $self->{output}->option_exit(); + }; + + foreach my $instance (@{$self->{option_results}->{volume_id}}) { + if ($instance ne '') { + push @{$self->{aws_instance}}, $instance; + }; + } + + $self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600; + $self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60; + + $self->{aws_statistics} = ['Average']; + if (defined($self->{option_results}->{statistic})) { + $self->{aws_statistics} = []; + foreach my $stat (@{$self->{option_results}->{statistic}}) { + if ($stat ne '') { + push @{$self->{aws_statistics}}, ucfirst(lc($stat)); + } + } + }; + foreach my $metric (keys %metrics_mapping) { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + push @{$self->{aws_metrics}}, $metric; + }; +} + +sub manage_selection { + my ($self, %options) = @_; + + my %metric_results; + foreach my $instance (@{$self->{aws_instance}}) { + $metric_results{$instance} = $options{custom}->cloudwatch_get_metrics( + region => $self->{option_results}->{region}, + namespace => 'AWS/EBS', + dimensions => [ { Name => 'VolumeId', Value => $instance } ], + metrics => $self->{aws_metrics}, + statistics => $self->{aws_statistics}, + timeframe => $self->{aws_timeframe}, + period => $self->{aws_period}, + ); + + foreach my $metric (@{$self->{aws_metrics}}) { + foreach my $statistic (@{$self->{aws_statistics}}) { + next if (!defined($metric_results{$instance}->{$metric}->{lc($statistic)}) && + !defined($self->{option_results}->{zeroed})); + $self->{metrics}->{$instance}->{display} = $instance; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{display} = $statistic; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{timeframe} = $self->{aws_timeframe}; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{period} = $self->{aws_period}; + $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{$metric} = + defined($metric_results{$instance}->{$metric}->{lc($statistic)}) ? + $metric_results{$instance}->{$metric}->{lc($statistic)} : 0; + } + } + } + + if (scalar(keys %{$self->{metrics}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Amazon Elastic Block Store volumes operations time. + +Example: +perl centreon_plugins.pl --plugin=cloud::aws::ebs::plugin --custommode=awscli --mode=volumetime --region='eu-west-1' +--volumeid='vol-1234abcd' --warning-write-time='40' --critical-write-time='50' --warning --verbose + +See 'https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using_cloudwatch_ebs.html' for more information. + + +=over 8 + +=item B<--volumeid> + +Set the VolumeId (Required). + +=item B<--filter-metric> + +Filter on a specific metric. +Can be: VolumeTotalReadTime, VolumeTotalWriteTime, VolumeIdleTime + +=item B<--warning-$metric$> + +Warning thresholds ($metric$ can be: 'total-read-time', 'total-write-time', 'idle-time'). + +=item B<--critical-$metric$> + +Critical thresholds ($metric$ can be: 'total-read-time', 'total-write-time', 'idle-time'). + +=back + +=cut diff --git a/cloud/aws/ebs/plugin.pm b/cloud/aws/ebs/plugin.pm index 324a9fc65..c0a96660e 100644 --- a/cloud/aws/ebs/plugin.pm +++ b/cloud/aws/ebs/plugin.pm @@ -29,11 +29,13 @@ sub new { my $self = $class->SUPER::new( package => __PACKAGE__, %options ); bless $self, $class; - $self->{version} = '0.1'; + $self->{version} = '1.0'; %{ $self->{modes} } = ( - 'volume-throughput' => 'cloud::aws::ebs::mode::volumethroughput', - 'volume-iops' => 'cloud::aws::ebs::mode::volumeiops', - 'volume-time' => 'cloud::aws::ebs::mode::volumetime', + 'discovery' => 'cloud::aws::ebs::mode::discovery', + 'list-volumes' => 'cloud::aws::ebs::mode::listvolumes', + 'volumeio' => 'cloud::aws::ebs::mode::volumeio', + 'volumeiops' => 'cloud::aws::ebs::mode::volumeiops', + 'volumetime' => 'cloud::aws::ebs::mode::volumetime' ); $self->{custom_modes}{paws} = 'cloud::aws::custom::paws';