refactoring some ups modes

This commit is contained in:
garnier-quentin 2019-04-15 12:06:25 +02:00
parent 690d9abd3a
commit cc073fdfc0
3 changed files with 320 additions and 590 deletions

View File

@ -20,64 +20,77 @@
package hardware::ups::mge::snmp::mode::inputlines; package hardware::ups::mge::snmp::mode::inputlines;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::values;
my %map_input_status = ( use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
1 => 'no',
2 => 'outoftolvolt',
3 => 'outoftolfreq',
4 => 'utilityoff',
);
my $maps_counters = { sub custom_status_output {
voltage => { class => 'centreon::plugins::values', obj => undef, my ($self, %options) = @_;
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 },
],
}
},
};
my $oid_upsmgInputPhaseNumEntry = '.1.3.6.1.4.1.705.1.6.1'; my $msg = "Input Line(s) bad status is '" . $self->{result_values}->{badstatus} . "' [failcause = " . $self->{result_values}->{failcause} . "]";
my $oid_mginputVoltageEntry = '.1.3.6.1.4.1.705.1.6.2.1.2'; # in dV return $msg;
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'; sub custom_status_calc {
my $oid_upsmgInputLineFailCauseEntry = '.1.3.6.1.4.1.705.1.6.4'; 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 { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -85,169 +98,83 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $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; return $self;
} }
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::check_options(%options);
foreach (keys %{$maps_counters}) { $self->change_macros(macros => ['warning_status', 'critical_status']);
$maps_counters->{$_}->{obj}->init(option_results => $self->{option_results});
}
} }
sub manage_counters { sub prefix_iline_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); return "Input Line '" . $options{instance_value}->{display} . "' ";
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);
}
} }
sub run { my %map_input_failcause = (
my ($self, %options) = @_; 1 => 'no',
$self->{snmp} = $options{snmp}; 2 => 'outoftolvolt',
3 => 'outoftolfreq',
4 => 'utilityoff',
);
my %map_bad_status = (
1 => 'yes',
2 => 'no',
);
$self->manage_selection(); my $mapping = {
mginputVoltage => { oid => '.1.3.6.1.4.1.705.1.6.2.1.2' }, # in dV
$self->{output}->output_add(severity => 'OK', mginputFrequency => { oid => '.1.3.6.1.4.1.705.1.6.2.1.3' }, # in dHz
short_msg => 'Input Line(s) status is ok'); mginputCurrent => { oid => '.1.3.6.1.4.1.705.1.6.2.1.6' }, # in dA
if (defined($self->{results}->{$oid_upsmgInputBadStatusEntry}->{$oid_upsmgInputBadStatusEntry . '.0'}) && };
$self->{results}->{$oid_upsmgInputBadStatusEntry}->{$oid_upsmgInputBadStatusEntry . '.0'} == 1) { my $oid_upsmgInput = '.1.3.6.1.4.1.705.1.6';
$self->{output}->output_add(severity => 'CRITICAL', my $oid_upsmgInputPhaseEntry = '.1.3.6.1.4.1.705.1.6.2.1';
short_msg => sprintf("Input Line(s) status is '%s'", my $oid_upsmgInputPhaseNum = '.1.3.6.1.4.1.705.1.6.1.0';
$map_input_status{$self->{results}->{$oid_upsmgInputLineFailCauseEntry}->{$oid_upsmgInputLineFailCauseEntry . '.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';
$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;
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{results} = $self->{snmp}->get_multiple_table(oids => [ $self->{iline} = {};
{ oid => $oid_upsmgInputPhaseNumEntry }, my $snmp_result = $options{snmp}->get_table(
{ oid => $oid_mginputVoltageEntry }, oid => $oid_upsmgInput,
{ oid => $oid_mginputFrequencyEntry }, nothing_quit => 1
{ oid => $oid_mginputCurrentEntry }, );
{ oid => $oid_upsmgInputBadStatusEntry },
{ oid => $oid_upsmgInputLineFailCauseEntry },
],
, nothing_quit => 1);
if (!defined($self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) || if (!defined($snmp_result->{$oid_upsmgInputPhaseNum}) ||
$self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'} == 0) { $snmp_result->{$oid_upsmgInputPhaseNum} == 0) {
$self->{output}->add_option_msg(short_msg => "No input lines found."); $self->{output}->add_option_msg(short_msg => "No input lines found.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my %instances = (); foreach my $oid (keys %{$snmp_result}) {
# can be 'xxx.1' or 'xxx.1.0' (cannot respect MIB :) next if ($oid !~ /^$oid_upsmgInputPhaseEntry\.\d+\.(.*)$/);
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mginputVoltageEntry}})) { my $instance = $1;
$oid =~ /^$oid_mginputVoltageEntry\.((\d+).*)/; next if (defined($self->{iline}->{$instance}));
if (scalar(keys %instances) < $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) {
$instances{$2} = 1; my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
$self->add_result(instance => $2, instance2 => $1, name => 'voltage', oid => $oid_mginputVoltageEntry); $result->{mginputVoltage} *= 0.1 if (defined($result->{mginputVoltage}));
} $result->{mginputFrequency} *= 0.1 if (defined($result->{mginputFrequency}));
} $result->{mginputCurrent} *= 0.1 if (defined($result->{mginputCurrent}));
%instances = (); next if ((!defined($result->{mginputVoltage}) || $result->{mginputVoltage} == 0) &&
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_mginputCurrentEntry}})) { (!defined($result->{mginputFrequency}) || $result->{mginputFrequency} == 0) &&
$oid =~ /^$oid_mginputCurrentEntry\.((\d+).*)/; (!defined($result->{mginputCurrent}) || $result->{mginputCurrent} == 0));
if (scalar(keys %instances) < $self->{results}->{$oid_upsmgInputPhaseNumEntry}->{$oid_upsmgInputPhaseNumEntry . '.0'}) { $self->{iline}->{$instance} = { display => $instance, %$result };
$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);
}
} }
if (scalar(keys %{$self->{instance_selected}}) <= 0) { $self->{global} = {
$self->{output}->add_option_msg(short_msg => "No input lines found."); badstatus => $map_bad_status{$snmp_result->{$oid_upsmgInputBadStatus}},
$self->{output}->option_exit(); failcause => $map_input_failcause{$snmp_result->{$oid_upsmgInputLineFailCause}},
} };
} }
1; 1;
@ -270,6 +197,16 @@ Can be: 'frequence', 'voltage', 'current'.
Threshold critical. Threshold critical.
Can be: 'frequence', 'voltage', 'current'. 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 =back
=cut =cut

