This commit is contained in:
David Sabatié 2015-11-06 11:53:54 +01:00
parent f44d56b7e3
commit e98771a033
2 changed files with 172 additions and 185 deletions

View File

@ -47,7 +47,8 @@ my $apiRequest = {
'subcommand' => 'get-metric-statistics',
};
sub new {
sub new
{
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
@ -72,7 +73,8 @@ sub new {
return $self;
}
sub check_options {
sub check_options
{
my ($self, %options) = @_;
$self->SUPER::init(%options);
@ -80,22 +82,17 @@ sub check_options {
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0)
{
$self->{output}
->add_option_msg( short_msg => "Wrong warning threshold '"
. $self->{option_results}->{warning}
. "'." );
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
$self->{output}->option_exit();
}
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0)
{
$self->{output}
->add_option_msg( short_msg => "Wrong critical threshold '"
. $self->{option_results}->{critical}
. "'." );
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
$self->{output}->option_exit();
}
if ( !defined( $self->{option_results}->{metric} ) ) {
if (!defined($self->{option_results}->{metric}))
{
$self->{output}->add_option_msg(
severity => 'UNKNOWN',
short_msg => "Please give a metric to watch (cpu, disk, ...)."
@ -103,7 +100,8 @@ sub check_options {
$self->{output}->option_exit();
}
if ( !defined( $self->{option_results}->{object} ) ) {
if (!defined($self->{option_results}->{object}))
{
$self->{output}->add_option_msg(
severity => 'UNKNOWN',
short_msg => "Please give the object to request (instanceid, ...)."
@ -111,27 +109,29 @@ sub check_options {
$self->{output}->option_exit();
}
if ( !defined( $self->{option_results}->{endtime} ) ) {
$self->{option_results}->{endtime} =
strftime( "%FT%H:%M:%S.000Z",
gmtime( $self->{option_results}->{def_endtime} ) );
if (!defined($self->{option_results}->{endtime}))
{
$self->{option_results}->{endtime} = strftime("%FT%H:%M:%S.000Z", gmtime($self->{option_results}->{def_endtime}));
}
if ( !defined( $self->{option_results}->{starttime} ) ) {
$self->{option_results}->{starttime} =
strftime( "%FT%H:%M:%S.000Z",
gmtime( $self->{option_results}->{def_endtime} - 600 ) );
if (!defined($self->{option_results}->{starttime}))
{
$self->{option_results}->{starttime} = strftime("%FT%H:%M:%S.000Z", gmtime($self->{option_results}->{def_endtime} - 600));
}
# Getting some parameters
# statistics
if ( $self->{option_results}->{statistics} eq 'all' ) {
if ($self->{option_results}->{statistics} eq 'all')
{
@{$self->{option_results}->{statisticstab}} = split(/,/, $StatisticsType);
}
else {
else
{
@{$self->{option_results}->{statisticstab}} = split(/,/, $self->{option_results}->{statistics});
foreach my $curstate ( @{ $self->{option_results}->{statisticstab} } ) {
if ( !grep { /^$curstate$/ } split( /,/, $StatisticsType ) ) {
foreach my $curstate (@{$self->{option_results}->{statisticstab}})
{
if (!grep { /^$curstate$/ } split(/,/, $StatisticsType))
{
$self->{output}->add_option_msg(
severity => 'UNKNOWN',
short_msg => "The statistic $curstate doesn't exist."
@ -142,26 +142,31 @@ sub check_options {
}
# exclusions
if (defined($self->{option_results}->{'exclude-statistics'})){
if (defined($self->{option_results}->{'exclude-statistics'}))
{
my @excludetab = split(/,/, $self->{option_results}->{'exclude-statistics'});
my %array1 = map { $_ => 1 } @excludetab;
@{$self->{option_results}->{statisticstab}} = grep { not $array1{$_} } @{$self->{option_results}->{statisticstab}};
}
# Force Average statistic
if ( ! grep $_ eq 'Average', @{$self->{option_results}->{statisticstab}} ) {
if (!grep $_ eq 'Average', @{$self->{option_results}->{statisticstab}})
{
my $statistics = join(',', @{$self->{option_results}->{statisticstab}});
if ( ! $statistics eq '' ) {
if (!$statistics eq '')
{
$statistics = $statistics . ',Average';
}
else {
else
{
$statistics = 'Average';
}
@{$self->{option_results}->{statisticstab}} = split(/,/, $statistics);
}
}
sub manage_selection {
sub manage_selection
{
my ($self, $metric) = @_;
my @result;
@ -172,7 +177,8 @@ sub manage_selection {
}
);
if ( defined( $metric->{ExtraDimensions} ) ) {
if (defined($metric->{ExtraDimensions}))
{
push @Dimensions, $metric->{ExtraDimensions};
}
@ -188,7 +194,8 @@ sub manage_selection {
};
}
sub run {
sub run
{
my ($self, %options) = @_;
my ($msg, $exit_code, $awsapi);
@ -198,53 +205,36 @@ sub run {
load $CloudwatchMetrics->{$self->{option_results}->{metric}}, qw/cloudwatchCheck/;
cloudwatchCheck($self);
}
else {
$self->{output}
->add_option_msg( short_msg => "Wrong option. Cannot find metric '"
. $self->{option_results}->{metric}
. "'." );
else
{
$self->{output}->add_option_msg(short_msg => "Wrong option. Cannot find metric '" . $self->{option_results}->{metric} . "'.");
$self->{output}->option_exit();
}
foreach my $metric ( @{ $self->{metric} } ) {
foreach my $metric (@{$self->{metric}})
{
$self->manage_selection($metric);
$awsapi = $options{custom};
$self->{command_return} = $awsapi->execReq($apiRequest);
$self->{output}->perfdata_add(
label => sprintf(
$metric->{Labels}->{PerfData},
unit => $metric->{Labels}->{Unit}
),
value => sprintf(
$metric->{Labels}->{Value},
$self->{command_return}->{Datapoints}[0]->{Average}
),
label => sprintf($metric->{Labels}->{PerfData}, unit => $metric->{Labels}->{Unit}),
value => sprintf($metric->{Labels}->{Value}, $self->{command_return}->{Datapoints}[0]->{Average}),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
#min => 0,
#max => 100
);
$exit_code = $self->{perfdata}->threshold_check(
value => $self->{command_return}->{Datapoints}[0]->{Average},
threshold => [
{ label => 'critical', 'exit_litteral' => 'critical' },
{ label => 'warning', exit_litteral => 'warning' }
]
threshold => [{label => 'critical', 'exit_litteral' => 'critical'}, {label => 'warning', exit_litteral => 'warning'}]
);
$self->{output}->output_add(
long_msg => sprintf(
$metric->{Labels}->{LongOutput},
$self->{command_return}->{Datapoints}[0]->{Average}
)
);
$self->{output}->output_add(long_msg => sprintf($metric->{Labels}->{LongOutput}, $self->{command_return}->{Datapoints}[0]->{Average}));
$self->{output}->output_add(
severity => $exit_code,
short_msg => sprintf(
$metric->{Labels}->{ShortOutput},
$self->{command_return}->{Datapoints}[0]->{Average}
)
short_msg => sprintf($metric->{Labels}->{ShortOutput}, $self->{command_return}->{Datapoints}[0]->{Average})
);
}

View File

@ -17,29 +17,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
package cloud::aws::mode::list;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use centreon::plugins::misc;
use Data::Dumper;
use JSON;
my $AWSServices = 'EC2,S3,RDS';
my @Disco_service_tab = ('EC2', 'RDS');
my @EC2_instance_states = ['running', 'stopped'];
my $awsapi;
sub new {
sub new
{
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
$options{options}->add_options(
arguments => {
"service:s" => {name => 'service', default => $AWSServices},
@ -50,38 +46,43 @@ sub new {
return $self;
}
sub check_options {
sub check_options
{
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub api_request {
sub api_request
{
my ($self, %options) = @_;
@{$self->{option_results}->{servicetab}} =
split(/,/, $self->{option_results}->{service});
foreach my $service ( @{ $self->{option_results}->{servicetab} } ) {
foreach my $service (@{$self->{option_results}->{servicetab}})
{
$self->{result}->{count}->{$service} = '0';
if ( $service eq 'EC2' ) {
if ($service eq 'EC2')
{
$self->EC2(%options);
}
elsif ( $service eq 'S3' ) {
elsif ($service eq 'S3')
{
$self->S3(%options);
}
elsif ( $service eq 'RDS' ) {
elsif ($service eq 'RDS')
{
$self->RDS(%options);
}
else {
$self->{output}->add_option_msg(
short_msg => "Service $service doesn't exists" );
else
{
$self->{output}->add_option_msg(short_msg => "Service $service doesn't exists");
$self->{output}->option_exit();
}
}
}
sub EC2 {
sub EC2
{
my ($self, %options) = @_;
my $apiRequest = {
'command' => 'ec2',
'subcommand' => 'describe-instances',
@ -103,26 +104,27 @@ sub EC2 {
$self->{command_return} = $awsapi->execReq($apiRequest);
# Compute data
foreach my $instance ( @{ $self->{command_return}->{Reservations} } ) {
foreach my $tags ( @{ $instance->{Instances}[0]->{Tags} } ) {
if ( $tags->{Key} eq 'Name' ) {
foreach my $instance (@{$self->{command_return}->{Reservations}})
{
foreach my $tags (@{$instance->{Instances}[0]->{Tags}})
{
if ($tags->{Key} eq 'Name')
{
$instance->{Instances}[0]->{Name} = $tags->{Value};
}
}
$self->{result}->{'EC2'}->{ $instance->{Instances}[0]->{InstanceId} } =
{
$self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} = {
'State' => $instance->{Instances}[0]->{State}->{Name},
'Name' => $instance->{Instances}[0]->{Name}
};
$self->{result}->{count}->{'EC2'}++;
}
}
sub S3 {
sub S3
{
my ($self, %options) = @_;
my (@buckets, @return) = ();
my $apiRequest = {
'command' => 's3',
'subcommand' => 'ls',
@ -134,23 +136,25 @@ sub S3 {
$self->{command_return} = $awsapi->execReq($apiRequest);
# Exec command
foreach my $line ( @{ $self->{command_return} } ) {
foreach my $line (@{$self->{command_return}})
{
my ($date, $time, $name) = split(/ /, $line);
my $creationdate = $date . " " . $time;
push(@buckets, {Name => $name, CreationDate => $creationdate});
}
# Compute data
foreach my $bucket (@buckets) {
foreach my $bucket (@buckets)
{
$self->{result}->{'S3'}->{$bucket->{Name}} =
{'Creation date' => $bucket->{CreationDate}};
$self->{result}->{count}->{'S3'}++;
}
}
sub RDS {
sub RDS
{
my ($self, %options) = @_;
my $apiRequest = {
'command' => 'rds',
'subcommand' => 'describe-db-instances',
@ -161,7 +165,8 @@ sub RDS {
$self->{command_return} = $awsapi->execReq($apiRequest);
# Compute data
foreach my $dbinstance ( @{ $self->{command_return}->{DBInstances} } ) {
foreach my $dbinstance (@{$self->{command_return}->{DBInstances}})
{
$self->{result}->{'RDS'}->{$dbinstance->{DBInstanceIdentifier}} = {
'State' => $dbinstance->{DBInstanceStatus},
'Name' => $dbinstance->{DBInstanceIdentifier}
@ -170,20 +175,21 @@ sub RDS {
}
}
sub disco_format {
sub disco_format
{
my ($self, %options) = @_;
my $names = ['name', 'id', 'state', 'service'];
$self->{output}->add_disco_format(elements => $names);
}
sub disco_show {
sub disco_show
{
my ($self, %options) = @_;
$self->api_request(%options);
foreach my $service ( @Disco_service_tab ) {
foreach my $device ( keys %{ $self->{result}->{$service} } ) {
foreach my $service (@Disco_service_tab)
{
foreach my $device (keys %{$self->{result}->{$service}})
{
$self->{output}->add_disco_entry(
name => $self->{result}->{$service}->{$device}->{Name},
id => $device,
@ -194,35 +200,28 @@ sub disco_show {
}
}
sub run {
sub run
{
my ($self, %options) = @_;
$self->api_request(%options);
# Send formated data to Centreon
foreach my $service ( @{ $self->{option_results}->{servicetab} } ) {
$self->{output}
->output_add( long_msg => sprintf( "AWS service: %s", $service ) );
foreach my $device ( keys %{ $self->{result}->{$service} } ) {
my $output = $device . " [";
foreach my $value (
sort( keys %{ $self->{result}->{$service}->{$device} } ) )
foreach my $service (@{$self->{option_results}->{servicetab}})
{
$output =
$output
. $value . " = "
. $self->{result}->{$service}->{$device}->{$value} . ", ";
$self->{output}->output_add(long_msg => sprintf("AWS service: %s", $service));
foreach my $device (keys %{$self->{result}->{$service}})
{
my $output = $device . " [";
foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}}))
{
$output = $output . $value . " = " . $self->{result}->{$service}->{$device}->{$value} . ", ";
}
$output =~ s/, $//;
$output = $output . "]";
$self->{output}->output_add(long_msg => $output);
}
$self->{output}->output_add(
short_msg => sprintf( "%s: %s",
$service, $self->{result}->{count}->{$service} )
);
$self->{output}->output_add(short_msg => sprintf("%s: %s", $service, $self->{result}->{count}->{$service}));
}
$self->{output}->display(
nolabel => 1,
force_ignore_perfdata => 1,
@ -230,9 +229,7 @@ sub run {
);
$self->{output}->exit();
}
1;
__END__
=head1 MODE