centreon-plugins/cloud/aws/ec2/mode/instancesstatus.pm

229 lines
7.8 KiB
Perl
Raw Normal View History

2017-12-15 10:42:49 +01:00
#
2019-01-09 09:57:11 +01:00
# Copyright 2019 Centreon (http://www.centreon.com/)
2017-12-15 10:42:49 +01:00
#
# 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.
#
2018-04-09 11:49:57 +02:00
package cloud::aws::ec2::mode::instancesstatus;
2017-12-15 10:42:49 +01:00
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
2017-12-15 10:42:49 +01:00
sub custom_status_output {
my ($self, %options) = @_;
2018-04-09 11:49:57 +02:00
my $msg = sprintf('state: %s, status: %s', $self->{result_values}->{state}, $self->{result_values}->{status});
2017-12-15 10:42:49 +01:00
return $msg;
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'};
2018-04-09 11:49:57 +02:00
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
2017-12-15 10:42:49 +01:00
$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' },
2019-09-10 13:39:18 +02:00
{ name => 'aws_instances', type => 1, cb_prefix_output => 'prefix_awsinstance_output',
message_multiple => 'All instances are ok' },
2017-12-15 10:42:49 +01:00
];
$self->{maps_counters}->{global} = [
2019-09-10 13:39:18 +02:00
{ label => 'pending', nlabel => 'ec2.instances.status.pending.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'pending' } ],
2019-09-10 13:39:18 +02:00
output_template => "Pending : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'pending_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
2019-09-10 13:39:18 +02:00
{ label => 'running', nlabel => 'ec2.instances.status.running.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'running' } ],
2019-09-10 13:39:18 +02:00
output_template => "Running : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'running_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
2019-09-10 13:39:18 +02:00
{ label => 'shuttingdown', nlabel => 'ec2.instances.status.shuttingdown.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'shutting-down' } ],
2019-09-10 13:39:18 +02:00
output_template => "Shutting Down : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'shutting-down_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
2019-09-10 13:39:18 +02:00
{ label => 'terminated', nlabel => 'ec2.instances.status.terminated.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'terminated' } ],
2019-09-10 13:39:18 +02:00
output_template => "Terminated : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'terminated_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
2019-09-10 13:39:18 +02:00
{ label => 'stopping', nlabel => 'ec2.instances.status.stopping.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'stopping' } ],
2019-09-10 13:39:18 +02:00
output_template => "Stopping : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'stopping_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
2019-09-10 13:39:18 +02:00
{ label => 'stopped', nlabel => 'ec2.instances.status.stopped.count', set => {
2017-12-15 10:42:49 +01:00
key_values => [ { name => 'stopped' } ],
2019-09-10 13:39:18 +02:00
output_template => "Stopped : %s",
2017-12-15 10:42:49 +01:00
perfdatas => [
2019-09-10 13:39:18 +02:00
{ value => 'stopped_absolute', template => '%d', min => 0 },
2017-12-15 10:42:49 +01:00
],
}
},
];
$self->{maps_counters}->{aws_instances} = [
{ label => 'status', threshold => 0, set => {
2018-04-09 11:49:57 +02:00
key_values => [ { name => 'state' }, { name => 'status' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
2017-12-15 10:42:49 +01:00
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
2017-12-15 10:42:49 +01:00
}
},
];
}
sub new {
my ($class, %options) = @_;
2019-09-10 13:39:18 +02:00
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
2017-12-15 10:42:49 +01:00
bless $self, $class;
2019-09-10 13:39:18 +02:00
$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 => '' },
});
2017-12-15 10:42:49 +01:00
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();
}
$self->change_macros(macros => ['warning_status', 'critical_status']);
2017-12-15 10:42:49 +01:00
}
sub prefix_global_output {
my ($self, %options) = @_;
return "Total instances ";
}
sub prefix_awsinstance_output {
my ($self, %options) = @_;
return "Instance '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection {
my ($self, %options) = @_;
$self->{global} = {
pending => 0, running => 0, 'shutting-down' => 0, terminated => 0, stopping => 0, stopped => 0,
};
$self->{aws_instances} = {};
my $result = $options{custom}->ec2_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},
2018-04-09 11:49:57 +02:00
status => $result->{$instance_id}->{status},
2017-12-15 10:42:49 +01:00
};
$self->{global}->{$result->{$instance_id}->{state}}++;
}
if (scalar(keys %{$self->{aws_instances}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No aws instance found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check EC2 instances status.
2018-04-09 11:49:57 +02:00
Example:
perl centreon_plugins.pl --plugin=cloud::aws::ec2::plugin --custommode=paws --mode=instances-status --region='eu-west-1'
2019-09-10 13:39:18 +02:00
--filter-instanceid='.*' --filter-counters='^running$' --critical-running='10' --verbose
2018-04-09 11:49:57 +02:00
See 'https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstanceStatus.html' for more informations.
2017-12-15 10:42:49 +01:00
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
2019-09-10 13:39:18 +02:00
Example: --filter-counters='^running$'
2017-12-15 10:42:49 +01:00
=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}
2019-09-10 13:39:18 +02:00
=item B<--warning-*> B<--critical-*>
2017-12-15 10:42:49 +01:00
Threshold warning.
2019-09-10 13:39:18 +02:00
Can be: 'pending', 'running', 'shuttingdown',
'terminated', 'stopping', 'stopped'.
2017-12-15 10:42:49 +01:00
=back
=cut