+ WIP snmp-autoreduce option
This commit is contained in:
parent
b89cda54f5
commit
02a5c15da8
|
@ -53,6 +53,7 @@ sub new {
|
|||
"maxrepetitions:s" => { name => 'maxrepetitions', default => 50 },
|
||||
"subsetleef:s" => { name => 'subsetleef', default => 50 },
|
||||
"subsettable:s" => { name => 'subsettable', default => 100 },
|
||||
"snmp-autoreduce" => { name => 'snmp_autoreduce' },
|
||||
"snmp-force-getnext" => { name => 'snmp_force_getnext' },
|
||||
"snmp-username:s" => { name => 'snmp_security_name' },
|
||||
"authpassphrase:s" => { name => 'snmp_auth_passphrase' },
|
||||
|
@ -138,6 +139,54 @@ sub load {
|
|||
push @{$self->{oids_loaded}}, @{$options{oids}};
|
||||
}
|
||||
|
||||
sub autoreduce_table {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return 1 if (defined($self->{snmp_force_getnext}) || $self->is_snmpv1());
|
||||
if ($self->{snmp_params}->{Retries} > 1) {
|
||||
$self->{snmp_params}->{Retries} = 1;
|
||||
$self->connect();
|
||||
}
|
||||
return 1 if (${$options{repeat_count}} == 1);
|
||||
${$options{repeat_count}} = int(${$options{repeat_count}} / 2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub autoreduce_leef {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
if ($self->{snmp_params}->{Retries} > 1) {
|
||||
$self->{snmp_params}->{Retries} = 1;
|
||||
$self->connect();
|
||||
}
|
||||
|
||||
return 1 if ($self->{subsetleef} == 1);
|
||||
$self->{subsetleef} = int($self->{subsetleef} / 2);
|
||||
|
||||
my $array_ref = [];
|
||||
my $subset_current = 0;
|
||||
my $subset_construct = [];
|
||||
foreach ([@{$options{current}}, @{$self->{array_ref_ar}}]) {
|
||||
foreach my $entry (@$_) {
|
||||
push @$subset_construct, [$entry->[0], $entry->[1]];
|
||||
$subset_current++;
|
||||
if ($subset_current == $self->{subsetleef}) {
|
||||
push @$array_ref, \@$subset_construct;
|
||||
$subset_construct = [];
|
||||
$subset_current = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($subset_current) {
|
||||
push @$array_ref, \@$subset_construct;
|
||||
}
|
||||
|
||||
$self->{array_ref_ar} = $array_ref;
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub get_leef {
|
||||
my ($self, %options) = @_;
|
||||
# $options{dont_quit} = integer
|
||||
|
@ -165,7 +214,7 @@ sub get_leef {
|
|||
}
|
||||
|
||||
my $results = {};
|
||||
my @array_ref_ar = ();
|
||||
$self->{array_ref_ar} = [];
|
||||
my $subset_current = 0;
|
||||
my $subset_construct = [];
|
||||
foreach my $oid (@{$options{oids}}) {
|
||||
|
@ -177,13 +226,13 @@ sub get_leef {
|
|||
push @$subset_construct, [$oid, $instance];
|
||||
$subset_current++;
|
||||
if ($subset_current == $self->{subsetleef}) {
|
||||
push @array_ref_ar, \@$subset_construct;
|
||||
push @{$self->{array_ref_ar}}, \@$subset_construct;
|
||||
$subset_construct = [];
|
||||
$subset_current = 0;
|
||||
}
|
||||
}
|
||||
if ($subset_current) {
|
||||
push @array_ref_ar, \@$subset_construct;
|
||||
push @{$self->{array_ref_ar}}, \@$subset_construct;
|
||||
}
|
||||
|
||||
############################
|
||||
|
@ -232,20 +281,23 @@ sub get_leef {
|
|||
############################
|
||||
|
||||
my $total = 0;
|
||||
foreach (@array_ref_ar) {
|
||||
my $vb = new SNMP::VarList(@{$_});
|
||||
while (my $entry = shift(@{$self->{array_ref_ar}})) {
|
||||
my $vb = new SNMP::VarList(@{$entry});
|
||||
$self->{session}->get($vb);
|
||||
if ($self->{session}->{ErrorNum}) {
|
||||
# 0 noError Pas d'erreurs.
|
||||
# 1 tooBig Reponse de taille trop grande.
|
||||
# 2 noSuchName Variable inexistante.
|
||||
# -24 Timeout
|
||||
if ($self->{session}->{ErrorNum} == 2) {
|
||||
# We are at the end with snmpv1. We next.
|
||||
next;
|
||||
}
|
||||
|
||||
my $msg = 'SNMP GET Request : ' . $self->{session}->{ErrorStr};
|
||||
|
||||
|
||||
if ($self->{snmp_autoreduce} == 1 && ($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == -24)) {
|
||||
next if ($self->autoreduce_leef(current => $entry) == 0);
|
||||
}
|
||||
my $msg = 'SNMP GET Request : ' . $self->{session}->{ErrorStr};
|
||||
if ($dont_quit == 0) {
|
||||
$self->{output}->add_option_msg(short_msg => $msg);
|
||||
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
|
||||
|
@ -503,6 +555,7 @@ sub get_table {
|
|||
$last_oid =~ /(.*)\.(\d+)([\.\s]*)$/;
|
||||
my $vb = new SNMP::VarList([$1, $2]);
|
||||
|
||||
print "===" . $repeat_count . "===\n";
|
||||
if ($self->is_snmpv1() || defined($self->{snmp_force_getnext})) {
|
||||
$self->{session}->getnext($vb);
|
||||
} else {
|
||||
|
@ -514,10 +567,15 @@ sub get_table {
|
|||
# 0 noError Pas d'erreurs.
|
||||
# 1 tooBig Reponse de taille trop grande.
|
||||
# 2 noSuchName Variable inexistante.
|
||||
# -24 Timeout
|
||||
if ($self->{session}->{ErrorNum} == 2) {
|
||||
# We are at the end with snmpv1. We quit.
|
||||
last;
|
||||
}
|
||||
if ($self->{snmp_autoreduce} == 1 && ($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == -24)) {
|
||||
next if ($self->autoreduce_table(repeat_count => \$repeat_count) == 0);
|
||||
}
|
||||
|
||||
my $msg = 'SNMP Table Request : ' . $self->{session}->{ErrorStr};
|
||||
|
||||
if ($dont_quit == 0) {
|
||||
|
@ -650,15 +708,18 @@ sub check_options {
|
|||
$self->{subsetleef} = (defined($options{option_results}->{subsetleef}) && $options{option_results}->{subsetleef} =~ /^[0-9]+$/) ? $options{option_results}->{subsetleef} : 50;
|
||||
$self->{subsettable} = (defined($options{option_results}->{subsettable}) && $options{option_results}->{subsettable} =~ /^[0-9]+$/) ? $options{option_results}->{subsettable} : 100;
|
||||
$self->{snmp_errors_exit} = $options{option_results}->{snmp_errors_exit};
|
||||
$self->{snmp_autoreduce} = defined($options{option_results}->{snmp_autoreduce}) ? 1 : 0;
|
||||
|
||||
%{$self->{snmp_params}} = (DestHost => $options{option_results}->{host},
|
||||
Community => $options{option_results}->{snmp_community},
|
||||
Version => $options{option_results}->{snmp_version},
|
||||
RemotePort => $options{option_results}->{snmp_port});
|
||||
RemotePort => $options{option_results}->{snmp_port},
|
||||
Retries => 5);
|
||||
|
||||
if (defined($options{option_results}->{snmp_timeout}) && $options{option_results}->{snmp_timeout} =~ /^[0-9]+$/) {
|
||||
$self->{snmp_params}->{Timeout} = $options{option_results}->{snmp_timeout} * (10**6);
|
||||
}
|
||||
|
||||
if (defined($options{option_results}->{snmp_retries}) && $options{option_results}->{snmp_retries} =~ /^[0-9]+$/) {
|
||||
$self->{snmp_params}->{Retries} = $options{option_results}->{snmp_retries};
|
||||
}
|
||||
|
@ -855,6 +916,10 @@ Max repetitions value (default: 50) (only for SNMP v2 and v3).
|
|||
|
||||
How many oid values per SNMP request (default: 50) (for get_leef method. Be cautious whe you set it. Prefer to let the default value).
|
||||
|
||||
=item B<--snmp-autoreduce>
|
||||
|
||||
Auto reduce SNMP request size in case of SNMP errors.
|
||||
|
||||
=item B<--snmp-force-getnext>
|
||||
|
||||
Use snmp getnext function (even in snmp v2c and v3).
|
||||
|
|
Loading…
Reference in New Issue