From 84ae04aa7aa47292eb80306de8cb6db994788861 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Mon, 6 Feb 2017 16:28:33 +0100 Subject: [PATCH 01/12] Add mode /virtualservice/ to play with centreon metrics --- apps/centreon/sql/mode/virtualservice.pm | 301 +++++++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 apps/centreon/sql/mode/virtualservice.pm diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm new file mode 100644 index 000000000..353b76f20 --- /dev/null +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -0,0 +1,301 @@ +# +# Copyright 2016 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 apps::centreon::sql::mode::virtualservice; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use List::Util qw (min max sum); +use JSON; + +my $instance_mode; +my $config_data; + +sub custom_metric_output { + my ($self, %options) = @_; + my $msg; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); + }; + + if (defined($message)) { + $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); + $self->{output}->option_exit(); + } + return $msg; +} + +sub custom_metric_calc { + my ($self, %options) = @_; + + $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_value'}; + $self->{result_values}->{instance} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{type} = $options{new_datas}->{$self->{instance} . '_type'}; + $self->{result_values}->{perfdata_unit} = $options{new_datas}->{$self->{instance} . '_unit'}; + $self->{result_values}->{min} = $options{new_datas}->{$self->{instance} . '_min'}; + $self->{result_values}->{max} = $options{new_datas}->{$self->{instance} . '_max'}; + + if (defined($config_data->{formatting}->{change_bytes})) { + ($self->{result_values}->{output_value}, $self->{result_values}->{unit}) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}); + } + return 0; +} + +sub custom_metric_perfdata { + my ($self, %options) = @_; + + if ($self->{result_values}->{type} eq 'unique') { + $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, + 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'), + unit => $self->{result_values}->{perfdata_unit}, + min => $self->{result_values}->{min}, + max => $self->{result_values}->{max}, + ); + } else { + $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, + value => $self->{result_values}->{value}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-global'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-global'), + unit => $self->{result_values}->{perfdata_unit}, + min => $self->{result_values}->{min}, + max => $self->{result_values}->{max}, + ); + } +} + +sub custom_metric_threshold { + my ($self, %options) = @_; + my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{value}; + + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label} , exit_litteral => 'critical' }, { label => 'warning-'.$self->{label}, exit_litteral => 'warning' } ]); + + return $exit; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 1, message_multiple => 'Global curves are OK' }, + { name => 'metric', type => 1, message_multiple => 'Metric curves are OK' } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'global', set => { + key_values => [ { name => 'value' }, { name => 'display' }, { name => 'type' }, { name => 'unit' }, { name => 'min' }, { name => 'max' } ], + closure_custom_calc => \&custom_metric_calc, + closure_custom_output => \&custom_metric_output, + closure_custom_perfdata => \&custom_metric_perfdata, + closure_custom_threshold_check => \&custom_metric_threshold, + } + } + ]; + + $self->{maps_counters}->{metric} = [ + { label => 'metric', set => { + key_values => [ { name => 'value' }, { name => 'display' }, { name => 'type' }, { name => 'unit' }, { name => 'min' }, { name => 'max' } ], + closure_custom_calc => \&custom_metric_calc, + closure_custom_output => \&custom_metric_output, + closure_custom_perfdata => \&custom_metric_perfdata, + closure_custom_threshold_check => \&custom_metric_threshold, + } + } + ]; +} + +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 => + { + "config-file:s" => { name => 'config_file' }, + }); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + if (!defined($self->{option_results}->{config_file}) || $self->{option_results}->{config_file} eq '') { + $self->{output}->add_option_msg(short_msg => "Please define --config-file option"); + $self->{output}->option_exit(); + } + if (! -f $self->{option_results}->{config_file}) { + $self->{output}->add_option_msg(short_msg => "Can't find plugin configuration file (--config-file should be a full path to an existing file)"); + $self->{output}->option_exit(); + } + + $config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); +} + +sub parse_json_config { + my ($self, %options) = @_; + my $data; + my $message; + + my $json_text = do { + open(my $json_fh, "<:encoding(UTF-8)", $options{config}) + or die("Can't open \$filename\": $!\n"); + local $/; + <$json_fh> + }; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + $data = JSON->new->utf8->decode($json_text); + }; + if ($message) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json config file: $message"); + $self->{output}->option_exit(); + } + return $data +} + +sub manage_selection { + my ($self, %options) = @_; + # $options{sql} = sqlmode object + $self->{sql} = $options{sql}; + $self->{sql}->connect(); + $self->{metrics} = {}; + $self->{vmetrics} = {}; + + #$config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); + + # Selection is prefered can't mix selection and sql matching + if (exists($config_data->{selection})) { + foreach my $id (keys %{$config_data->{selection}}) { + my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max "; + $query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id "; + $query .= "AND index_data.service_description = '" . $config_data->{selection}{$id}->{service_name} . "'"; + $query .= "AND index_data.host_name = '" . $config_data->{selection}{$id}->{host_name} . "'" ; + $query .= "AND metrics.metric_name = '" . $config_data->{selection}{$id}->{metric_name} . "'"; + $self->{sql}->query(query => $query); + while ((my $row = $self->{sql}->fetchrow_hashref())) { + my $metric_key = $id; + $self->{metrics}{$metric_key}{name} = $row->{metric_name}; + $self->{metrics}{$metric_key}{display_name} = $id; + $self->{metrics}{$metric_key}{current} = $row->{current_value}; + $self->{metrics}{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; + $self->{metrics}{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; + $self->{metrics}{$metric_key}{display} = (defined($config_data->{selection}{$id}->{display}) && $config_data->{selection}{$id}->{display} eq 'true') ? 1 : 0; + } + } + } elsif (exists($config_data->{filters})) { + my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max "; + $query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id "; + $query .= "AND index_data.service_description LIKE '" . $config_data->{filters}->{service} . "'" if (defined($config_data->{filters}->{service}) && ($config_data->{filters}->{service} ne '')); + $query .= "AND index_data.host_name LIKE '" . $config_data->{filters}->{host} . "'" if (defined($config_data->{filters}->{host}) && ($config_data->{filters}->{host} ne '')); + $query .= "AND metrics.metric_name LIKE '" . $config_data->{filters}->{metric} . "'" if (defined($config_data->{filters}->{metric}) && ($config_data->{filters}->{metric} ne '')); + $self->{sql}->query(query => $query); + while ((my $row = $self->{sql}->fetchrow_hashref())) { + my $metric_key = $row->{host_name}.$config_data->{formatting}->{cannonical_separator}.$row->{service_description}.$config_data->{formatting}->{cannonical_separator}.$row->{metric_name}; + $self->{metrics}{$metric_key}{display_name} = $metric_key; + $self->{metrics}{$metric_key}{name} = $row->{metric_name}; + $self->{metrics}{$metric_key}{current} = $row->{current_value}; + $self->{metrics}{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; + $self->{metrics}{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; + $self->{metrics}{$metric_key}{display} = (defined($config_data->{filters}->{display})) ? 1 : 0; + } + } + + foreach my $metric (keys %{$self->{metrics}}) { + foreach my $vcurve (keys %{$config_data->{virtualcurve}}) { + if (defined($config_data->{virtualcurve}{$vcurve}->{pattern}) && $config_data->{virtualcurve}{$vcurve}->{pattern} ne '') { + push (@{$self->{vmetrics}->{$vcurve}->{values}}, $self->{metrics}->{$metric}->{current}) if $self->{metrics}->{$metric}->{name} =~ /$config_data->{virtualcurve}{$vcurve}->{pattern}/; + } else { + push (@{$self->{vmetrics}->{$vcurve}->{values}}, $self->{metrics}->{$metric}->{current}); + } + + next if (!defined(@{$self->{vmetrics}->{$vcurve}{values}})); + + $self->{vmetrics}->{$vcurve}{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, + sum(@{$self->{vmetrics}->{$vcurve}{values}})/scalar(@{$self->{vmetrics}->{$vcurve}{values}})) if ($config_data->{virtualcurve}{$vcurve}->{aggregation} eq 'avg'); + $self->{vmetrics}->{$vcurve}{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, + sum(@{$self->{vmetrics}->{$vcurve}{values}})) if ($config_data->{virtualcurve}{$vcurve}->{aggregation} eq 'sum'); + $self->{vmetrics}->{$vcurve}{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, + min(@{$self->{vmetrics}->{$vcurve}{values}})) if ($config_data->{virtualcurve}{$vcurve}->{aggregation} eq 'min'); + $self->{vmetrics}->{$vcurve}{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, + max(@{$self->{vmetrics}->{$vcurve}{values}})) if ($config_data->{virtualcurve}{$vcurve}->{aggregation} eq 'max'); + $self->{vmetrics}->{$vcurve}{aggregated_value} = eval "$self->{vmetrics}->{$vcurve}{aggregated_value} $config_data->{virtualcurve}->{$vcurve}{custom}" if (defined($config_data->{virtualcurve}->{$vcurve}{custom})); + + $self->{vmetrics}->{$vcurve}{unit} = (defined($config_data->{virtualcurve}{$vcurve}->{unit})) ? $config_data->{virtualcurve}{$vcurve}->{unit} : ''; + $self->{vmetrics}->{$vcurve}{min} = (defined($config_data->{virtualcurve}{$vcurve}->{min})) ? $config_data->{virtualcurve}{$vcurve}->{min} : ''; + $self->{vmetrics}->{$vcurve}{max} = (defined($config_data->{virtualcurve}{$vcurve}->{max})) ? $config_data->{virtualcurve}{$vcurve}->{max} : ''; + + $self->{option_results}->{'warning-global'} = (defined($self->{option_results}->{'warning-global'})) ? $self->{option_results}->{'warning-global'} : $config_data->{virtualcurve}{$vcurve}->{warning}; + $self->{option_results}->{'critical-global'} = (defined($self->{option_results}->{'critical-global'})) ? $self->{option_results}->{'critical-global'} : $config_data->{virtualcurve}{$vcurve}->{critical}; + + $self->{global}->{$vcurve} = {display => $vcurve, + type => 'global', + unit => $self->{vmetrics}->{$vcurve}->{unit}, + value => $self->{vmetrics}->{$vcurve}{aggregated_value}, + min => $self->{vmetrics}->{$vcurve}{min}, + max => $self->{vmetrics}->{$vcurve}{max} }; + + } + + $self->{metric}->{$metric} = {display => $self->{metrics}->{$metric}->{display_name}, + type => 'unique', + unit => $self->{metrics}->{$metric}->{unit}, + value => $self->{metrics}->{$metric}->{current}, + min => $self->{metrics}->{$metric}{min}, + max => $self->{metrics}->{$metric}{max} } if ($self->{metrics}->{$metric}->{display} == 1); + } + +} + +1; + +__END__ + +=head1 MODE + +Mode to play with centreon metrics +e.g: display two curves of different service on the same graph +e.g: aggregate multiple metrics (min,max,avg,sum) or custom operation + +=over 8 + +=item B<--config-file> + +Specify the full path to a json config file + +=back + +=cut From caafafd8c5b15eb39ef21ece82416f9950b683b1 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Mon, 6 Feb 2017 17:44:45 +0100 Subject: [PATCH 02/12] + add some options in help --- apps/centreon/sql/mode/virtualservice.pm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 353b76f20..258f3150a 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -296,6 +296,20 @@ e.g: aggregate multiple metrics (min,max,avg,sum) or custom operation Specify the full path to a json config file +=item B<--filter-counters> + +Filter some counter (can be 'unique' or 'global') + +=item B<--warning-*> + +Warning threshold (can be 'unique' or 'global') +BE CAREFUL, will override config file definition + +=item B<--critical-*> + +Critical threshold (can be 'unique' or 'global') +BE CAREFUL, will override config file definition + =back =cut From 98a114006f604d84cba1df2b6ea435db604b9d5c Mon Sep 17 00:00:00 2001 From: Sims24 Date: Tue, 7 Feb 2017 14:48:52 +0100 Subject: [PATCH 03/12] + Enhance customization on global message and counters & refactoring --- apps/centreon/sql/mode/virtualservice.pm | 42 +++++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 258f3150a..0fa7778f9 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -68,25 +68,15 @@ sub custom_metric_calc { sub custom_metric_perfdata { my ($self, %options) = @_; - if ($self->{result_values}->{type} eq 'unique') { - $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, - 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'), - unit => $self->{result_values}->{perfdata_unit}, - min => $self->{result_values}->{min}, - max => $self->{result_values}->{max}, - ); - } else { - $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, - value => $self->{result_values}->{value}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-global'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-global'), - unit => $self->{result_values}->{perfdata_unit}, - min => $self->{result_values}->{min}, - max => $self->{result_values}->{max}, - ); - } + $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, + value => $self->{result_values}->{value}, + warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global'), + critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global'), + unit => $self->{result_values}->{perfdata_unit}, + min => $self->{result_values}->{min}, + max => $self->{result_values}->{max}, + ); + } sub custom_metric_threshold { @@ -193,10 +183,17 @@ sub manage_selection { $self->{metrics} = {}; $self->{vmetrics} = {}; - #$config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); + if (exists($config_data->{virtualcurve})) { + push @{$self->{maps_counters_type}}, { + name => 'global', type => 1, message_separator => $config_data->{formatting}->{message_separator}, message_multiple => $config_data->{formatting}->{custom_message_global}, + }; + } # Selection is prefered can't mix selection and sql matching if (exists($config_data->{selection})) { + push @{$self->{maps_counters_type}}, { + name => 'metric', type => 1, message_separator => $config_data->{formatting}->{message_separator}, message_multiple => $config_data->{formatting}->{custom_message_metric}, + }; foreach my $id (keys %{$config_data->{selection}}) { my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max "; $query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id "; @@ -216,6 +213,9 @@ sub manage_selection { } } } elsif (exists($config_data->{filters})) { + push @{$self->{maps_counters_type}}, { + name => 'metric', type => 1, message_separator => $config_data->{formatting}->{message_separator}, message_multiple => $config_data->{formatting}->{custom_message_metric}, + }; my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max "; $query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id "; $query .= "AND index_data.service_description LIKE '" . $config_data->{filters}->{service} . "'" if (defined($config_data->{filters}->{service}) && ($config_data->{filters}->{service} ne '')); @@ -299,6 +299,8 @@ Specify the full path to a json config file =item B<--filter-counters> Filter some counter (can be 'unique' or 'global') +Useless, if you use selection/filter but not +global/virtual curves =item B<--warning-*> From 031a9bae39c875bc74e674c34f8dc706656dba63 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Wed, 15 Feb 2017 12:14:10 +0100 Subject: [PATCH 04/12] enhance code after review --- apps/centreon/sql/mode/virtualservice.pm | 35 ++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 0fa7778f9..a811013c2 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -129,6 +129,7 @@ sub new { $options{options}->add_options(arguments => { "config-file:s" => { name => 'config_file' }, + "json-data:s" => { name => 'json_data' }, }); return $self; } @@ -138,16 +139,30 @@ sub check_options { $self->SUPER::check_options(%options); $instance_mode = $self; - if (!defined($self->{option_results}->{config_file}) || $self->{option_results}->{config_file} eq '') { - $self->{output}->add_option_msg(short_msg => "Please define --config-file option"); + + if (!defined($self->{option_results}->{config_file}) && !defined($self->{option_results}->{json_data}) { + $self->{output}->add_option_msg(short_msg => "Please define --config-file or --json-data option"); $self->{output}->option_exit(); } - if (! -f $self->{option_results}->{config_file}) { - $self->{output}->add_option_msg(short_msg => "Can't find plugin configuration file (--config-file should be a full path to an existing file)"); + if (-f $self->{option_results}->{config_file} && $self->{option_results}->{config_file} eq '') { + $config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); + } elsif (defined($self->{option_results}->{json_data}) && $self->{option_results}->{json_data} eq '') { + $config_data = $self->parse_json_config(config => $self->{option_results}->{json_data}; + } else { + $self->{output}->add_option_msg(short_msg => "Can't find plugin configuration file / Cannot read from --json-data option"); + $self->{output}->option_exit(); + } + + if (!exists($config_data->{selection}) && !exists($config_data->{filters})) { + $self->{output}->add_option_msg(short_msg => "Config_error: there is neither filters nor selection section in your JSON configuration !"); + $self->{output}->option_exit(); + } + + if (!exists($config_data->{formatting})) { + $self->{output}->add_option_msg(short_msg => "Config_error: formatting section is mandatory in yout JSON condiguration !"); $self->{output}->option_exit(); } - $config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); } sub parse_json_config { @@ -278,6 +293,12 @@ sub manage_selection { max => $self->{metrics}->{$metric}{max} } if ($self->{metrics}->{$metric}->{display} == 1); } + if (scalar(keys %{$self->{metric}}) <= 0 && scalar(keys %{$self->{vmetrics}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No metrics returned - are your selection/filters correct ?"); + $self->{output}->option_exit(); + } + + } 1; @@ -296,6 +317,10 @@ e.g: aggregate multiple metrics (min,max,avg,sum) or custom operation Specify the full path to a json config file +=item B<--json-data> + +Specify the full path to a json config file + =item B<--filter-counters> Filter some counter (can be 'unique' or 'global') From 9f2776f1cc495f357f5b714a473a8d56d939c69e Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 16 Feb 2017 13:21:18 +0100 Subject: [PATCH 05/12] Add possibility to override global formatting definition --- apps/centreon/sql/mode/virtualservice.pm | 27 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index a811013c2..4a276565f 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -35,18 +35,33 @@ sub custom_metric_output { my $msg; my $message; - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - - $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); - }; + if (defined($config_data->{filters}) && defined($config_data->{filters}->{formatting})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); + }; + } elsif (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); + }; + } else { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); + }; + } if (defined($message)) { $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); $self->{output}->option_exit(); } + return $msg; + } sub custom_metric_calc { From fb647abf22e9c80085ffb636f63e19d6d784fb1b Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 16 Feb 2017 16:55:39 +0100 Subject: [PATCH 06/12] clean code and add last enhancement --- apps/centreon/sql/mode/virtualservice.pm | 34 +++++++++++------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 4a276565f..f448bfaf0 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -35,18 +35,18 @@ sub custom_metric_output { my $msg; my $message; - if (defined($config_data->{filters}) && defined($config_data->{filters}->{formatting})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); - }; - } elsif (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { + if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); }; + } elsif (defined($config_data->{filters}) && defined($config_data->{filters}->{formatting})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); + }; } else { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; @@ -108,8 +108,6 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'global', type => 1, message_multiple => 'Global curves are OK' }, - { name => 'metric', type => 1, message_multiple => 'Metric curves are OK' } ]; $self->{maps_counters}->{global} = [ @@ -144,7 +142,7 @@ sub new { $options{options}->add_options(arguments => { "config-file:s" => { name => 'config_file' }, - "json-data:s" => { name => 'json_data' }, + "json-data:s" => { name => 'json_data' }, }); return $self; } @@ -155,17 +153,17 @@ sub check_options { $instance_mode = $self; - if (!defined($self->{option_results}->{config_file}) && !defined($self->{option_results}->{json_data}) { + if (!defined($self->{option_results}->{config_file}) && !defined($self->{option_results}->{json_data})) { $self->{output}->add_option_msg(short_msg => "Please define --config-file or --json-data option"); $self->{output}->option_exit(); } - if (-f $self->{option_results}->{config_file} && $self->{option_results}->{config_file} eq '') { - $config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); - } elsif (defined($self->{option_results}->{json_data}) && $self->{option_results}->{json_data} eq '') { - $config_data = $self->parse_json_config(config => $self->{option_results}->{json_data}; + if (defined($self->{option_results}->{config_file}) && $self->{option_results}->{config_file} ne '') { + $config_data = $self->parse_json_config(config => $self->{option_results}->{config_file}); + } elsif (defined($self->{option_results}->{json_data}) && $self->{option_results}->{json_data} ne '') { + $config_data = $self->parse_json_config(config => $self->{option_results}->{json_data}); } else { $self->{output}->add_option_msg(short_msg => "Can't find plugin configuration file / Cannot read from --json-data option"); - $self->{output}->option_exit(); + $self->{output}->option_exit(); } if (!exists($config_data->{selection}) && !exists($config_data->{filters})) { @@ -339,7 +337,7 @@ Specify the full path to a json config file =item B<--filter-counters> Filter some counter (can be 'unique' or 'global') -Useless, if you use selection/filter but not +Useless, if you use selection/filter but not global/virtual curves =item B<--warning-*> @@ -354,4 +352,4 @@ BE CAREFUL, will override config file definition =back -=cut +=cut \ No newline at end of file From 4d356553df2f5dfe5ecdbfb95d660477b0ff5347 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Wed, 1 Mar 2017 20:11:44 +0100 Subject: [PATCH 07/12] Set default value when no formatting defined --- apps/centreon/sql/mode/virtualservice.pm | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index f448bfaf0..8c46bf1c8 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -47,12 +47,14 @@ sub custom_metric_output { local $SIG{__DIE__} = sub { $message = $_[0]; }; $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); }; - } else { + } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); }; + } else { + $msg = sprintf($config_data->{formatting}->{printf_msg}, $self->{result_values}->{instance}, $self->{result_values}->{value}); } if (defined($message)) { @@ -172,8 +174,11 @@ sub check_options { } if (!exists($config_data->{formatting})) { - $self->{output}->add_option_msg(short_msg => "Config_error: formatting section is mandatory in yout JSON condiguration !"); - $self->{output}->option_exit(); + $config_data->{formatting}->{printf_msg} = "Metric '%s' value is %d"; + $config_data->{formatting}->{printf_metric_value} = "%d"; + $config_data->{formatting}->{custom_message_global} = "Global metrics are OK"; + $config_data->{formatting}->{custom_message_metric} = "All metrics are OK"; + $config_data->{formatting}->{cannonical_separator} = "#"; } } @@ -352,4 +357,4 @@ BE CAREFUL, will override config file definition =back -=cut \ No newline at end of file +=cut From 061a101b5b85b8a77bb28bb436ba253ba1ccfd7d Mon Sep 17 00:00:00 2001 From: Sims24 Date: Mon, 6 Mar 2017 11:09:37 +0100 Subject: [PATCH 08/12] Fix bugs in sprintf when overriding default' --- apps/centreon/sql/mode/virtualservice.pm | 85 ++++++++++++------------ 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 8c46bf1c8..b549a46e7 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -26,7 +26,6 @@ use strict; use warnings; use List::Util qw (min max sum); use JSON; - my $instance_mode; my $config_data; @@ -35,28 +34,41 @@ sub custom_metric_output { my $msg; my $message; - if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); - }; - } elsif (defined($config_data->{filters}) && defined($config_data->{filters}->{formatting})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); - }; - } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); - }; - } else { - $msg = sprintf($config_data->{formatting}->{printf_msg}, $self->{result_values}->{instance}, $self->{result_values}->{value}); + if ($self->{result_values}->{type} eq 'unique') { + if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); + }; + } elsif (exists($config_data->{filters}->{formatting}->{printf_var}) && exists($config_data->{filters}->{formatting}->{printf_msg})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); + }; + } else { + $msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); + } } + if ($self->{result_values}->{type} eq 'global') { + if (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); + }; + } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); + }; + } else { + $msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); + } + } if (defined($message)) { $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); $self->{output}->option_exit(); @@ -100,9 +112,7 @@ sub custom_metric_threshold { my ($self, %options) = @_; my ($exit, $threshold_value); $threshold_value = $self->{result_values}->{value}; - $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label} , exit_litteral => 'critical' }, { label => 'warning-'.$self->{label}, exit_litteral => 'warning' } ]); - return $exit; } @@ -173,13 +183,11 @@ sub check_options { $self->{output}->option_exit(); } - if (!exists($config_data->{formatting})) { - $config_data->{formatting}->{printf_msg} = "Metric '%s' value is %d"; - $config_data->{formatting}->{printf_metric_value} = "%d"; - $config_data->{formatting}->{custom_message_global} = "Global metrics are OK"; - $config_data->{formatting}->{custom_message_metric} = "All metrics are OK"; - $config_data->{formatting}->{cannonical_separator} = "#"; - } + $config_data->{formatting}->{printf_msg} = "Metric '%s' value is %d" if (!exists($config_data->{formatting}->{printf_msg})); + $config_data->{formatting}->{printf_metric_value} = "%d" if (!exists($config_data->{formatting}->{printf_metric_value})); + $config_data->{formatting}->{custom_message_global} = "Global metrics are OK" if (!exists($config_data->{formatting}->{custom_message_global})); + $config_data->{formatting}->{custom_message_metric} = "All metrics are OK" if (!exists($config_data->{formatting}->{custom_message_metric})); + $config_data->{formatting}->{cannonical_separator} = "#" if (!exists($config_data->{formatting}->{cannonical_separator})); } @@ -291,24 +299,20 @@ sub manage_selection { $self->{vmetrics}->{$vcurve}{min} = (defined($config_data->{virtualcurve}{$vcurve}->{min})) ? $config_data->{virtualcurve}{$vcurve}->{min} : ''; $self->{vmetrics}->{$vcurve}{max} = (defined($config_data->{virtualcurve}{$vcurve}->{max})) ? $config_data->{virtualcurve}{$vcurve}->{max} : ''; - $self->{option_results}->{'warning-global'} = (defined($self->{option_results}->{'warning-global'})) ? $self->{option_results}->{'warning-global'} : $config_data->{virtualcurve}{$vcurve}->{warning}; - $self->{option_results}->{'critical-global'} = (defined($self->{option_results}->{'critical-global'})) ? $self->{option_results}->{'critical-global'} : $config_data->{virtualcurve}{$vcurve}->{critical}; - $self->{global}->{$vcurve} = {display => $vcurve, type => 'global', unit => $self->{vmetrics}->{$vcurve}->{unit}, - value => $self->{vmetrics}->{$vcurve}{aggregated_value}, - min => $self->{vmetrics}->{$vcurve}{min}, - max => $self->{vmetrics}->{$vcurve}{max} }; - + value => $self->{vmetrics}->{$vcurve}->{aggregated_value}, + min => $self->{vmetrics}->{$vcurve}->{min}, + max => $self->{vmetrics}->{$vcurve}->{max} }; } $self->{metric}->{$metric} = {display => $self->{metrics}->{$metric}->{display_name}, type => 'unique', unit => $self->{metrics}->{$metric}->{unit}, value => $self->{metrics}->{$metric}->{current}, - min => $self->{metrics}->{$metric}{min}, - max => $self->{metrics}->{$metric}{max} } if ($self->{metrics}->{$metric}->{display} == 1); + min => $self->{metrics}->{$metric}->{min}, + max => $self->{metrics}->{$metric}->{max} } if ($self->{metrics}->{$metric}->{display} == 1); } if (scalar(keys %{$self->{metric}}) <= 0 && scalar(keys %{$self->{vmetrics}}) <= 0) { @@ -316,10 +320,9 @@ sub manage_selection { $self->{output}->option_exit(); } - } -1; +1 __END__ From 638fc679d33a947e92c32f399504840f673f39a7 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Mon, 6 Mar 2017 13:30:18 +0100 Subject: [PATCH 09/12] clean instance_mode and allow getting threshold from config --- apps/centreon/sql/mode/virtualservice.pm | 52 +++++++++++++++--------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index b549a46e7..7c0b9c7ba 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -26,7 +26,6 @@ use strict; use warnings; use List::Util qw (min max sum); use JSON; -my $instance_mode; my $config_data; sub custom_metric_output { @@ -69,6 +68,7 @@ sub custom_metric_output { $msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); } } + if (defined($message)) { $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); $self->{output}->option_exit(); @@ -111,8 +111,15 @@ sub custom_metric_perfdata { sub custom_metric_threshold { my ($self, %options) = @_; my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{value}; - $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label} , exit_litteral => 'critical' }, { label => 'warning-'.$self->{label}, exit_litteral => 'warning' } ]); + my $exit_conf = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label}.'-'.$self->{result_values}->{instance} , exit_litteral => 'critical' }, + { label => 'warning-'.$self->{label}.'-'.$self->{result_values}->{instance}, exit_litteral => 'warning' } ]); + + my $exit_opts = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label} , exit_litteral => 'critical' }, + { label => 'warning-'.$self->{label}, exit_litteral => 'warning' } ]); + + $exit = $self->{output}->get_most_critical(status => [ $exit_conf, $exit_opts ]); return $exit; } @@ -163,8 +170,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - $instance_mode = $self; - if (!defined($self->{option_results}->{config_file}) && !defined($self->{option_results}->{json_data})) { $self->{output}->add_option_msg(short_msg => "Please define --config-file or --json-data option"); $self->{output}->option_exit(); @@ -244,13 +249,13 @@ sub manage_selection { $self->{sql}->query(query => $query); while ((my $row = $self->{sql}->fetchrow_hashref())) { my $metric_key = $id; - $self->{metrics}{$metric_key}{name} = $row->{metric_name}; - $self->{metrics}{$metric_key}{display_name} = $id; - $self->{metrics}{$metric_key}{current} = $row->{current_value}; - $self->{metrics}{$metric_key}{unit} = $row->{unit_name}; - $self->{metrics}{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; - $self->{metrics}{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; - $self->{metrics}{$metric_key}{display} = (defined($config_data->{selection}{$id}->{display}) && $config_data->{selection}{$id}->{display} eq 'true') ? 1 : 0; + $self->{metrics}->{$metric_key}{name} = $row->{metric_name}; + $self->{metrics}->{$metric_key}{display_name} = $id; + $self->{metrics}->{$metric_key}{current} = $row->{current_value}; + $self->{metrics}->{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}->{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; + $self->{metrics}->{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; + $self->{metrics}->{$metric_key}{display} = (defined($config_data->{selection}{$id}->{display}) && $config_data->{selection}{$id}->{display} eq 'true') ? 1 : 0; } } } elsif (exists($config_data->{filters})) { @@ -265,13 +270,13 @@ sub manage_selection { $self->{sql}->query(query => $query); while ((my $row = $self->{sql}->fetchrow_hashref())) { my $metric_key = $row->{host_name}.$config_data->{formatting}->{cannonical_separator}.$row->{service_description}.$config_data->{formatting}->{cannonical_separator}.$row->{metric_name}; - $self->{metrics}{$metric_key}{display_name} = $metric_key; - $self->{metrics}{$metric_key}{name} = $row->{metric_name}; - $self->{metrics}{$metric_key}{current} = $row->{current_value}; - $self->{metrics}{$metric_key}{unit} = $row->{unit_name}; - $self->{metrics}{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; - $self->{metrics}{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; - $self->{metrics}{$metric_key}{display} = (defined($config_data->{filters}->{display})) ? 1 : 0; + $self->{metrics}->{$metric_key}{display_name} = $metric_key; + $self->{metrics}->{$metric_key}{name} = $row->{metric_name}; + $self->{metrics}->{$metric_key}{current} = $row->{current_value}; + $self->{metrics}->{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}->{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; + $self->{metrics}->{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; + $self->{metrics}->{$metric_key}{display} = (defined($config_data->{filters}->{display})) ? 1 : 0; } } @@ -299,6 +304,13 @@ sub manage_selection { $self->{vmetrics}->{$vcurve}{min} = (defined($config_data->{virtualcurve}{$vcurve}->{min})) ? $config_data->{virtualcurve}{$vcurve}->{min} : ''; $self->{vmetrics}->{$vcurve}{max} = (defined($config_data->{virtualcurve}{$vcurve}->{max})) ? $config_data->{virtualcurve}{$vcurve}->{max} : ''; + if (!defined($self->{option_results}->{warning_global}) && (defined($config_data->{virtualcurve}->{$vcurve}->{warning}))) { + $self->{perfdata}->threshold_validate(label => 'warning-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{warning}); + } + if (!defined($self->{option_results}->{critical_global}) && (defined($config_data->{virtualcurve}->{$vcurve}->{critical}))) { + $self->{perfdata}->threshold_validate(label => 'critical-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{critical}); + } + $self->{global}->{$vcurve} = {display => $vcurve, type => 'global', unit => $self->{vmetrics}->{$vcurve}->{unit}, @@ -351,12 +363,12 @@ global/virtual curves =item B<--warning-*> Warning threshold (can be 'unique' or 'global') -BE CAREFUL, will override config file definition +(if also defined in config file, most critical is prefered) =item B<--critical-*> Critical threshold (can be 'unique' or 'global') -BE CAREFUL, will override config file definition +(if also defined in config file, most critical is prefered) =back From 67329202982deb924c1014aaf10f8f7a023cf46a Mon Sep 17 00:00:00 2001 From: Sims24 Date: Mon, 6 Mar 2017 13:33:49 +0100 Subject: [PATCH 10/12] change loop conditions to match new behaviour -most critical first --- apps/centreon/sql/mode/virtualservice.pm | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 7c0b9c7ba..5c5743b3e 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -304,12 +304,8 @@ sub manage_selection { $self->{vmetrics}->{$vcurve}{min} = (defined($config_data->{virtualcurve}{$vcurve}->{min})) ? $config_data->{virtualcurve}{$vcurve}->{min} : ''; $self->{vmetrics}->{$vcurve}{max} = (defined($config_data->{virtualcurve}{$vcurve}->{max})) ? $config_data->{virtualcurve}{$vcurve}->{max} : ''; - if (!defined($self->{option_results}->{warning_global}) && (defined($config_data->{virtualcurve}->{$vcurve}->{warning}))) { - $self->{perfdata}->threshold_validate(label => 'warning-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{warning}); - } - if (!defined($self->{option_results}->{critical_global}) && (defined($config_data->{virtualcurve}->{$vcurve}->{critical}))) { - $self->{perfdata}->threshold_validate(label => 'critical-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{critical}); - } + $self->{perfdata}->threshold_validate(label => 'warning-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{warning}) if (defined($config_data->{virtualcurve}->{$vcurve}->{warning})); + $self->{perfdata}->threshold_validate(label => 'critical-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{critical}) if (defined($config_data->{virtualcurve}->{$vcurve}->{critical})); $self->{global}->{$vcurve} = {display => $vcurve, type => 'global', From 3a43373062371d1e6a1ec496b68c414096f92319 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Tue, 7 Mar 2017 16:53:31 +0100 Subject: [PATCH 11/12] Optimize display, change_bytes, threholds and fix help --- apps/centreon/sql/mode/virtualservice.pm | 63 ++++++++++++++---------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index 5c5743b3e..b7e0f2f09 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -38,7 +38,8 @@ sub custom_metric_output { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); + $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", + eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); }; } elsif (exists($config_data->{filters}->{formatting}->{printf_var}) && exists($config_data->{filters}->{formatting}->{printf_msg})) { eval { @@ -56,7 +57,8 @@ sub custom_metric_output { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); + $msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", + eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); }; } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { eval { @@ -82,14 +84,21 @@ sub custom_metric_calc { my ($self, %options) = @_; $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_value'}; + $self->{result_values}->{unit} = $options{new_datas}->{$self->{instance} . '_unit'}; $self->{result_values}->{instance} = $options{new_datas}->{$self->{instance} . '_display'}; $self->{result_values}->{type} = $options{new_datas}->{$self->{instance} . '_type'}; + $self->{result_values}->{perfdata_value} = $options{new_datas}->{$self->{instance} . '_value'}; $self->{result_values}->{perfdata_unit} = $options{new_datas}->{$self->{instance} . '_unit'}; $self->{result_values}->{min} = $options{new_datas}->{$self->{instance} . '_min'}; $self->{result_values}->{max} = $options{new_datas}->{$self->{instance} . '_max'}; - if (defined($config_data->{formatting}->{change_bytes})) { - ($self->{result_values}->{output_value}, $self->{result_values}->{unit}) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}); + my $change_bytes_global = ($config_data->{formatting}->{change_bytes} eq 'true'); + my $change_bytes_metric_filter = (defined($config_data->{filters}->{formatting}->{change_bytes}) && $config_data->{filters}->{formatting}->{change_bytes} eq 'true'); + my $change_bytes_metric_selection = (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{change_bytes}) + && $config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{change_bytes} eq 'true'); + + if ($change_bytes_global && ($change_bytes_metric_filter || $change_bytes_metric_selection)) { + ($self->{result_values}->{value}, $self->{result_values}->{unit}) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value}); } return 0; } @@ -98,9 +107,9 @@ sub custom_metric_perfdata { my ($self, %options) = @_; $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, - value => $self->{result_values}->{value}, - warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global'), - critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global'), + value => $self->{result_values}->{perfdata_value}, + warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}), + critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}), unit => $self->{result_values}->{perfdata_unit}, min => $self->{result_values}->{min}, max => $self->{result_values}->{max}, @@ -110,16 +119,13 @@ sub custom_metric_perfdata { sub custom_metric_threshold { my ($self, %options) = @_; - my ($exit, $threshold_value); - $threshold_value = $self->{result_values}->{value}; - my $exit_conf = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label}.'-'.$self->{result_values}->{instance} , exit_litteral => 'critical' }, - { label => 'warning-'.$self->{label}.'-'.$self->{result_values}->{instance}, exit_litteral => 'warning' } ]); + my $label_warn = ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}; + my $label_crit = ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}; - my $exit_opts = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-'.$self->{label} , exit_litteral => 'critical' }, - { label => 'warning-'.$self->{label}, exit_litteral => 'warning' } ]); - - $exit = $self->{output}->get_most_critical(status => [ $exit_conf, $exit_opts ]); + my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{perfdata_value}, + threshold => [ { label => $label_crit, exit_litteral => 'critical' }, + { label => $label_warn, exit_litteral => 'warning' } ]); return $exit; } @@ -193,6 +199,7 @@ sub check_options { $config_data->{formatting}->{custom_message_global} = "Global metrics are OK" if (!exists($config_data->{formatting}->{custom_message_global})); $config_data->{formatting}->{custom_message_metric} = "All metrics are OK" if (!exists($config_data->{formatting}->{custom_message_metric})); $config_data->{formatting}->{cannonical_separator} = "#" if (!exists($config_data->{formatting}->{cannonical_separator})); + $config_data->{formatting}->{change_bytes} = 'false' if (!exists($config_data->{formatting}->{change_bytes})); } @@ -252,7 +259,7 @@ sub manage_selection { $self->{metrics}->{$metric_key}{name} = $row->{metric_name}; $self->{metrics}->{$metric_key}{display_name} = $id; $self->{metrics}->{$metric_key}{current} = $row->{current_value}; - $self->{metrics}->{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}->{$metric_key}{unit} = defined($row->{unit_name}) ? $row->{unit_name} : ''; $self->{metrics}->{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; $self->{metrics}->{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; $self->{metrics}->{$metric_key}{display} = (defined($config_data->{selection}{$id}->{display}) && $config_data->{selection}{$id}->{display} eq 'true') ? 1 : 0; @@ -273,7 +280,7 @@ sub manage_selection { $self->{metrics}->{$metric_key}{display_name} = $metric_key; $self->{metrics}->{$metric_key}{name} = $row->{metric_name}; $self->{metrics}->{$metric_key}{current} = $row->{current_value}; - $self->{metrics}->{$metric_key}{unit} = $row->{unit_name}; + $self->{metrics}->{$metric_key}{unit} = defined($row->{unit_name}) ? $row->{unit_name} : ''; $self->{metrics}->{$metric_key}{min} = defined($row->{min}) ? $row->{min} : ''; $self->{metrics}->{$metric_key}{max} = defined($row->{max}) ? $row->{max} : ''; $self->{metrics}->{$metric_key}{display} = (defined($config_data->{filters}->{display})) ? 1 : 0; @@ -304,8 +311,14 @@ sub manage_selection { $self->{vmetrics}->{$vcurve}{min} = (defined($config_data->{virtualcurve}{$vcurve}->{min})) ? $config_data->{virtualcurve}{$vcurve}->{min} : ''; $self->{vmetrics}->{$vcurve}{max} = (defined($config_data->{virtualcurve}{$vcurve}->{max})) ? $config_data->{virtualcurve}{$vcurve}->{max} : ''; - $self->{perfdata}->threshold_validate(label => 'warning-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{warning}) if (defined($config_data->{virtualcurve}->{$vcurve}->{warning})); - $self->{perfdata}->threshold_validate(label => 'critical-global-'.$vcurve, value => $config_data->{virtualcurve}->{$vcurve}->{critical}) if (defined($config_data->{virtualcurve}->{$vcurve}->{critical})); + if (defined($self->{option_results}->{'warning-global'}) || defined($config_data->{virtualcurve}->{$vcurve}->{warning})) { + $self->{perfdata}->threshold_validate(label => 'warning-global-'.$vcurve, + value => (defined($self->{option_results}->{'warning-global'})) ? $self->{option_results}->{'warning-global'} : $config_data->{virtualcurve}->{$vcurve}->{warning}); + } + if (defined($self->{option_results}->{'critical-global'}) || defined($config_data->{virtualcurve}->{$vcurve}->{critical})) { + $self->{perfdata}->threshold_validate(label => 'critical-global-'.$vcurve, + value => (defined($self->{option_results}->{'critical-global'})) ? $self->{option_results}->{'critical-global'} : $config_data->{virtualcurve}->{$vcurve}->{critical}); + } $self->{global}->{$vcurve} = {display => $vcurve, type => 'global', @@ -348,23 +361,23 @@ Specify the full path to a json config file =item B<--json-data> -Specify the full path to a json config file +Get JSON on the fly =item B<--filter-counters> -Filter some counter (can be 'unique' or 'global') +Filter some counter (can be 'metric' or 'global') Useless, if you use selection/filter but not global/virtual curves =item B<--warning-*> -Warning threshold (can be 'unique' or 'global') -(if also defined in config file, most critical is prefered) +Warning threshold (can be 'metric' or 'global') +(Override config_file if set) =item B<--critical-*> -Critical threshold (can be 'unique' or 'global') -(if also defined in config file, most critical is prefered) +Critical threshold (can be 'metric' or 'global') +(Override config_file if set) =back From 4bbed8ceffd0bf1496816462b6078abbe70e1ba1 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Wed, 8 Mar 2017 11:24:32 +0100 Subject: [PATCH 12/12] fix condition regarding to new change_bytes handling --- apps/centreon/sql/mode/virtualservice.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index b7e0f2f09..0e37b40e3 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -34,14 +34,14 @@ sub custom_metric_output { my $message; if ($self->{result_values}->{type} eq 'unique') { - if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting})) { + if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); }; - } elsif (exists($config_data->{filters}->{formatting}->{printf_var}) && exists($config_data->{filters}->{formatting}->{printf_msg})) { + } elsif (defined($config_data->{filters}->{formatting}->{printf_var}) && defined($config_data->{filters}->{formatting}->{printf_msg})) { eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; @@ -361,22 +361,22 @@ Specify the full path to a json config file =item B<--json-data> -Get JSON on the fly +Specify the full path to a json config file =item B<--filter-counters> -Filter some counter (can be 'metric' or 'global') +Filter some counter (can be 'unique' or 'global') Useless, if you use selection/filter but not global/virtual curves =item B<--warning-*> -Warning threshold (can be 'metric' or 'global') +Warning threshold (can be 'unique' or 'global') (Override config_file if set) =item B<--critical-*> -Critical threshold (can be 'metric' or 'global') +Critical threshold (can be 'unique' or 'global') (Override config_file if set) =back