enhance printerror mode

This commit is contained in:
garnier-quentin 2019-02-28 09:32:43 +01:00
parent 26b4b287e2
commit 6f000196ee
3 changed files with 113 additions and 40 deletions

View File

@ -38,7 +38,10 @@ sub catalog_status_threshold {
my $label = $self->{label}; my $label = $self->{label};
$label =~ s/-/_/g; $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}") { eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' &&

View File

@ -20,29 +20,54 @@
package snmp_standard::mode::printererror; package snmp_standard::mode::printererror;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
my %errors_printer = ( sub custom_status_calc {
0 => ["Printer is low paper", 'WARNING'], my ($self, %options) = @_;
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'], $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
9 => ["Printer has output tray missing", 'WARNING'], return 0;
10 => ["Printer has maker supply missing", 'WARNING'], }
11 => ["Printer output is near full", 'WARNING'],
12 => ["Printer output is full", 'WARNING'], sub set_counters {
13 => ["Printer has input tray empty", 'WARNING'], my ($self, %options) = @_;
14 => ["Printer is 'overdue prevent maint'", 'WARNING'],
); $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 { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -50,8 +75,11 @@ 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 => {
{ "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; return $self;
@ -59,34 +87,56 @@ sub new {
sub check_options { sub check_options {
my ($self, %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 %errors_printer = (
my ($self, %options) = @_; 0 => 'low paper',
$self->{snmp} = $options{snmp}; 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',
);
$self->{output}->output_add(severity => 'OK', sub manage_selection {
short_msg => "Printer is ok."); my ($self, %options) = @_;
my $oid_hrPrinterDetectedErrorState = '.1.3.6.1.2.1.25.3.5.1.2'; 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) { foreach (keys %$result) {
/\.(\d+)$/;
my $instance = $1;
# 16 bits value # 16 bits value
my $value = unpack('S', $result->{$_}); my $value = unpack('S', $result->{$_});
foreach my $key (keys %errors_printer) { $self->{printer}->{$instance} = { display => $instance, errors => {} };
if (($value & (1 << $key)) && if ($result->{$_} == 0) {
(!$self->{output}->is_status(value => ${$errors_printer{$key}}[1], compare => 'ok', litteral => 1))) { $self->{printer}->{$instance}->{errors}->{0} = { status => 'ok' };
$self->{output}->output_add(severity => ${$errors_printer{$key}}[1], next;
short_msg => sprintf(${$errors_printer{$key}}[0]));
}
}
} }
$self->{output}->display(); my $i = 0;
$self->{output}->exit(); foreach my $key (keys %errors_printer) {
if (($value & (1 << $key))) {
$self->{printer}->{$instance}->{errors}->{$i} = { status => $errors_printer{$key} };
$i++;
}
}
}
} }
1; 1;
@ -99,6 +149,26 @@ Check printer errors (HOST-RESOURCES-MIB).
=over 8 =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 =back
=cut =cut