From b414805a0e6882d40b4af1406fd47afd5d0c52fb Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 10 Aug 2015 12:05:37 +0200 Subject: [PATCH] + Ref #122 --- .../raritan/snmp/mode/components/resources.pm | 222 +++++++++++++++ .../raritan/snmp/mode/components/sensor.pm | 112 ++++++++ .../pdu/raritan/snmp/mode/inletsensors.pm | 257 ++++++++++++++++++ .../pdu/raritan/snmp/mode/outletsensors.pm | 257 ++++++++++++++++++ hardware/pdu/raritan/snmp/plugin.pm | 50 ++++ 5 files changed, 898 insertions(+) create mode 100644 hardware/pdu/raritan/snmp/mode/components/resources.pm create mode 100644 hardware/pdu/raritan/snmp/mode/components/sensor.pm create mode 100644 hardware/pdu/raritan/snmp/mode/inletsensors.pm create mode 100644 hardware/pdu/raritan/snmp/mode/outletsensors.pm create mode 100644 hardware/pdu/raritan/snmp/plugin.pm diff --git a/hardware/pdu/raritan/snmp/mode/components/resources.pm b/hardware/pdu/raritan/snmp/mode/components/resources.pm new file mode 100644 index 000000000..15ae8baa8 --- /dev/null +++ b/hardware/pdu/raritan/snmp/mode/components/resources.pm @@ -0,0 +1,222 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::pdu::raritan::snmp::mode::components::resources; + +use strict; +use warnings; +use Exporter; + +our $thresholds; +our $mapping; +our %raritan_type; +our %map_type; + +our @ISA = qw(Exporter); +our @EXPORT_OK = qw($thresholds $mapping %raritan_type %map_type); + +my %map_units = ( + -1 => '', # none + 0 => 'other', + 1 => 'V', # volt, + 2 => 'A', # amp, + 3 => 'W', # watt + 4 => 'voltamp', + 5 => 'wattHour', + 6 => 'voltampHour', + 7 => 'C', # degreeC + 8 => 'Hz', # hertz + 9 => '%', # percent + 10 => 'meterpersec', + 11 => 'pascal', + 12 => 'psi', + 13 => 'g', + 14 => 'degreeF', + 15 => 'feet', + 16 => 'inches', + 17 => 'cm', + 18 => 'meters', + 19 => 'rpm', + 20 => 'degrees', + 21 => 'lux', +); + +my %map_state = ( + -1 => 'unavailable', 0 => 'open', 1 => 'closed', 2 => 'belowLowerCritical', + 3 => 'belowLowerWarning', 4 => 'normal', 5 => 'aboveUpperWarning', 6 => 'aboveUpperCritical', + 7 => 'on', 8 => 'off', 9 => 'detected', 10 => 'notDetected', 11 => 'alarmed', + 12 => 'ok', 14 => 'fail', 15 => 'yes', 16 => 'no', 17 => 'standby', + 18 => 'one', 19 => 'two', 20 => 'inSync', 21 => 'outOfSync', + 22 => 'i1OpenFault', 23 => 'i1ShortFault', + 24 => 'i2OpenFault', 25 => 'i2ShortFault', 26 => 'fault', + 27 => 'warning', 28 => 'critical', + 29 => 'selfTest', +); + +$mapping = { + inlet_label => { + Label => { oid => '.1.3.6.1.4.1.13742.6.3.3.3.1.2' }, # inletLabel + }, + inlet => { + Unit => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.6', map => \%map_units }, # inletSensorUnits + Decimal => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.7' }, # inletSensorDecimalDigits + EnabledThresholds => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.25' }, # inletSensorEnabledThresholds + LowerCriticalThreshold => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.21' }, # inletSensorLowerCriticalThreshold + LowerWarningThreshold => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.22' }, # inletSensorLowerWarningThreshold + UpperCriticalThreshold => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.23' }, # inletSensorUpperCriticalThreshold + UpperWarningThreshold => { oid => '.1.3.6.1.4.1.13742.6.3.3.4.1.24' }, # inletSensorUpperWarningThreshold + State => { oid => '.1.3.6.1.4.1.13742.6.5.2.3.1.3', map => \%map_state }, # measurementsInletSensorState + Value => { oid => '.1.3.6.1.4.1.13742.6.5.2.3.1.4' }, # measurementsInletSensorValue + }, +}; + +%raritan_type = ( + rmsCurrent => 1, peakCurrent => 2, unbalancedCurrent => 3, + rmsVoltage => 4, activePower => 5, apparentPower => 6, + powerFactor => 7, activeEnergy => 8, apparentEnergy => 9, + temperature => 10, humidity => 11, airFlow => 12, + airPressure => 13, onOff => 14, trip => 15, + vibration => 16, waterDetection => 17, smokeDetection => 18, + binary => 19, contact => 20, fanSpeed => 21, + surgeProtectorStatus => 22, frequency => 23, phaseAngle => 24, + rmsVoltageLN => 25, residualCurrent => 26, rcmState => 27, + other => 30, none => 31, powerQuality => 32, + overloadStatus => 33, overheatStatus => 34, fanStatus => 37, + inletPhaseSyncAngle => 38, inletPhaseSync => 39, operatingState => 40, + activeInlet => 41, illuminance => 42, doorContact => 43, + tamperDetection => 44, motionDetection => 45, i1smpsStatus => 46, + i2smpsStatus => 47, switchStatus => 48, +); + +%map_type = ( + 1 => 'numeric', + 2 => 'numeric', + 3 => 'numeric', + 4 => 'numeric', + 5 => 'numeric', + 6 => 'numeric', + 7 => 'numeric', + 8 => 'numeric', + 9 => 'numeric', + 10 => 'numeric', + 11 => 'numeric', + 12 => 'numeric', + 13 => 'numeric', + 14 => 'onoff', + 15 => 'contact', + 16 => 'alarm', + 17 => 'alarm', + 18 => 'alarm', + 19 => 'alarm', + 20 => 'alarm', + 21 => 'numeric', + 22 => 'fault', + 23 => 'numeric', + 24 => 'numeric', + 25 => 'numeric', + 26 => 'numeric', + 27 => 'alarm', + 30 => 'numeric', + 31 => 'numeric', + 32 => 'powerQuality', + 33 => 'fault', + 34 => 'fault', + 37 => 'fault', + 38 => 'numeric', + 39 => 'inletPhaseSync', + 40 => 'operatingState', + 41 => 'activeInlet', + 42 => 'numeric', + 43 => 'contact', + 44 => 'alarm', + 45 => 'motionDetection', + 46 => 'fault', + 47 => 'fault', + 48 => 'switchStatus', +); + +$thresholds = { + numeric => [ + ['unavailable', 'UNKNOWN'], + ['normal', 'OK'], + ['belowLowerCritical', 'CRITICAL'], + ['belowLowerWarning', 'WARNING'], + ['aboveUpperWarning', 'WARNING'], + ['aboveUpperCritical', 'CRITICAL'], + ], + onoff => [ + ['unavailable', 'UNKNOWN'], + ['ok', 'OK'], + ['off', 'OK'], + ], + contact => [ + ['unavailable', 'UNKNOWN'], + ['open', 'OK'], + ['closed', 'OK'], + ], + alarm => [ + ['unavailable', 'UNKNOWN'], + ['normal', 'OK'], + ['alarmed', 'CRITICAL'], + ['selfTest', 'OK'], + ['fail', 'CRITICAL'], + ], + fault => [ + ['unavailable', 'UNKNOWN'], + ['ok', 'OK'], + ['fault', 'CRITICAL'], + ], + powerQuality => [ + ['unavailable', 'UNKNOWN'], + ['normal', 'OK'], + ['warning', 'WARNING'], + ['critical', 'CRITICAL'], + ], + inletPhaseSync => [ + ['unavailable', 'UNKNOWN'], + ['inSync', 'OK'], + ['outOfSync', 'CRITICAL'], + ], + operatingState => [ + ['unavailable', 'UNKNOWN'], + ['normal', 'OK'], + ['standby', 'OK'], + ['off', 'CRITICAL'], + ], + activeInlet => [ + ['unavailable', 'UNKNOWN'], + ['one', 'OK'], + ['two', 'OK'], + ['none', 'WARNING'], + ], + motionDetection => [ + ['unavailable', 'UNKNOWN'], + ], + switchStatus => [ + ['unavailable', 'UNKNOWN'], + ['ok', 'OK'], + ['i1OpenFault', 'WARNING'], + ['i1ShortFault', 'WARNING'], + ['i2OpenFault', 'WARNING'], + ['i2ShortFault', 'WARNING'], + ], +}; + +1; \ No newline at end of file diff --git a/hardware/pdu/raritan/snmp/mode/components/sensor.pm b/hardware/pdu/raritan/snmp/mode/components/sensor.pm new file mode 100644 index 000000000..8ce3b6b5a --- /dev/null +++ b/hardware/pdu/raritan/snmp/mode/components/sensor.pm @@ -0,0 +1,112 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::pdu::raritan::snmp::mode::components::sensor; + +use strict; +use warnings; +use hardware::pdu::raritan::snmp::mode::components::resources qw($mapping %raritan_type %map_type); + +sub load { + my (%options) = @_; + + push @{$options{request}}, { oid => $mapping->{$options{type} . '_label'}->{Label}->{oid} }, + { oid => $mapping->{$options{type}}->{Unit}->{oid} }, + { oid => $mapping->{$options{type}}->{Decimal}->{oid} }, + { oid => $mapping->{$options{type}}->{EnabledThresholds}->{oid} }, + { oid => $mapping->{$options{type}}->{LowerCriticalThreshold}->{oid} }, + { oid => $mapping->{$options{type}}->{LowerWarningThreshold}->{oid} }, + { oid => $mapping->{$options{type}}->{UpperCriticalThreshold}->{oid} }, + { oid => $mapping->{$options{type}}->{UpperWarningThreshold}->{oid} }, + { oid => $mapping->{$options{type}}->{State}->{oid} }, + { oid => $mapping->{$options{type}}->{Value}->{oid} }; + +} + +sub check { + my ($self, %options) = @_; + + foreach my $component (sort keys %raritan_type) { + my $long_msg = 0; + next if ($component !~ /$options{component}/); + $self->{components}->{$component} = {name => $component, total => 0, skip => 0}; + next if ($self->check_exclude(section => $component)); + + my $instance_type = $raritan_type{$component}; + my $value_type = $map_type{$instance_type}; + foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}})) { + next if ($oid !~ /^$mapping->{$options{type}}->{Label}->{State}\.(\d+)\.(\d+)\.$instance_type$/); + my $instance = $1 . '.' . $2 . '.' . $instance_type; + my $result = $self->{snmp}->map_instance(mapping => $mapping->{$options{type}}, results => $self->{results}, instance => $instance); + my $result2 = $self->{snmp}->map_instance(mapping => $mapping->{$options{type} . '_label'}, results => $self->{results}, instance => $1 . '.' . $2); + + next if ($self->check_exclude(section => $component, instance => $instance)); + + if ($long_msg == 0) { + $self->{output}->output_add(long_msg => "Checking " . $component); + $long_msg = 1; + } + + $self->{components}->{$component}->{total}++; + + my $value = defined($result->{SignedValue}) ? $result->{SignedValue} : '-'; + if ($value =~ /[0-9]/) { + $value *= 10 ** -int($result->{Decimal}); + } + $self->{output}->output_add(long_msg => sprintf("'%s' %s state is '%s' [instance: %s, value: %s, unit: %s]", + $result2->{Label}, $component, $result->{State}, + $instance, $value, $result->{Unit})); + my $exit = $self->get_severity(section => $component, label => $value_type, + instance => $instance, value => $result->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("'%s' %s state is '%s'", + $result2->{Label}, $component, $result->{State})); + } + + if ($value->{value} =~ /[0-9]/) { + my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => $component, instance => $instance, value => $value); + if ($checked == 0) { + my $warn_th = '~:'; + $warn_th = $result->{LowerWarningThreshold} if (($result->{EnabledThresholds} & (1 << 1))); + $warn_th .= $result->{UpperWarningThreshold} if (($result->{EnabledThresholds} & (1 << 2))); + my $crit_th = '~:'; + $crit_th = $result->{LowerCriticalThreshold} if (($result->{EnabledThresholds} & (1 << 0))); + $crit_th .= $result->{UpperCriticalThreshold} if (($result->{EnabledThresholds} & (1 << 3))); + $self->{perfdata}->threshold_validate(label => 'warning-' . $component . '-instance-' . $instance, value => $warn_th); + $self->{perfdata}->threshold_validate(label => 'critical-' . $component . '-instance-' . $instance, value => $crit_th); + $warn = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $component . '-instance-' . $instance); + $crit = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $component . '-instance-' . $instance); + } + if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit2, + short_msg => sprintf("'%s' %s value is %s %s", + $result2->{Label}, $component, $value, $result->{Unit})); + } + $self->{output}->perfdata_add(label => $result2->{label} . "_" . $component, unit => $result->{Unit}, + value => $value, + warning => $warn, + critical => $crit); + } + } + } +} + +1; \ No newline at end of file diff --git a/hardware/pdu/raritan/snmp/mode/inletsensors.pm b/hardware/pdu/raritan/snmp/mode/inletsensors.pm new file mode 100644 index 000000000..5ed7eeeda --- /dev/null +++ b/hardware/pdu/raritan/snmp/mode/inletsensors.pm @@ -0,0 +1,257 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::pdu::raritan::snmp::mode::inletsensors; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use hardware::pdu::raritan::snmp::mode::components::resources qw($thresholds %raritan_type); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "exclude:s" => { name => 'exclude' }, + "component:s" => { name => 'component', default => '.*' }, + "no-component:s" => { name => 'no_component' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + "warning:s@" => { name => 'warning' }, + "critical:s@" => { name => 'critical' }, + }); + + $self->{components} = {}; + $self->{no_components} = undef; + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (defined($self->{option_results}->{no_component})) { + if ($self->{option_results}->{no_component} ne '') { + $self->{no_components} = $self->{option_results}->{no_component}; + } else { + $self->{no_components} = 'critical'; + } + } + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + my @values = split (/,/, $val); + if (scalar(@values) < 3) { + $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) = @_; + $instance = '.*'; + } else { + ($section, $instance, $status, $filter) = @_; + } + 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} = {}; + foreach my $option (('warning', 'critical')) { + foreach my $val (@{$self->{option_results}->{$option}}) { + 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 (!defined($raritan_type{$section})) { + $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 }; + } + } +} + +sub run { + my ($self, %options) = @_; + $self->{snmp} = $options{snmp}; + + my $snmp_request = []; + + my $mod_name = "hardware::pdu::raritan::snmp::mode::components::sensor"; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $mod_name, + error_msg => "Cannot load module '$mod_name'."); + my $func = $mod_name->can('load'); + $func->(type => 'inlet', request => $snmp_request); + + $self->{results} = $self->{snmp}->get_multiple_table(oids => $snmp_request, return_type => 1); + + $func = $mod_name->can('check'); + $func->($self, component => $self->{option_results}->{component}, type => 'inlet'); + + my $total_components = 0; + my $display_by_component = ''; + my $display_by_component_append = ''; + foreach my $comp (sort(keys %{$self->{components}})) { + # Skipping short msg when no components + next if ($self->{components}->{$comp}->{total} == 0 && $self->{components}->{$comp}->{skip} == 0); + $total_components += $self->{components}->{$comp}->{total} + $self->{components}->{$comp}->{skip}; + my $count_by_components = $self->{components}->{$comp}->{total} + $self->{components}->{$comp}->{skip}; + $display_by_component .= $display_by_component_append . $self->{components}->{$comp}->{total} . '/' . $count_by_components . ' ' . $self->{components}->{$comp}->{name}; + $display_by_component_append = ', '; + } + + $self->{output}->output_add(severity => 'OK', + short_msg => sprintf("All %s sensors are ok [%s].", + $total_components, + $display_by_component) + ); + + if (defined($self->{option_results}->{no_component}) && $total_components == 0) { + $self->{output}->output_add(severity => $self->{no_components}, + short_msg => 'No sensors are checked.'); + } + + $self->{output}->display(); + $self->{output}->exit(); +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($options{instance})) { + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{section}}[^,]*#\Q$options{instance}\E#/) { + $self->{components}->{$options{section}}->{skip}++; + $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section $options{instance} instance.")); + return 1; + } + } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)$options{section}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section.")); + return 1; + } + return 0; +} + +sub get_severity_numeric { + my ($self, %options) = @_; + my $status = 'OK'; # default + my $thresholds = { warning => undef, critical => undef }; + my $checked = 0; + + if (defined($self->{numeric_threshold}->{$options{section}})) { + my $exits = []; + foreach (@{$self->{numeric_threshold}->{$options{section}}}) { + 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); +} + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $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; + +__END__ + +=head1 MODE + +Check inlet sensors. + +=over 8 + +=item B<--component> + +Which component to check (Default: '.*'). + +=item B<--exclude> + +Exclude some parts (comma seperated list) (Example: --exclude=airPressure,rmsVoltage) +Can also exclude specific instance: --exclude=rmsVoltage#1.2.3#,airPressure + +=item B<--no-component> + +Return an error if no compenents are checked. +If total (with skipped) is 0. (Default: 'critical' returns). + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,[instance,]status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='powerQuality,CRITICAL,^(?!(normal)$)' + +=item B<--warning> + +Set warning threshold for temperatures (syntax: type,instance,threshold) +Example: --warning='powerQuality,.*,30' + +=item B<--critical> + +Set critical threshold for temperatures (syntax: type,instance,threshold) +Example: --critical='powerQuality,.*,40' + +=back + +=cut \ No newline at end of file diff --git a/hardware/pdu/raritan/snmp/mode/outletsensors.pm b/hardware/pdu/raritan/snmp/mode/outletsensors.pm new file mode 100644 index 000000000..f902b44f9 --- /dev/null +++ b/hardware/pdu/raritan/snmp/mode/outletsensors.pm @@ -0,0 +1,257 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::pdu::raritan::snmp::mode::outletsensors; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use hardware::pdu::raritan::snmp::mode::components::resources qw($thresholds %raritan_type); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "exclude:s" => { name => 'exclude' }, + "component:s" => { name => 'component', default => '.*' }, + "no-component:s" => { name => 'no_component' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + "warning:s@" => { name => 'warning' }, + "critical:s@" => { name => 'critical' }, + }); + + $self->{components} = {}; + $self->{no_components} = undef; + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (defined($self->{option_results}->{no_component})) { + if ($self->{option_results}->{no_component} ne '') { + $self->{no_components} = $self->{option_results}->{no_component}; + } else { + $self->{no_components} = 'critical'; + } + } + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + my @values = split (/,/, $val); + if (scalar(@values) < 3) { + $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) = @_; + $instance = '.*'; + } else { + ($section, $instance, $status, $filter) = @_; + } + 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} = {}; + foreach my $option (('warning', 'critical')) { + foreach my $val (@{$self->{option_results}->{$option}}) { + 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 (!defined($raritan_type{$section})) { + $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 }; + } + } +} + +sub run { + my ($self, %options) = @_; + $self->{snmp} = $options{snmp}; + + my $snmp_request = []; + + my $mod_name = "hardware::pdu::raritan::snmp::mode::components::sensor"; + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $mod_name, + error_msg => "Cannot load module '$mod_name'."); + my $func = $mod_name->can('load'); + $func->(type => 'outlet', request => $snmp_request); + + $self->{results} = $self->{snmp}->get_multiple_table(oids => $snmp_request, return_type => 1); + + $func = $mod_name->can('check'); + $func->($self, component => $self->{option_results}->{component}, type => 'outlet'); + + my $total_components = 0; + my $display_by_component = ''; + my $display_by_component_append = ''; + foreach my $comp (sort(keys %{$self->{components}})) { + # Skipping short msg when no components + next if ($self->{components}->{$comp}->{total} == 0 && $self->{components}->{$comp}->{skip} == 0); + $total_components += $self->{components}->{$comp}->{total} + $self->{components}->{$comp}->{skip}; + my $count_by_components = $self->{components}->{$comp}->{total} + $self->{components}->{$comp}->{skip}; + $display_by_component .= $display_by_component_append . $self->{components}->{$comp}->{total} . '/' . $count_by_components . ' ' . $self->{components}->{$comp}->{name}; + $display_by_component_append = ', '; + } + + $self->{output}->output_add(severity => 'OK', + short_msg => sprintf("All %s sensors are ok [%s].", + $total_components, + $display_by_component) + ); + + if (defined($self->{option_results}->{no_component}) && $total_components == 0) { + $self->{output}->output_add(severity => $self->{no_components}, + short_msg => 'No sensors are checked.'); + } + + $self->{output}->display(); + $self->{output}->exit(); +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($options{instance})) { + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{section}}[^,]*#\Q$options{instance}\E#/) { + $self->{components}->{$options{section}}->{skip}++; + $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section $options{instance} instance.")); + return 1; + } + } elsif (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)$options{section}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section.")); + return 1; + } + return 0; +} + +sub get_severity_numeric { + my ($self, %options) = @_; + my $status = 'OK'; # default + my $thresholds = { warning => undef, critical => undef }; + my $checked = 0; + + if (defined($self->{numeric_threshold}->{$options{section}})) { + my $exits = []; + foreach (@{$self->{numeric_threshold}->{$options{section}}}) { + 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); +} + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $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; + +__END__ + +=head1 MODE + +Check outlet sensors. + +=over 8 + +=item B<--component> + +Which component to check (Default: '.*'). + +=item B<--exclude> + +Exclude some parts (comma seperated list) (Example: --exclude=airPressure,rmsVoltage) +Can also exclude specific instance: --exclude=rmsVoltage#1.2.3#,airPressure + +=item B<--no-component> + +Return an error if no compenents are checked. +If total (with skipped) is 0. (Default: 'critical' returns). + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,[instance,]status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='powerQuality,CRITICAL,^(?!(normal)$)' + +=item B<--warning> + +Set warning threshold for temperatures (syntax: type,instance,threshold) +Example: --warning='powerQuality,.*,30' + +=item B<--critical> + +Set critical threshold for temperatures (syntax: type,instance,threshold) +Example: --critical='powerQuality,.*,40' + +=back + +=cut \ No newline at end of file diff --git a/hardware/pdu/raritan/snmp/plugin.pm b/hardware/pdu/raritan/snmp/plugin.pm new file mode 100644 index 000000000..c04ea96a5 --- /dev/null +++ b/hardware/pdu/raritan/snmp/plugin.pm @@ -0,0 +1,50 @@ +# +# Copyright 2015 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::pdu::raritan::snmp::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_snmp); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + # $options->{options} = options object + + $self->{version} = '1.0'; + %{$self->{modes}} = ( + 'inlet-sensors' => 'hardware::pdu::raritan::snmp::mode::inletsensors', + 'outlet-sensors' => 'hardware::pdu::raritan::snmp::mode::outletsensors', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Rarian PDU in SNMP. + +=cut