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