From 6f000196ee966d733f01fe8cff9f664d600316bc Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 28 Feb 2019 09:32:43 +0100 Subject: [PATCH] enhance printerror mode --- .../plugins/templates/catalog_functions.pm | 5 +- centreon/plugins/templates/counter.pm | 2 +- snmp_standard/mode/printererror.pm | 146 +++++++++++++----- 3 files changed, 113 insertions(+), 40 deletions(-) diff --git a/centreon/plugins/templates/catalog_functions.pm b/centreon/plugins/templates/catalog_functions.pm index be4b19fe7..9dee196ab 100644 --- a/centreon/plugins/templates/catalog_functions.pm +++ b/centreon/plugins/templates/catalog_functions.pm @@ -38,7 +38,10 @@ sub catalog_status_threshold { my $label = $self->{label}; $label =~ s/-/_/g; - if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' && + if (defined($self->{instance_mode}->{option_results}->{ok_status}) && $self->{instance_mode}->{option_results}->{ok_status} ne '' && + eval "$self->{instance_mode}->{option_results}->{ok_status}") { + $status = 'ok'; + } elsif (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' && eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") { $status = 'critical'; } elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && diff --git a/centreon/plugins/templates/counter.pm b/centreon/plugins/templates/counter.pm index 22c40a203..f69a6433c 100644 --- a/centreon/plugins/templates/counter.pm +++ b/centreon/plugins/templates/counter.pm @@ -386,7 +386,7 @@ sub run_multiple_instances { } my $message_separator = defined($options{config}->{message_separator}) ? - $options{config}->{message_separator}: ', '; + $options{config}->{message_separator} : ', '; foreach my $id (sort keys %{$self->{$options{config}->{name}}}) { my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); diff --git a/snmp_standard/mode/printererror.pm b/snmp_standard/mode/printererror.pm index 00818a12c..cb1717cf1 100644 --- a/snmp_standard/mode/printererror.pm +++ b/snmp_standard/mode/printererror.pm @@ -20,29 +20,54 @@ package snmp_standard::mode::printererror; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); -my %errors_printer = ( - 0 => ["Printer is low paper", 'WARNING'], - 1 => ["Printer has no paper", 'WARNING'], - 2 => ["Printer is low toner", 'WARNING'], - 3 => ["Printer has no toner", 'WARNING'], - 4 => ["Printer has a door open", 'WARNING'], - 5 => ["Printer is jammed", 'WARNING'], - 6 => ["Printer is offline", 'WARNING'], - 7 => ["Printer needs service requested", 'WARNING'], - - 8 => ["Printer has input tray missing", 'WARNING'], - 9 => ["Printer has output tray missing", 'WARNING'], - 10 => ["Printer has maker supply missing", 'WARNING'], - 11 => ["Printer output is near full", 'WARNING'], - 12 => ["Printer output is full", 'WARNING'], - 13 => ["Printer has input tray empty", 'WARNING'], - 14 => ["Printer is 'overdue prevent maint'", 'WARNING'], -); +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'printer', type => 3, cb_prefix_output => 'prefix_printer_output', cb_long_output => 'printer_long_output', indent_long_output => ' ', message_multiple => 'All printers are ok', + group => [ + { name => 'errors', message_multiple => 'Printer is ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{errors} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' } ], + closure_custom_calc => $self->can('custom_status_calc'), + output_template => "status is '%s'", + output_use => 'status', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + ]; +} + +sub prefix_printer_output { + my ($self, %options) = @_; + + return "Printer '" . $options{instance_value}->{display} . "' "; +} + +sub printer_long_output { + my ($self, %options) = @_; + + return "checking printer '" . $options{instance_value}->{display} . "'"; +} sub new { my ($class, %options) = @_; @@ -50,43 +75,68 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - }); + $options{options}->add_options(arguments => { + "ok-status:s" => { name => 'ok_status', default => '%{status} =~ /ok/' }, + "unknown-status:s" => { name => 'unknown_status', default => '' }, + "warning-status:s" => { name => 'warning_status', default => '%{status} =~ /.*/' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + }); return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['ok_status', 'unknown_status', 'warning_status', 'critical_status']); } -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; +my %errors_printer = ( + 0 => 'low paper', + 1 => 'no paper', + 2 => 'low toner', + 3 => 'no toner', + 4 => 'door open', + 5 => 'jammed', + 6 => 'offline', + 7 => 'service requested', + 8 => 'input tray missing', + 9 => 'output tray missing', + 10 => 'maker supply missing', + 11 => 'output near full', + 12 => 'output full', + 13 => 'input tray empty', + 14 => 'overdue prevent maint', +); + +sub manage_selection { + my ($self, %options) = @_; - $self->{output}->output_add(severity => 'OK', - short_msg => "Printer is ok."); - my $oid_hrPrinterDetectedErrorState = '.1.3.6.1.2.1.25.3.5.1.2'; - my $result = $self->{snmp}->get_table(oid => $oid_hrPrinterDetectedErrorState, nothing_quit => 1); - + my $result = $options{snmp}->get_table(oid => $oid_hrPrinterDetectedErrorState, nothing_quit => 1); + + $self->{printer} = {}; foreach (keys %$result) { + /\.(\d+)$/; + my $instance = $1; # 16 bits value my $value = unpack('S', $result->{$_}); + $self->{printer}->{$instance} = { display => $instance, errors => {} }; + if ($result->{$_} == 0) { + $self->{printer}->{$instance}->{errors}->{0} = { status => 'ok' }; + next; + } + + my $i = 0; foreach my $key (keys %errors_printer) { - if (($value & (1 << $key)) && - (!$self->{output}->is_status(value => ${$errors_printer{$key}}[1], compare => 'ok', litteral => 1))) { - $self->{output}->output_add(severity => ${$errors_printer{$key}}[1], - short_msg => sprintf(${$errors_printer{$key}}[0])); + if (($value & (1 << $key))) { + $self->{printer}->{$instance}->{errors}->{$i} = { status => $errors_printer{$key} }; + $i++; } } } - - $self->{output}->display(); - $self->{output}->exit(); } 1; @@ -99,6 +149,26 @@ Check printer errors (HOST-RESOURCES-MIB). =over 8 +=item B<--ok-status> + +Set warning threshold for status (Default: '%{status} =~ /ok/'). +Can used special variables like: %{status} + +=item B<--unknown-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{status} + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /.*/'). +Can used special variables like: %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{status} + =back =cut