From cc073fdfc0c7aceb9345c548505c29508b7ecdf1 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 15 Apr 2019 12:06:25 +0200 Subject: [PATCH] refactoring some ups modes --- hardware/ups/mge/snmp/mode/inputlines.pm | 319 +++++++---------- hardware/ups/mge/snmp/mode/outputlines.pm | 338 +++++------------- .../standard/rfc1628/snmp/mode/outputlines.pm | 253 +++++-------- 3 files changed, 320 insertions(+), 590 deletions(-) diff --git a/hardware/ups/mge/snmp/mode/inputlines.pm b/hardware/ups/mge/snmp/mode/inputlines.pm index 700df1997..b94c40118 100644 --- a/hardware/ups/mge/snmp/mode/inputlines.pm +++ b/hardware/ups/mge/snmp/mode/inputlines.pm @@ -20,64 +20,77 @@ package hardware::ups::mge::snmp::mode::inputlines; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; -my %map_input_status = ( - 1 => 'no', - 2 => 'outoftolvolt', - 3 => 'outoftolfreq', - 4 => 'utilityoff', -); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); -my $maps_counters = { - voltage => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'voltage', no_value => 0, }, - ], - output_template => 'Voltage: %.2f V', output_error_template => 'Voltage: %s', - perfdatas => [ - { value => 'voltage_absolute', label => 'voltage', template => '%.2f', - unit => 'V', min => 0, label_extra_instance => 1 }, - ], - } - }, - current => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'current', no_value => 0 }, - ], - output_template => 'Current: %.2f A', output_error_template => 'Current: %s', - perfdatas => [ - { value => 'current_absolute', label => 'current', template => '%.2f', - unit => 'A', min => 0, label_extra_instance => 1 }, - ], - } - }, - frequence => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'frequence', no_value => 0, }, - ], - output_template => 'Frequence: %.2f Hz', output_error_template => 'Frequence: %s', - perfdatas => [ - { value => 'frequence_absolute', label => 'frequence', template => '%.2f', - unit => 'Hz', min => 0 }, - ], - } - }, -}; +sub custom_status_output { + my ($self, %options) = @_; -my $oid_upsmgInputPhaseNumEntry = '.1.3.6.1.4.1.705.1.6.1'; -my $oid_mginputVoltageEntry = '.1.3.6.1.4.1.705.1.6.2.1.2'; # in dV -my $oid_mginputFrequencyEntry = '.1.3.6.1.4.1.705.1.6.2.1.3'; # in dHz -my $oid_mginputCurrentEntry = '.1.3.6.1.4.1.705.1.6.2.1.6'; # in dA -my $oid_upsmgInputBadStatusEntry = '.1.3.6.1.4.1.705.1.6.3'; -my $oid_upsmgInputLineFailCauseEntry = '.1.3.6.1.4.1.705.1.6.4'; + my $msg = "Input Line(s) bad status is '" . $self->{result_values}->{badstatus} . "' [failcause = " . $self->{result_values}->{failcause} . "]"; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{badstatus} = $options{new_datas}->{$self->{instance} . '_badstatus'}; + $self->{result_values}->{failcause} = $options{new_datas}->{$self->{instance} . '_failcause'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'iline', type => 1, cb_prefix_output => 'prefix_iline_output', message_multiple => 'All input lines are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'status', set => { + key_values => [ { name => 'badstatus' }, { name => 'failcause' } ], + 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 => \&catalog_status_threshold, + } + }, + ]; + + $self->{maps_counters}->{iline} = [ + { label => 'current', set => { + key_values => [ { name => 'mginputCurrent', no_value => 0 } ], + output_template => 'Current : %.2f A', + perfdatas => [ + { label => 'current', value => 'mginputCurrent_absolute', template => '%.2f', + min => 0, unit => 'A', label_extra_instance => 1 }, + ], + } + }, + { label => 'voltage', set => { + key_values => [ { name => 'mginputVoltage', no_value => 0 } ], + output_template => 'Voltage : %.2f V', + perfdatas => [ + { label => 'voltage', value => 'mginputVoltage_absolute', template => '%.2f', + unit => 'V', label_extra_instance => 1 }, + ], + } + }, + { label => 'frequence', set => { + key_values => [ { name => 'mginputFrequency', no_value => 0 } ], + output_template => 'Frequence : %.2f Hz', + perfdatas => [ + { label => 'frequence', value => 'mginputFrequency_absolute', template => '%.2f', + unit => 'Hz', label_extra_instance => 1 }, + ], + } + }, + ]; +} sub new { my ($class, %options) = @_; @@ -85,169 +98,83 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - }); + $options{options}->add_options(arguments => { + "warning-status:s" => { name => 'warning_status' }, + "critical-status:s" => { name => 'critical_status', default => '%{badstatus} =~ /yes/' }, + }); - $self->{instance_selected} = {}; - - foreach (keys %{$maps_counters}) { - $options{options}->add_options(arguments => { - 'warning-' . $_ . ':s' => { name => 'warning-' . $_ }, - 'critical-' . $_ . ':s' => { name => 'critical-' . $_ }, - }); - my $class = $maps_counters->{$_}->{class}; - $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $_); - $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); - } - return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); - } + $self->change_macros(macros => ['warning_status', 'critical_status']); } -sub manage_counters { +sub prefix_iline_output { my ($self, %options) = @_; - - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$options{maps_counters}}) { - $options{maps_counters}->{$_}->{obj}->set(instance => $options{instance}); - - my ($value_check) = $options{maps_counters}->{$_}->{obj}->execute(values => $self->{instance_selected}->{$options{instance}}); - # We don't want to display no value - next if ($value_check == -10); - if ($value_check != 0) { - $long_msg .= $long_msg_append . $options{maps_counters}->{$_}->{obj}->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $options{maps_counters}->{$_}->{obj}->threshold_check(); - push @exits, $exit2; - - my $output = $options{maps_counters}->{$_}->{obj}->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $options{maps_counters}->{$_}->{obj}->perfdata(level => 1, extra_instance => $self->{multiple}); - } - - $self->{output}->output_add(long_msg => $options{label} . " " . $long_msg); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => $options{label} . " " . $short_msg - ); - } - - if ($self->{multiple} == 0) { - $self->{output}->output_add(short_msg => $options{label} . " " . $long_msg); - } + return "Input Line '" . $options{instance_value}->{display} . "' "; } -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; +my %map_input_failcause = ( + 1 => 'no', + 2 => 'outoftolvolt', + 3 => 'outoftolfreq', + 4 => 'utilityoff', +); +my %map_bad_status = ( + 1 => 'yes', + 2 => 'no', +); - $self->manage_selection(); - - $self->{output}->output_add(severity => 'OK', - short_msg => 'Input Line(s) status is ok'); - if (defined($self->{results}->{$oid_upsmgInputBadStatusEntry}->{$oid_upsmgInputBadStatusEntry . '.0'}) && - $self->{results}->{$oid_upsmgInputBadStatusEntry}->{$oid_upsmgInputBadStatusEntry . '.0'} == 1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => sprintf("Input Line(s) status is '%s'", - $map_input_status{$self->{results}->{$oid_upsmgInputLineFailCauseEntry}->{$oid_upsmgInputLineFailCauseEntry . '.0'}})); - } - - $self->{multiple} = 1; - if (scalar(keys %{$self->{instance_selected}}) == 1) { - $self->{multiple} = 0; - } - - if ($self->{multiple} == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'Input Lines are ok'); - } - - foreach my $id (sort keys %{$self->{instance_selected}}) { - $self->manage_counters(instance => $id, maps_counters => $maps_counters, label => "Input Line '" . $id . "'"); - } - - $self->{output}->display(); - $self->{output}->exit(); -} - -sub add_result { - my ($self, %options) = @_; - - $self->{instance_selected}->{$options{instance}} = {} if (!defined($self->{instance_selected}->{$options{instance}})); - $self->{instance_selected}->{$options{instance}}->{$options{name}} = $self->{results}->{$options{oid}}->{$options{oid} . '.' . $options{instance2}} * 0.1; -} +my $mapping = { + mginputVoltage => { oid => '.1.3.6.1.4.1.705.1.6.2.1.2' }, # in dV + mginputFrequency => { oid => '.1.3.6.1.4.1.705.1.6.2.1.3' }, # in dHz + mginputCurrent => { oid => '.1.3.6.1.4.1.705.1.6.2.1.6' }, # in dA +}; +my $oid_upsmgInput = '.1.3.6.1.4.1.705.1.6'; +my $oid_upsmgInputPhaseEntry = '.1.3.6.1.4.1.705.1.6.2.1'; +my $oid_upsmgInputPhaseNum = '.1.3.6.1.4.1.705.1.6.1.0'; +my $oid_upsmgInputBadStatus = '.1.3.6.1.4.1.705.1.6.3.0'; +my $oid_upsmgInputLineFailCause = '.1.3.6.1.4.1.705.1.6.4.0'; sub manage_selection { my ($self, %options) = @_; - - $self->{results} = $self->{snmp}->get_multiple_table(oids => [ - { oid => $oid_upsmgInputPhaseNumEntry }, - { oid => $oid_mginputVoltageEntry }, - { oid => $oid_mginputFrequencyEntry }, - { oid => $oid_mginputCurrentEntry }, - { oid => $oid_upsmgInputBadStatusEntry }, - { oid => $oid_upsmgInputLineFailCauseEntry }, - ], - , nothing_quit => 1); - if (!defined($self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) || - $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'} == 0) { + $self->{iline} = {}; + my $snmp_result = $options{snmp}->get_table( + oid => $oid_upsmgInput, + nothing_quit => 1 + ); + + if (!defined($snmp_result->{$oid_upsmgInputPhaseNum}) || + $snmp_result->{$oid_upsmgInputPhaseNum} == 0) { $self->{output}->add_option_msg(short_msg => "No input lines found."); $self->{output}->option_exit(); } - my %instances = (); - # can be 'xxx.1' or 'xxx.1.0' (cannot respect MIB :) - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mginputVoltageEntry}})) { - $oid =~ /^$oid_mginputVoltageEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'voltage', oid => $oid_mginputVoltageEntry); - } - } - %instances = (); - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mginputCurrentEntry}})) { - $oid =~ /^$oid_mginputCurrentEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'current', oid => $oid_mginputCurrentEntry); - } - } - %instances = (); - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mginputFrequencyEntry}})) { - $oid =~ /^$oid_mginputFrequencyEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'frequence', oid => $oid_mginputFrequencyEntry); - } + foreach my $oid (keys %{$snmp_result}) { + next if ($oid !~ /^$oid_upsmgInputPhaseEntry\.\d+\.(.*)$/); + my $instance = $1; + next if (defined($self->{iline}->{$instance})); + + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); + $result->{mginputVoltage} *= 0.1 if (defined($result->{mginputVoltage})); + $result->{mginputFrequency} *= 0.1 if (defined($result->{mginputFrequency})); + $result->{mginputCurrent} *= 0.1 if (defined($result->{mginputCurrent})); + next if ((!defined($result->{mginputVoltage}) || $result->{mginputVoltage} == 0) && + (!defined($result->{mginputFrequency}) || $result->{mginputFrequency} == 0) && + (!defined($result->{mginputCurrent}) || $result->{mginputCurrent} == 0)); + $self->{iline}->{$instance} = { display => $instance, %$result }; } - if (scalar(keys %{$self->{instance_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No input lines found."); - $self->{output}->option_exit(); - } + $self->{global} = { + badstatus => $map_bad_status{$snmp_result->{$oid_upsmgInputBadStatus}}, + failcause => $map_input_failcause{$snmp_result->{$oid_upsmgInputLineFailCause}}, + }; } 1; @@ -270,6 +197,16 @@ Can be: 'frequence', 'voltage', 'current'. Threshold critical. Can be: 'frequence', 'voltage', 'current'. +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{badstatus}, %{failcause} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{badstatus} =~ /yes/'). +Can used special variables like: %{badstatus}, %{failcause} + =back =cut diff --git a/hardware/ups/mge/snmp/mode/outputlines.pm b/hardware/ups/mge/snmp/mode/outputlines.pm index 6d154499b..e888621d2 100644 --- a/hardware/ups/mge/snmp/mode/outputlines.pm +++ b/hardware/ups/mge/snmp/mode/outputlines.pm @@ -20,83 +20,69 @@ package hardware::ups::mge::snmp::mode::outputlines; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; -my $maps_counters = { - voltage => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'voltage', no_value => 0 }, - ], - output_template => 'Voltage: %.2f V', output_error_template => 'Voltage: %s', - perfdatas => [ - { value => 'voltage_absolute', label => 'voltage', template => '%.2f', - unit => 'V', min => 0, label_extra_instance => 1 }, - ], - } - }, - current => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'current', no_value => 0 }, - ], - output_template => 'Current: %.2f A', output_error_template => 'Current: %s', - perfdatas => [ - { value => 'current_absolute', label => 'current', template => '%.2f', - unit => 'A', min => 0, label_extra_instance => 1 }, - ], - } - }, - frequence => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'frequence', no_value => 0 }, - ], - output_template => 'Frequence: %.2f Hz', output_error_template => 'Frequence: %s', - perfdatas => [ - { value => 'frequence_absolute', label => 'frequence', template => '%.2f', - unit => 'Hz', min => 0 }, - ], - } - }, - load => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'load', no_value => -1 }, - ], - output_template => 'Load: %.2f %%', output_error_template => 'Load: %s', - perfdatas => [ - { value => 'load_absolute', label => 'load', template => '%.2f', - unit => '%', min => 0 }, - ], - } - }, -}; +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'oline', type => 1, cb_prefix_output => 'prefix_oline_output', message_multiple => 'All output lines are ok', skipped_code => { -10 => 1 } }, + ]; -my $maps_counters2 = { - 'stdev-3phases' => { class => 'centreon::plugins::values', obj => undef, - set => { - key_values => [ - { name => 'stdev' }, - ], - output_template => 'Load Standard Deviation : %.2f', output_error_template => 'Load Standard Deviation : %s', - perfdatas => [ - { value => 'stdev_absolute', label => 'stdev', template => '%.2f', - min => 0 }, - ], - } - }, -}; + $self->{maps_counters}->{global} = [ + { label => 'stdev-3phases', set => { + key_values => [ { name => 'stdev' } ], + output_template => 'Load Standard Deviation : %.2f', + perfdatas => [ + { label => 'stdev', value => 'stdev_absolute', template => '%.2f' }, + ], + } + }, + ]; -my $oid_upsmgOutputPhaseNumEntry = '.1.3.6.1.4.1.705.1.7.1'; -my $oid_mgoutputVoltageEntry = '.1.3.6.1.4.1.705.1.7.2.1.2'; # in dV -my $oid_mgoutputFrequencyEntry = '.1.3.6.1.4.1.705.1.7.2.1.3'; # in dHz -my $oid_mgoutputCurrentEntry = '.1.3.6.1.4.1.705.1.7.2.1.5'; # in dA -my $oid_mgoutputLoadPerPhaseEntry = '.1.3.6.1.4.1.705.1.7.2.1.4'; # in % + $self->{maps_counters}->{oline} = [ + { label => 'load', set => { + key_values => [ { name => 'mgoutputLoadPerPhase', no_value => 0 } ], + output_template => 'Load : %.2f %%', + perfdatas => [ + { label => 'load', value => 'mgoutputLoadPerPhase_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1 }, + ], + } + }, + { label => 'current', set => { + key_values => [ { name => 'mgoutputCurrent', no_value => 0 } ], + output_template => 'Current : %.2f A', + perfdatas => [ + { label => 'current', value => 'mgoutputCurrent_absolute', template => '%.2f', + min => 0, unit => 'A', label_extra_instance => 1 }, + ], + } + }, + { label => 'voltage', set => { + key_values => [ { name => 'mgoutputVoltage', no_value => 0 } ], + output_template => 'Voltage : %.2f V', + perfdatas => [ + { label => 'voltage', value => 'mgoutputVoltage_absolute', template => '%.2f', + unit => 'V', label_extra_instance => 1 }, + ], + } + }, + { label => 'frequence', set => { + key_values => [ { name => 'mgoutputFrequency', no_value => -1 } ], + output_template => 'Frequence : %.2f Hz', + perfdatas => [ + { label => 'frequence', value => 'mgoutputFrequency_absolute', template => '%.2f', + unit => 'Hz', label_extra_instance => 1 }, + ], + } + }, + ]; +} sub new { my ($class, %options) = @_; @@ -104,195 +90,69 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - }); + $options{options}->add_options(arguments => { + }); - $self->{instance_selected} = {}; - - foreach (keys %{$maps_counters}) { - $options{options}->add_options(arguments => { - 'warning-' . $_ . ':s' => { name => 'warning-' . $_ }, - 'critical-' . $_ . ':s' => { name => 'critical-' . $_ }, - }); - my $class = $maps_counters->{$_}->{class}; - $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $_); - $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); - } - foreach (keys %{$maps_counters2}) { - $options{options}->add_options(arguments => { - 'warning-' . $_ . ':s' => { name => 'warning-' . $_ }, - 'critical-' . $_ . ':s' => { name => 'critical-' . $_ }, - }); - my $class = $maps_counters2->{$_}->{class}; - $maps_counters2->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $_); - $maps_counters2->{$_}->{obj}->set(%{$maps_counters2->{$_}->{set}}); - } - return $self; } -sub check_options { +sub prefix_oline_output { my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); - } - foreach (keys %{$maps_counters2}) { - $maps_counters2->{$_}->{obj}->init(option_results => $self->{option_results}); - } + + return "Output Line '" . $options{instance_value}->{display} . "' "; } -sub manage_counters { +sub stdev { my ($self, %options) = @_; - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$options{maps_counters}}) { - $options{maps_counters}->{$_}->{obj}->set(instance => $options{instance}); - - my ($value_check) = $options{maps_counters}->{$_}->{obj}->execute(values => $self->{instance_selected}->{$options{instance}}); - - # We don't want to display no value - next if ($value_check == -10); - if ($value_check != 0) { - $long_msg .= $long_msg_append . $options{maps_counters}->{$_}->{obj}->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $options{maps_counters}->{$_}->{obj}->threshold_check(); - push @exits, $exit2; - - my $output = $options{maps_counters}->{$_}->{obj}->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $options{maps_counters}->{$_}->{obj}->perfdata(level => 1, extra_instance => $self->{multiple}); - } - - $self->{output}->output_add(long_msg => $options{label} . " " . $long_msg); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => $options{label} . " " . $short_msg - ); + # Calculate stdev + my $total = 0; + my $num_present = scalar(keys %{$self->{oline}}); + foreach my $instance (keys %{$self->{oline}}) { + next if (!defined($self->{oline}->{$instance}->{mgoutputLoadPerPhase})); + $total += $self->{oline}->{$instance}->{mgoutputLoadPerPhase}; } - if ($self->{multiple} == 0) { - $self->{output}->output_add(short_msg => $options{label} . " " . $long_msg); + my $mean = $total / $num_present; + $total = 0; + foreach my $instance (keys %{$self->{oline}}) { + next if (!defined($self->{oline}->{$instance}->{mgoutputLoadPerPhase})); + $total += ($mean - $self->{oline}->{$instance}->{mgoutputLoadPerPhase}) ** 2; } + my $stdev = sqrt($total / $num_present); + $self->{global} = { stdev => $stdev }; } -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - $self->{multiple} = 1; - if (scalar(keys %{$self->{instance_selected}}) == 1) { - $self->{multiple} = 0; - } - - if ($self->{multiple} == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'Output Lines are ok.'); - } - - foreach my $id (sort keys %{$self->{instance_selected}}) { - $self->manage_counters(instance => $id, maps_counters => $maps_counters, label => "Output Line '" . $id . "'"); - } - - if ($self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'} > 1) { - $self->{instance_selected}->{lines} = { stdev => $self->{stdev} }; - $self->manage_counters(instance => 'lines', maps_counters => $maps_counters2, label => "Output Lines"); - } - - $self->{output}->display(); - $self->{output}->exit(); -} - -sub add_result { - my ($self, %options) = @_; - - $self->{instance_selected}->{$options{instance}} = {} if (!defined($self->{instance_selected}->{$options{instance}})); - $self->{instance_selected}->{$options{instance}}->{$options{name}} = $self->{results}->{$options{oid}}->{$options{oid} . '.' . $options{instance2}} * $options{multiple}; -} +my $mapping = { + mgoutputVoltage => { oid => '.1.3.6.1.4.1.705.1.7.2.1.2' }, # in dV + mgoutputFrequency => { oid => '.1.3.6.1.4.1.705.1.7.2.1.3' }, # in dHz + mgoutputLoadPerPhase => { oid => '.1.3.6.1.4.1.705.1.7.2.1.4' }, # in % + mgoutputCurrent => { oid => '.1.3.6.1.4.1.705.1.7.2.1.5' }, # in dA +}; +my $oid_upsmgOutputPhaseEntry = '.1.3.6.1.4.1.705.1.7.2.1'; sub manage_selection { my ($self, %options) = @_; - - $self->{results} = $self->{snmp}->get_multiple_table(oids => [ - { oid => $oid_upsmgOutputPhaseNumEntry }, - { oid => $oid_mgoutputVoltageEntry }, - { oid => $oid_mgoutputFrequencyEntry }, - { oid => $oid_mgoutputCurrentEntry }, - { oid => $oid_mgoutputLoadPerPhaseEntry }, - ], - , nothing_quit => 1); - if (!defined($self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) || - $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'} == 0) { - $self->{output}->add_option_msg(short_msg => "No output lines found."); - $self->{output}->option_exit(); - } + $self->{oline} = {}; + my $snmp_result = $options{snmp}->get_table( + oid => $oid_upsmgOutputPhaseEntry, + nothing_quit => 1 + ); + foreach my $oid (keys %{$snmp_result}) { + $oid =~ /^$oid_upsmgOutputPhaseEntry\.\d+\.(.*)$/; + my $instance = $1; + next if (defined($self->{oline}->{$instance})); - my %instances = (); - # can be 'xxx.1' or 'xxx.1.0' (cannot respect MIB :) - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mgoutputVoltageEntry}})) { - $oid =~ /^$oid_mgoutputVoltageEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'voltage', oid => $oid_mgoutputVoltageEntry, multiple => 0.1); - } - } - %instances = (); - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mgoutputCurrentEntry}})) { - $oid =~ /^$oid_mgoutputCurrentEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'current', oid => $oid_mgoutputCurrentEntry, multiple => 0.1); - } - } - %instances = (); - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mgoutputFrequencyEntry}})) { - $oid =~ /^$oid_mgoutputFrequencyEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) { - $instances{$2} = 1; - $self->add_result(instance => $2, instance2 => $1, name => 'frequence', oid => $oid_mgoutputFrequencyEntry, multiple => 0.1); - } + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); + $result->{mgoutputVoltage} *= 0.1 if (defined($result->{mgoutputVoltage})); + $result->{mgoutputFrequency} *= 0.1 if (defined($result->{mgoutputFrequency})); + $result->{mgoutputCurrent} *= 0.1 if (defined($result->{mgoutputCurrent})); + $self->{oline}->{$instance} = { display => $instance, %$result }; } - %instances = (); - # Calculate stdev - my $total = 0; - foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mgoutputLoadPerPhaseEntry}})) { - $oid =~ /^$oid_mgoutputLoadPerPhaseEntry\.((\d+).*)/; - if (scalar(keys %instances) < $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) { - $instances{$2} = $self->{results}->{$oid_mgoutputLoadPerPhaseEntry}->{$oid}; - $self->add_result(instance => $2, instance2 => $1, name => 'load', oid => $oid_mgoutputLoadPerPhaseEntry, multiple => 1); - $total += $self->{results}->{$oid_mgoutputLoadPerPhaseEntry}->{$oid}; - } - } - - my $mean = $total / $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}; - $total = 0; - foreach (keys %instances) { - $total += ($mean - $instances{$_}) ** 2; - } - $self->{stdev} = sqrt($total / $self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}); - - if (scalar(keys %{$self->{instance_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No output lines found."); - $self->{output}->option_exit(); + if (scalar(keys %{$self->{oline}}) > 1) { + $self->stdev(); } } diff --git a/hardware/ups/standard/rfc1628/snmp/mode/outputlines.pm b/hardware/ups/standard/rfc1628/snmp/mode/outputlines.pm index ef7c0bd2e..1566a7093 100644 --- a/hardware/ups/standard/rfc1628/snmp/mode/outputlines.pm +++ b/hardware/ups/standard/rfc1628/snmp/mode/outputlines.pm @@ -20,48 +20,69 @@ package hardware::ups::standard::rfc1628::snmp::mode::outputlines; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -my %oids = ( - '.1.3.6.1.2.1.33.1.4.4.1.5' => { counter => 'load', no_present => -1 }, # upsOutputPercentLoad - '.1.3.6.1.2.1.33.1.4.4.1.2' => { counter => 'voltage', no_present => 0 }, # in Volt upsOutputVoltage - '.1.3.6.1.2.1.33.1.4.4.1.3' => { counter => 'current', no_present => 0 }, # in dA upsOutputCurrent - '.1.3.6.1.2.1.33.1.4.4.1.4' => { counter => 'power', no_present => 0 }, # in Watt upsOutputPower -); +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'oline', type => 1, cb_prefix_output => 'prefix_oline_output', message_multiple => 'All output lines are ok', skipped_code => { -10 => 1 } }, + ]; -my $maps_counters = { - load => { thresholds => { - warning_frequence => { label => 'warning-load', exit_value => 'warning' }, - critical_frequence => { label => 'critical-load', exit_value => 'critical' }, - }, - output_msg => 'Load : %.2f %%', no_present => -1, - factor => 1, unit => '%', - }, - voltage => { thresholds => { - warning_voltage => { label => 'warning-voltage', exit_value => 'warning' }, - critical_voltage => { label => 'critical-voltage', exit_value => 'critical' }, - }, - output_msg => 'Voltage : %.2f V', no_present => 0, - factor => 1, unit => 'V', - }, - current => { thresholds => { - warning_current => { label => 'warning-current', exit_value => 'warning' }, - critical_current => { label => 'critical-current', exit_value => 'critical' }, - }, - output_msg => 'Current : %.2f A', no_present => 0, - factor => 0.1, unit => 'A', - }, - power => { thresholds => { - warning_power => { label => 'warning-power', exit_value => 'warning' }, - critical_power => { label => 'critical-power', exit_value => 'critical' }, - }, - output_msg => 'Power : %.2f W', no_present => 0, - factor => 1, unit => 'W', - }, -}; + $self->{maps_counters}->{global} = [ + { label => 'stdev-3phases', set => { + key_values => [ { name => 'stdev' } ], + output_template => 'Load Standard Deviation : %.2f', + perfdatas => [ + { label => 'stdev', value => 'stdev_absolute', template => '%.2f' }, + ], + } + }, + ]; + + $self->{maps_counters}->{oline} = [ + { label => 'load', set => { + key_values => [ { name => 'upsOutputPercentLoad' } ], + output_template => 'Load : %.2f %%', + perfdatas => [ + { label => 'load', value => 'upsOutputPercentLoad_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1 }, + ], + } + }, + { label => 'current', set => { + key_values => [ { name => 'upsOutputCurrent' } ], + output_template => 'Current : %.2f A', + perfdatas => [ + { label => 'current', value => 'upsOutputCurrent_absolute', template => '%.2f', + min => 0, unit => 'A', label_extra_instance => 1 }, + ], + } + }, + { label => 'voltage', set => { + key_values => [ { name => 'upsOutputVoltage' } ], + output_template => 'Voltage : %.2f V', + perfdatas => [ + { label => 'voltage', value => 'upsOutputVoltage_absolute', template => '%.2f', + unit => 'V', label_extra_instance => 1 }, + ], + } + }, + { label => 'power', set => { + key_values => [ { name => 'upsOutputPower' } ], + output_template => 'Power : %.2f W', + perfdatas => [ + { label => 'power', value => 'upsOutputPower_absolute', template => '%.2f', + unit => 'W', label_extra_instance => 1 }, + ], + } + }, + ]; +} sub new { my ($class, %options) = @_; @@ -69,69 +90,16 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning-stdev-3phases:s" => { name => 'warning_stdev' }, - "critical-stdev-3phases:s" => { name => 'critical_stdev' }, - }); - foreach (keys %{$maps_counters}) { - foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) { - $options{options}->add_options(arguments => { - $maps_counters->{$_}->{thresholds}->{$name}->{label} . ':s' => { name => $name }, - }); - } - } + $options{options}->add_options(arguments => { + }); - $self->{counters_value} = {}; - $self->{instances_done} = {}; return $self; } -sub check_options { +sub prefix_oline_output { my ($self, %options) = @_; - $self->SUPER::init(%options); - if (($self->{perfdata}->threshold_validate(label => 'warning-stdev-3phases', value => $self->{option_results}->{warning_stdev})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning-stdev-3phases threshold '" . $self->{option_results}->{warning_stdev} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical-stdev-3phases', value => $self->{option_results}->{critical_stdev})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical-stdev-3phases threshold '" . $self->{option_results}->{critical_stdev} . "'."); - $self->{output}->option_exit(); - } - - foreach (keys %{$maps_counters}) { - foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) { - if (($self->{perfdata}->threshold_validate(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, value => $self->{option_results}->{$name})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong " . $maps_counters->{$_}->{thresholds}->{$name}->{label} . " threshold '" . $self->{option_results}->{$name} . "'."); - $self->{output}->option_exit(); - } - } - } -} - -sub build_values { - my ($self, %options) = @_; - my $counters_value = {}; - my $instance = undef; - - foreach my $oid (keys %oids) { - if ($options{current} =~ /^$oid\.(.*)/) { - $instance = $1; - last; - } - } - - # Skip already done - if (!defined($instance) || defined($self->{instances_done}->{$instance})) { - return 0; - } - - $self->{instances_done}->{$instance} = 1; - $self->{counters_value}->{$instance} = {}; - foreach my $oid (keys %oids) { - $self->{counters_value}->{$instance}->{$oids{$oid}->{counter}} = defined($options{result}->{$oid . '.' . $instance}) ? $options{result}->{$oid . '.' . $instance} : $oids{$oid}->{no_present}; - } + return "Output Line '" . $options{instance_value}->{display} . "' "; } sub stdev { @@ -139,86 +107,51 @@ sub stdev { # Calculate stdev my $total = 0; - my $num_present = 0; - foreach my $instance (keys %{$self->{instances_done}}) { - next if ($self->{counters_value}->{$instance}->{load} == -1); # Not present - $total += $self->{counters_value}->{$instance}->{load}; - $num_present++; + my $num_present = scalar(keys %{$self->{oline}}); + foreach my $instance (keys %{$self->{oline}}) { + next if (!defined($self->{oline}->{$instance}->{upsOutputPercentLoad})); + $total += $self->{oline}->{$instance}->{upsOutputPercentLoad}; } + my $mean = $total / $num_present; $total = 0; - foreach my $instance (keys %{$self->{instances_done}}) { - next if ($self->{counters_value}->{$instance}->{load} == -1); # Not present - $total += ($mean - $self->{counters_value}->{$instance}->{load}) ** 2; + foreach my $instance (keys %{$self->{oline}}) { + next if (!defined($self->{oline}->{$instance}->{upsOutputPercentLoad})); + $total += ($mean - $self->{oline}->{$instance}->{upsOutputPercentLoad}) ** 2; } my $stdev = sqrt($total / $num_present); - - my $exit = $self->{perfdata}->threshold_check(value => $stdev, threshold => [ { label => 'critical-stdev-3phases', 'exit_litteral' => 'critical' }, { label => 'warning-stdev-3phases', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Load Standard Deviation : %.2f", $stdev)); - - $self->{output}->perfdata_add(label => 'stdev', - value => sprintf("%.2f", $stdev), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-stdev-3phases'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-stdev-3phases')); + $self->{global} = { stdev => $stdev }; } -sub run { +my $mapping = { + upsOutputVoltage => { oid => '.1.3.6.1.2.1.33.1.4.4.1.2' }, # in Volt + upsOutputCurrent => { oid => '.1.3.6.1.2.1.33.1.4.4.1.3' }, # in dA + upsOutputPower => { oid => '.1.3.6.1.2.1.33.1.4.4.1.4' }, # in Watt + upsOutputPercentLoad => { oid => '.1.3.6.1.2.1.33.1.4.4.1.5' }, +}; +my $oid_upsOutputEntry = '.1.3.6.1.2.1.33.1.4.4.1'; + +sub manage_selection { my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - my $oid_upsOutputEntry = '.1.3.6.1.2.1.33.1.4.4.1'; - my $result = $self->{snmp}->get_table(oid => $oid_upsOutputEntry, nothing_quit => 1); - foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { - $self->build_values(current => $key, result => $result); - } - my $num = scalar(keys %{$self->{instances_done}}); - foreach my $instance (keys %{$self->{instances_done}}) { - my $instance_output = $instance; - $instance_output =~ s/\./#/g; + $self->{oline} = {}; + my $snmp_result = $options{snmp}->get_table( + oid => $oid_upsOutputEntry, + nothing_quit => 1 + ); + foreach my $oid (keys %{$snmp_result}) { + $oid =~ /^$oid_upsOutputEntry\.\d+\.(.*)$/; + my $instance = $1; + next if (defined($self->{oline}->{$instance})); - my @exits; - foreach (keys %{$maps_counters}) { - foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) { - if (defined($self->{counters_value}->{$instance}->{$_}) && $self->{counters_value}->{$instance}->{$_} =~ /\d/ && $self->{counters_value}->{$instance}->{$_} != $maps_counters->{$_}->{no_present}) { - push @exits, $self->{perfdata}->threshold_check(value => $self->{counters_value}->{$instance}->{$_} * $maps_counters->{$_}->{factor}, threshold => [ { label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, 'exit_litteral' => $maps_counters->{$_}->{thresholds}->{$name}->{exit_value} }]); - } - } - } - - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - my $extra_label = ''; - $extra_label = '_' . $instance_output if ($num > 1); - - my $str_output = "Output Line '$instance_output' "; - my $str_append = ''; - foreach (keys %{$maps_counters}) { - next if (!defined($self->{counters_value}->{$instance}->{$_}) || $self->{counters_value}->{$instance}->{$_} !~ /\d/ || $self->{counters_value}->{$instance}->{$_} == $maps_counters->{$_}->{no_present}); - - $str_output .= $str_append . sprintf($maps_counters->{$_}->{output_msg}, $self->{counters_value}->{$instance}->{$_} * $maps_counters->{$_}->{factor}); - $str_append = ', '; - my ($warning, $critical); - foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) { - $warning = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'warning'); - $critical = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'critical'); - } - - $self->{output}->perfdata_add(label => $_ . $extra_label, unit => $maps_counters->{$_}->{unit}, - value => sprintf("%.2f", $self->{counters_value}->{$instance}->{$_} * $maps_counters->{$_}->{factor}), - warning => $warning, - critical => $critical); - } - $self->{output}->output_add(severity => $exit, - short_msg => $str_output); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); + $result->{upsOutputCurrent} *= 0.1 if (defined($result->{upsOutputCurrent})); + $self->{oline}->{$instance} = { display => $instance, %$result }; } - if ($num > 1) { + if (scalar(keys %{$self->{oline}}) > 1) { $self->stdev(); } - - $self->{output}->display(); - $self->{output}->exit(); } 1;