From 45b787f84e9004a48e960f06fa767caf797a0b8f Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 6 Jan 2020 15:17:10 +0100 Subject: [PATCH] wip: cloudwatch logs + indent --- cloud/aws/cloudwatchlogs/mode/getlogs.pm | 170 ++++++++++++++++++++ cloud/aws/cloudwatchlogs/mode/listgroups.pm | 4 +- cloud/aws/cloudwatchlogs/plugin.pm | 1 + cloud/aws/custom/awscli.pm | 10 +- cloud/aws/custom/paws.pm | 6 + 5 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 cloud/aws/cloudwatchlogs/mode/getlogs.pm diff --git a/cloud/aws/cloudwatchlogs/mode/getlogs.pm b/cloud/aws/cloudwatchlogs/mode/getlogs.pm new file mode 100644 index 000000000..45868056b --- /dev/null +++ b/cloud/aws/cloudwatchlogs/mode/getlogs.pm @@ -0,0 +1,170 @@ +# +# 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::getlogs; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::misc; +use centreon::plugins::statefile; +use Digest::MD5 qw(md5_hex); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf( + 'log [created: %s] [stream: %s] [message: %s] ', + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{since}), + $self->{result_values}->{stream_name}, + $self->{result_values}->{message} + ); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'alarms', type => 2, message_multiple => '0 logs detected', display_counter_problem => { label => 'logs', min => 0 }, + group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] + } + ]; + + $self->{maps_counters}->{alarm} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'message' }, { name => 'stream_name' }, { name => 'since' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'group-name:s' => { name => 'group_name' }, + 'stream-names:s@' => { name => 'stream_names' }, + 'unknown-status:s' => { name => 'unknown_status', default => '' }, + 'warning-status:s' => { name => 'warning_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '' }, + }); + + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{group_name}) || $self->{option_results}->{group_name} eq '') { + $self->{output}->add_option_msg(short_msg => 'please set --group-name option'); + $self->{output}->option_exit(); + } + + $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']); + $self->{statefile_cache}->check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{statefile_cache}->read( + statefile => + 'cache_aws_' . $self->{mode} . '_' . $options{custom}->get_region() . + (defined($self->{option_results}->{group_name}) ? md5_hex($self->{option_results}->{group_name}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{stream_names}) ? md5_hex(join(',' . $self->{option_results}->{stream_names})) : md5_hex('all')) + ); + my $last_time = $self->{statefile_cache}->get(name => 'last_time'); + my $current_time = time(); + $self->{statefile_cache}->write(data => { last_time => $current_time }); + + if (defined($last_time)) { + $self->{output}->output_add( + severity => 'OK', + short_msg => 'first execution. create cache' + ); + $self->{output}->display(); + $self->{output}->exit(); + } + + $self->{alarms}->{global} = { alarm => {} }; + my $results = $options{custom}->cloudwatchlogs_filter_log_events( + group_name => $self->{option_results}->{group_name}, + stream_names => $self->{option_results}->{stream_names}, + start_time => $last_time * 1000 + ); + + my $i = 1; + foreach (@$results) { + $self->{alarms}->{global}->{alarm}->{$i} = { + message => $_->{message}, + stream_name => $_->{logStreamName}, + since => int($current_time - ($_->{timestamp} / 1000)) + }; + $i++; + } +} + +1; + +__END__ + +=head1 MODE + +Check cloudwatch logs. + +=over 8 + +=item B<--group-name> + +Set log group name (Required). + +=item B<--stream-name> + +Filters the results to only logs from the log streams (multiple option). + +=item B<--unknown-status> + +Set unknown threshold for status (Default: '') +Can used special variables like: %{message}, %{stream_name}, %{since} + +=item B<--warning-status> + +Set warning threshold for status (Default: '') +Can used special variables like: %{message}, %{stream_name}, %{since} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{message}, %{stream_name}, %{since} + +=back + +=cut diff --git a/cloud/aws/cloudwatchlogs/mode/listgroups.pm b/cloud/aws/cloudwatchlogs/mode/listgroups.pm index 6a903e4ac..653ceeee0 100644 --- a/cloud/aws/cloudwatchlogs/mode/listgroups.pm +++ b/cloud/aws/cloudwatchlogs/mode/listgroups.pm @@ -44,9 +44,7 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - return $options{custom}->cloudwatchlogs_describe_log_groups( - region => $self->{option_results}->{region} - ); + return $options{custom}->cloudwatchlogs_describe_log_groups(); } sub run { diff --git a/cloud/aws/cloudwatchlogs/plugin.pm b/cloud/aws/cloudwatchlogs/plugin.pm index 62ff86275..73f4b01c6 100644 --- a/cloud/aws/cloudwatchlogs/plugin.pm +++ b/cloud/aws/cloudwatchlogs/plugin.pm @@ -32,6 +32,7 @@ sub new { $self->{version} = '0.1'; %{ $self->{modes} } = ( 'list-groups' => 'cloud::aws::cloudwatchlogs::mode::listgroups', + 'get-logs' => 'cloud::aws::cloudwatchlogs::mode::getlogs', ); $self->{custom_modes}{paws} = 'cloud::aws::custom::paws'; diff --git a/cloud/aws/custom/awscli.pm b/cloud/aws/custom/awscli.pm index 177bd9128..e84ca287c 100644 --- a/cloud/aws/custom/awscli.pm +++ b/cloud/aws/custom/awscli.pm @@ -66,6 +66,12 @@ sub new { return $self; } +sub get_region { + my ($self, %options) = @_; + + return $self->{option_results}->{region}; +} + sub set_options { my ($self, %options) = @_; @@ -293,7 +299,7 @@ sub cloudwatchlogs_describe_log_groups_set_cmd { 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"; + my $cmd_options = "logs describe-log-groups --region $self->{option_results}->{region} --output json"; $cmd_options .= " --endpoint-url $self->{endpoint_url}" if (defined($self->{endpoint_url}) && $self->{endpoint_url} ne ''); return $cmd_options; @@ -313,7 +319,7 @@ sub cloudwatchlogs_filter_log_events_set_cmd { 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}'"; + my $cmd_options = "logs filter-log-events --region $self->{option_results}->{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"; diff --git a/cloud/aws/custom/paws.pm b/cloud/aws/custom/paws.pm index 39b2d9a80..9fc150452 100644 --- a/cloud/aws/custom/paws.pm +++ b/cloud/aws/custom/paws.pm @@ -60,6 +60,12 @@ sub new { return $self; } +sub get_region { + my ($self, %options) = @_; + + return $self->{option_results}->{region}; +} + sub set_options { my ($self, %options) = @_;