diff --git a/cloud/google/gcp/compute/computeengine/mode/cpu.pm b/cloud/google/gcp/compute/computeengine/mode/cpu.pm new file mode 100644 index 000000000..ddec5ae43 --- /dev/null +++ b/cloud/google/gcp/compute/computeengine/mode/cpu.pm @@ -0,0 +1,242 @@ +# +# 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::google::gcp::compute::computeengine::mode::cpu; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use cloud::google::gcp::custom::misc; + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "Instance '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " "; +} + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + $self->{result_values}->{metric_name} = $options{extra_options}->{metric_name}; + $self->{result_values}->{metric_label} = $options{extra_options}->{metric_label}; + $self->{result_values}->{metric_perf} = $options{extra_options}->{metric_perf}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric} . '_' . $self->{result_values}->{stat}} * 100; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => $self->{result_values}->{value}, + threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ] + ); + return $exit; +} + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + + $self->{output}->perfdata_add( + label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label, + unit => '%', + value => sprintf("%.2f", $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + min => 0, max => 100 + ); +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my $msg = sprintf("%s: %.2f%%", $self->{result_values}->{metric_name}, $self->{result_values}->{value}); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', + message_multiple => "All CPU metrics are ok", skipped_code => { -10 => 1 } }, + ]; + + foreach my $aggregation ('minimum', 'maximum', 'average', 'total') { + foreach my $metric ('instance/cpu/utilization') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, { name => 'stat' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric_perf => $metric_perf, + metric_label => $metric_label, metric_name => $metric_name, metric => $metric }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold'), + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + foreach my $metric ('instance/cpu/reserved_cores') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, { name => 'stat' } ], + output_template => $metric_name . ': %.2f', + perfdatas => [ + { label => $metric_perf . '_' . $aggregation, value => $metric . '_' . $aggregation . '_absolute', + template => '%.2f', unit => '', label_extra_instance => 1, instance_use => 'display_absolute', + min => 0 }, + ], + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + } +} + +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 => { + "instance:s@" => { name => 'instance' }, + "filter-metric:s" => { name => 'filter_metric' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{instance})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --instance ."); + $self->{output}->option_exit(); + } + + $self->{gcp_instance} = $self->{option_results}->{instance}; + $self->{gcp_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; + $self->{gcp_aggregations} = ['average']; + if (defined($self->{option_results}->{aggregation})) { + $self->{gcp_aggregations} = []; + foreach my $stat (@{$self->{option_results}->{aggregation}}) { + if ($stat ne '') { + push @{$self->{gcp_aggregations}}, $stat; + } + } + } + + foreach my $metric ('instance/cpu/utilization', 'instance/cpu/reserved_cores') { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + + push @{$self->{gcp_metrics}}, $metric; + } + + $self->{gcp_api} = "compute.googleapis.com"; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $metric_results; + foreach my $instance (@{$self->{gcp_instance}}) { + foreach my $metric (@{$self->{gcp_metrics}}) { + ($metric_results, undef) = $options{custom}->gcp_get_metrics( + instance => $instance, + metric => $metric, + api => $self->{gcp_api}, + aggregations => $self->{gcp_aggregations}, + timeframe => $self->{gcp_timeframe}, + ); + + foreach my $aggregation (@{$self->{gcp_aggregations}}) { + next if (!defined($metric_results->{$metric}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed})); + + $self->{metric}->{$instance . "_" . lc($aggregation)}->{display} = $metric_results->{labels}->{instance_name}; + $self->{metric}->{$instance . "_" . lc($aggregation)}->{stat} = lc($aggregation); + $self->{metric}->{$instance . "_" . lc($aggregation)}->{$metric . "_" . lc($aggregation)} = defined($metric_results->{$metric}->{lc($aggregation)}) ? $metric_results->{$metric}->{lc($aggregation)} : 0; + } + } + } + + if (scalar(keys %{$self->{metric}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Compute Engine instances CPU metrics. + +Example: + +perl centreon_plugins.pl --plugin=cloud::google::gcp::compute::computeengine::plugin --custommode=api --mode=cpu +--instance=mycomputeinstance --filter-metric='utilization' --aggregation='average' +--critical-cpu-utilization-average='10' --verbose + +Default aggregation: 'average' / All aggregations are valid. + +=over 8 + +=item B<--instance> + +Set instance name (Required). + +=item B<--filter-metric> + +Filter metrics (Can be: 'instance/cpu/utilization', +'instance/cpu/reserved_cores') (Can be a regexp). + +=item B<--warning-$metric$-$aggregation$> + +Thresholds warning ($metric$ can be: 'cpu-utilization', +'cpu-reserved-cores', $aggregation$ can be: 'minimum', +'maximum', 'average', 'total'). + +=item B<--critical-$metric$-$aggregation$> + +Thresholds critical ($metric$ can be: 'cpu-utilization', +'cpu-reserved-cores', $aggregation$ can be: 'minimum', +'maximum', 'average', 'total'). + +=back + +=cut diff --git a/cloud/google/gcp/compute/computeengine/mode/diskio.pm b/cloud/google/gcp/compute/computeengine/mode/diskio.pm new file mode 100644 index 000000000..d583b3175 --- /dev/null +++ b/cloud/google/gcp/compute/computeengine/mode/diskio.pm @@ -0,0 +1,297 @@ +# +# 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::google::gcp::compute::computeengine::mode::diskio; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use cloud::google::gcp::custom::misc; + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "Instance '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " "; +} + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'}; + $self->{result_values}->{metric_perf} = $options{extra_options}->{metric_perf}; + $self->{result_values}->{metric_label} = $options{extra_options}->{metric_label}; + $self->{result_values}->{metric_name} = $options{extra_options}->{metric_name}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric} . '_' . $self->{result_values}->{stat}}; + $self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}, + threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ] + ); + return $exit; +} + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + + $self->{output}->perfdata_add( + label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label, + unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? 'B/s' : 'B', + value => sprintf("%d", defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + min => 0 + ); +} + +sub custom_usage_output { + my ($self, %options) = @_; + my $msg = ""; + + if (defined($self->{instance_mode}->{option_results}->{per_sec})) { + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec}); + $msg = $self->{result_values}->{metric_name} . ": " . $value . $unit . "/s"; + } else { + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}); + $msg = $self->{result_values}->{metric_name} . ": " . $value . $unit; + } + return $msg; +} + +sub custom_ops_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => $self->{result_values}->{value}, + threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ] + ); + return $exit; +} + +sub custom_ops_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + + $self->{output}->perfdata_add( + label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label, + unit => 'ops/s', + value => sprintf("%.2f", $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + min => 0 + ); +} + +sub custom_ops_output { + my ($self, %options) = @_; + + my $msg = sprintf("%s: %.2f ops/s", $self->{result_values}->{metric_name}, $self->{result_values}->{value}); + + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All disk metrics are ok", skipped_code => { -10 => 1 } }, + ]; + + foreach my $aggregation ('minimum', 'maximum', 'average', 'total') { + foreach my $metric ('instance/disk/read_bytes_count', 'instance/disk/throttled_read_bytes_count', + 'instance/disk/write_bytes_count', 'instance/disk/throttled_write_bytes_count') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, + { name => 'stat' }, { name => 'timeframe' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric_perf => $metric_perf, + metric_label => $metric_label, metric_name => $metric_name, metric => $metric }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + foreach my $metric ('instance/disk/read_ops_count', 'instance/disk/write_ops_count') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, + { name => 'stat' }, { name => 'timeframe' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric_perf => $metric_perf, + metric_label => $metric_label, metric_name => $metric_name, metric => $metric }, + closure_custom_output => $self->can('custom_ops_output'), + closure_custom_perfdata => $self->can('custom_ops_perfdata'), + closure_custom_threshold_check => $self->can('custom_ops_threshold'), + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + } +} + +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 => { + "instance:s@" => { name => 'instance' }, + "filter-metric:s" => { name => 'filter_metric' }, + "per-sec" => { name => 'per_sec' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{instance})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --instance ."); + $self->{output}->option_exit(); + } + + $self->{gcp_instance} = $self->{option_results}->{instance}; + $self->{gcp_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; + $self->{gcp_aggregations} = ['average']; + if (defined($self->{option_results}->{aggregation})) { + $self->{gcp_aggregations} = []; + foreach my $stat (@{$self->{option_results}->{aggregation}}) { + if ($stat ne '') { + push @{$self->{gcp_aggregations}}, $stat; + } + } + } + + foreach my $metric ('instance/disk/read_bytes_count', 'instance/disk/throttled_read_bytes_count', + 'instance/disk/write_bytes_count', 'instance/disk/throttled_write_bytes_count', + 'instance/disk/read_ops_count', 'instance/disk/write_ops_count') { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + + push @{$self->{gcp_metrics}}, $metric; + } + + $self->{gcp_api} = "compute.googleapis.com"; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $metric_results; + foreach my $instance (@{$self->{gcp_instance}}) { + foreach my $metric (@{$self->{gcp_metrics}}) { + ($metric_results, undef) = $options{custom}->gcp_get_metrics( + instance => $instance, + metric => $metric, + api => $self->{gcp_api}, + aggregations => $self->{gcp_aggregations}, + timeframe => $self->{gcp_timeframe}, + ); + + foreach my $aggregation (@{$self->{gcp_aggregations}}) { + next if (!defined($metric_results->{$metric}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed})); + + $self->{metric}->{$instance . "_" . lc($aggregation)}->{display} = $metric_results->{labels}->{instance_name}; + $self->{metric}->{$instance . "_" . lc($aggregation)}->{timeframe} = $self->{gcp_timeframe}; + $self->{metric}->{$instance . "_" . lc($aggregation)}->{stat} = lc($aggregation); + $self->{metric}->{$instance . "_" . lc($aggregation)}->{$metric . "_" . lc($aggregation)} = defined($metric_results->{$metric}->{lc($aggregation)}) ? $metric_results->{$metric}->{lc($aggregation)} : 0; + } + } + } + + if (scalar(keys %{$self->{metric}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Compute Engine instances disk IO metrics. + +Example: + +perl centreon_plugins.pl --plugin=cloud::google::gcp::compute::computeengine::plugin --custommode=api --mode=diskio +--instance=mycomputeinstance --filter-metric='throttled' --aggregation='average' +--critical-disk-throttled-write-bytes-count-average='10' --verbose + +Default aggregation: 'average' / All aggregations are valid. + +=over 8 + +=item B<--instance> + +Set instance name (Required). + +=item B<--filter-metric> + +Filter metrics (Can be: 'instance/disk/read_bytes_count', 'instance/disk/throttled_read_bytes_count', +'instance/disk/write_bytes_count', 'instance/disk/throttled_write_bytes_count', +'instance/disk/read_ops_count', 'instance/disk/write_ops_count') (Can be a regexp). + +=item B<--warning-$metric$-$aggregation$> + +Thresholds warning ($metric$ can be: 'disk-read-bytes-count', 'disk-throttled-read-bytes-count', +'disk-write-bytes-count', 'disk-throttled-write-bytes-count','disk-read-ops-count', +'disk-write-ops-count', $aggregation$ can be: 'minimum', 'maximum', 'average', 'total'). + +=item B<--critical-$metric$-$aggregation$> + +Thresholds critical ($metric$ can be: 'disk-read-bytes-count', 'disk-throttled-read-bytes-count', +'disk-write-bytes-count', 'disk-throttled-write-bytes-count','disk-read-ops-count', +'disk-write-ops-count', $aggregation$ can be: 'minimum', 'maximum', 'average', 'total'). + +=item B<--per-sec> + +Change the data to be unit/sec. + +=back + +=cut diff --git a/cloud/google/gcp/compute/computeengine/mode/network.pm b/cloud/google/gcp/compute/computeengine/mode/network.pm new file mode 100644 index 000000000..9f2fefef6 --- /dev/null +++ b/cloud/google/gcp/compute/computeengine/mode/network.pm @@ -0,0 +1,289 @@ +# +# 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::google::gcp::compute::computeengine::mode::network; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use cloud::google::gcp::custom::misc; + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "Instance '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " "; +} + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'}; + $self->{result_values}->{metric_perf} = $options{extra_options}->{metric_perf}; + $self->{result_values}->{metric_label} = $options{extra_options}->{metric_label}; + $self->{result_values}->{metric_name} = $options{extra_options}->{metric_name}; + $self->{result_values}->{metric} = $options{extra_options}->{metric}; + $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'}; + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric} . '_' . $self->{result_values}->{stat}}; + $self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check( + value => defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}, + threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ] + ); + return $exit; +} + +sub custom_traffic_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + + $self->{output}->perfdata_add( + label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label, + unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? 'B/s' : 'B', + value => sprintf("%d", defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + min => 0 + ); +} + +sub custom_traffic_output { + my ($self, %options) = @_; + my $msg = ""; + + if (defined($self->{instance_mode}->{option_results}->{per_sec})) { + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec}); + $msg = $self->{result_values}->{metric_name} . ": " . $value . $unit . "/s"; + } else { + my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}); + $msg = $self->{result_values}->{metric_name} . ": " . $value . $unit; + } + return $msg; +} + +sub custom_packet_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + + $self->{output}->perfdata_add( + label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label, + unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? 'packet/s' : 'packet', + value => sprintf("%.2f", defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}), + min => 0 + ); +} + +sub custom_packet_output { + my ($self, %options) = @_; + my $msg = ""; + + if (defined($self->{instance_mode}->{option_results}->{per_sec})) { + $msg = sprintf("%s: %.2f packets/s", $self->{result_values}->{metric_name}, $self->{result_values}->{value_per_sec}); + } else { + $msg = sprintf("%s: %.2f packets", $self->{result_values}->{metric_name}, $self->{result_values}->{value}); + } + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', + message_multiple => "All network metrics are ok", skipped_code => { -10 => 1 } }, + ]; + + foreach my $aggregation ('minimum', 'maximum', 'average', 'total') { + foreach my $metric ('instance/network/received_bytes_count', 'instance/network/sent_bytes_count') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, + { name => 'stat' }, { name => 'timeframe' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric_perf => $metric_perf, + metric_label => $metric_label, metric_name => $metric_name, metric => $metric }, + closure_custom_output => $self->can('custom_traffic_output'), + closure_custom_perfdata => $self->can('custom_traffic_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold'), + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + foreach my $metric ('instance/network/received_packets_count', 'instance/network/sent_packets_count') { + my $metric_label = cloud::google::gcp::custom::misc::format_metric_label(metric => $metric, remove => 'instance/'); + my $metric_perf = cloud::google::gcp::custom::misc::format_metric_perf(metric => $metric, remove => 'instance/'); + my $metric_name = cloud::google::gcp::custom::misc::format_metric_name(metric => $metric, remove => 'instance/'); + my $entry = { label => $metric_label . '-' . $aggregation, set => { + key_values => [ { name => $metric . '_' . $aggregation }, { name => 'display' }, + { name => 'stat' }, { name => 'timeframe' } ], + closure_custom_calc => $self->can('custom_metric_calc'), + closure_custom_calc_extra_options => { metric_perf => $metric_perf, + metric_label => $metric_label, metric_name => $metric_name, metric => $metric }, + closure_custom_output => $self->can('custom_packet_output'), + closure_custom_perfdata => $self->can('custom_packet_perfdata'), + closure_custom_threshold_check => $self->can('custom_metric_threshold'), + } + }; + push @{$self->{maps_counters}->{metric}}, $entry; + } + } +} + +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 => { + "instance:s@" => { name => 'instance' }, + "filter-metric:s" => { name => 'filter_metric' }, + "per-sec" => { name => 'per_sec' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{instance})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --instance ."); + $self->{output}->option_exit(); + } + + $self->{gcp_instance} = $self->{option_results}->{instance}; + $self->{gcp_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; + $self->{gcp_aggregations} = ['average']; + if (defined($self->{option_results}->{aggregation})) { + $self->{gcp_aggregations} = []; + foreach my $stat (@{$self->{option_results}->{aggregation}}) { + if ($stat ne '') { + push @{$self->{gcp_aggregations}}, $stat; + } + } + } + + foreach my $metric ('instance/network/received_bytes_count', 'instance/network/sent_bytes_count', + 'instance/network/received_packets_count', 'instance/network/sent_packets_count') { + next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '' + && $metric !~ /$self->{option_results}->{filter_metric}/); + + push @{$self->{gcp_metrics}}, $metric; + } + + $self->{gcp_api} = "compute.googleapis.com"; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $metric_results; + foreach my $instance (@{$self->{gcp_instance}}) { + foreach my $metric (@{$self->{gcp_metrics}}) { + ($metric_results, undef) = $options{custom}->gcp_get_metrics( + instance => $instance, + metric => $metric, + api => $self->{gcp_api}, + aggregations => $self->{gcp_aggregations}, + timeframe => $self->{gcp_timeframe}, + ); + + foreach my $aggregation (@{$self->{gcp_aggregations}}) { + next if (!defined($metric_results->{$metric}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed})); + + $self->{metric}->{$instance . "_" . lc($aggregation)}->{display} = $metric_results->{labels}->{instance_name}; + $self->{metric}->{$instance . "_" . lc($aggregation)}->{timeframe} = $self->{gcp_timeframe}; + $self->{metric}->{$instance . "_" . lc($aggregation)}->{stat} = lc($aggregation); + $self->{metric}->{$instance . "_" . lc($aggregation)}->{$metric . "_" . lc($aggregation)} = defined($metric_results->{$metric}->{lc($aggregation)}) ? $metric_results->{$metric}->{lc($aggregation)} : 0; + } + } + } + + if (scalar(keys %{$self->{metric}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Compute Engine instances network metrics. + +Example: + +perl centreon_plugins.pl --plugin=cloud::google::gcp::compute::computeengine::plugin --custommode=api --mode=network +--instance=mycomputeinstance --filter-metric='bytes' --aggregation='average' +--critical-network-received-bytes-count-average='10' --verbose + +Default aggregation: 'average' / All aggregations are valid. + +=over 8 + +=item B<--instance> + +Set instance name (Required). + +=item B<--filter-metric> + +Filter metrics (Can be: 'instance/network/received_bytes_count', +'instance/network/sent_bytes_count', 'instance/network/received_packets_count', +'instance/network/sent_packets_count') (Can be a regexp). + +=item B<--warning-$metric$-$aggregation$> + +Thresholds warning ($metric$ can be: 'network-received-bytes-count', 'network-sent-bytes-count', +'network-received-packets-count','network-sent-packets-count', +$aggregation$ can be: 'minimum', 'maximum', 'average', 'total'). + +=item B<--critical-$metric$-$aggregation$> + +Thresholds critical ($metric$ can be: 'network-received-bytes-count', 'network-sent-bytes-count', +'network-received-packets-count','network-sent-packets-count', +$aggregation$ can be: 'minimum', 'maximum', 'average', 'total'). + +=item B<--per-sec> + +Change the data to be unit/sec. + +=back + +=cut diff --git a/cloud/google/gcp/compute/computeengine/plugin.pm b/cloud/google/gcp/compute/computeengine/plugin.pm new file mode 100644 index 000000000..79ee41944 --- /dev/null +++ b/cloud/google/gcp/compute/computeengine/plugin.pm @@ -0,0 +1,51 @@ +# +# 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::google::gcp::compute::computeengine::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} } = ( + 'cpu' => 'cloud::google::gcp::compute::computeengine::mode::cpu', + 'diskio' => 'cloud::google::gcp::compute::computeengine::mode::diskio', + 'network' => 'cloud::google::gcp::compute::computeengine::mode::network', + ); + + $self->{custom_modes}{api} = 'cloud::google::gcp::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Google Cloud Platform Compute Engine. + +=cut diff --git a/cloud/google/gcp/custom/api.pm b/cloud/google/gcp/custom/api.pm index b6daa99a6..3f3cf7abe 100644 --- a/cloud/google/gcp/custom/api.pm +++ b/cloud/google/gcp/custom/api.pm @@ -189,7 +189,7 @@ sub get_access_token { full_url => $self->{authorization_endpoint}, hostname => ''); - if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) { + if (!defined($content) || $content eq '') { $self->{output}->add_option_msg(short_msg => "Authorization endpoint API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); $self->{output}->option_exit(); } @@ -255,7 +255,7 @@ sub request_api { my $content = $self->{http}->request(%options); - if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) { + if (!defined($content) || $content eq '') { $self->{output}->add_option_msg(short_msg => "Monitoring endpoint API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); $self->{output}->option_exit(); } diff --git a/cloud/google/gcp/custom/misc.pm b/cloud/google/gcp/custom/misc.pm new file mode 100644 index 000000000..6b93aaab4 --- /dev/null +++ b/cloud/google/gcp/custom/misc.pm @@ -0,0 +1,62 @@ +# +# 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::google::gcp::custom::misc; + +use strict; +use warnings; + +sub format_metric_label { + my (%options) = @_; + + my $metric = $options{metric}; + $metric =~ s/$options{remove}// if (defined($options{remove}) && $options{remove} ne ''); + $metric = lc($metric); + $metric =~ s/(\/)|(_)/-/g; + + return $metric; +} + +sub format_metric_perf { + my (%options) = @_; + + my $metric = $options{metric}; + $metric =~ s/$options{remove}// if (defined($options{remove}) && $options{remove} ne ''); + $metric = lc($metric); + $metric =~ s/\//_/g; + + return $metric; +} + +sub format_metric_name { + my (%options) = @_; + + my $metric = $options{metric}; + $metric =~ s/$options{remove}// if (defined($options{remove}) && $options{remove} ne ''); + $metric =~ s/(\/)|(_)/ /g; + $metric =~ s/(\w+)/\u$1/g; + + return $metric; +} + +1; + +__END__ +