This commit is contained in:
garnier-quentin 2019-05-24 14:25:38 +02:00
parent b00491024d
commit 38e6cef13b
1 changed files with 261 additions and 191 deletions

View File

@ -20,39 +20,129 @@
package hardware::pdu::apc::snmp::mode::load; package hardware::pdu::apc::snmp::mode::load;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::misc; use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
my $thresholds = { sub custom_status_output {
load => [ my ($self, %options) = @_;
['phaseLoadNormal', 'OK'],
['phaseLoadLow', 'WARNING'], my $msg = 'status : ' . $self->{result_values}->{status};
['phaseLoadNearOverload', 'WARNING'], return $msg;
['phaseLoadOverload', 'CRITICAL'], }
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'device', type => 1, cb_prefix_output => 'prefix_device_output', message_multiple => 'All devices are ok', skipped_code => { -10 => 1 } },
{ name => 'bank', type => 1, cb_prefix_output => 'prefix_bank_output', message_multiple => 'All banks are ok', skipped_code => { -10 => 1 } },
{ name => 'phase', type => 1, cb_prefix_output => 'prefix_phase_output', message_multiple => 'All phases are ok', skipped_code => { -10 => 1 } }
];
$self->{maps_counters}->{bank} = [
{ label => 'bank-status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'display' } ],
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,
}
},
{ label => 'current', nlabel => 'bank.current.ampere', set => {
key_values => [ { name => 'current' }, { name => 'display' } ],
output_template => 'current : %s A',
perfdatas => [
{ label => 'current_bank', template => '%s', value => 'current_absolute',
unit => 'A', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
], ],
}; }
my %map_status = ( },
1 => 'phaseLoadNormal', ];
2 => 'phaseLoadLow',
3 => 'phaseLoadNearOverload', $self->{maps_counters}->{phase} = [
4 => 'phaseLoadOverload', { label => 'phase-status', threshold => 0, set => {
); key_values => [ { name => 'status' }, { name => 'display' } ],
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,
}
},
{ label => 'current', nlabel => 'phase.current.ampere', set => {
key_values => [ { name => 'current' }, { name => 'display' } ],
output_template => 'current : %s A',
perfdatas => [
{ label => 'current_phase', template => '%s', value => 'current_absolute',
unit => 'A', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'power', nlabel => 'phase.power.watt', set => {
key_values => [ { name => 'power' }, { name => 'display' } ],
output_template => 'power : %s W',
perfdatas => [
{ label => 'power_phase', template => '%s', value => 'power_absolute',
unit => 'W', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
$self->{maps_counters}->{device} = [
{ label => 'power', nlabel => 'device.power.watt', set => {
key_values => [ { name => 'power' }, { name => 'display' } ],
output_template => 'power : %s W',
perfdatas => [
{ label => 'power_phase', template => '%s', value => 'power_absolute',
unit => 'W', min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
sub prefix_bank_output {
my ($self, %options) = @_;
return "Bank '" . $options{instance_value}->{display} . "' ";
}
sub prefix_phase_output {
my ($self, %options) = @_;
return "Phase '" . $options{instance_value}->{display} . "' ";
}
sub prefix_device_output {
my ($self, %options) = @_;
return "Device '" . $options{instance_value}->{display} . "' ";
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ "unknown-bank-status:s" => { name => 'unknown_bank_status', default => '' },
"filter:s@" => { name => 'filter' }, "warning-bank-status:s" => { name => 'warning_bank_status', default => '%{status} =~ /low|nearOverload/i' },
"threshold-overload:s@" => { name => 'threshold_overload' }, "critical-bank-status:s" => { name => 'critical_bank_status', default => '%{status} =~ /^overload/i' },
"warning:s@" => { name => 'warning' }, "unknown-phase-status:s" => { name => 'unknown_phase_status', default => '' },
"critical:s@" => { name => 'critical' }, "warning-phase-status:s" => { name => 'warning_phase_status', default => '%{status} =~ /low|nearOverload/i' },
"critical-phase-status:s" => { name => 'critical_phase_status', default => '%{status} =~ /^overload/i' },
}); });
return $self; return $self;
@ -60,186 +150,151 @@ sub new {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::check_options(%options);
$self->{filter} = []; $self->change_macros(macros => [
foreach my $val (@{$self->{option_results}->{filter}}) { 'warning_bank_status', 'critical_bank_status', 'unknown_bank_status',
next if (!defined($val) || $val eq ''); 'warning_phase_status', 'critical_phase_status', 'unknown_phase_status',
my @values = split (/,/, $val); ]);
push @{$self->{filter}}, { filter => $values[0], instance => $values[1] };
} }
$self->{overload_th} = {}; my $map_rpdu_status = {
foreach my $val (@{$self->{option_results}->{threshold_overload}}) { 1 => 'normal',
next if (!defined($val) || $val eq ''); 2 => 'low',
my @values = split (/,/, $val); 3 => 'nearOverload',
if (scalar(@values) < 3) { 4 => 'overload',
$self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); };
$self->{output}->option_exit();
}
my ($section, $instance, $status, $filter);
if (scalar(@values) == 3) {
($section, $status, $filter) = @values;
$instance = '.*';
} else {
($section, $instance, $status, $filter) = @values;
}
if ($section !~ /^load$/) {
$self->{output}->add_option_msg(short_msg => "Wrong threshold-overload section '" . $val . "'.");
$self->{output}->option_exit();
}
if ($self->{output}->is_litteral_status(status => $status) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'.");
$self->{output}->option_exit();
}
$self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section}));
push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status, instance => $instance };
}
$self->{numeric_threshold} = {}; sub check_rpdu {
foreach my $option (('warning', 'critical')) { my ($self, %options) = @_;
foreach my $val (@{$self->{option_results}->{$option}}) {
next if (!defined($val) || $val eq ''); return if (scalar(keys %{$self->{phase}}) > 0);
if ($val !~ /^(.*?),(.*?),(.*)$/) {
$self->{output}->add_option_msg(short_msg => "Wrong $option option '" . $val . "'.");
$self->{output}->option_exit();
}
my ($section, $instance, $value) = ($1, $2, $3);
if ($section !~ /^load$/) {
$self->{output}->add_option_msg(short_msg => "Wrong $option option '" . $val . "'.");
$self->{output}->option_exit();
}
my $position = 0;
if (defined($self->{numeric_threshold}->{$section})) {
$position = scalar(@{$self->{numeric_threshold}->{$section}});
}
if (($self->{perfdata}->threshold_validate(label => $option . '-' . $section . '-' . $position, value => $value)) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong $option threshold '" . $value . "'.");
$self->{output}->option_exit();
}
$self->{numeric_threshold}->{$section} = [] if (!defined($self->{numeric_threshold}->{$section}));
push @{$self->{numeric_threshold}->{$section}}, { label => $option . '-' . $section . '-' . $position, threshold => $option, instance => $instance };
}
}
}
my $mapping = { my $mapping = {
rPDULoadStatusLoad => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.2' }, rPDULoadStatusLoad => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.2' },
rPDULoadStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.3', map => \%map_status }, rPDULoadStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.3', map => $map_rpdu_status },
rPDULoadStatusPhaseNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.4' }, rPDULoadStatusPhaseNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.4' },
rPDULoadStatusBankNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.5' }, rPDULoadStatusBankNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.5' }, # Bank 0 = no bank (phase total)
}; };
sub run {
my ($self, %options) = @_;
$self->{snmp} = $options{snmp};
my $oid_rPDULoadStatusEntry = '.1.3.6.1.4.1.318.1.1.12.2.3.1.1'; my $oid_rPDULoadStatusEntry = '.1.3.6.1.4.1.318.1.1.12.2.3.1.1';
my $snmp_result = $options{snmp}->get_table(oid => $oid_rPDULoadStatusEntry, nothing_quit => 1);
$self->{results} = $self->{snmp}->get_table(oid => $oid_rPDULoadStatusEntry, nothing_quit => 1); foreach my $oid (keys %{$snmp_result}) {
$self->{output}->output_add(severity => 'OK',
short_msg => 'All phase/bank loads are ok');
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}})) {
next if ($oid !~ /^$mapping->{rPDULoadStatusLoadState}->{oid}\.(.*)$/); next if ($oid !~ /^$mapping->{rPDULoadStatusLoadState}->{oid}\.(.*)$/);
my $instance = $1; my $instance = $1;
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
next if ($self->check_filter(section => 'load', instance => $instance)); if ($result->{rPDULoadStatusBankNumber} == 0) {
$self->{phase}->{$result->{rPDULoadStatusPhaseNumber}} = {
$self->{output}->output_add(long_msg => sprintf("Phase '%s' on Bank '%s' status is '%s' [instance: %s, value: %s]", display => $result->{rPDULoadStatusPhaseNumber},
$result->{rPDULoadStatusPhaseNumber}, $result->{rPDULoadStatusBankNumber}, $result->{rPDULoadStatusLoadState}, status => $result->{rPDULoadStatusLoadState},
$instance, defined($result->{rPDULoadStatusLoad}) ? $result->{rPDULoadStatusLoad} : '-')); current => $result->{rPDULoadStatusLoad} / 10,
my $exit = $self->get_severity(section => 'load', instance => $instance, value => $result->{rPDULoadStatusLoadState}); };
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { } else {
$self->{output}->output_add(severity => $exit, $self->{bank}->{$result->{rPDULoadStatusBankNumber}} = { display => $result->{rPDULoadStatusBankNumber}, current => 0 }
short_msg => sprintf("Phase '%s' on Bank '%s' status is '%s'", if (!defined($self->{bank}->{$result->{rPDULoadStatusBankNumber}}));
$result->{rPDULoadStatusPhaseNumber}, $result->{rPDULoadStatusBankNumber}, $result->{rPDULoadStatusLoadState})); $self->{bank}->{$result->{rPDULoadStatusBankNumber}}->{status} = $result->{rPDULoadStatusLoadState};
} $self->{bank}->{$result->{rPDULoadStatusBankNumber}}->{current} += $result->{rPDULoadStatusLoad} / 10;
if (defined($result->{rPDULoadStatusLoad}) && $result->{rPDULoadStatusLoad} =~ /[0-9]/) {
$result->{rPDULoadStatusLoad} /= 10;
my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'load', instance => $instance, value => $result->{rPDULoadStatusLoad});
if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit2,
short_msg => sprintf("Phase '%s' on Bank '%s' load is %s A",
$result->{rPDULoadStatusPhaseNumber}, $result->{rPDULoadStatusBankNumber}, $result->{rPDULoadStatusLoad}));
}
$self->{output}->perfdata_add(label => 'load_bank_' . $result->{rPDULoadStatusBankNumber} . '_phase_' . $result->{rPDULoadStatusPhaseNumber}, unit => 'A',
value => $result->{rPDULoadStatusLoad},
warning => $warn,
critical => $crit,
min => 0);
} }
} }
$self->{output}->display(); my $oid_rPDUIdentName = '.1.3.6.1.4.1.318.1.1.12.1.1.0';
$self->{output}->exit(); my $oid_rPDUIdentDevicePowerWatts = '.1.3.6.1.4.1.318.1.1.12.1.16.0';
$snmp_result = $options{snmp}->get_leef(oids => [$oid_rPDUIdentName, $oid_rPDUIdentDevicePowerWatts]);
$self->{device}->{0} = {
display => $snmp_result->{$oid_rPDUIdentName},
power => $snmp_result->{$oid_rPDUIdentDevicePowerWatts} * 10,
};
} }
sub check_filter { my $map_rpdu2_status = {
1 => 'low',
2 => 'normal',
3 => 'nearOverload',
4 => 'overload',
};
sub check_rpdu2 {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach (@{$self->{filter}}) { my $mapping_phase = {
if ($options{section} =~ /$_->{filter}/) { rPDU2PhaseStatusNumber => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.3' },
if (!defined($options{instance}) && !defined($_->{instance})) { rPDU2PhaseStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.4', map => $map_rpdu2_status },
$self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section.")); rPDU2PhaseStatusCurrent => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.5' },
return 1; rPDU2PhaseStatusPower => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.7' },
} elsif (defined($options{instance}) && $options{instance} =~ /$_->{instance}/) { };
$self->{components}->{$options{section}}->{skip}++ if (defined($self->{components}->{$options{section}})); my $mapping_bank = {
$self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section $options{instance} instance.")); rPDU2BankStatusNumber => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.3' },
return 1; rPDU2BankStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.4', map => $map_rpdu2_status },
rPDU2BankStatusCurrent => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.5' },
};
my $mapping_device = {
rPDU2DeviceStatusName => { oid => '.1.3.6.1.4.1.318.1.1.26.4.3.1.3' },
rPDU2DeviceStatusPower => { oid => '.1.3.6.1.4.1.318.1.1.26.4.3.1.5' },
};
my $oid_rPDU2DeviceStatusEntry = '.1.3.6.1.4.1.318.1.1.26.4.3.1';
my $oid_rPDU2PhaseStatusEntry = '.1.3.6.1.4.1.318.1.1.26.6.3.1';
my $oid_rPDU2BankStatusEntry = '.1.3.6.1.4.1.318.1.1.26.8.3.1';
my $snmp_result = $options{snmp}->get_multiple_table(oids => [
{ oid => $oid_rPDU2PhaseStatusEntry, end => $mapping_phase->{rPDU2PhaseStatusPower}->{oid} },
{ oid => $oid_rPDU2BankStatusEntry, end => $mapping_bank->{rPDU2BankStatusCurrent}->{oid} },
{ oid => $oid_rPDU2DeviceStatusEntry, end => $mapping_bank->{rPDU2DeviceStatusPower}->{oid} },
]);
foreach my $oid (keys %{$snmp_result->{$oid_rPDU2PhaseStatusEntry}}) {
next if ($oid !~ /^$mapping_phase->{rPDU2PhaseStatusLoadState}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping_phase, results => $snmp_result->{$oid_rPDU2PhaseStatusEntry}, instance => $instance);
$self->{phase}->{$result->{rPDU2PhaseStatusNumber}} = {
display => $result->{rPDU2PhaseStatusNumber},
status => $result->{rPDU2PhaseStatusLoadState},
current => $result->{rPDU2PhaseStatusCurrent} / 10,
power => $result->{rPDU2PhaseStatusPower} * 10, # hundreth of kW. So * 10 for watt
}
}
foreach my $oid (keys %{$snmp_result->{$oid_rPDU2BankStatusEntry}}) {
next if ($oid !~ /^$mapping_phase->{rPDU2BankStatusLoadState}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping_bank, results => $snmp_result->{$oid_rPDU2BankStatusEntry}, instance => $instance);
$self->{bank}->{$result->{rPDU2BankStatusNumber}} = {
display => $result->{rPDU2BankStatusNumber},
status => $result->{rPDU2BankStatusLoadState},
current => $result->{rPDU2BankStatusCurrent} / 10,
}
}
foreach my $oid (keys %{$snmp_result->{$oid_rPDU2DeviceStatusEntry}}) {
next if ($oid !~ /^$mapping_device->{rPDU2DeviceStatusPower}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping_device, results => $snmp_result->{$oid_rPDU2DeviceStatusEntry}, instance => $instance);
$self->{device}->{$result->{rPDU2DeviceStatusName}} = {
display => $result->{rPDU2DeviceStatusName},
power => $result->{rPDU2DeviceStatusPower} * 10,
} }
} }
} }
return 0; sub manage_selection {
}
sub get_severity_numeric {
my ($self, %options) = @_; my ($self, %options) = @_;
my $status = 'OK'; # default
my $thresholds = { warning => undef, critical => undef };
my $checked = 0;
if (defined($self->{numeric_threshold}->{$options{section}})) { $self->{bank} = {};
my $exits = []; $self->{phase} = {};
foreach (@{$self->{numeric_threshold}->{$options{section}}}) { $self->{device} = {};
if ($options{instance} =~ /$_->{instance}/) {
push @{$exits}, $self->{perfdata}->threshold_check(value => $options{value}, threshold => [ { label => $_->{label}, exit_litteral => $_->{threshold} } ]);
$thresholds->{$_->{threshold}} = $self->{perfdata}->get_perfdata_for_output(label => $_->{label});
$checked = 1;
}
}
$status = $self->{output}->get_most_critical(status => $exits) if (scalar(@{$exits}) > 0);
}
return ($status, $thresholds->{warning}, $thresholds->{critical}, $checked); $self->check_rpdu2(%options);
} $self->check_rpdu(%options);
sub get_severity { if (scalar(keys %{$self->{device}}) <= 0) {
my ($self, %options) = @_; $self->{output}->add_option_msg(short_msg => "No device found.");
my $status = 'UNKNOWN'; # default $self->{output}->option_exit();
if (defined($self->{overload_th}->{$options{section}})) {
foreach (@{$self->{overload_th}->{$options{section}}}) {
if ($options{value} =~ /$_->{filter}/i &&
(!defined($options{instance}) || $options{instance} =~ /$_->{instance}/)) {
$status = $_->{status};
return $status;
} }
} }
}
my $label = defined($options{label}) ? $options{label} : $options{section};
foreach (@{$thresholds->{$label}}) {
if ($options{value} =~ /$$_[0]/i) {
$status = $$_[1];
return $status;
}
}
return $status;
}
1; 1;
@ -247,30 +302,45 @@ __END__
=head1 MODE =head1 MODE
Check phase/bank load state. Check phase/bank load.
=over 8 =over 8
=item B<--filter> =item B<--unknown-bank-status>
Exclude some parts (comma seperated list) Set warning threshold for status.
Can also exclude specific instance: --filter=load,1 Can used special variables like: %{type}, %{status}, %{display}
=item B<--threshold-overload> =item B<--warning-bank-status>
Set to overload default threshold values (syntax: section,[instance,]status,regexp) Set warning threshold for status (Default: '%{status} =~ /low|nearOverload/i').
It used before default thresholds (order stays). Can used special variables like: %{type}, %{status}, %{display}
Example: --threshold-overload='load,CRITICAL,^(?!(PhaseLoadNormal)$)'
=item B<--warning> =item B<--critical-bank-status>
Set warning threshold for temperatures (syntax: type,instance,threshold) Set critical threshold for status (Default: '%{status} =~ /^overload/').
Example: --warning='load,.*,30' Can used special variables like: %{type}, %{status}, %{display}
=item B<--critical> =item B<--unknown-phase-status>
Set warning threshold for status.
Can used special variables like: %{status}, %{display}
=item B<--warning-pĥase-status>
Set warning threshold for status (Default: '%{status} =~ /low|nearOverload/i').
Can used special variables like: %{status}, %{display}
=item B<--critical-phase-status>
Set critical threshold for status (Default: '%{status} =~ /^overload/i').
Can used special variables like: %{status}, %{display}
=item B<--warning-*> =item B<--critical-*>
Thresholds.
Can be: 'current', 'power'.
Set critical threshold for temperatures (syntax: type,instance,threshold)
Example: --critical='load,.*,40'
=back =back
=cut =cut