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
.sync-config.cson

View File

@ -29,12 +29,12 @@ use POSIX;
use JSON;
my $CloudwatchMetrics = {
cpu => "cloud::aws::mode::metrics::ec2instancecpu",
traffic => "cloud::aws::mode::metrics::ec2instancenetwork",
cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage",
cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance",
bucketsize => "cloud::aws::mode::metrics::s3bucketsize",
rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu",
cpu => "cloud::aws::mode::metrics::ec2instancecpu",
traffic => "cloud::aws::mode::metrics::ec2instancenetwork",
cpucreditusage => "cloud::aws::mode::metrics::ec2instancecpucreditusage",
cpucreditbalance => "cloud::aws::mode::metrics::ec2instancecpucreditbalance",
bucketsize => "cloud::aws::mode::metrics::s3bucketsize",
rdscpu => "cloud::aws::mode::metrics::rdsinstancecpu",
};
my $StatisticsType = "Average,Minimum,Maximum,Sum,SampleCount";
@ -45,24 +45,25 @@ my $apiRequest = {
'subcommand' => 'get-metric-statistics',
};
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new( package => __PACKAGE__, %options );
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 => {
"metric:s" => { name => 'metric' },
"period:s" => { name => 'period', default => 300 },
"starttime:s" => { name => 'starttime' },
"endtime:s" => { name => 'endtime' },
"statistics:s" => { name => 'statistics', default => 'Average' },
"exclude-statistics:s" => { name => 'exclude-statistics' },
"object:s" => { name => 'object' },
"warning:s" => { name => 'warning' },
"critical:s" => { name => 'critical' },
"metric:s" => {name => 'metric'},
"period:s" => {name => 'period', default => 300},
"starttime:s" => {name => 'starttime'},
"endtime:s" => {name => 'endtime'},
"statistics:s" => {name => 'statistics', default => 'Average'},
"exclude-statistics:s" => {name => 'exclude-statistics'},
"object:s" => {name => 'object'},
"warning:s" => {name => 'warning'},
"critical:s" => {name => 'critical'},
}
);
$self->{result} = {};
@ -70,30 +71,26 @@ sub new {
return $self;
}
sub check_options {
my ( $self, %options ) = @_;
sub check_options
{
my ($self, %options) = @_;
$self->SUPER::init(%options);
$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}
->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 )
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, ...)."
@ -101,7 +98,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, ...)."
@ -109,27 +107,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' ) {
@{ $self->{option_results}->{statisticstab} } = split( /,/, $StatisticsType );
if ($self->{option_results}->{statistics} eq 'all')
{
@{$self->{option_results}->{statisticstab}} = split(/,/, $StatisticsType);
}
else {
@{ $self->{option_results}->{statisticstab} } = split( /,/, $self->{option_results}->{statistics} );
foreach my $curstate ( @{ $self->{option_results}->{statisticstab} } ) {
if ( !grep { /^$curstate$/ } split( /,/, $StatisticsType ) ) {
else
{
@{$self->{option_results}->{statisticstab}} = split(/,/, $self->{option_results}->{statistics});
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."
@ -140,37 +140,43 @@ sub check_options {
}
# exclusions
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}};
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}} ) {
my $statistics = join(',',@{ $self->{option_results}->{statisticstab} });
if ( ! $statistics eq '' ) {
if (!grep $_ eq 'Average', @{$self->{option_results}->{statisticstab}})
{
my $statistics = join(',', @{$self->{option_results}->{statisticstab}});
if (!$statistics eq '')
{
$statistics = $statistics . ',Average';
}
else {
else
{
$statistics = 'Average';
}
@{ $self->{option_results}->{statisticstab} } = split( /,/, $statistics );
@{$self->{option_results}->{statisticstab}} = split(/,/, $statistics);
}
}
sub manage_selection {
my ( $self, $metric ) = @_;
sub manage_selection
{
my ($self, $metric) = @_;
my @result;
my @Dimensions = (
{
'Value' => $self->{option_results}->{object},
'Name' => $metric->{ObjectName}
}
);
{
'Value' => $self->{option_results}->{object},
'Name' => $metric->{ObjectName}
}
);
if ( defined( $metric->{ExtraDimensions} ) ) {
if (defined($metric->{ExtraDimensions}))
{
push @Dimensions, $metric->{ExtraDimensions};
}
@ -186,10 +192,11 @@ sub manage_selection {
};
}
sub run {
my ( $self, %options ) = @_;
sub run
{
my ($self, %options) = @_;
my ( $msg, $exit_code, $awsapi );
my ($msg, $exit_code, $awsapi);
if ( defined( $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');
$func->($self);
} else {
$self->{output}
->add_option_msg( short_msg => "Wrong option. Cannot find metric '"
. $self->{option_results}->{metric}
. "'." );
$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}
),
warning => $self->{perfdata}->get_perfdata_for_output( label => 'warning' ),
critical => $self->{perfdata}->get_perfdata_for_output( label => 'critical' ),
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,33 +17,31 @@
# 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 $AWSServices = 'EC2,S3,RDS';
my @Disco_service_tab = ('EC2', 'RDS');
my $EC2_instance_states = 'running,stopped';
my $awsapi;
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new( package => __PACKAGE__, %options );
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 },
"exclude:s" => { name => 'exclude' },
"service:s" => {name => 'service', default => $AWSServices},
"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} = {};
@ -59,6 +57,13 @@ sub api_request {
my ($self, %options) = @_;
@{$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}}) {
$self->{result}->{count}->{$service} = 0;
if ($service eq 'EC2') {
@ -74,21 +79,28 @@ sub api_request {
}
}
sub EC2 {
my ( $self, %options ) = @_;
sub EC2
{
my ($self, %options) = @_;
my $apiRequest = {
'command' => 'ec2',
'subcommand' => 'describe-instances',
};
# 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} = {
'DryRun' => JSON::false,
'Filters' => [
{
'Name' => 'instance-state-name',
'Values' => @EC2_instance_states,
'Values' => [@ec2_statestab],
}
],
};
@ -107,17 +119,17 @@ sub EC2 {
$self->{result}->{'EC2'}->{$instance->{Instances}[0]->{InstanceId}} =
{
State => $instance->{Instances}[0]->{State}->{Name},
Name => $instance->{Instances}[0]->{Name}
Name => $instance->{Instances}[0]->{Name}
};
$self->{result}->{count}->{'EC2'}++;
}
}
sub S3 {
my ( $self, %options ) = @_;
my ( @buckets, @return ) = ();
sub S3
{
my ($self, %options) = @_;
my (@buckets, @return) = ();
my $apiRequest = {
'command' => 's3',
'subcommand' => 'ls',
@ -137,15 +149,15 @@ sub S3 {
# Compute data
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'}++;
}
}
sub RDS {
my ( $self, %options ) = @_;
sub RDS
{
my ($self, %options) = @_;
my $apiRequest = {
'command' => 'rds',
'subcommand' => 'describe-db-instances',
@ -172,25 +184,25 @@ sub disco_format {
$self->{output}->add_disco_format( elements => $names );
}
sub disco_show {
my ( $self, %options ) = @_;
sub disco_show
{
my ($self, %options) = @_;
$self->api_request(%options);
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,
state => $self->{result}->{$service}->{$device}->{State},
name => $self->{result}->{$service}->{$device}->{Name},
id => $device,
state => $self->{result}->{$service}->{$device}->{State},
service => $service,
);
}
}
}
sub run {
my ( $self, %options ) = @_;
sub run
{
my ($self, %options) = @_;
$self->api_request(%options);
# Send formated data to Centreon
@ -199,21 +211,14 @@ sub run {
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 = $output . $value . " = " . $self->{result}->{$service}->{$device}->{$value} . ", ";
}
$output =~ s/, $//;
$output = $output . "]";
$self->{output}->output_add( long_msg => $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,
@ -221,9 +226,7 @@ sub run {
);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
@ -236,10 +239,18 @@ List your EC2, RDS instance and S3 buckets
(optional) List one particular service.
=item B<--exclude>
=item B<--exclude-service>
(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
=cut