wip: cloudwatch logs + indent

This commit is contained in:
garnier-quentin 2020-01-06 14:40:07 +01:00
parent e0f26e73a0
commit bd27affe20
7 changed files with 364 additions and 100 deletions

View File

@ -31,9 +31,12 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold)
sub custom_status_output {
my ($self, %options) = @_;
my $msg = sprintf("alarm [name: %s] [state: %s] [metric: %s] [reason: %s] %s", $self->{result_values}->{alarm_name},
my $msg = sprintf(
'alarm [name: %s] [state: %s] [metric: %s] [reason: %s] %s',
$self->{result_values}->{alarm_name},
$self->{result_values}->{state_value}, $self->{result_values}->{metric_name},
$self->{result_values}->{state_reason}, centreon::plugins::misc::change_seconds(value => $self->{result_values}->{last_update}));
$self->{result_values}->{state_reason}, centreon::plugins::misc::change_seconds(value => $self->{result_values}->{last_update})
);
return $msg;
}
@ -75,16 +78,17 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"filter-alarm-name:s" => { name => 'filter_alarm_name' },
"warning-status:s" => { name => 'warning_status', default => '%{state_value} =~ /INSUFFICIENT_DATA/i' },
"critical-status:s" => { name => 'critical_status', default => '%{state_value} =~ /ALARM/i' },
"memory" => { name => 'memory' },
});
$options{options}->add_options(arguments => {
'filter-alarm-name:s' => { name => 'filter_alarm_name' },
'warning-status:s' => { name => 'warning_status', default => '%{state_value} =~ /INSUFFICIENT_DATA/i' },
'critical-status:s' => { name => 'critical_status', default => '%{state_value} =~ /ALARM/i' },
'memory' => { name => 'memory' },
});
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'.");
centreon::plugins::misc::mymodule_load(
output => $self->{output}, module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'."
);
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self;
}
@ -94,7 +98,6 @@ sub check_options {
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->check_options(%options);
}
@ -118,8 +121,10 @@ sub manage_selection {
foreach my $alarm (@{$alarm_results}) {
my $create_time = Date::Parse::str2time($alarm->{StateUpdatedTimestamp});
if (!defined($create_time)) {
$self->{manager}->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Can't Parse date '" . $alarm->{StateUpdatedTimestamp} . "'");
$self->{manager}->{output}->output_add(
severity => 'UNKNOWN',
short_msg => "Can't Parse date '" . $alarm->{StateUpdatedTimestamp} . "'"
);
next;
}

View File

@ -28,20 +28,24 @@ use warnings;
sub custom_metric_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(label => $self->{result_values}->{perf_label},
value => $self->{result_values}->{value},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric'),
);
$self->{output}->perfdata_add(
label => $self->{result_values}->{perf_label},
value => $self->{result_values}->{value},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric'),
);
}
sub custom_metric_threshold {
my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{value},
threshold => [ { label => 'critical-metric', exit_litteral => 'critical' },
{ label => 'warning-metric', exit_litteral => 'warning' } ]);
my $exit = $self->{perfdata}->threshold_check(
value => $self->{result_values}->{value},
threshold => [
{ label => 'critical-metric', exit_litteral => 'critical' },
{ label => 'warning-metric', exit_litteral => 'warning' }
]
);
return $exit;
}
@ -87,12 +91,11 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"namespace:s" => { name => 'namespace' },
"dimension:s%" => { name => 'dimension' },
"metric:s@" => { name => 'metric' },
});
$options{options}->add_options(arguments => {
'namespace:s' => { name => 'namespace' },
'dimension:s%' => { name => 'dimension' },
'metric:s@' => { name => 'metric' },
});
return $self;
}

View File

@ -30,11 +30,10 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"namespace:s" => { name => 'namespace' },
"metric:s" => { name => 'metric' },
});
$options{options}->add_options(arguments => {
'namespace:s' => { name => 'namespace' },
'metric:s' => { name => 'metric' },
});
return $self;
}
@ -47,9 +46,11 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
$self->{metrics} = $options{custom}->cloudwatch_list_metrics(region => $self->{option_results}->{region},
namespace => $self->{option_results}->{namespace},
metric => $self->{option_results}->{metric});
$self->{metrics} = $options{custom}->cloudwatch_list_metrics(
region => $self->{option_results}->{region},
namespace => $self->{option_results}->{namespace},
metric => $self->{option_results}->{metric}
);
}
sub get_dimensions_str {
@ -74,8 +75,10 @@ sub run {
$_->{Namespace}, $self->get_dimensions_str(dimensions => $_->{Dimensions}), $_->{MetricName}));
}
$self->{output}->output_add(severity => 'OK',
short_msg => 'List metrics:');
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List metrics:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}

View File

