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

View File

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

View File

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