enh juniper mseries hardware by adding cache

This commit is contained in:
Colin Gagnaire 2018-12-28 18:00:50 +01:00
parent 6cd6b37cde
commit 8a6e2e49dc
3 changed files with 135 additions and 48 deletions

View File

@ -65,22 +65,6 @@ my %map_fru_states = (
10 => 'standby',
);
# In MIB 'mib-jnx-chassis'
my $mapping = {
jnxFruName => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.5' },
jnxFruType => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.6', map => \%map_fru_type },
jnxFruState => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.8', map => \%map_fru_states },
jnxFruTemp => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.9' },
jnxFruOfflineReason => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.10', map => \%map_fru_offline },
};
my $oid_jnxFruEntry = '.1.3.6.1.4.1.2636.3.1.15.1';
sub load {
my ($self) = @_;
push @{$self->{request}}, { oid => $oid_jnxFruEntry, start => $mapping->{jnxFruName}->{oid}, end => $mapping->{jnxFruOfflineReason}->{oid} };
}
sub check {
my ($self) = @_;
@ -88,23 +72,31 @@ sub check {
$self->{components}->{fru} = {name => 'frus', total => 0, skip => 0};
return if ($self->check_filter(section => 'fru'));
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_jnxFruEntry}})) {
next if ($oid !~ /^$mapping->{jnxFruName}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_jnxFruEntry}, instance => $instance);
my $mapping = {
jnxFruState => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.8', map => \%map_fru_states },
jnxFruTemp => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.9' },
jnxFruOfflineReason => { oid => '.1.3.6.1.4.1.2636.3.1.15.1.10', map => \%map_fru_offline },
};
my $results = $self->{snmp}->get_table(oid => $self->{oids_fru}->{jnxFruEntry}, start => $mapping->{jnxFruState}->{oid}, end => $mapping->{jnxFruOfflineReason}->{oid});
foreach my $instance (sort $self->get_instances(oid_entry => $self->{oids_fru}->{jnxFruEntry}, oid_name => $self->{oids_fru}->{jnxFruName})) {
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance);
next if ($self->check_filter(section => 'fru', instance => $instance));
next if ($result->{jnxFruState} =~ /empty/i &&
$self->absent_problem(section => 'fru', instance => $instance));
$self->{components}->{fru}->{total}++;
my $name = $self->get_cache(oid_entry => $self->{oids_fru}->{jnxFruEntry}, oid_name => $self->{oids_fru}->{jnxFruName}, instance => $instance);
my $type = $self->get_cache(oid_entry => $self->{oids_fru}->{jnxFruEntry}, oid_name => $self->{oids_fru}->{jnxFruType}, instance => $instance);
$self->{output}->output_add(long_msg => sprintf("Fru '%s' state is %s [instance: %s, type: %s, offline reason: %s]",
$result->{jnxFruName}, $result->{jnxFruState},
$instance, $result->{jnxFruType}, $result->{jnxFruOfflineReason}));
$name, $result->{jnxFruState},
$instance, $map_fru_type{$type}, $result->{jnxFruOfflineReason}));
my $exit = $self->get_severity(section => 'fru', value => $result->{jnxFruState});
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Fru '%s' state is %s [offline reason: %s]", $result->{jnxFruName}, $result->{jnxFruState},
short_msg => sprintf("Fru '%s' state is %s [offline reason: %s]", $name, $result->{jnxFruState},
$result->{jnxFruOfflineReason}));
}
@ -112,9 +104,9 @@ sub check {
my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'fru-temperature', instance => $instance, value => $result->{jnxFruTemp});
if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit2,
short_msg => sprintf("Fru '%s' temperature is %s degree centigrade", $result->{jnxFruName}, $result->{jnxFruTemp}));
short_msg => sprintf("Fru '%s' temperature is %s degree centigrade", $name, $result->{jnxFruTemp}));
}
$self->{output}->perfdata_add(label => "temp_" . $result->{jnxFruName}, unit => 'C',
$self->{output}->perfdata_add(label => "temp_" . $name, unit => 'C',
value => $result->{jnxFruTemp},
warning => $warn,
critical => $crit);

View File

@ -33,19 +33,6 @@ my %map_operating_states = (
7 => 'standby',
);
# In MIB 'mib-jnx-chassis'
my $mapping = {
jnxOperatingDescr => { oid => '.1.3.6.1.4.1.2636.3.1.13.1.5' },
jnxOperatingState => { oid => '.1.3.6.1.4.1.2636.3.1.13.1.6', map => \%map_operating_states },
};
my $oid_jnxOperatingEntry = '.1.3.6.1.4.1.2636.3.1.13.1';
sub load {
my ($self) = @_;
push @{$self->{request}}, { oid => $oid_jnxOperatingEntry, start => $mapping->{jnxOperatingDescr}->{oid}, end => $mapping->{jnxOperatingState}->{oid} };
}
sub check {
my ($self) = @_;
@ -53,21 +40,26 @@ sub check {
$self->{components}->{operating} = {name => 'operatings', total => 0, skip => 0};
return if ($self->check_filter(section => 'operating'));
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_jnxOperatingEntry}})) {
next if ($oid !~ /^$mapping->{jnxOperatingDescr}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_jnxOperatingEntry}, instance => $instance);
my $mapping = {
jnxOperatingState => { oid => '.1.3.6.1.4.1.2636.3.1.13.1.6', map => \%map_operating_states },
};
my $results = $self->{snmp}->get_table(oid => $self->{oids_operating}->{jnxOperatingEntry}, start => $mapping->{jnxOperatingState}->{oid}, end => $mapping->{jnxOperatingState}->{oid});
foreach my $instance (sort $self->get_instances(oid_entry => $self->{oids_operating}->{jnxOperatingEntry}, oid_name => $self->{oids_operating}->{jnxOperatingDescr})) {
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance);
next if ($self->check_filter(section => 'operating', instance => $instance));
$self->{components}->{operating}->{total}++;
my $desc = $self->get_cache(oid_entry => $self->{oids_operating}->{jnxOperatingEntry}, oid_name => $self->{oids_operating}->{jnxOperatingDescr}, instance => $instance);
$self->{output}->output_add(long_msg => sprintf("Operating '%s' state is %s [instance: %s]",
$result->{jnxOperatingDescr}, $result->{jnxOperatingState}, $instance));
$desc, $result->{jnxOperatingState}, $instance));
my $exit = $self->get_severity(section => 'operating', value => $result->{jnxOperatingState});
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Operating '%s' state is %s",
$result->{jnxOperatingDescr}, $result->{jnxOperatingState}));
$desc, $result->{jnxOperatingState}));
}
}
}