@ -0,0 +1,103 @@
#
# Copyright 2019 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::cloudwatchlogs::mode::listgroups;
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;
$options{options}->add_options(arguments => {
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
return $options{custom}->cloudwatchlogs_describe_log_groups(
region => $self->{option_results}->{region}
);
}
sub run {
my ($self, %options) = @_;
my $log_groups = $self->manage_selection(%options);
foreach (@$log_groups) {
$self->{output}->output_add(
long_msg => sprintf(
'[group name = %s]',
$_->{logGroupName}
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List log groups:'
);
$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 => ['group_name']);
}
sub disco_show {
my ($self, %options) = @_;
my $log_groups = $self->manage_selection(%options);
foreach (@$log_groups) {
$self->{output}->add_disco_entry(
group_name => $_->{logGroupName}
);
}
}
1;
__END__
=head1 MODE
List cloudwatch logs groups.
=over 8
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2019 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::cloudwatchlogs::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
%{ $self->{modes} } = (
'list-groups' => 'cloud::aws::cloudwatchlogs::mode::listgroups',
);
$self->{custom_modes}{paws} = 'cloud::aws::custom::paws';
$self->{custom_modes}{awscli} = 'cloud::aws::custom::awscli';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Amazon CloudWatchLogs.
=cut

View File

@ -262,6 +262,7 @@ sub cloudwatch_get_alarms {
StateUpdatedTimestamp => $alarm->{StateUpdatedTimestamp},
};
}
return $alarm_results;
}
@ -271,8 +272,8 @@ sub cloudwatch_list_metrics_set_cmd {
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "cloudwatch list-metrics --region $options{region} --output json";
$cmd_options .= " --namespace $options{namespace}" if defined($options{namespace});
$cmd_options .= " --metric-name $options{metric}" if defined($options{metric});
$cmd_options .= " --namespace $options{namespace}" if (defined($options{namespace}));
$cmd_options .= " --metric-name $options{metric}" if (defined($options{metric}));
$cmd_options .= " --endpoint-url $self->{endpoint_url}" if (defined($self->{endpoint_url}) && $self->{endpoint_url} ne '');
return $cmd_options;
@ -287,6 +288,53 @@ sub cloudwatch_list_metrics {
return $raw_results->{Metrics};
}
sub cloudwatchlogs_describe_log_groups_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "logs describe-log-groups --region $options{region} --output json";
$cmd_options .= " --endpoint-url $self->{endpoint_url}" if (defined($self->{endpoint_url}) && $self->{endpoint_url} ne '');
return $cmd_options;
}
sub cloudwatchlogs_describe_log_groups {
my ($self, %options) = @_;
my $cmd_options = $self->cloudwatchlogs_describe_log_groups_set_cmd(%options);
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results->{logGroups};
}
sub cloudwatchlogs_filter_log_events_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "logs filter-log-events --region $options{region} --output json --log-group-name '$options{group_name}'";
$cmd_options .= " --start-time $options{start_time}" if (defined($options{start_time}));
if (defined($options{stream_names})) {
$cmd_options .= " --log-stream-names";
foreach (@{$options{stream_names}}) {
$cmd_options .= " '$_'";
}
}
$cmd_options .= " --endpoint-url $self->{endpoint_url}" if (defined($self->{endpoint_url}) && $self->{endpoint_url} ne '');
return $cmd_options;
}
sub cloudwatchlogs_filter_log_events {
my ($self, %options) = @_;
my $cmd_options = $self->cloudwatchlogs_filter_log_events_set_cmd(%options);
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results->{events};
}
sub ec2_get_instances_status_set_cmd {
my ($self, %options) = @_;

View File

@ -232,6 +232,58 @@ sub cloudwatch_list_metrics {
return $metric_results;
}
sub cloudwatchlogs_describe_log_groups {
my ($self, %options) = @_;
my $log_groups_results = [];
eval {
my $lwp_caller = new Paws::Net::LWPCaller();
my $cw = Paws->service('CloudWatchLogs', caller => $lwp_caller, region => $options{region});
my %cw_options = ();
while ((my $list_log_groups = $cw->DescribeLogGroups(%cw_options))) {
foreach (@{$list_log_groups->{logGroups}}) {
push @$log_groups_results, $_;
}
last if (!defined($list_log_groups->{NextToken}));
$cw_options{NextToken} = $list_log_groups->{NextToken};
}
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "error: $@");
$self->{output}->option_exit();
}
return $log_groups_results;
}
sub cloudwatchlogs_filter_log_events {
my ($self, %options) = @_;
my $log_groups_results = [];
eval {
my $lwp_caller = new Paws::Net::LWPCaller();
my $cw = Paws->service('CloudWatchLogs', caller => $lwp_caller, region => $options{region});
my %cw_options = ();
$cw_options{StartTime} = $options{start_time} if (defined($options{start_time}));
$cw_options{LogStreamNames} = [@{$options{LogStreamNames}}] if (defined($options{LogStreamNames}));
while ((my $list_log_groups = $cw->FilterLogEvents(%cw_options))) {
foreach (@{$list_log_groups->{logGroups}}) {
push @$log_groups_results, $_;
}
last if (!defined($list_log_groups->{NextToken}));
$cw_options{NextToken} = $list_log_groups->{NextToken};
}
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "error: $@");
$self->{output}->option_exit();
}
return $log_groups_results;
}
sub ec2_get_instances_status {
my ($self, %options) = @_;