From 09080180eef4e40c0df2664387dd435eba8f2708 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 22 Dec 2016 16:25:52 +0100 Subject: [PATCH] + update qnap --- storage/qnap/snmp/mode/components/disk.pm | 10 +- storage/qnap/snmp/mode/components/fan.pm | 10 +- .../qnap/snmp/mode/components/temperature.pm | 11 +- storage/qnap/snmp/mode/hardware.pm | 255 +++--------------- 4 files changed, 55 insertions(+), 231 deletions(-) diff --git a/storage/qnap/snmp/mode/components/disk.pm b/storage/qnap/snmp/mode/components/disk.pm index ebb5eaaeb..46423ee47 100644 --- a/storage/qnap/snmp/mode/components/disk.pm +++ b/storage/qnap/snmp/mode/components/disk.pm @@ -43,10 +43,10 @@ my $mapping2 = { my $oid_HdEntry = '.1.3.6.1.4.1.24681.1.2.11.1'; sub load { - my (%options) = @_; + my ($self) = @_; - push @{$options{request}}, { oid => $oid_HdEntry, start => $mapping->{HdDescr}->{oid}, end => $mapping->{HdStatus}->{oid} }; - push @{$options{request}}, { oid => $mapping2->{HdSmartInfo}->{oid} }; + push @{$self->{request}}, { oid => $oid_HdEntry, start => $mapping->{HdDescr}->{oid}, end => $mapping->{HdStatus}->{oid} }; + push @{$self->{request}}, { oid => $mapping2->{HdSmartInfo}->{oid} }; } sub check { @@ -54,7 +54,7 @@ sub check { $self->{output}->output_add(long_msg => "Checking disks"); $self->{components}->{disk} = {name => 'disks', total => 0, skip => 0}; - return if ($self->check_exclude(section => 'disk')); + return if ($self->check_filter(section => 'disk')); foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_HdEntry}})) { next if ($oid !~ /^$mapping->{HdDescr}->{oid}\.(\d+)$/); @@ -63,7 +63,7 @@ sub check { my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_HdEntry}, instance => $instance); my $result2 = $self->{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{ $mapping2->{HdSmartInfo}->{oid} }, instance => $instance); - next if ($self->check_exclude(section => 'disk', instance => $instance)); + next if ($self->check_filter(section => 'disk', instance => $instance)); next if ($result->{HdStatus} eq 'noDisk' && $self->absent_problem(section => 'disk', instance => $instance)); diff --git a/storage/qnap/snmp/mode/components/fan.pm b/storage/qnap/snmp/mode/components/fan.pm index 6de0e134c..f06ff9a47 100644 --- a/storage/qnap/snmp/mode/components/fan.pm +++ b/storage/qnap/snmp/mode/components/fan.pm @@ -28,10 +28,10 @@ my $oid_SysFanDescr = '.1.3.6.1.4.1.24681.1.2.15.1.2'; my $oid_SysFanSpeed = '.1.3.6.1.4.1.24681.1.2.15.1.3'; sub load { - my (%options) = @_; + my ($self) = @_; - push @{$options{request}}, { oid => $oid_SysFanDescr }; - push @{$options{request}}, { oid => $oid_SysFanSpeed }; + push @{$self->{request}}, { oid => $oid_SysFanDescr }; + push @{$self->{request}}, { oid => $oid_SysFanSpeed }; } sub check { @@ -39,7 +39,7 @@ sub check { $self->{output}->output_add(long_msg => "Checking fans"); $self->{components}->{fan} = {name => 'fans', total => 0, skip => 0}; - return if ($self->check_exclude(section => 'fan')); + return if ($self->check_filter(section => 'fan')); foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_SysFanDescr}})) { $oid =~ /\.(\d+)$/; @@ -48,7 +48,7 @@ sub check { my $fan_speed = defined($self->{results}->{$oid_SysFanSpeed}->{$oid_SysFanSpeed . '.' . $instance}) ? $self->{results}->{$oid_SysFanSpeed}->{$oid_SysFanSpeed . '.' . $instance} : 'unknown'; - next if ($self->check_exclude(section => 'fan', instance => $instance)); + next if ($self->check_filter(section => 'fan', instance => $instance)); $self->{components}->{fan}->{total}++; $self->{output}->output_add(long_msg => sprintf("Fan '%s' [instance: %s] speed is '%s'.", diff --git a/storage/qnap/snmp/mode/components/temperature.pm b/storage/qnap/snmp/mode/components/temperature.pm index 2b51667d4..870cc8141 100644 --- a/storage/qnap/snmp/mode/components/temperature.pm +++ b/storage/qnap/snmp/mode/components/temperature.pm @@ -30,10 +30,9 @@ my $oid_SystemTemperature_entry = '.1.3.6.1.4.1.24681.1.2.6'; my $oid_SystemTemperature = '.1.3.6.1.4.1.24681.1.2.6.0'; sub load { - my (%options) = @_; + my ($self) = @_; - push @{$options{request}}, { oid => $oid_CPU_Temperature_entry }; - push @{$options{request}}, { oid => $oid_SystemTemperature_entry }; + push @{$self->{request}}, { oid => $oid_CPU_Temperature_entry }, { oid => $oid_SystemTemperature_entry }; } sub check { @@ -41,11 +40,11 @@ sub check { $self->{output}->output_add(long_msg => "Checking temperatures"); $self->{components}->{temperature} = {name => 'temperatures', total => 0, skip => 0}; - return if ($self->check_exclude(section => 'temperature')); + return if ($self->check_filter(section => 'temperature')); my $cpu_temp = defined($self->{results}->{$oid_CPU_Temperature_entry}->{$oid_CPU_Temperature}) ? $self->{results}->{$oid_CPU_Temperature_entry}->{$oid_CPU_Temperature} : 'unknown'; - if ($cpu_temp =~ /([0-9]+)\s*C/ && !$self->check_exclude(section => 'temperature', instance => 'cpu')) { + if ($cpu_temp =~ /([0-9]+)\s*C/ && !$self->check_filter(section => 'temperature', instance => 'cpu')) { my $value = $1; $self->{components}->{temperature}->{total}++; $self->{output}->output_add(long_msg => sprintf("CPU Temperature is '%s' degree centigrade", @@ -62,7 +61,7 @@ sub check { my $system_temp = defined($self->{results}->{$oid_SystemTemperature_entry}->{$oid_SystemTemperature}) ? $self->{results}->{$oid_SystemTemperature_entry}->{$oid_SystemTemperature} : 'unknown'; - if ($system_temp =~ /([0-9]+)\s*C/ && !$self->check_exclude(section => 'temperature', instance => 'system')) { + if ($system_temp =~ /([0-9]+)\s*C/ && !$self->check_filter(section => 'temperature', instance => 'system')) { my $value = $1; $self->{components}->{temperature}->{total}++; $self->{output}->output_add(long_msg => sprintf("System Temperature is '%s' degree centigrade", diff --git a/storage/qnap/snmp/mode/hardware.pm b/storage/qnap/snmp/mode/hardware.pm index bc1860b18..1abc5bf99 100644 --- a/storage/qnap/snmp/mode/hardware.pm +++ b/storage/qnap/snmp/mode/hardware.pm @@ -20,26 +20,45 @@ package storage::qnap::snmp::mode::hardware; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::hardware); use strict; use warnings; -my $thresholds = { - disk => [ - ['noDisk', 'OK'], - ['ready', 'OK'], - ['invalid', 'CRITICAL'], - ['rwError', 'CRITICAL'], - ['unknown', 'UNKNOWN'], - ], - smartdisk => [ - ['GOOD', 'OK'], - ['NORMAL', 'WARNING'], - ['--', 'OK'], - ['.*', 'CRITICAL'], - ], -}; +sub set_system { + my ($self, %options) = @_; + + $self->{regexp_threshold_overload_check_section_option} = '^(temperature|disk|fan)$'; + $self->{regexp_threshold_numeric_check_section_option} = '^(temperature|disk|fan)$'; + + $self->{cb_hook2} = 'snmp_execute'; + + $self->{thresholds} = { + disk => [ + ['noDisk', 'OK'], + ['ready', 'OK'], + ['invalid', 'CRITICAL'], + ['rwError', 'CRITICAL'], + ['unknown', 'UNKNOWN'], + ], + smartdisk => [ + ['GOOD', 'OK'], + ['NORMAL', 'WARNING'], + ['--', 'OK'], + ['.*', 'CRITICAL'], + ], + }; + + $self->{components_path} = 'storage::qnap::snmp::mode::components'; + $self->{components_module} = ['temperature', 'disk', 'fan']; +} + +sub snmp_execute { + my ($self, %options) = @_; + + $self->{snmp} = $options{snmp}; + $self->{results} = $self->{snmp}->get_multiple_table(oids => $self->{request}); +} sub new { my ($class, %options) = @_; @@ -49,204 +68,11 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => { - "exclude:s" => { name => 'exclude' }, - "component:s" => { name => 'component', default => '.*' }, - "absent-problem:s" => { name => 'absent' }, - "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}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - 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}; - } - - $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, $regexp, $value) = ($1, $2, $3); - if ($section !~ /(temperature|fan|disk)/) { - $self->{output}->add_option_msg(short_msg => "Wrong $option option '" . $val . "' (type must be: temperature, fan or disk)."); - $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, regexp => $regexp }; - } - } -} - -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - my $snmp_request = []; - my @components = ('temperature', 'disk', 'fan'); - foreach (@components) { - if (/$self->{option_results}->{component}/) { - my $mod_name = "storage::qnap::snmp::mode::components::$_"; - 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->(request => $snmp_request); - } - } - - if (scalar(@{$snmp_request}) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong option. Cannot find component '" . $self->{option_results}->{component} . "'."); - $self->{output}->option_exit(); - } - $self->{results} = $self->{snmp}->get_multiple_table(oids => $snmp_request); - - foreach (@components) { - if (/$self->{option_results}->{component}/) { - my $mod_name = "storage::qnap::snmp::mode::components::$_"; - my $func = $mod_name->can('check'); - $func->($self); - } - } - - 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 components 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 components 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 absent_problem { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{absent}) && - $self->{option_results}->{absent} =~ /(^|\s|,)($options{section}(\s*,|$)|${options{section}}[^,]*#\Q$options{instance}\E#)/) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => sprintf("Component '%s' instance '%s' is not present", - $options{section}, $options{instance})); - } - - $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section $options{instance} instance (not present)")); - $self->{components}->{$options{section}}->{skip}++; - return 1; -} - -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; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -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} =~ /$_->{regexp}/) { - 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); -} - 1; __END__ @@ -262,10 +88,10 @@ Check hardware (NAS.mib) (Fans, Temperatures, Disks). Which component to check (Default: '.*'). Can be: 'fan', 'disk', 'temperature'. -=item B<--exclude> +=item B<--filter> -Exclude some parts (comma seperated list) (Example: --exclude=disk) -Can also exclude specific instance: --exclude='disk#1#' +Exclude some parts (comma seperated list) (Example: --filter=disk) +Can also exclude specific instance: --filter=disk,1 =item B<--absent-problem> @@ -295,5 +121,4 @@ Example: --critical='temperature,system,40' --critical='disk,.*,40' =back -=cut - +=cut \ No newline at end of file