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