Merge pull request #248 from golgoth31/CloudAmazon

Cloud amazon
This commit is contained in:
qgarnier 2015-12-18 16:21:59 +01:00
commit 02a9aa46af
3 changed files with 146 additions and 145 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.project .project
.sync-config.cson

View File

@ -29,12 +29,12 @@ use POSIX;
use JSON; use JSON;
my $CloudwatchMetrics = { my $CloudwatchMetrics = {
cpu => "cloud::aws::mode::metrics::ec2instancecpu", cpu => "cloud::aws::mode::metrics::ec2instancecpu",
traffic => "cloud::aws::mode::metrics::ec2instancenetwork", traffic => "cloud::aws::mode::metrics::ec2instancenetwork",
cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage", cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage",
cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance", cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance",
bucketsize => "cloud::aws::mode::metrics::s3bucketsize", bucketsize => "cloud::aws::mode::metrics::s3bucketsize",
rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu", rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu",
}; };
my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount"; my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount";
@ -45,24 +45,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} = {};
@ -70,30 +71,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, ...)."
@ -101,7 +98,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, ...)."
@ -109,27 +107,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."
@ -140,37 +140,43 @@ 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 %array1 = map { $_ => 1 } @excludetab; my @excludetab = split(/,/, $self->{option_results}->{'exclude-statistics'});
@{$self->{option_results}->{statisticstab}} = grep { not $array1{$_} } @{$self->{option_results}->{statisticstab}}; my %array1 = map { $_ => 1 } @excludetab;
@{$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 = (
{ {
'Value' => $self->{option_results}->{object}, 'Value' => $self->{option_results}->{object},
'Name' => $metric->{ObjectName} 'Name' => $metric->{ObjectName}
} }
); );
if ( defined( $metric->{ExtraDimensions} ) ) { if (defined($metric->{ExtraDimensions}))
{
push @Dimensions, $metric->{ExtraDimensions}; push @Dimensions, $metric->{ExtraDimensions};
} }
@ -186,10 +192,11 @@ 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} } ) ) {
centreon::plugins::misc::mymodule_load(output => $options{output}, module => $CloudwatchMetrics->{$self->{option_results}->{metric}}, centreon::plugins::misc::mymodule_load(output => $options{output}, module => $CloudwatchMetrics->{$self->{option_results}->{metric}},
@ -197,52 +204,34 @@ sub run {
my $func = $CloudwatchMetrics->{$self->{option_results}->{metric}}->can('cloudwatchCheck'); my $func = $CloudwatchMetrics->{$self->{option_results}->{metric}}->can('cloudwatchCheck');
$func->($self); $func->($self);
} else { } else {
$self->{output} $self->{output}->add_option_msg( short_msg => "Wrong option. Cannot find metric '" . $self->{option_results}->{metric} . "'." );
->add_option_msg( short_msg => "Wrong option. Cannot find 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,33 +17,31 @@
# 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-service:s" => {name => 'exclude_service'},
"ec2-state:s"=>{name=> 'ec2_state', default => $EC2_instance_states},
"ec2-exclude-state:s" => {name => 'ec2_exclude_state'},
} }
); );
$self->{result} = {}; $self->{result} = {};
@ -59,6 +57,13 @@ sub api_request {
my ($self, %options) = @_; my ($self, %options) = @_;
@{$self->{option_results}->{servicetab}} = split( /,/, $self->{option_results}->{service} ); @{$self->{option_results}->{servicetab}} = split( /,/, $self->{option_results}->{service} );
# exclusions
if (defined($self->{option_results}->{exclude_service})) {
my @excludetab = split /,/, $self->{option_results}->{exclude_service};
my %array1 = map { $_ => 1 } @excludetab;
@{$self->{option_results}->{servicetab}} = grep { not $array1{$_} } @{$self->{option_results}->{servicetab}};
}
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') {
@ -74,21 +79,28 @@ sub api_request {
} }
} }
sub EC2 { sub EC2
my ( $self, %options ) = @_; {
my ($self, %options) = @_;
my $apiRequest = { my $apiRequest = {
'command' => 'ec2', 'command' => 'ec2',
'subcommand' => 'describe-instances', 'subcommand' => 'describe-instances',
}; };
# Building JSON # Building JSON
my @ec2_statestab = split(/,/, $self->{option_results}->{ec2_state});
# exclusions
if (defined($self->{option_results}->{ec2_exclude_state})) {
my @excludetab = split /,/, $self->{option_results}->{ec2_exclude_state};
my %array1 = map { $_ => 1 } @excludetab;
@ec2_statestab = grep { not $array1{$_} } @ec2_statestab;
}
$apiRequest->{json} = { $apiRequest->{json} = {
'DryRun' => JSON::false, 'DryRun' => JSON::false,
'Filters' => [ 'Filters' => [
{ {
'Name' => 'instance-state-name', 'Name' => 'instance-state-name',
'Values' => @EC2_instance_states, 'Values' => [@ec2_statestab],
} }
], ],
}; };
@ -107,17 +119,17 @@ sub EC2 {
$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',
@ -137,15 +149,15 @@ sub S3 {
# Compute data # Compute data
foreach my $bucket (@buckets) { foreach my $bucket (@buckets) {
$self->{result}->{'S3'}->{ $bucket->{Name} } = $self->{result}->{'S3'}->{$bucket->{Name}} =
{ 'Creation date' => $bucket->{CreationDate} }; {'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',
@ -172,25 +184,25 @@ sub disco_format {
$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,
state => $self->{result}->{$service}->{$device}->{State}, state => $self->{result}->{$service}->{$device}->{State},
service => $service, service => $service,
); );
} }
} }
} }
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
@ -199,21 +211,14 @@ sub run {
foreach my $device (keys %{$self->{result}->{$service}}) { foreach my $device (keys %{$self->{result}->{$service}}) {
my $output = $device . " ["; my $output = $device . " [";
foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) { foreach my $value (sort(keys %{$self->{result}->{$service}->{$device}})) {
$output = $output = $output . $value . " = " . $self->{result}->{$service}->{$device}->{$value} . ", ";
$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,
@ -221,9 +226,7 @@ sub run {
); );
$self->{output}->exit(); $self->{output}->exit();
} }
1; 1;
__END__ __END__
=head1 MODE =head1 MODE
@ -236,10 +239,18 @@ List your EC2, RDS instance and S3 buckets
(optional) List one particular service. (optional) List one particular service.
=item B<--exclude> =item B<--exclude-service>
(optional) Service to exclude from the scan. (optional) Service to exclude from the scan.
=item B<--ec2-state>
(optional) State to request (default: 'running','stopped')
=item B<--ec2-exclude-state>
(optional) State to exclude from the scan.
=back =back
=cut =cut