From 7eafc453aeff15ffe11f7de29f485b8bd617b9f2 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 14 Jan 2019 15:11:33 +0100 Subject: [PATCH] WIP catalog functions --- apps/selenium/mode/scenariokatalon.pm | 30 +--------- .../plugins/templates/catalog_functions.pm | 58 +++++++++++++++++++ centreon/plugins/templates/counter.pm | 1 + hardware/ups/apc/snmp/mode/batterystatus.pm | 33 +---------- 4 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 centreon/plugins/templates/catalog_functions.pm diff --git a/apps/selenium/mode/scenariokatalon.pm b/apps/selenium/mode/scenariokatalon.pm index a53d73bde..fbe3f98d6 100644 --- a/apps/selenium/mode/scenariokatalon.pm +++ b/apps/selenium/mode/scenariokatalon.pm @@ -27,8 +27,7 @@ use warnings; use Time::HiRes qw(gettimeofday); use XML::XPath; use WWW::Selenium; - -my $instance_mode; +use centreon::plugins::templates::catalog_functions; my %handlers = (ALRM => {} ); @@ -84,30 +83,6 @@ sub custom_count_calc { return 0; } -sub custom_state_threshold { - my ($self, %options) = @_; - my $status = 'ok'; - my $message; - - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - - if (defined($instance_mode->{option_results}->{critical_state}) && $instance_mode->{option_results}->{critical_state} ne '' && - eval "$instance_mode->{option_results}->{critical_state}") { - $status = 'critical'; - } elsif (defined($instance_mode->{option_results}->{warning_state}) && $instance_mode->{option_results}->{warning_state} ne '' && - eval "$instance_mode->{option_results}->{warning_state}") { - $status = 'warning'; - } - }; - if (defined($message)) { - $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); - } - - return $status; -} - sub custom_state_output { my ($self, %options) = @_; @@ -186,7 +161,7 @@ sub set_counters { closure_custom_calc => $self->can('custom_state_calc'), closure_custom_output => $self->can('custom_state_output'), closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => $self->can('custom_state_threshold'), + closure_custom_threshold_check => ¢reon::plugins::templates::catalog_functions::catalog_status_threshold, } }, { label => 'time-step', set => { @@ -260,7 +235,6 @@ sub check_options { $self->{output}->option_exit(); } - $instance_mode = $self; $self->change_macros(); } diff --git a/centreon/plugins/templates/catalog_functions.pm b/centreon/plugins/templates/catalog_functions.pm new file mode 100644 index 000000000..07d204189 --- /dev/null +++ b/centreon/plugins/templates/catalog_functions.pm @@ -0,0 +1,58 @@ +# +# Copyright 2018 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::templates::catalog_functions; + +use strict; +use warnings; + +sub catalog_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + my $label = $self->{label}; + $label =~ s/-/_/g; + if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' && + eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") { + $status = 'critical'; + } elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && + eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") { + $status = 'warning'; + } elsif (defined($self->{instance_mode}->{option_results}->{'unknown_' . $label}) && $self->{instance_mode}->{option_results}->{'unknown_' . $label} ne '' && + eval "$self->{instance_mode}->{option_results}->{'unknown_' . $label}") { + $status = 'unknown'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +1; + +__END__ + diff --git a/centreon/plugins/templates/counter.pm b/centreon/plugins/templates/counter.pm index ab4facd98..32a7006b8 100644 --- a/centreon/plugins/templates/counter.pm +++ b/centreon/plugins/templates/counter.pm @@ -106,6 +106,7 @@ sub new { $_->{obj} = centreon::plugins::values->new(statefile => $self->{statefile_value}, output => $self->{output}, perfdata => $self->{perfdata}, label => $_->{label}); + $_->{obj}->{instance_mode} = $self; $_->{obj}->set(%{$_->{set}}); } } diff --git a/hardware/ups/apc/snmp/mode/batterystatus.pm b/hardware/ups/apc/snmp/mode/batterystatus.pm index daa246617..9a9abba3b 100644 --- a/hardware/ups/apc/snmp/mode/batterystatus.pm +++ b/hardware/ups/apc/snmp/mode/batterystatus.pm @@ -24,35 +24,7 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; - -my $instance_mode; - -sub custom_threshold_output { - my ($self, %options) = @_; - my $status = 'ok'; - my $message; - - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - - if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && - eval "$instance_mode->{option_results}->{critical_status}") { - $status = 'critical'; - } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && - eval "$instance_mode->{option_results}->{warning_status}") { - $status = 'warning'; - } elsif (defined($instance_mode->{option_results}->{unknown_status}) && $instance_mode->{option_results}->{unknown_status} ne '' && - eval "$instance_mode->{option_results}->{unknown_status}") { - $status = 'unknown'; - } - }; - if (defined($message)) { - $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); - } - - return $status; -} +use centreon::plugins::templates::catalog_functions; sub custom_status_output { my ($self, %options) = @_; @@ -82,7 +54,7 @@ sub set_counters { closure_custom_calc => $self->can('custom_status_calc'), closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => $self->can('custom_threshold_output'), + closure_custom_threshold_check => \¢reon::plugins::templates::catalog_functions::catalog_status_threshold, } }, { label => 'load', set => { @@ -153,7 +125,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - $instance_mode = $self; $self->change_macros(); }