diff --git a/cloud/aws/custom/awscli.pm b/cloud/aws/custom/awscli.pm index a4a943446..2359f553f 100644 --- a/cloud/aws/custom/awscli.pm +++ b/cloud/aws/custom/awscli.pm @@ -274,6 +274,43 @@ sub ec2_get_instances_status { return $instance_results; } +sub rds_get_instances_status_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + my $cmd_options = "rds describe-db-instances --region $options{region} --output json"; + return $cmd_options; +} + +sub rds_get_instances_status { + my ($self, %options) = @_; + + my $cmd_options = $self->rds_get_instances_status_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_instances; + eval { + $list_instances = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + my $instance_results = {}; + foreach (@{$list_instances->{DBInstances}}) { + $instance_results->{$_->{DBInstanceIdentifier}} = { state => $_->{DBInstanceStatus} }; + } + + return $instance_results; +} + 1; __END__ diff --git a/cloud/aws/custom/paws.pm b/cloud/aws/custom/paws.pm index 3f9b7a054..f6d00b583 100644 --- a/cloud/aws/custom/paws.pm +++ b/cloud/aws/custom/paws.pm @@ -222,6 +222,25 @@ sub ec2_get_instances_status { return $instance_results; } +sub rds_get_instances_status { + my ($self, %options) = @_; + + my $instance_results = {}; + eval { + my $rds = Paws->service('RDS', region => $options{region}); + my $instances = $rds->DescribeDBInstances(); + foreach (@{$instances->{DBInstances}}) { + $instance_results->{$_->{DBInstanceIdentifier}} = { state => $_->{DBInstanceStatus} }; + } + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "error: $@"); + $self->{output}->option_exit(); + } + + return $instance_results; +} + 1; __END__ diff --git a/cloud/aws/mode/ec2instancestatus.pm b/cloud/aws/mode/ec2instancestatus.pm index 108066fba..d85679aca 100644 --- a/cloud/aws/mode/ec2instancestatus.pm +++ b/cloud/aws/mode/ec2instancestatus.pm @@ -80,7 +80,7 @@ sub set_counters { $self->{maps_counters}->{global} = [ { label => 'total-pending', set => { key_values => [ { name => 'pending' } ], - output_template => "pending : '%s'", + output_template => "pending : %s", perfdatas => [ { label => 'total_pending', value => 'pending_absolute', template => '%d', min => 0 }, ], @@ -88,7 +88,7 @@ sub set_counters { }, { label => 'total-running', set => { key_values => [ { name => 'running' } ], - output_template => "running : '%s'", + output_template => "running : %s", perfdatas => [ { label => 'total_running', value => 'running_absolute', template => '%d', min => 0 }, ], @@ -96,7 +96,7 @@ sub set_counters { }, { label => 'total-shutting-down', set => { key_values => [ { name => 'shutting-down' } ], - output_template => "shutting-down : '%s'", + output_template => "shutting-down : %s", perfdatas => [ { label => 'total_shutting_down', value => 'shutting-down_absolute', template => '%d', min => 0 }, ], @@ -104,7 +104,7 @@ sub set_counters { }, { label => 'total-terminated', set => { key_values => [ { name => 'terminated' } ], - output_template => "terminated : '%s'", + output_template => "terminated : %s", perfdatas => [ { label => 'total_terminated', value => 'terminated_absolute', template => '%d', min => 0 }, ], @@ -112,7 +112,7 @@ sub set_counters { }, { label => 'total-stopping', set => { key_values => [ { name => 'stopping' } ], - output_template => "stopping : '%s'", + output_template => "stopping : %s", perfdatas => [ { label => 'total_stopping', value => 'stopping_absolute', template => '%d', min => 0 }, ], @@ -120,7 +120,7 @@ sub set_counters { }, { label => 'total-stopped', set => { key_values => [ { name => 'stopped' } ], - output_template => "stopped : '%s'", + output_template => "stopped : %s", perfdatas => [ { label => 'total_stopped', value => 'stopped_absolute', template => '%d', min => 0 }, ], diff --git a/cloud/aws/mode/rdsinstancestatus.pm b/cloud/aws/mode/rdsinstancestatus.pm new file mode 100644 index 000000000..a9cbe77fa --- /dev/null +++ b/cloud/aws/mode/rdsinstancestatus.pm @@ -0,0 +1,266 @@ +# +# 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::rdsinstancestatus; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + my $label = $self->{label}; + $label =~ s/-/_/g; + if (defined($instance_mode->{option_results}->{'critical_' . $label}) && $instance_mode->{option_results}->{'critical_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'critical_' . $label}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{'warning_' . $label}) && $instance_mode->{option_results}->{'warning_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'warning_' . $label}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf('state : %s', + $self->{result_values}->{health}, $self->{result_values}->{replication_health}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, + { name => 'aws_instances', type => 1, cb_prefix_output => 'prefix_awsinstance_output', message_multiple => 'All instances are ok' }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total-available', set => { + key_values => [ { name => 'available' } ], + output_template => "available : %s", + perfdatas => [ + { label => 'total_available', value => 'available_absolute', template => '%d', min => 0 }, + ], + } + }, + { label => 'total-failed', set => { + key_values => [ { name => 'failed' } ], + output_template => "failed : %s", + perfdatas => [ + { label => 'total_failed', value => 'failed_absolute', template => '%d', min => 0 }, + ], + } + }, + { label => 'total-backing-up', set => { + key_values => [ { name => 'backing-up' } ], + output_template => "backing-up : %s", + perfdatas => [ + { label => 'total_backing_up', value => 'backing-up_absolute', template => '%d', min => 0 }, + ], + } + }, + { label => 'total-maintenance', set => { + key_values => [ { name => 'maintenance' } ], + output_template => "maintenance : %s", + perfdatas => [ + { label => 'total_maintenance', value => 'maintenance_absolute', template => '%d', min => 0 }, + ], + } + }, + { label => 'total-stopped', set => { + key_values => [ { name => 'stopped' } ], + output_template => "stopped : %s", + perfdatas => [ + { label => 'total_stopped', value => 'stopped_absolute', template => '%d', min => 0 }, + ], + } + }, + { label => 'total-storage-full', set => { + key_values => [ { name => 'storage-full' } ], + output_template => "storage-full : %s", + perfdatas => [ + { label => 'total_storage_full', value => 'storage-full_absolute', template => '%d', min => 0 }, + ], + } + }, + ]; + + $self->{maps_counters}->{aws_instances} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'state' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_nas_status_calc'), + closure_custom_output => $self->can('custom_nas_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + ]; +} + +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' }, + "filter-instanceid:s" => { name => 'filter_instanceid' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%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(); + } + + $instance_mode = $self; + $self->change_macros(); +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return "Total instances "; +} + +sub prefix_awsinstance_output { + my ($self, %options) = @_; + + return "Instance '" . $options{instance_value}->{display} . "' "; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_status', 'critical_status')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global} = { + available => 0, 'backing-up' => 0, failed => 0, maintenance => 0, stopped => 0, 'storage-full' => 0, + }; + $self->{aws_instances} = {}; + my $result = $options{custom}->rds_get_instances_status(region => $self->{option_results}->{region}); + foreach my $instance_id (keys %{$result}) { + if (defined($self->{option_results}->{filter_instanceid}) && $self->{option_results}->{filter_instanceid} ne '' && + $instance_id !~ /$self->{option_results}->{filter_instanceid}/) { + $self->{output}->output_add(long_msg => "skipping '" . $instance_id . "': no matching filter.", debug => 1); + next; + } + + $self->{aws_instances}->{$instance_id} = { + display => $instance_id, + state => $result->{$instance_id}->{state}, + }; + $self->{global}->{$result->{$instance_id}->{state}}++ if (defined($self->{global}->{$result->{$instance_id}->{state}})); + } + + if (scalar(keys %{$self->{aws_instances}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No aws rds instance found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check RDS instances status. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^total-available$' + +=item B<--filter-instanceid> + +Filter by instance id (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{state}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{state}, %{display} + +=item B<--warning-*> + +Threshold warning. +Can be: 'total-available', 'total-backing-up', 'total-failed', +'total-maintenance', 'total-stopped', 'total-storage-full'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'total-available', 'total-backing-up', 'total-failed', +'total-maintenance', 'total-stopped', 'total-storage-full'. + +=back + +=cut diff --git a/cloud/aws/plugin.pm b/cloud/aws/plugin.pm index 97bdd955d..bf8e60d84 100644 --- a/cloud/aws/plugin.pm +++ b/cloud/aws/plugin.pm @@ -35,6 +35,7 @@ sub new { 'cloudwatch-get-metrics' => 'cloud::aws::mode::cloudwatchgetmetrics', 'cloudwatch-list-metrics' => 'cloud::aws::mode::cloudwatchlistmetrics', 'ec2-instance-status' => 'cloud::aws::mode::ec2instancestatus', + 'rds-instance-status' => 'cloud::aws::mode::rdsinstancestatus', ); $self->{custom_modes}{paws} = 'cloud::aws::custom::paws';