View File

@ -20,83 +20,69 @@
package hardware::ups::mge::snmp::mode::outputlines; package hardware::ups::mge::snmp::mode::outputlines;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::values;
my $maps_counters = { sub set_counters {
voltage => { class => 'centreon::plugins::values', obj => undef, my ($self, %options) = @_;
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 },
],
}
},
};
my $maps_counters2 = { $self->{maps_counters_type} = [
'stdev-3phases' => { class => 'centreon::plugins::values', obj => undef, { name => 'global', type => 0, skipped_code => { -10 => 1 } },
set => { { name => 'oline', type => 1, cb_prefix_output => 'prefix_oline_output', message_multiple => 'All output lines are ok', skipped_code => { -10 => 1 } },
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 },
],
}
},
};
my $oid_upsmgOutputPhaseNumEntry = '.1.3.6.1.4.1.705.1.7.1'; $self->{maps_counters}->{global} = [
my $oid_mgoutputVoltageEntry = '.1.3.6.1.4.1.705.1.7.2.1.2'; # in dV { label => 'stdev-3phases', set => {
my $oid_mgoutputFrequencyEntry = '.1.3.6.1.4.1.705.1.7.2.1.3'; # in dHz key_values => [ { name => 'stdev' } ],
my $oid_mgoutputCurrentEntry = '.1.3.6.1.4.1.705.1.7.2.1.5'; # in dA output_template => 'Load Standard Deviation : %.2f',
my $oid_mgoutputLoadPerPhaseEntry = '.1.3.6.1.4.1.705.1.7.2.1.4'; # in % perfdatas => [
{ label => 'stdev', value => 'stdev_absolute', template => '%.2f' },
],
}
},
];
$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 { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -104,195 +90,69 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $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; 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});
}
}
sub manage_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); return "Output Line '" . $options{instance_value}->{display} . "' ";
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);
}
} }
sub run { sub stdev {
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) = @_; my ($self, %options) = @_;
$self->{instance_selected}->{$options{instance}} = {} if (!defined($self->{instance_selected}->{$options{instance}})); # Calculate stdev
$self->{instance_selected}->{$options{instance}}->{$options{name}} = $self->{results}->{$options{oid}}->{$options{oid} . '.' . $options{instance2}} * $options{multiple}; 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};
}
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 };
} }
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 { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{results} = $self->{snmp}->get_multiple_table(oids => [ $self->{oline} = {};
{ oid => $oid_upsmgOutputPhaseNumEntry }, my $snmp_result = $options{snmp}->get_table(
{ oid => $oid_mgoutputVoltageEntry }, oid => $oid_upsmgOutputPhaseEntry,
{ oid => $oid_mgoutputFrequencyEntry }, nothing_quit => 1
{ oid => $oid_mgoutputCurrentEntry }, );
{ oid => $oid_mgoutputLoadPerPhaseEntry }, foreach my $oid (keys %{$snmp_result}) {
], $oid =~ /^$oid_upsmgOutputPhaseEntry\.\d+\.(.*)$/;
, nothing_quit => 1); my $instance = $1;
next if (defined($self->{oline}->{$instance}));
if (!defined($self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'}) || my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
$self->{results}->{$oid_upsmgOutputPhaseNumEntry}->{$oid_upsmgOutputPhaseNumEntry . '.0'} == 0) { $result->{mgoutputVoltage} *= 0.1 if (defined($result->{mgoutputVoltage}));
$self->{output}->add_option_msg(short_msg => "No output lines found."); $result->{mgoutputFrequency} *= 0.1 if (defined($result->{mgoutputFrequency}));
$self->{output}->option_exit(); $result->{mgoutputCurrent} *= 0.1 if (defined($result->{mgoutputCurrent}));
$self->{oline}->{$instance} = { display => $instance, %$result };
} }
my %instances = (); if (scalar(keys %{$self->{oline}}) > 1) {
# can be 'xxx.1' or 'xxx.1.0' (cannot respect MIB :) $self->stdev();
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);
}
}
%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();
} }
} }

