From 79cc4c27e2ba6d520059c3f7b2926e09a0944e2f Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 8 Jul 2019 14:15:29 +0200 Subject: [PATCH] wip: multi meta mode --- centreon/plugins/multi.pm | 115 ++++++++++++++++++++++++++++++ centreon/plugins/output.pm | 5 +- centreon/plugins/script_custom.pm | 23 +++--- centreon/plugins/script_simple.pm | 21 +++--- centreon/plugins/script_snmp.pm | 31 ++++---- centreon/plugins/script_sql.pm | 21 +++--- centreon/plugins/script_wsman.pm | 21 +++--- 7 files changed, 188 insertions(+), 49 deletions(-) create mode 100644 centreon/plugins/multi.pm diff --git a/centreon/plugins/multi.pm b/centreon/plugins/multi.pm new file mode 100644 index 000000000..763337e1b --- /dev/null +++ b/centreon/plugins/multi.pm @@ -0,0 +1,115 @@ +# +# 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 centreon::plugins::multi; + +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 => { + 'modes-exec:s' => { name => 'modes_exec' }, + 'option-mode:s@' => { name => 'option_mode' }, + }); + $self->{options} = $options{options}; + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{modes_exec})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --modes-exec option."); + $self->{output}->option_exit(); + } + $self->{options_mode_extra} = {}; + if (defined($self->{option_results}->{option_mode})) { + foreach (@{$self->{option_results}->{option_mode}}) { + next if (! /^(.+?),(.*)$/); + $self->{options_mode_extra}->{$1} = [] if (!defined($self->{options_mode_extra}->{$1})); + push @{$self->{options_mode_extra}->{$1}}, $2; + } + } + + $self->{modes} = $options{modes}; +} + +sub run { + my ($self, %options) = @_; + + $self->{output}->parameter(attr => 'nodisplay', value => 1); + $self->{output}->parameter(attr => 'noexit_die', value => 1); + $self->{output}->use_new_perfdata(value => 1); + + my @modes = split /,/, $self->{option_results}->{modes_exec}; + foreach (@modes) { + next if (!defined($self->{modes}->{$_})); + eval { + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => $self->{modes}->{$_}, + error_msg => "Cannot load module --mode $_" + ); + @ARGV = (@{$self->{options_mode_extra}->{$_}}) if (defined($self->{options_mode_extra}->{$_})); + $self->{output}->mode(name => $_); + + my $mode = $self->{modes}->{$_}->new(options => $self->{options}, output => $self->{output}, mode => $_); + $self->{options}->parse_options(); + my $option_results = $self->{options}->get_options(); + $mode->check_options(option_results => $option_results, %options); + $mode->run(%options); + }; + if ($@) { + $self->{output}->output_add(long_msg => 'eval result mode ' . $_ . ': ' . $@, debug => 1); + } + } + + $self->{output}->parameter(attr => 'nodisplay', value => 0); + $self->{output}->parameter(attr => 'noexit_die', value => 0); + $self->{output}->display(); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Check multiple modes at once. You cannot set specific thresholds or filter options for modes. + +=over 8 + +=item B<--modes-exec> + +Which modes to select (separated by coma). +Example for linux: --modes-exec=cpu,memory,storage,interfaces + +=back + +=cut diff --git a/centreon/plugins/output.pm b/centreon/plugins/output.pm index a71f122c8..1ef936100 100644 --- a/centreon/plugins/output.pm +++ b/centreon/plugins/output.pm @@ -188,7 +188,7 @@ sub output_add { sub perfdata_add { my ($self, %options) = @_; my $perfdata = { - label => '', value => '', unit => '', warning => '', critical => '', min => '', max => '' + label => '', value => '', unit => '', warning => '', critical => '', min => '', max => '', mode => $self->{mode}, }; foreach (keys %options) { next if (!defined($options{$_})); @@ -397,7 +397,7 @@ sub output_openmetrics { if ($label =~ /^(.*?)#(.*)$/) { ($perf->{instance}, $label) = ($1, $2); } - my ($bucket, $append) = ('{plugin="' . $self->{plugin} . '",mode="' . $self->{mode} . '"', ''); + my ($bucket, $append) = ('{plugin="' . $self->{plugin} . '",mode="' . $perf->{mode} . '"', ''); foreach ('unit', 'warning', 'critical', 'min', 'max', 'instance') { if (defined($perf->{$_}) && $perf->{$_} ne '') { $bucket .= ',' . $_ . '="' . $perf->{$_} . '"'; @@ -495,6 +495,7 @@ sub display { my $force_long_output = (defined($options{force_long_output}) && $options{force_long_output} == 1) ? 1 : 0; $force_long_output = 1 if (defined($self->{option_results}->{debug})); + return if ($self->{nodisplay} == 1); if (defined($self->{option_results}->{output_file})) { if (!open (STDOUT, '>', $self->{option_results}->{output_file})) { $self->output_add(severity => 'UNKNOWN', diff --git a/centreon/plugins/script_custom.pm b/centreon/plugins/script_custom.pm index 5ea3251b4..8e0034c10 100644 --- a/centreon/plugins/script_custom.pm +++ b/centreon/plugins/script_custom.pm @@ -28,9 +28,6 @@ sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{package} = parent package caller - # $options{options} = options object - # $options{output} = output object $self->{options} = $options{options}; $self->{output} = $options{output}; @@ -78,9 +75,9 @@ sub load_custom_mode { sub init { my ($self, %options) = @_; - # $options{version} = string version - # $options{help} = string help + # add meta mode + $self->{modes}->{multi} = 'centreon::plugins::multi'; if (defined($options{help}) && !defined($self->{mode_name}) && !defined($self->{dynmode_name})) { $self->{options}->display_help(); $self->{output}->option_exit(); @@ -153,7 +150,11 @@ sub init { $self->{custommode_current}->set_options(option_results => $self->{option_results}); push @{$self->{custommode_stored}}, $self->{custommode_current}; } - $self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default}); + $self->{mode}->check_options( + option_results => $self->{option_results}, + default => $self->{default}, + modes => $self->{modes} # for meta mode multi + ); } sub load_password_mgr { @@ -197,7 +198,6 @@ sub run { sub is_mode { my ($self, %options) = @_; - # $options->{mode} = mode if (!defined($self->{modes}{$options{mode}})) { $self->{output}->add_option_msg(short_msg => "mode '" . $options{mode} . "' doesn't exist (use --list-mode option to show available modes)."); $self->{output}->option_exit(); @@ -207,7 +207,6 @@ sub is_mode { sub is_custommode { my ($self, %options) = @_; - # $options->{custommode} = mode if (!defined($self->{custom_modes}{$options{custommode}})) { $self->{output}->add_option_msg(short_msg => "mode '" . $options{custommode} . "' doesn't exist (use --list-custommode option to show available modes)."); $self->{output}->option_exit(); @@ -224,9 +223,13 @@ sub list_mode { my $self = shift; $self->{options}->display_help(); - $self->{output}->add_option_msg(long_msg => "Modes Available:"); + $self->{output}->add_option_msg(long_msg => 'Modes Meta:'); + $self->{output}->add_option_msg(long_msg => ' multi'); + $self->{output}->add_option_msg(long_msg => ''); + $self->{output}->add_option_msg(long_msg => 'Modes Available:'); foreach (sort keys %{$self->{modes}}) { - $self->{output}->add_option_msg(long_msg => " " . $_); + next if ($_ eq 'multi'); + $self->{output}->add_option_msg(long_msg => ' ' . $_); } $self->{output}->option_exit(nolabel => 1); } diff --git a/centreon/plugins/script_simple.pm b/centreon/plugins/script_simple.pm index 569d03e4a..ca141f7e5 100644 --- a/centreon/plugins/script_simple.pm +++ b/centreon/plugins/script_simple.pm @@ -27,9 +27,6 @@ sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{package} = parent package caller - # $options{options} = options object - # $options{output} = output object $self->{options} = $options{options}; $self->{output} = $options{output}; @@ -62,9 +59,9 @@ sub new { sub init { my ($self, %options) = @_; - # $options{version} = string version - # $options{help} = string help + # add meta mode + $self->{modes}->{multi} = 'centreon::plugins::multi'; if (defined($options{help}) && !defined($self->{mode_name}) && !defined($self->{dynmode_name})) { $self->{options}->display_help(); $self->{output}->option_exit(); @@ -119,7 +116,11 @@ sub init { $self->{option_results} = $self->{options}->get_options(); $self->{pass_mgr}->manage_options(option_results => $self->{option_results}) if (defined($self->{pass_mgr})); - $self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default}); + $self->{mode}->check_options( + option_results => $self->{option_results}, + default => $self->{default}, + modes => $self->{modes} # for meta mode multi + ); } sub load_password_mgr { @@ -171,9 +172,13 @@ sub list_mode { my $self = shift; $self->{options}->display_help(); - $self->{output}->add_option_msg(long_msg => "Modes Available:"); + $self->{output}->add_option_msg(long_msg => 'Modes Meta:'); + $self->{output}->add_option_msg(long_msg => ' multi'); + $self->{output}->add_option_msg(long_msg => ''); + $self->{output}->add_option_msg(long_msg => 'Modes Available:'); foreach (sort keys %{$self->{modes}}) { - $self->{output}->add_option_msg(long_msg => " " . $_); + next if ($_ eq 'multi'); + $self->{output}->add_option_msg(long_msg => ' ' . $_); } $self->{output}->option_exit(nolabel => 1); } diff --git a/centreon/plugins/script_snmp.pm b/centreon/plugins/script_snmp.pm index fc7378dd9..783e62e5e 100644 --- a/centreon/plugins/script_snmp.pm +++ b/centreon/plugins/script_snmp.pm @@ -29,9 +29,6 @@ sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{package} = parent package caller - # $options{options} = options object - # $options{output} = output object $self->{options} = $options{options}; $self->{output} = $options{output}; @@ -46,7 +43,7 @@ sub new { } ); $self->{version} = '1.0'; - %{$self->{modes}} = (); + $self->{modes} = {}; $self->{default} = undef; $self->{options}->parse_options(); @@ -65,9 +62,9 @@ sub new { sub init { my ($self, %options) = @_; - # $options{version} = string version - # $options{help} = string help + # add meta mode + $self->{modes}->{multi} = 'centreon::plugins::multi'; if (defined($options{help}) && !defined($self->{mode_name}) && !defined($self->{dynmode_name})) { $self->{options}->display_help(); $self->{output}->option_exit(); @@ -126,7 +123,12 @@ sub init { $self->{pass_mgr}->manage_options(option_results => $self->{option_results}) if (defined($self->{pass_mgr})); $self->{snmp}->check_options(option_results => $self->{option_results}); - $self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default}, snmp => $self->{snmp}); + $self->{mode}->check_options( + option_results => $self->{option_results}, + default => $self->{default}, + snmp => $self->{snmp}, + modes => $self->{modes} # for meta mode multi + ); } sub load_password_mgr { @@ -162,8 +164,7 @@ sub run { sub is_mode { my ($self, %options) = @_; - - # $options->{mode} = mode + if (!defined($self->{modes}{$options{mode}})) { $self->{output}->add_option_msg(short_msg => "mode '" . $options{mode} . "' doesn't exist (use --list-mode option to show available modes)."); $self->{output}->option_exit(); @@ -172,17 +173,21 @@ sub is_mode { sub version { my ($self) = @_; - $self->{output}->add_option_msg(short_msg => "Plugin Version: " . $self->{version}); + $self->{output}->add_option_msg(short_msg => 'Plugin Version: ' . $self->{version}); $self->{output}->option_exit(nolabel => 1); } sub list_mode { my ($self) = @_; $self->{options}->display_help(); - - $self->{output}->add_option_msg(long_msg => "Modes Available:"); + + $self->{output}->add_option_msg(long_msg => 'Modes Meta:'); + $self->{output}->add_option_msg(long_msg => ' multi'); + $self->{output}->add_option_msg(long_msg => ''); + $self->{output}->add_option_msg(long_msg => 'Modes Available:'); foreach (sort keys %{$self->{modes}}) { - $self->{output}->add_option_msg(long_msg => " " . $_); + next if ($_ eq 'multi'); + $self->{output}->add_option_msg(long_msg => ' ' . $_); } $self->{output}->option_exit(nolabel => 1); } diff --git a/centreon/plugins/script_sql.pm b/centreon/plugins/script_sql.pm index 4f782f927..f97d0812d 100644 --- a/centreon/plugins/script_sql.pm +++ b/centreon/plugins/script_sql.pm @@ -28,9 +28,6 @@ sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{package} = parent package caller - # $options{options} = options object - # $options{output} = output object $self->{options} = $options{options}; $self->{output} = $options{output}; @@ -70,9 +67,9 @@ sub new { sub init { my ($self, %options) = @_; - # $options{version} = string version - # $options{help} = string help + # add meta mode + $self->{modes}->{multi} = 'centreon::plugins::multi'; if (defined($options{help}) && !defined($self->{mode_name}) && !defined($self->{dynmode_name})) { $self->{options}->display_help(); $self->{output}->option_exit(); @@ -149,7 +146,11 @@ sub init { $self->{sqlmode_current}->set_options(option_results => $self->{option_results}); push @{$self->{sqlmode_stored}}, $self->{sqlmode_current}; } - $self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default}); + $self->{mode}->check_options( + option_results => $self->{option_results}, + default => $self->{default}, + modes => $self->{modes} # for meta mode multi + ); } sub load_password_mgr { @@ -221,9 +222,13 @@ sub list_mode { my $self = shift; $self->{options}->display_help(); - $self->{output}->add_option_msg(long_msg => "Modes Available:"); + $self->{output}->add_option_msg(long_msg => 'Modes Meta:'); + $self->{output}->add_option_msg(long_msg => ' multi'); + $self->{output}->add_option_msg(long_msg => ''); + $self->{output}->add_option_msg(long_msg => 'Modes Available:'); foreach (sort keys %{$self->{modes}}) { - $self->{output}->add_option_msg(long_msg => " " . $_); + next if ($_ eq 'multi'); + $self->{output}->add_option_msg(long_msg => ' ' . $_); } $self->{output}->option_exit(nolabel => 1); } diff --git a/centreon/plugins/script_wsman.pm b/centreon/plugins/script_wsman.pm index 493429677..2c9b62159 100644 --- a/centreon/plugins/script_wsman.pm +++ b/centreon/plugins/script_wsman.pm @@ -29,9 +29,6 @@ sub new { my ($class, %options) = @_; my $self = {}; bless $self, $class; - # $options{package} = parent package caller - # $options{options} = options object - # $options{output} = output object $self->{options} = $options{options}; $self->{output} = $options{output}; @@ -64,9 +61,9 @@ sub new { sub init { my ($self, %options) = @_; - # $options{version} = string version - # $options{help} = string help + # add meta mode + $self->{modes}->{multi} = 'centreon::plugins::multi'; if (defined($options{help}) && !defined($self->{mode_name}) && !defined($self->{dynmode_name})) { $self->{options}->display_help(); $self->{output}->option_exit(); @@ -121,7 +118,11 @@ sub init { $self->{pass_mgr}->manage_options(option_results => $self->{option_results}) if (defined($self->{pass_mgr})); $self->{wsman}->check_options(option_results => $self->{option_results}); - $self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default}); + $self->{mode}->check_options( + option_results => $self->{option_results}, + default => $self->{default}, + modes => $self->{modes} # for meta mode multi + ); } sub load_password_mgr { @@ -175,9 +176,13 @@ sub list_mode { my $self = shift; $self->{options}->display_help(); - $self->{output}->add_option_msg(long_msg => "Modes Available:"); + $self->{output}->add_option_msg(long_msg => 'Modes Meta:'); + $self->{output}->add_option_msg(long_msg => ' multi'); + $self->{output}->add_option_msg(long_msg => ''); + $self->{output}->add_option_msg(long_msg => 'Modes Available:'); foreach (sort keys %{$self->{modes}}) { - $self->{output}->add_option_msg(long_msg => " " . $_); + next if ($_ eq 'multi'); + $self->{output}->add_option_msg(long_msg => ' ' . $_); } $self->{output}->option_exit(nolabel => 1); }