Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws friendly plugins #942

Merged
merged 1 commit into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions cloud/aws/billing/mode/estimatedcharges.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#
# Copyright 2018 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package cloud::aws::billing::mode::estimatedcharges;

use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;

sub prefix_charges_output {
my ($self, %options) = @_;

return "Service '" . $self->{option_results}->{service} . "' ";
}

sub set_counters {
my ($self, %options) = @_;

$self->{maps_counters_type} = [
{ name => 'estimatedcharges', type => 0, cb_prefix_output => 'prefix_charges_output' },
];

$self->{maps_counters}->{estimatedcharges} = [
{ label => 'billing', set => {
key_values => [ { name => 'estimated_charges' }, { name => 'display' } ],
output_template => 'estimated charges: %.2f USD',
perfdatas => [
{ label => 'billing', value => 'estimated_charges_absolute', template => '%.2f',
unit => 'USD' },
],
}
},
];
}

sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;

$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"service:s" => { name => 'service' },
"currency:s" => { name => 'currency', default => 'USD' },
});

return $self;
}

sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);

if (!defined($self->{option_results}->{service}) || $self->{option_results}->{service} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --service option.");
$self->{output}->option_exit();
}

if (!defined($self->{option_results}->{currency}) || $self->{option_results}->{currency} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --currency option.");
$self->{output}->option_exit();
}

$self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 86400;
$self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60;
}

sub manage_selection {
my ($self, %options) = @_;

my $metric_results = $options{custom}->cloudwatch_get_metrics(
region => $self->{option_results}->{region},
namespace => 'AWS/Billing',
dimensions => [ { Name => 'ServiceName', Value => $self->{option_results}->{service} }, { Name => 'Currency', Value => $self->{option_results}->{currency} } ],
metrics => ['EstimatedCharges'],
statistics => ['Maximum'],
timeframe => $self->{aws_timeframe},
period => $self->{aws_period},
);

$self->{estimatedcharges}->{estimated_charges} = $metric_results->{'EstimatedCharges'}->{'maximum'} if defined($metric_results->{'EstimatedCharges'}->{'maximum'});
$self->{estimatedcharges}->{display} = $self->{option_results}->{service};

if (scalar(keys %{$self->{estimatedcharges}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No value.');
$self->{output}->option_exit();
}
}

1;

__END__

=head1 MODE

Check Billing estimated charges for a service.

Example:
perl centreon_plugins.pl --plugin=cloud::aws::billing::plugin --custommode=paws --mode=estimated-charges
--region='us-east-1' --service='AWSService' --verbose

See 'https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/billing-metricscollected.html' for more informations.

=over 8

=item B<--service>

Set the Amazon service (Required).

=item B<--warning-billing>

Thresholds warning.

=item B<--critical-billing>

Thresholds critical.

=back

=cut
120 changes: 120 additions & 0 deletions cloud/aws/billing/mode/listservices.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#
# Copyright 2018 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package cloud::aws::billing::mode::listservices;

use base qw(centreon::plugins::mode);

use strict;
use warnings;

sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;

$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
});

return $self;
}

sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);

$self->{aws_region} = defined($self->{option_results}->{region}) ? $self->{option_results}->{region} : 'us-east-1';
}

sub manage_selection {
my ($self, %options) = @_;

$self->{dimensions} = $options{custom}->cloudwatch_list_metrics(region => $self->{aws_region},
namespace => 'AWS/Billing');
}

sub run {
my ($self, %options) = @_;

$self->manage_selection(%options);

my %already;
foreach my $dimension (@{$self->{dimensions}}) {
my $servicename = '';
my $currency = '';
foreach my $name (@{$dimension->{Dimensions}}) {
$servicename = $name->{Value} if ($name->{Name} =~ m/ServiceName/ && $name->{Value} ne '');
$currency = $name->{Value} if ($name->{Name} =~ m/Currency/ && $name->{Value} ne '');
}
next if (defined($already{$servicename}) || $servicename eq '');
$self->{output}->output_add(long_msg => sprintf("[ServiceName = %s][Currency = %s]", $servicename, $currency));
$already{$servicename} = 1;
}

$self->{output}->output_add(severity => 'OK',
short_msg => 'List services:');
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}

sub disco_format {
my ($self, %options) = @_;

$self->{output}->add_disco_format(elements => ['servicename', 'currency']);
}

sub disco_show {
my ($self, %options) = @_;

$self->manage_selection(%options);

my %already;
foreach my $dimension (@{$self->{dimensions}}) {
my $servicename = '';
my $currency = '';
foreach my $name (@{$dimension->{Dimensions}}) {
$servicename = $name->{Value} if ($name->{Name} =~ m/ServiceName/ && $name->{Value} ne '');
$currency = $name->{Value} if ($name->{Name} =~ m/Currency/ && $name->{Value} ne '');
}
next if (defined($already{$servicename}) || $servicename eq '');
$self->{output}->add_disco_entry(
servicename => $servicename,
currency => $currency,
);
$already{$servicename} = 1;
}
}

1;

__END__

=head1 MODE

List billed Amazon services.

=over 8

=back

=cut

13 changes: 6 additions & 7 deletions cloud/aws/plugin.pm → cloud/aws/billing/plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# limitations under the License.
#

package cloud::aws::plugin;
package cloud::aws::billing::plugin;

use strict;
use warnings;
Expand All @@ -31,11 +31,8 @@ sub new {

$self->{version} = '0.1';
%{ $self->{modes} } = (
'cloudwatch-get-alarms' => 'cloud::aws::mode::cloudwatchgetalarms',
'cloudwatch-get-metrics' => 'cloud::aws::mode::cloudwatchgetmetrics',
'cloudwatch-list-metrics' => 'cloud::aws::mode::cloudwatchlistmetrics',
'ec2-instance-status' => 'cloud::aws::mode::ec2instancestatus',
'rds-instance-status' => 'cloud::aws::mode::rdsinstancestatus',
'estimated-charges' => 'cloud::aws::billing::mode::estimatedcharges',
'list-services' => 'cloud::aws::billing::mode::listservices',
);

$self->{custom_modes}{paws} = 'cloud::aws::custom::paws';
Expand All @@ -49,6 +46,8 @@ __END__

=head1 PLUGIN DESCRIPTION

Check Amazon AWS cloud.
Check Amazon Billing.

Works for 'us-east-1' region only.

=cut
Loading