View File

@ -20,48 +20,69 @@
package hardware::ups::standard::rfc1628::snmp::mode::outputlines; package hardware::ups::standard::rfc1628::snmp::mode::outputlines;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
my %oids = ( sub set_counters {
'.1.3.6.1.2.1.33.1.4.4.1.5' => { counter => 'load', no_present => -1 }, # upsOutputPercentLoad my ($self, %options) = @_;
'.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
);
my $maps_counters = { $self->{maps_counters_type} = [
load => { thresholds => { { name => 'global', type => 0, skipped_code => { -10 => 1 } },
warning_frequence => { label => 'warning-load', exit_value => 'warning' }, { name => 'oline', type => 1, cb_prefix_output => 'prefix_oline_output', message_multiple => 'All output lines are ok', skipped_code => { -10 => 1 } },
critical_frequence => { label => 'critical-load', exit_value => 'critical' }, ];
},
output_msg => 'Load : %.2f %%', no_present => -1, $self->{maps_counters}->{global} = [
factor => 1, unit => '%', { label => 'stdev-3phases', set => {
}, key_values => [ { name => 'stdev' } ],
voltage => { thresholds => { output_template => 'Load Standard Deviation : %.2f',
warning_voltage => { label => 'warning-voltage', exit_value => 'warning' }, perfdatas => [
critical_voltage => { label => 'critical-voltage', exit_value => 'critical' }, { label => 'stdev', value => 'stdev_absolute', template => '%.2f' },
}, ],
output_msg => 'Voltage : %.2f V', no_present => 0, }
factor => 1, unit => 'V', },
}, ];
current => { thresholds => {
warning_current => { label => 'warning-current', exit_value => 'warning' }, $self->{maps_counters}->{oline} = [
critical_current => { label => 'critical-current', exit_value => 'critical' }, { label => 'load', set => {
}, key_values => [ { name => 'upsOutputPercentLoad' } ],
output_msg => 'Current : %.2f A', no_present => 0, output_template => 'Load : %.2f %%',
factor => 0.1, unit => 'A', perfdatas => [
}, { label => 'load', value => 'upsOutputPercentLoad_absolute', template => '%.2f',
power => { thresholds => { min => 0, max => 100, unit => '%', label_extra_instance => 1 },
warning_power => { label => 'warning-power', exit_value => 'warning' }, ],
critical_power => { label => 'critical-power', exit_value => 'critical' }, }
}, },
output_msg => 'Power : %.2f W', no_present => 0, { label => 'current', set => {
factor => 1, unit => 'W', 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 { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -69,69 +90,16 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
$options{options}->add_options(arguments => $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 },
});
}
}
$self->{counters_value} = {};
$self->{instances_done} = {};
return $self; return $self;
} }
sub check_options { sub prefix_oline_output {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options);
if (($self->{perfdata}->threshold_validate(label => 'warning-stdev-3phases', value => $self->{option_results}->{warning_stdev})) == 0) { return "Output Line '" . $options{instance_value}->{display} . "' ";
$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};
}
} }
sub stdev { sub stdev {
@ -139,86 +107,51 @@ sub stdev {
# Calculate stdev # Calculate stdev
my $total = 0; my $total = 0;
my $num_present = 0; my $num_present = scalar(keys %{$self->{oline}});
foreach my $instance (keys %{$self->{instances_done}}) { foreach my $instance (keys %{$self->{oline}}) {
next if ($self->{counters_value}->{$instance}->{load} == -1); # Not present next if (!defined($self->{oline}->{$instance}->{upsOutputPercentLoad}));
$total += $self->{counters_value}->{$instance}->{load}; $total += $self->{oline}->{$instance}->{upsOutputPercentLoad};
$num_present++;
} }
my $mean = $total / $num_present; my $mean = $total / $num_present;
$total = 0; $total = 0;
foreach my $instance (keys %{$self->{instances_done}}) { foreach my $instance (keys %{$self->{oline}}) {
next if ($self->{counters_value}->{$instance}->{load} == -1); # Not present next if (!defined($self->{oline}->{$instance}->{upsOutputPercentLoad}));
$total += ($mean - $self->{counters_value}->{$instance}->{load}) ** 2; $total += ($mean - $self->{oline}->{$instance}->{upsOutputPercentLoad}) ** 2;
} }
my $stdev = sqrt($total / $num_present); my $stdev = sqrt($total / $num_present);
$self->{global} = { stdev => $stdev };
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'));
} }
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) = @_; my ($self, %options) = @_;
$self->{snmp} = $options{snmp};
my $oid_upsOutputEntry = '.1.3.6.1.2.1.33.1.4.4.1'; $self->{oline} = {};
my $result = $self->{snmp}->get_table(oid => $oid_upsOutputEntry, nothing_quit => 1); my $snmp_result = $options{snmp}->get_table(
foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { oid => $oid_upsOutputEntry,
$self->build_values(current => $key, result => $result); nothing_quit => 1
);
foreach my $oid (keys %{$snmp_result}) {
$oid =~ /^$oid_upsOutputEntry\.\d+\.(.*)$/;
my $instance = $1;
next if (defined($self->{oline}->{$instance}));
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 };
} }
my $num = scalar(keys %{$self->{instances_done}}); if (scalar(keys %{$self->{oline}}) > 1) {
foreach my $instance (keys %{$self->{instances_done}}) {
my $instance_output = $instance;
$instance_output =~ s/\./#/g;
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);
}
if ($num > 1) {
$self->stdev(); $self->stdev();
} }
$self->{output}->display();
$self->{output}->exit();
} }
1; 1;