View File

@ -24,6 +24,7 @@ use base qw(centreon::plugins::templates::hardware);
use strict;
use warnings;
use centreon::plugins::statefile;
sub set_system {
my ($self, %options) = @_;
@ -31,9 +32,10 @@ sub set_system {
$self->{regexp_threshold_overload_check_section_option} = '^(fru|operating)$';
$self->{regexp_threshold_numeric_check_section_option} = '^(fru-temperature)$';
$self->{cb_hook1} = 'get_type';
$self->{cb_hook1} = 'init_cache';
$self->{cb_hook2} = 'snmp_execute';
$self->{cb_hook3} = 'get_type';
$self->{thresholds} = {
fru => [
['unknown', 'UNKNOWN'],
@ -67,6 +69,7 @@ sub snmp_execute {
$self->{snmp} = $options{snmp};
$self->{results} = $self->{snmp}->get_multiple_table(oids => $self->{request});
$self->write_cache();
}
sub new {
@ -77,11 +80,81 @@ sub new {
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"reload-cache-time:s" => { name => 'reload_cache_time', default => 180 },
});
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->{statefile_cache}->check_options(%options);
}
sub init_cache {
my ($self, %options) = @_;
$self->{hostname} = $options{snmp}->get_hostname();
$self->{snmp_port} = $options{snmp}->get_port();
$self->{oids_fru} = {
jnxFruEntry => '.1.3.6.1.4.1.2636.3.1.15.1',
jnxFruName => '.1.3.6.1.4.1.2636.3.1.15.1.5',
jnxFruType => '.1.3.6.1.4.1.2636.3.1.15.1.6',
};
$self->{oids_operating} = {
jnxOperatingEntry => '.1.3.6.1.4.1.2636.3.1.13.1',
jnxOperatingDescr => '.1.3.6.1.4.1.2636.3.1.13.1.5',
};
$self->{write_cache} = 0;
my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_juniper_mseries_' . $self->{hostname} . '_' . $self->{snmp_port});
my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp');
if ($has_cache_file == 0 ||
!defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{option_results}->{reload_cache_time}) * 60)) && $self->{option_results}->{reload_cache_time} != '-1') {
push @{$self->{request}}, { oid => $self->{oids_fru}->{jnxFruEntry}, start => $self->{oids_fru}->{jnxFruName}, end => $self->{oids_fru}->{jnxFruType} };
push @{$self->{request}}, { oid => $self->{oids_operating}->{jnxOperatingEntry}, start => $self->{oids_operating}->{jnxOperatingDescr}, end => $self->{oids_operating}->{jnxOperatingDescr} };
$self->{write_cache} = 1;
}
}
sub write_cache {
my ($self, %options) = @_;
if ($self->{write_cache} == 1) {
my $datas = {};
$datas->{last_timestamp} = time();
$datas->{$self->{oids_fru}->{jnxFruEntry}} = $self->{results}->{$self->{oids_fru}->{jnxFruEntry}};
$datas->{$self->{oids_operating}->{jnxOperatingEntry}} = $self->{results}->{$self->{oids_operating}->{jnxOperatingEntry}};
$self->{statefile_cache}->write(data => $datas);
} else {
$self->{results}->{$self->{oids_fru}->{jnxFruEntry}} = $self->{statefile_cache}->get(name => $self->{oids_fru}->{jnxFruEntry});
$self->{results}->{$self->{oids_operating}->{jnxOperatingEntry}} = $self->{statefile_cache}->get(name => $self->{oids_operating}->{jnxOperatingEntry});
}
}
sub get_cache {
my ($self, %options) = @_;
return $self->{results}->{$options{oid_entry}}->{$options{oid_name} . '.' . $options{instance}};
}
sub get_instances {
my ($self, %options) = @_;
my @instances = ();
foreach (keys %{$self->{results}->{$options{oid_entry}}}) {
if (/^$options{oid_name}\.(.*)/) {
push @instances, $1;
}
}
return @instances;
}
sub get_type {
my ($self, %options) = @_;
@ -93,13 +166,38 @@ sub get_type {
$self->{output}->output_add(long_msg => sprintf("Environment type: %s", $self->{env_type}));
}
sub load_components {
my ($self, %options) = @_;
foreach (@{$self->{components_module}}) {
if (/$self->{option_results}->{component}/) {
my $mod_name = $self->{components_path} . "::$_";
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $mod_name,
error_msg => "Cannot load module '$mod_name'.") if ($self->{load_components} == 1);
$self->{loaded} = 1;
}
}
}
sub exec_components {
my ($self, %options) = @_;
foreach (@{$self->{components_module}}) {
if (/$self->{option_results}->{component}/) {
my $mod_name = $self->{components_path} . "::$_";
my $func = $mod_name->can('check');
$func->($self);
}
}
}
1;
__END__
=head1 MODE
Check Hardware (mib-jnx-chassis) (frus, operating).
Check Hardware (JUNIPER-MIB) (frus, operating).
=over 8
@ -139,6 +237,11 @@ Example: --warning='fru-temperature,.*,30'
Set critical threshold for fru temperatures (syntax: type,regexp,threshold)
Example: --critical='fru-temperature,.*,40'
=item B<--reload-cache-time>
Time in minutes before reloading cache file (Default: 180).
Use '-1' to disable cache reload.
=back
=cut