use 'execute' function from centreon

This commit is contained in:
David Sabatié 2015-09-15 18:02:15 +02:00
parent c1eaedb612
commit c6284de6d6

View File

@ -24,11 +24,13 @@ use strict;
use warnings;
use JSON;
use Data::Dumper;
use centreon::plugins::misc;
sub new {
my ( $class, %options ) = @_;
my $self = {};
bless $self, $class;
# $options{options} = options object
# $options{output} = output object
# $options{exit_value} = integer
@ -44,12 +46,18 @@ sub new {
}
if ( !defined( $options{noptions} ) ) {
$options{options}->add_options(arguments =>
{
$options{options}->add_options( arguments => {
"region:s" => { name => 'region' },
"command:s" => { name => 'command', default => 'aws' },
"command-path:s" => { name => 'command_path' },
"sudo" => { name => 'sudo' },
} );
}
$options{options}->add_help(package => __PACKAGE__, sections => 'AWSCLI OPTIONS', once => 1);
$options{options}->add_help(
package => __PACKAGE__,
sections => 'AWSCLI OPTIONS',
once => 1
);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
@ -60,6 +68,7 @@ sub new {
# Method to manage multiples
sub set_options {
my ( $self, %options ) = @_;
# options{options_result}
$self->{option_results} = $options{option_results};
@ -68,12 +77,14 @@ sub set_options {
# Method to manage multiples
sub set_defaults {
my ( $self, %options ) = @_;
# options{default}
# Manage default value
foreach ( keys %{ $options{default} } ) {
if ( $_ eq $self->{mode} ) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
for ( my $i = 0 ; $i < scalar( @{ $options{default}->{$_} } ) ; $i++ )
{
foreach my $opt ( keys %{ $options{default}->{$_}[$i] } ) {
if ( !defined( $self->{option_results}->{$opt}[$i] ) ) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
@ -98,7 +109,7 @@ sub execReq {
}
my $json = JSON->new;
my $json_encoded = "aws ".$options->{command}." ".$options->{subcommand};
my $json_encoded = $options->{command} . " " . $options->{subcommand};
if ( defined( $self->{option_results}->{region} ) ) {
$json_encoded = $json_encoded . " --region '". $self->{option_results}->{region} . "'";
}
@ -106,21 +117,35 @@ sub execReq {
$json_encoded = $json_encoded . " --cli-input-json '" . $json->encode( $options->{json} ) . "'";
}
$self->{option_results}->{timeout} = 30;
if ( $options->{output} eq 'text' ) {
my @return = `$json_encoded`;
chomp(@return);
$self->{stdout} = 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 => $json_encoded
);
my @return = split /\n/, $self->{stdout};
$jsoncontent = $json->encode( [@return] );
}
else {
$jsoncontent = `$json_encoded`;
$jsoncontent = 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 => $json_encoded
);
}
if ( $? > 0 ) {
$self->{output}->add_option_msg( short_msg => "Cannot run aws" );
$self->{output}->option_exit();
}
eval {
$self->{command_return} = $json->decode($jsoncontent);
};
eval { $self->{command_return} = $json->decode($jsoncontent); };
if ($@) {
$self->{output}->add_option_msg( short_msg => "Cannot decode json answer" );
$self->{output}->option_exit();
@ -146,6 +171,18 @@ AWS cli API custom mode
(optional) The region to use (should be configured directly in aws).
=item B<--sudo>
Use 'sudo' to execute the command.
=item B<--command>
(optional) Command to get information (Default: 'aws').
=item B<--command-path>
(optional) Command path (Default: none).
=back
=head1 DESCRIPTION