enhance indent

This commit is contained in:
garnier-quentin 2020-01-10 14:26:44 +01:00
parent 0ac3ceb44c
commit b70286b323

View File

@ -33,7 +33,7 @@ sub new {
# $options{options} = options object # $options{options} = options object
# $options{output} = output object # $options{output} = output object
# $options{exit_value} = integer # $options{exit_value} = integer
if (!defined($options{output})) { if (!defined($options{output})) {
print "Class SNMP: Need to specify 'output' argument.\n"; print "Class SNMP: Need to specify 'output' argument.\n";
exit 3; exit 3;
@ -45,26 +45,26 @@ sub new {
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"hostname|host:s" => { name => 'host' }, 'hostname|host:s' => { name => 'host' },
"snmp-community:s" => { name => 'snmp_community', default => 'public' }, 'snmp-community:s' => { name => 'snmp_community', default => 'public' },
"snmp-version:s" => { name => 'snmp_version', default => 1 }, 'snmp-version:s' => { name => 'snmp_version', default => 1 },
"snmp-port:s" => { name => 'snmp_port', default => 161 }, 'snmp-port:s' => { name => 'snmp_port', default => 161 },
"snmp-timeout:s" => { name => 'snmp_timeout', default => 1 }, 'snmp-timeout:s' => { name => 'snmp_timeout', default => 1 },
"snmp-retries:s" => { name => 'snmp_retries', default => 5 }, 'snmp-retries:s' => { name => 'snmp_retries', default => 5 },
"maxrepetitions:s" => { name => 'maxrepetitions', default => 50 }, 'maxrepetitions:s' => { name => 'maxrepetitions', default => 50 },
"subsetleef:s" => { name => 'subsetleef', default => 50 }, 'subsetleef:s' => { name => 'subsetleef', default => 50 },
"subsettable:s" => { name => 'subsettable', default => 100 }, 'subsettable:s' => { name => 'subsettable', default => 100 },
"snmp-autoreduce:s" => { name => 'snmp_autoreduce' }, 'snmp-autoreduce:s' => { name => 'snmp_autoreduce' },
"snmp-force-getnext" => { name => 'snmp_force_getnext' }, 'snmp-force-getnext' => { name => 'snmp_force_getnext' },
"snmp-username:s" => { name => 'snmp_security_name' }, 'snmp-username:s' => { name => 'snmp_security_name' },
"authpassphrase:s" => { name => 'snmp_auth_passphrase' }, 'authpassphrase:s' => { name => 'snmp_auth_passphrase' },
"authprotocol:s" => { name => 'snmp_auth_protocol' }, 'authprotocol:s' => { name => 'snmp_auth_protocol' },
"privpassphrase:s" => { name => 'snmp_priv_passphrase' }, 'privpassphrase:s' => { name => 'snmp_priv_passphrase' },
"privprotocol:s" => { name => 'snmp_priv_protocol' }, 'privprotocol:s' => { name => 'snmp_priv_protocol' },
"contextname:s" => { name => 'snmp_context_name' }, 'contextname:s' => { name => 'snmp_context_name' },
"contextengineid:s" => { name => 'snmp_context_engine_id' }, 'contextengineid:s' => { name => 'snmp_context_engine_id' },
"securityengineid:s" => { name => 'snmp_security_engine_id' }, 'securityengineid:s' => { name => 'snmp_security_engine_id' },
"snmp-errors-exit:s" => { name => 'snmp_errors_exit', default => 'unknown' }, 'snmp-errors-exit:s' => { name => 'snmp_errors_exit', default => 'unknown' },
}); });
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'SNMP OPTIONS'); $options{options}->add_help(package => __PACKAGE__, sections => 'SNMP OPTIONS');
@ -73,7 +73,7 @@ sub new {
$self->{session} = undef; $self->{session} = undef;
$self->{output} = $options{output}; $self->{output} = $options{output};
$self->{snmp_params} = {}; $self->{snmp_params} = {};
# Dont load MIB # Dont load MIB
$SNMP::auto_init_mib = 0; $SNMP::auto_init_mib = 0;
$ENV{MIBS} = ''; $ENV{MIBS} = '';
@ -81,16 +81,16 @@ sub new {
$self->{RetryNoSuch} = 1; $self->{RetryNoSuch} = 1;
# Dont try to translate OID (we keep value) # Dont try to translate OID (we keep value)
$self->{UseNumeric} = 1; $self->{UseNumeric} = 1;
$self->{error_msg} = undef; $self->{error_msg} = undef;
$self->{error_status} = 0; $self->{error_status} = 0;
return $self; return $self;
} }
sub connect { sub connect {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{snmp_params}->{RetryNoSuch} = $self->{RetryNoSuch}; $self->{snmp_params}->{RetryNoSuch} = $self->{RetryNoSuch};
$self->{snmp_params}->{UseNumeric} = $self->{UseNumeric}; $self->{snmp_params}->{UseNumeric} = $self->{UseNumeric};
@ -98,7 +98,7 @@ sub connect {
$self->{output}->add_option_msg(short_msg => "Unknown value '" . $self->{snmp_errors_exit} . "' for --snmp-errors-exit."); $self->{output}->add_option_msg(short_msg => "Unknown value '" . $self->{snmp_errors_exit} . "' for --snmp-errors-exit.");
$self->{output}->option_exit(exit_litteral => 'unknown'); $self->{output}->option_exit(exit_litteral => 'unknown');
} }
$self->{session} = new SNMP::Session(%{$self->{snmp_params}}); $self->{session} = new SNMP::Session(%{$self->{snmp_params}});
if (!defined($self->{session})) { if (!defined($self->{session})) {
$self->{output}->add_option_msg(short_msg => 'SNMP Session : unable to create'); $self->{output}->add_option_msg(short_msg => 'SNMP Session : unable to create');
@ -211,14 +211,14 @@ sub get_leef {
# $options{dont_quit} = integer # $options{dont_quit} = integer
# $options{nothing_quit} = integer # $options{nothing_quit} = integer
# $options{oids} = ref to array of oids (example: ['.1.2', '.1.2']) # $options{oids} = ref to array of oids (example: ['.1.2', '.1.2'])
# Returns array # Returns array
# 'undef' value for an OID means NoSuchValue # 'undef' value for an OID means NoSuchValue
my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0; my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0;
my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0; my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0;
$self->set_error(); $self->set_error();
if (!defined($options{oids})) { if (!defined($options{oids})) {
if ($#{$self->{oids_loaded}} < 0) { if ($#{$self->{oids_loaded}} < 0) {
if ($dont_quit == 1) { if ($dont_quit == 1) {
@ -231,7 +231,7 @@ sub get_leef {
push @{$options{oids}}, @{$self->{oids_loaded}}; push @{$options{oids}}, @{$self->{oids_loaded}};
@{$self->{oids_loaded}} = (); @{$self->{oids_loaded}} = ();
} }
my $results = {}; my $results = {};
$self->{array_ref_ar} = []; $self->{array_ref_ar} = [];
my $subset_current = 0; my $subset_current = 0;
@ -253,7 +253,7 @@ sub get_leef {
if ($subset_current) { if ($subset_current) {
push @{$self->{array_ref_ar}}, \@$subset_construct; push @{$self->{array_ref_ar}}, \@$subset_construct;
} }
############################ ############################
# If wrong oid with SNMP v1, packet resent (2 packets more). Not the case with SNMP > 1. # If wrong oid with SNMP v1, packet resent (2 packets more). Not the case with SNMP > 1.
# Can have "NoSuchName", if nothing works... # Can have "NoSuchName", if nothing works...
@ -298,7 +298,7 @@ sub get_leef {
# 'NOSUCHOBJECT' # 'NOSUCHOBJECT'
# ], 'SNMP::Varbind' ) # ], 'SNMP::Varbind' )
############################ ############################
my $total = 0; my $total = 0;
while (my $entry = shift(@{$self->{array_ref_ar}})) { while (my $entry = shift(@{$self->{array_ref_ar}})) {
my $vb = new SNMP::VarList(@{$entry}); my $vb = new SNMP::VarList(@{$entry});
@ -323,22 +323,22 @@ sub get_leef {
$self->{output}->add_option_msg(short_msg => $msg); $self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
$self->set_error(error_status => -1, error_msg => $msg); $self->set_error(error_status => -1, error_msg => $msg);
return undef; return undef;
} }
# Some equipments gives a partial response and no error. # Some equipments gives a partial response and no error.
# We look the last value if it's empty or not # We look the last value if it's empty or not
# In snmpv1 we have the retryNoSuch # In snmpv1 we have the retryNoSuch
if (((scalar(@$vb) != scalar(@{$entry})) || (scalar(@{@$vb[-1]}) < 3)) && !$self->is_snmpv1()) { if (((scalar(@$vb) != scalar(@{$entry})) || (scalar(@{@$vb[-1]}) < 3)) && !$self->is_snmpv1()) {
next if ($self->{snmp_autoreduce} == 1 && $self->autoreduce_leef(current => $entry) == 0); next if ($self->{snmp_autoreduce} == 1 && $self->autoreduce_leef(current => $entry) == 0);
if ($dont_quit == 0) { if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP partial response. Please try --snmp-autoreduce option"); $self->{output}->add_option_msg(short_msg => 'SNMP partial response. Please try --snmp-autoreduce option');
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
$self->set_error(error_status => -1, error_msg => "SNMP partial response"); $self->set_error(error_status => -1, error_msg => 'SNMP partial response');
return undef; return undef;
} }
@ -356,12 +356,12 @@ sub get_leef {
$results->{${$entry}[0] . "." . ${$entry}[1]} = ${$entry}[2]; $results->{${$entry}[0] . "." . ${$entry}[1]} = ${$entry}[2];
} }
} }
if ($nothing_quit == 1 && $total == 0) { if ($nothing_quit == 1 && $total == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP GET Request : Cant get a single value."); $self->{output}->add_option_msg(short_msg => 'SNMP GET Request : Cant get a single value.');
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
return $results; return $results;
} }
@ -385,12 +385,12 @@ sub get_multiple_table {
# $options{oids} = refs array # $options{oids} = refs array
# [ { oid => 'x.x.x.x', start => '', end => ''}, { oid => 'y.y.y.y', start => '', end => ''} ] # [ { oid => 'x.x.x.x', start => '', end => ''}, { oid => 'y.y.y.y', start => '', end => ''} ]
# $options{return_type} = integer # $options{return_type} = integer
my ($return_type) = (defined($options{return_type}) && $options{return_type} == 1) ? 1 : 0; my ($return_type) = (defined($options{return_type}) && $options{return_type} == 1) ? 1 : 0;
my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0; my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0;
my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0; my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0;
$self->set_error(); $self->set_error();
my $working_oids = {}; my $working_oids = {};
my $results = {}; my $results = {};
# Check overlap # Check overlap
@ -404,18 +404,18 @@ sub get_multiple_table {
$self->{output}->add_option_msg(short_msg => "Method 'get_multiple_table': Wrong OID '" . $entry->{oid} . "'."); $self->{output}->add_option_msg(short_msg => "Method 'get_multiple_table': Wrong OID '" . $entry->{oid} . "'.");
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
if (defined($entry->{start})) { if (defined($entry->{start})) {
$working_oids->{$entry->{oid}} = { start => $entry->{start}, end => $entry->{end} }; # last in it $working_oids->{$entry->{oid}} = { start => $entry->{start}, end => $entry->{end} }; # last in it
} else { } else {
$working_oids->{$entry->{oid}} = { start => $entry->{oid}, end => $entry->{end} }; $working_oids->{$entry->{oid}} = { start => $entry->{oid}, end => $entry->{end} };
} }
if ($return_type == 0) { if ($return_type == 0) {
$results->{$entry->{oid}} = {}; $results->{$entry->{oid}} = {};
} }
} }
# we use a medium (UDP have a PDU limit. SNMP protcol cant send multiples for one request) # we use a medium (UDP have a PDU limit. SNMP protcol cant send multiples for one request)
# So we need to manage # So we need to manage
# It's for "bulk". We ask 50 next values. If you set 1, it's like a getnext (snmp v1) # It's for "bulk". We ask 50 next values. If you set 1, it's like a getnext (snmp v1)
@ -424,7 +424,7 @@ sub get_multiple_table {
$self->{maxrepetitions} =~ /^\d+$/) { $self->{maxrepetitions} =~ /^\d+$/) {
$repeat_count = $self->{maxrepetitions}; $repeat_count = $self->{maxrepetitions};
} }
# Quit if base not the same or 'ENDOFMIBVIEW' value. Need all oid finish otherwise we continue :) # Quit if base not the same or 'ENDOFMIBVIEW' value. Need all oid finish otherwise we continue :)
while (1) { while (1) {
my $current_oids = 0; my $current_oids = 0;
@ -434,16 +434,16 @@ sub get_multiple_table {
$working_oids->{$key}->{start} =~ /(.*)\.(\d+)([\.\s]*)$/; $working_oids->{$key}->{start} =~ /(.*)\.(\d+)([\.\s]*)$/;
push @bindings, [$1, $2]; push @bindings, [$1, $2];
push @bases, $key; push @bases, $key;
$current_oids++; $current_oids++;
last if ($current_oids > $self->{subsettable}); last if ($current_oids > $self->{subsettable});
} }
# Nothing more to check. We quit # Nothing more to check. We quit
last if ($current_oids == 0); last if ($current_oids == 0);
my $vb = new SNMP::VarList(@bindings); my $vb = new SNMP::VarList(@bindings);
if ($self->is_snmpv1() || defined($self->{snmp_force_getnext})) { if ($self->is_snmpv1() || defined($self->{snmp_force_getnext})) {
$self->{session}->getnext($vb); $self->{session}->getnext($vb);
} else { } else {
@ -451,7 +451,7 @@ sub get_multiple_table {
$current_repeat_count = 1 if ($current_repeat_count == 0); $current_repeat_count = 1 if ($current_repeat_count == 0);
$self->{session}->getbulk(0, $current_repeat_count, $vb); $self->{session}->getbulk(0, $current_repeat_count, $vb);
} }
# Error # Error
if ($self->{session}->{ErrorNum}) { if ($self->{session}->{ErrorNum}) {
# 0 noError Pas d'erreurs. # 0 noError Pas d'erreurs.
@ -463,39 +463,39 @@ sub get_multiple_table {
delete $working_oids->{$oid_base}; delete $working_oids->{$oid_base};
next; next;
} }
if ($self->{snmp_autoreduce} == 1 && if ($self->{snmp_autoreduce} == 1 &&
($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == 5 || $self->{session}->{ErrorNum} == -24)) { ($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == 5 || $self->{session}->{ErrorNum} == -24)) {
next if ($self->autoreduce_multiple_table(repeat_count => \$repeat_count) == 0); next if ($self->autoreduce_multiple_table(repeat_count => \$repeat_count) == 0);
} }
my $msg = 'SNMP Table Request : ' . $self->{session}->{ErrorStr}; my $msg = 'SNMP Table Request : ' . $self->{session}->{ErrorStr};
if ($dont_quit == 0) { if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg); $self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
$self->set_error(error_status => -1, error_msg => $msg); $self->set_error(error_status => -1, error_msg => $msg);
return undef; return undef;
} }
# Manage # Manage
# step by step: [ 1 => 1, 2 => 1, 3 => 1 ], [ 1 => 2, 2 => 2, 3 => 2 ],... # step by step: [ 1 => 1, 2 => 1, 3 => 1 ], [ 1 => 2, 2 => 2, 3 => 2 ],...
my $pos = -1; my $pos = -1;
foreach my $entry (@$vb) { foreach my $entry (@$vb) {
$pos++; $pos++;
# Already destruct. we continue # Already destruct. we continue
next if (!defined($working_oids->{ $bases[$pos % $current_oids] })); next if (!defined($working_oids->{ $bases[$pos % $current_oids] }));
# ENDOFMIBVIEW is on each iteration. So we need to delete and skip after that # ENDOFMIBVIEW is on each iteration. So we need to delete and skip after that
if (${$entry}[2] eq 'ENDOFMIBVIEW') { if (${$entry}[2] eq 'ENDOFMIBVIEW') {
delete $working_oids->{ $bases[$pos % $current_oids] }; delete $working_oids->{ $bases[$pos % $current_oids] };
# END mib # END mib
next; next;
} }
# Not in same table # Not in same table
my $complete_oid = ${$entry}[0] . "." . ${$entry}[1]; my $complete_oid = ${$entry}[0] . "." . ${$entry}[1];
my $base = $bases[$pos % $current_oids]; my $base = $bases[$pos % $current_oids];
@ -505,7 +505,7 @@ sub get_multiple_table {
delete $working_oids->{ $bases[$pos % $current_oids] }; delete $working_oids->{ $bases[$pos % $current_oids] };
next; next;
} }
if ($return_type == 0) { if ($return_type == 0) {
$results->{$bases[$pos % $current_oids]}->{$complete_oid} = ${$entry}[2]; $results->{$bases[$pos % $current_oids]}->{$complete_oid} = ${$entry}[2];
} else { } else {
@ -514,14 +514,14 @@ sub get_multiple_table {
$working_oids->{ $bases[$pos % $current_oids] }->{start} = $complete_oid; $working_oids->{ $bases[$pos % $current_oids] }->{start} = $complete_oid;
} }
# infinite loop. Some equipments it returns nothing!!?? # infinite loop. Some equipments it returns nothing!!??
if ($pos == -1) { if ($pos == -1) {
$self->{output}->add_option_msg(short_msg => "SNMP Table Request: problem to get values (try --snmp-force-getnext option)"); $self->{output}->add_option_msg(short_msg => 'SNMP Table Request: problem to get values (try --snmp-force-getnext option)');
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
} }
my $total = 0; my $total = 0;
if ($nothing_quit == 1) { if ($nothing_quit == 1) {
if ($return_type == 1) { if ($return_type == 1) {
@ -531,13 +531,13 @@ sub get_multiple_table {
$total += scalar(keys %{$results->{$_}}); $total += scalar(keys %{$results->{$_}});
} }
} }
if ($total == 0) { if ($total == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP Table Request: Cant get a single value."); $self->{output}->add_option_msg(short_msg => 'SNMP Table Request: Cant get a single value.');
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
} }
return $results; return $results;
} }
@ -547,18 +547,18 @@ sub get_table {
# $options{oid} = string (example: '.1.2') # $options{oid} = string (example: '.1.2')
# $options{start} = string (example: '.1.2') # $options{start} = string (example: '.1.2')
# $options{end} = string (example: '.1.2') # $options{end} = string (example: '.1.2')
my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0; my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0;
my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0; my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0;
$self->set_error(); $self->set_error();
if (defined($options{start})) { if (defined($options{start})) {
$options{start} = $self->clean_oid($options{start}); $options{start} = $self->clean_oid($options{start});
} }
if (defined($options{end})) { if (defined($options{end})) {
$options{end} = $self->clean_oid($options{end}); $options{end} = $self->clean_oid($options{end});
} }
# we use a medium (UDP have a PDU limit. SNMP protcol cant send multiples for one request) # we use a medium (UDP have a PDU limit. SNMP protcol cant send multiples for one request)
# So we need to manage # So we need to manage
# It's for "bulk". We ask 50 next values. If you set 1, it's like a getnext (snmp v1) # It's for "bulk". We ask 50 next values. If you set 1, it's like a getnext (snmp v1)
@ -567,7 +567,7 @@ sub get_table {
$self->{maxrepetitions} =~ /^\d+$/) { $self->{maxrepetitions} =~ /^\d+$/) {
$repeat_count = $self->{maxrepetitions}; $repeat_count = $self->{maxrepetitions};
} }
# Transform asking # Transform asking
if ($options{oid} !~ /(.*)\.(\d+)([\.\s]*)$/) { if ($options{oid} !~ /(.*)\.(\d+)([\.\s]*)$/) {
if ($dont_quit == 1) { if ($dont_quit == 1) {
@ -577,10 +577,10 @@ sub get_table {
$self->{output}->add_option_msg(short_msg => "Method 'get_table': Wrong OID '" . $options{oid} . "'."); $self->{output}->add_option_msg(short_msg => "Method 'get_table': Wrong OID '" . $options{oid} . "'.");
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
my $main_indice = $1 . "." . $2; my $main_indice = $1 . "." . $2;
my $results = {}; my $results = {};
# Quit if base not the same or 'ENDOFMIBVIEW' value # Quit if base not the same or 'ENDOFMIBVIEW' value
my $leave = 1; my $leave = 1;
my $last_oid; my $last_oid;
@ -593,13 +593,13 @@ sub get_table {
while ($leave) { while ($leave) {
$last_oid =~ /(.*)\.(\d+)([\.\s]*)$/; $last_oid =~ /(.*)\.(\d+)([\.\s]*)$/;
my $vb = new SNMP::VarList([$1, $2]); my $vb = new SNMP::VarList([$1, $2]);
if ($self->is_snmpv1() || defined($self->{snmp_force_getnext})) { if ($self->is_snmpv1() || defined($self->{snmp_force_getnext})) {
$self->{session}->getnext($vb); $self->{session}->getnext($vb);
} else { } else {
$self->{session}->getbulk(0, $repeat_count, $vb); $self->{session}->getbulk(0, $repeat_count, $vb);
} }
# Error # Error
if ($self->{session}->{ErrorNum}) { if ($self->{session}->{ErrorNum}) {
# 0 noError Pas d'erreurs. # 0 noError Pas d'erreurs.
@ -614,18 +614,18 @@ sub get_table {
($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == 5 || $self->{session}->{ErrorNum} == -24)) { ($self->{session}->{ErrorNum} == 1 || $self->{session}->{ErrorNum} == 5 || $self->{session}->{ErrorNum} == -24)) {
next if ($self->autoreduce_table(repeat_count => \$repeat_count) == 0); next if ($self->autoreduce_table(repeat_count => \$repeat_count) == 0);
} }
my $msg = 'SNMP Table Request : ' . $self->{session}->{ErrorStr}; my $msg = 'SNMP Table Request : ' . $self->{session}->{ErrorStr};
if ($dont_quit == 0) { if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg); $self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
$self->set_error(error_status => -1, error_msg => $msg); $self->set_error(error_status => -1, error_msg => $msg);
return undef; return undef;
} }
# Manage # Manage
foreach my $entry (@$vb) { foreach my $entry (@$vb) {
if (${$entry}[2] eq 'ENDOFMIBVIEW') { if (${$entry}[2] eq 'ENDOFMIBVIEW') {
@ -633,7 +633,7 @@ sub get_table {
$leave = 0; $leave = 0;
last; last;
} }
# Not in same table # Not in same table
my $complete_oid = ${$entry}[0] . "." . ${$entry}[1]; my $complete_oid = ${$entry}[0] . "." . ${$entry}[1];
if ($complete_oid !~ /^$main_indice\./ || if ($complete_oid !~ /^$main_indice\./ ||
@ -641,17 +641,17 @@ sub get_table {
$leave = 0; $leave = 0;
last; last;
} }
$results->{$complete_oid} = ${$entry}[2]; $results->{$complete_oid} = ${$entry}[2];
$last_oid = $complete_oid; $last_oid = $complete_oid;
} }
} }
if ($nothing_quit == 1 && scalar(keys %$results) == 0) { if ($nothing_quit == 1 && scalar(keys %$results) == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP Table Request: Cant get a single value."); $self->{output}->add_option_msg(short_msg => 'SNMP Table Request: Cant get a single value.');
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
return $results; return $results;
} }
@ -666,26 +666,26 @@ sub set {
foreach my $oid (keys %{$options{oids}}) { foreach my $oid (keys %{$options{oids}}) {
# Get last value # Get last value
next if ($oid !~ /(.*)\.(\d+)([\.\s]*)$/); next if ($oid !~ /(.*)\.(\d+)([\.\s]*)$/);
my $value = $options{oids}->{$oid}->{value}; my $value = $options{oids}->{$oid}->{value};
my $type = $options{oids}->{$oid}->{type}; my $type = $options{oids}->{$oid}->{type};
my ($oid, $instance) = ($1, $2); my ($oid, $instance) = ($1, $2);
push @$vars, [$oid, $instance, $value, $type]; push @$vars, [$oid, $instance, $value, $type];
} }
$self->{session}->set($vars); $self->{session}->set($vars);
if ($self->{session}->{ErrorNum}) { if ($self->{session}->{ErrorNum}) {
# 0 noError Pas d'erreurs. # 0 noError Pas d'erreurs.
# 1 tooBig Reponse de taille trop grande. # 1 tooBig Reponse de taille trop grande.
# 2 noSuchName Variable inexistante. # 2 noSuchName Variable inexistante.
my $msg = 'SNMP SET Request : ' . $self->{session}->{ErrorStr}; my $msg = 'SNMP SET Request : ' . $self->{session}->{ErrorStr};
if ($dont_quit == 0) { if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg); $self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
} }
$self->set_error(error_status => -1, error_msg => $msg); $self->set_error(error_status => -1, error_msg => $msg);
return undef; return undef;
} }
@ -695,7 +695,7 @@ sub set {
sub is_snmpv1 { sub is_snmpv1 {
my ($self) = @_; my ($self) = @_;
if ($self->{snmp_params}->{Version} eq '1') { if ($self->{snmp_params}->{Version} eq '1') {
return 1; return 1;
} }
@ -704,7 +704,7 @@ sub is_snmpv1 {
sub clean_oid { sub clean_oid {
my ($self, $oid) = @_; my ($self, $oid) = @_;
$oid =~ s/\.$//; $oid =~ s/\.$//;
$oid =~ s/^(\d)/\.$1/; $oid =~ s/^(\d)/\.$1/;
return $oid; return $oid;
@ -715,7 +715,7 @@ sub check_oid_up {
my $current_oid = $options{current}; my $current_oid = $options{current};
my $end_oid = $options{end}; my $end_oid = $options{end};
my @current_oid_splitted = split /\./, $current_oid; my @current_oid_splitted = split /\./, $current_oid;
my @end_oid_splitted = split /\./, $end_oid; my @end_oid_splitted = split /\./, $end_oid;
# Skip first value (before first '.' empty) # Skip first value (before first '.' empty)
@ -724,22 +724,22 @@ sub check_oid_up {
return 1; return 1;
} }
} }
return 0; return 0;
} }
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
# $options{option_results} = ref to options result # $options{option_results} = ref to options result
if (!defined($options{option_results}->{host})) { if (!defined($options{option_results}->{host})) {
$self->{output}->add_option_msg(short_msg => "Missing parameter --hostname."); $self->{output}->add_option_msg(short_msg => 'Missing parameter --hostname.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$options{option_results}->{snmp_version} =~ s/^v//; $options{option_results}->{snmp_version} =~ s/^v//;
if ($options{option_results}->{snmp_version} !~ /1|2c|2|3/) { if ($options{option_results}->{snmp_version} !~ /1|2c|2|3/) {
$self->{output}->add_option_msg(short_msg => "Unknown snmp version."); $self->{output}->add_option_msg(short_msg => 'Unknown snmp version.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
@ -755,27 +755,29 @@ sub check_options {
$self->{snmp_autoreduce_divisor} = $1 if ($options{option_results}->{snmp_autoreduce} =~ /(\d+(\.\d+)?)/ && $1 > 1); $self->{snmp_autoreduce_divisor} = $1 if ($options{option_results}->{snmp_autoreduce} =~ /(\d+(\.\d+)?)/ && $1 > 1);
} }
%{$self->{snmp_params}} = (DestHost => $options{option_results}->{host}, %{$self->{snmp_params}} = (
Community => $options{option_results}->{snmp_community}, DestHost => $options{option_results}->{host},
Version => $options{option_results}->{snmp_version}, Community => $options{option_results}->{snmp_community},
RemotePort => $options{option_results}->{snmp_port}, Version => $options{option_results}->{snmp_version},
Retries => 5); RemotePort => $options{option_results}->{snmp_port},
Retries => 5
);
if (defined($options{option_results}->{snmp_timeout}) && $options{option_results}->{snmp_timeout} =~ /^[0-9]+$/) { 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); $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]+$/) { if (defined($options{option_results}->{snmp_retries}) && $options{option_results}->{snmp_retries} =~ /^[0-9]+$/) {
$self->{snmp_params}->{Retries} = $options{option_results}->{snmp_retries}; $self->{snmp_params}->{Retries} = $options{option_results}->{snmp_retries};
} }
if ($options{option_results}->{snmp_version} eq "3") { if ($options{option_results}->{snmp_version} eq '3') {
$self->{snmp_params}->{Context} = $options{option_results}->{snmp_context_name} if (defined($options{option_results}->{snmp_context_name})); $self->{snmp_params}->{Context} = $options{option_results}->{snmp_context_name} if (defined($options{option_results}->{snmp_context_name}));
$self->{snmp_params}->{ContextEngineId} = $options{option_results}->{snmp_context_engine_id} if (defined($options{option_results}->{snmp_context_engine_id})); $self->{snmp_params}->{ContextEngineId} = $options{option_results}->{snmp_context_engine_id} if (defined($options{option_results}->{snmp_context_engine_id}));
$self->{snmp_params}->{SecEngineId} = $options{option_results}->{snmp_security_engine_id} if (defined($options{option_results}->{snmp_security_engine_id})); $self->{snmp_params}->{SecEngineId} = $options{option_results}->{snmp_security_engine_id} if (defined($options{option_results}->{snmp_security_engine_id}));
$self->{snmp_params}->{SecName} = $options{option_results}->{snmp_security_name} if (defined($options{option_results}->{snmp_security_name})); $self->{snmp_params}->{SecName} = $options{option_results}->{snmp_security_name} if (defined($options{option_results}->{snmp_security_name}));
# Certificate SNMPv3. Need net-snmp > 5.6 # Certificate SNMPv3. Need net-snmp > 5.6
if ($options{option_results}->{host} =~ /^(dtls|tls|ssh).*:/) { if ($options{option_results}->{host} =~ /^(dtls|tls|ssh).*:/) {
$self->{snmp_params}->{OurIdentity} = $options{option_results}->{snmp_our_identity} if (defined($options{option_results}->{snmp_our_identity})); $self->{snmp_params}->{OurIdentity} = $options{option_results}->{snmp_our_identity} if (defined($options{option_results}->{snmp_our_identity}));
@ -785,28 +787,27 @@ sub check_options {
$self->{snmp_params}->{SecLevel} = 'authPriv'; $self->{snmp_params}->{SecLevel} = 'authPriv';
return ; return ;
} }
if (!defined($options{option_results}->{snmp_security_name}) || $options{option_results}->{snmp_security_name} eq '') { if (!defined($options{option_results}->{snmp_security_name}) || $options{option_results}->{snmp_security_name} eq '') {
$self->{output}->add_option_msg(short_msg => "Missing parameter Security Name."); $self->{output}->add_option_msg(short_msg => 'Missing parameter Security Name.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
# unauthenticated and unencrypted # unauthenticated and unencrypted
$self->{snmp_params}->{SecLevel} = 'noAuthNoPriv'; $self->{snmp_params}->{SecLevel} = 'noAuthNoPriv';
my $user_activate = 0; my $user_activate = 0;
if (defined($options{option_results}->{snmp_auth_passphrase}) && $options{option_results}->{snmp_auth_passphrase} ne '') { if (defined($options{option_results}->{snmp_auth_passphrase}) && $options{option_results}->{snmp_auth_passphrase} ne '') {
if (!defined($options{option_results}->{snmp_auth_protocol})) { if (!defined($options{option_results}->{snmp_auth_protocol})) {
$self->{output}->add_option_msg(short_msg => "Missing parameter authenticate protocol."); $self->{output}->add_option_msg(short_msg => 'Missing parameter authenticate protocol.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$options{option_results}->{snmp_auth_protocol} = uc($options{option_results}->{snmp_auth_protocol}); $options{option_results}->{snmp_auth_protocol} = uc($options{option_results}->{snmp_auth_protocol});
if ($options{option_results}->{snmp_auth_protocol} ne "MD5" && $options{option_results}->{snmp_auth_protocol} ne "SHA") { if ($options{option_results}->{snmp_auth_protocol} ne 'MD5' && $options{option_results}->{snmp_auth_protocol} ne 'SHA') {
$self->{output}->add_option_msg(short_msg => "Wrong authentication protocol. Must be MD5 or SHA."); $self->{output}->add_option_msg(short_msg => 'Wrong authentication protocol. Must be MD5 or SHA.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{snmp_params}->{SecLevel} = 'authNoPriv'; $self->{snmp_params}->{SecLevel} = 'authNoPriv';
$self->{snmp_params}->{AuthProto} = $options{option_results}->{snmp_auth_protocol}; $self->{snmp_params}->{AuthProto} = $options{option_results}->{snmp_auth_protocol};
$self->{snmp_params}->{AuthPass} = $options{option_results}->{snmp_auth_passphrase}; $self->{snmp_params}->{AuthPass} = $options{option_results}->{snmp_auth_passphrase};
@ -815,17 +816,17 @@ sub check_options {
if (defined($options{option_results}->{snmp_priv_passphrase}) && $options{option_results}->{snmp_priv_passphrase} ne '') { if (defined($options{option_results}->{snmp_priv_passphrase}) && $options{option_results}->{snmp_priv_passphrase} ne '') {
if (!defined($options{option_results}->{snmp_priv_protocol})) { if (!defined($options{option_results}->{snmp_priv_protocol})) {
$self->{output}->add_option_msg(short_msg => "Missing parameter privacy protocol."); $self->{output}->add_option_msg(short_msg => 'Missing parameter privacy protocol.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$options{option_results}->{snmp_priv_protocol} = uc($options{option_results}->{snmp_priv_protocol}); $options{option_results}->{snmp_priv_protocol} = uc($options{option_results}->{snmp_priv_protocol});
if ($options{option_results}->{snmp_priv_protocol} ne 'DES' && $options{option_results}->{snmp_priv_protocol} ne 'AES') { if ($options{option_results}->{snmp_priv_protocol} ne 'DES' && $options{option_results}->{snmp_priv_protocol} ne 'AES') {
$self->{output}->add_option_msg(short_msg => "Wrong privacy protocol. Must be DES or AES."); $self->{output}->add_option_msg(short_msg => 'Wrong privacy protocol. Must be DES or AES.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if ($user_activate == 0) { if ($user_activate == 0) {
$self->{output}->add_option_msg(short_msg => "Cannot use snmp v3 privacy option without snmp v3 authentification options."); $self->{output}->add_option_msg(short_msg => 'Cannot use snmp v3 privacy option without snmp v3 authentification options.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{snmp_params}->{SecLevel} = 'authPriv'; $self->{snmp_params}->{SecLevel} = 'authPriv';
@ -837,7 +838,7 @@ sub check_options {
sub set_snmp_connect_params { sub set_snmp_connect_params {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach (keys %options) { foreach (keys %options) {
$self->{snmp_params}->{$_} = $options{$_}; $self->{snmp_params}->{$_} = $options{$_};
} }
@ -845,7 +846,7 @@ sub set_snmp_connect_params {
sub set_snmp_params { sub set_snmp_params {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach (keys %options) { foreach (keys %options) {
$self->{$_} = $options{$_}; $self->{$_} = $options{$_};
} }
@ -855,20 +856,20 @@ sub set_error {
my ($self, %options) = @_; my ($self, %options) = @_;
# $options{error_msg} = string error # $options{error_msg} = string error
# $options{error_status} = integer status # $options{error_status} = integer status
$self->{error_status} = defined($options{error_status}) ? $options{error_status} : 0; $self->{error_status} = defined($options{error_status}) ? $options{error_status} : 0;
$self->{error_msg} = defined($options{error_msg}) ? $options{error_msg} : undef; $self->{error_msg} = defined($options{error_msg}) ? $options{error_msg} : undef;
} }
sub error_status { sub error_status {
my ($self) = @_; my ($self) = @_;
return $self->{error_status}; return $self->{error_status};
} }
sub error { sub error {
my ($self) = @_; my ($self) = @_;
return $self->{error_msg}; return $self->{error_msg};
} }
@ -888,7 +889,7 @@ sub get_port {
sub map_instance { sub map_instance {
my ($self, %options) = @_; my ($self, %options) = @_;
my $results = {}; my $results = {};
my $instance = ''; my $instance = '';
$instance = '.' . $options{instance} if (defined($options{instance})); $instance = '.' . $options{instance} if (defined($options{instance}));
@ -904,14 +905,14 @@ sub map_instance {
} else { } else {
$results->{$name} = defined($options{default}) ? $options{default} : undef; $results->{$name} = defined($options{default}) ? $options{default} : undef;
} }
if (defined($options{mapping}->{$name}->{map})) { if (defined($options{mapping}->{$name}->{map})) {
if (defined($results->{$name})) { if (defined($results->{$name})) {
$results->{$name} = defined($options{mapping}->{$name}->{map}->{$results->{$name}}) ? $options{mapping}->{$name}->{map}->{$results->{$name}} : (defined($options{default}) ? $options{default} : 'unknown'); $results->{$name} = defined($options{mapping}->{$name}->{map}->{$results->{$name}}) ? $options{mapping}->{$name}->{map}->{$results->{$name}} : (defined($options{default}) ? $options{default} : 'unknown');
} }
} }
} }
return $results; return $results;
} }
@ -923,14 +924,13 @@ sub oid_lex_sort {
} }
return map { $_->[0] } return map { $_->[0] }
sort { $a->[1] cmp $b->[1] } sort { $a->[1] cmp $b->[1] }
map map {
{ my $oid = $_;
my $oid = $_; $oid =~ s/^\.//;
$oid =~ s/^\.//; $oid =~ s/ /\.0/g;
$oid =~ s/ /\.0/g; [$_, pack 'N*', split m/\./, $oid]
[$_, pack 'N*', split m/\./, $oid] } @_;
} @_;
} }
1; 1;