Home > Archive > PERL Beginners > January 2006 > Passing variables from one subroutine to another....
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Passing variables from one subroutine to another....
|
|
|
| Hello,
I have no idea how to do this, but I have taken the code here:
http://search.cpan.org/~dtown/Net-S...le1.3.6.1.2.1.1
(under the section "3. Non-blocking SNMPv2c get-bulk-request for
ifTable")
and modified it to what I have below so that instead of one sub
function, there are now two, one calling the other? The problem is, I
don't know how to get the code in snmp_collect to pass the variables it
was originally passed ($host_id, $address, $oid_string) to table_cb.
Can someone please help me with this? I am just plain :(
-Regan
#!/usr/bin/perl
use strict;
use Net::SNMP qw(:snmp);
require './db.pl';
sub snmp_collect
{
my $host_id = shift;
my $address = shift;
my $oid_string = shift;
my ($session, $error) = Net::SNMP->session(
-version => 'snmpv2c',
-nonblocking => 1,
-hostname => $address,
-community => shift || 'public',
-port => shift || 161
);
if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit 1;
}
printf $oid_string;
my $result = $session->get_bulk_request(
-callback => [\&table_cb, {}],
-maxrepetitions => 10,
-varbindlist => [$oid_string]
);
printf $oid_string;
if (!defined($result)) {
printf("ERROR (snmp_collect #2): %s.\n", $session->error);
$session->close;
exit 1;
}
snmp_dispatcher();
$session->close;
exit 0;
}
sub table_cb
{
printf("Host ID: %s Address: %s OID: %s", $host_id, $address,
$oid_string);
my ($session, $table) = @_;
if (!defined($session->var_bind_list)) {
printf("ERROR (table_cb): %s\n", $session->error);
} else {
printf("Else Area");
my $time = time();
# Loop through each of the OIDs in the response and assign
# the key/value pairs to the anonymous hash that is passed
# to the callback. Make sure that we are still in the table
# before assigning the key/values.
my $next;
foreach my $oid
(oid_lex_sort(keys(%{$session->var_bind_list}))) {
if (!oid_base_match($main::oid_string, $oid)) {
$next = undef;
last;
}
$next = $oid;
$table->{$oid} = $session->var_bind_list->{$oid};
}
# If $next is defined we need to send another request
# to get more of the table.
if (defined($next)) {
my $result = $session->get_bulk_request(
-callback => [\&table_cb, $table],
-maxrepetitions => 10,
-varbindlist => [$next]
);
if (!defined($result)) {
printf("ERROR: %s\n", $session->error);
}
} else {
# We are no longer in the table, so print the results.
foreach my $oid (oid_lex_sort(keys(%{$table}))) {
#prepare and execute SQL statement
$DBI::sqlstatement="INSERT INTO results
(created,node,oid,value) VALUES
('$time','$host_id','$oid_string','$tabl
e->{$oid}')";
$DBI::sth = $DBI::dbh->prepare($DBI::sqlstatement);
$DBI::sth->execute ||
die "Could not execute SQL statement ... maybe
invalid?";
printf("%s => %s\n", $oid, $table->{$oid});
}
}
}
}
snmp_collect ('1','localhost','1.3.6.1.2.1.1');
| |
| Paul Lalli 2006-01-27, 7:56 am |
| Regan wrote:
> I have no idea how to do this, but I have taken the code here:
>
> http://search.cpan.org/~dtown/Net-S...le1.3.6.1.2.1.1
>
> (under the section "3. Non-blocking SNMPv2c get-bulk-request for
> ifTable")
>
> and modified it to what I have below so that instead of one sub
> function, there are now two, one calling the other? The problem is, I
> don't know how to get the code in snmp_collect to pass the variables it
> was originally passed ($host_id, $address, $oid_string) to table_cb.
>
> Can someone please help me with this? I am just plain :(
Did you consider reading the documentation for the module you're using?
I have never heard of this module, let alone used it, but within about
10 seconds of searching for "callback", I found this:
----------------------------------------------------------------
Callback
Most methods associated with a non-blocking object have an optional
named argument called -callback. The -callback argument expects a
reference to a subroutine or to an array whose first element must be a
reference to a subroutine. The subroutine defined by the -callback
option is executed when a response to a SNMP message is received, an
error condition has occurred, or the number of retries for the message
has been exceeded.
When the -callback argument only contains a subroutine reference,
the subroutine is evaluated passing a reference to the original
Net::SNMP object as the only parameter. If the -callback argument was
defined as an array reference, all elements in the array are passed to
subroutine after the reference to the Net::SNMP object. The first
element, which is required to be a reference to a subroutine, is
removed before the remaining arguments are passed to that subroutine.
------------------------------------------------------------------
So instead of:
-callback => [\&table_cb, {}],
Make it:
-callback => [[\&table_cb, $host_id, $address,
$oid_string] , {}],
Paul Lalli
|
|
|
|
|