Whops, copied the wrong version, Try this:

Note, test 1 and 2 work correctly, test 3 does not, but it is using the same
instance of the object as test 2.

Here are the snmpget commands I am using and their output:
[root@elara trunk]# snmpget -v 1 -c debug localhost .1.3.6.1.4.1.999999.1
SNMPv2-SMI::enterprises.999999.1 = STRING: "Package OID Test Successful"

[root@elara trunk]# snmpget -v 1 -c debug localhost .1.3.6.1.4.1.999999.2
SNMPv2-SMI::enterprises.999999.2 = STRING: "Object Method Test Successful"

[root@elara trunk]# snmpget -v 1 -c debug localhost .1.3.6.1.4.1.999999.3
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: SNMPv2-SMI::enterprises.999999.3


#!/usr/bin/perl
use common::sense;

package Test;
use NetSNMP::ASN     (':all');

sub new {
    my $obj_class       = shift;
    my $root_oid        = shift;

    my $class = ref $obj_class || $obj_class;
    my $self = {};

    bless $self, $class;
    return $self;
}

sub monitor {
    use Data::Dumper;
    print STDOUT Dumper @_;
    my ($self, $handler, $registration_info, $request_info, $requests) = @_;
    my $request;

    for($request = $requests; $request; $request = $request->next()) {
        $request->setValue(ASN_OCTET_STR, "Object Method Test Successful");
    }
}


package main;


# Global Vars:
our $agent;

# Private vars
my $root_oid = '.1.3.6.1.4.1.999999';
my $running;
# set to 1 to get debugging information
my $debugging = 0;
# since we're embedded, set this to 0
my $subagent = 0;

use NetSNMP::OID     (':all');
use NetSNMP::agent     (':all');
use NetSNMP::ASN     (':all');

#Test 1
my $reg_oid1 = new NetSNMP::OID($root_oid . ".1");
$agent->register('test1', $reg_oid1, \&test1);

sub test1 {
    use Data::Dumper;
    print STDOUT Dumper @_;

    my ($handler, $registration_info, $request_info, $requests) = @_;
    my $request;

    for($request = $requests; $request; $request = $request->next()) {
        $request->setValue(ASN_OCTET_STR, "Package OID Test Successful");
    }
}

# Test 2
my $test = new Test;
my $reg_oid2 = new NetSNMP::OID($root_oid . ".2");

# this works
$agent->register('test2', $reg_oid2, \&test2);

sub test2 {
    use Data::Dumper;
    print STDOUT @_;
    $test->monitor(@_);
}

#Test 3:

#this does not work
my $reg_oid3 = new NetSNMP::OID($root_oid . ".3");
$agent->register('test3', $reg_oid3, $test->monitor);

On Sun, Sep 18, 2011 at 4:00 PM, Jon Jon <[email protected]>wrote:

> Hello All,
>
> To further expound on my previous post, what I am seeing is the required
> variables are not being passed to the object method like they are to the
> subroutine in the same package.  (See my example below)
>
> Is there an issue with my code or is there a bug in NetSNMP?
>
> #!/usr/bin/perl
> #
> use common::sense;
>
> package Test;
> use NetSNMP::ASN     (':all');
>
> sub new {
>     my $obj_class       = shift;
>     my $root_oid        = shift;
>
>     my $class = ref $obj_class || $obj_class;
>     my $self = {};
>
>     bless $self, $class;
>     return $self;
> }
>
> sub monitor {
>     use Data::Dumper;
>     print STDOUT Dumper @_;
>     my ($self, $handler, $registration_info, $request_info, $requests) =
> @_;
>     my $request;
>
>     for($request = $requests; $request; $request = $request->next()) {
>         $request->setValue(ASN_OCTET_STR, "Object Method Test Successful");
>     }
> }
>
>
> package main;
>
>
> # Global Vars:
> our $agent;
>
> # Private vars
> my $root_oid = '.1.3.6.1.4.1.999999';
> my $running;
> # set to 1 to get debugging information
> my $debugging = 0;
> # since we're embedded, set this to 0
> my $subagent = 0;
>
>
> BEGIN {
>     print STDERR "starting mail_queue_monitor.pl\n";
> }
>
> use NetSNMP::OID     (':all');
> use NetSNMP::agent     (':all');
> use NetSNMP::ASN     (':all');
>
> my $reg_oid1 = new NetSNMP::OID($root_oid . ".1");
> $agent->register('test1', $reg_oid3, \&test1);
>
> sub test1 {
>     use Data::Dumper;
>     print STDOUT Dumper @_;
>
>     my ($handler, $registration_info, $request_info, $requests) = @_;
>     my $request;
>
>     for($request = $requests; $request; $request = $request->next()) {
>         $request->setValue(ASN_OCTET_STR, "Object Method Test Successful");
>     }
> }
>
>
> my $test = new Test;
> my $reg_oid2 = new NetSNMP::OID($root_oid . ".2");
>
> # this works
> $agent->register('test2', $reg_oid3, \&test2);
>
> # this does not work: uncomment one line below to reproduce my error
> # $agent->register('test2', $reg_oid3, \$test->monitor());
> # $agent->register('test2', $reg_oid3, $test->monitor);
>
> sub test2 {
>     use Data::Dumper;
>     print STDOUT @_;
>     $test->monitor(@_);
> }
>
> On Sun, Sep 18, 2011 at 12:48 PM, Jon Jon 
> <[email protected]>wrote:
>
>> Hello Dave,
>>
>> Thanks for your response, I think perhaps there is an issue with the way
>> in which I am registering my plugin modules.  After looking over you
>> response and comparing my original version vs my new version, I don't think
>> I am even getting that far in my program execution.
>>
>> When I execute:
>>     snmpget -v 1 -c debug localhost .1.3.6.1.4.1.38007.0.0.0
>>
>> I receive the error:
>>     Error in packet
>>     Reason: (noSuchName) There is no such variable name in this MIB.
>>     Failed object: SNMPv2-SMI::enterprises.38007.0.0.0
>>
>> Even though it is creating the new OID object:
>>
>>     RegOID: enterprises.38007.0.0.0
>>
>>     Dumper: $VAR1 = bless( {
>>                  'plugin_oid' => '0.0.0,
>>                  'reg_oid' => bless( {
>>                                      'oidptr' => bless( do{\(my $o =
>> '47058369330256')}, 'netsnmp_oidPtr' )
>>                                    }, 'NetSNMP::OID' ),
>>                  'name' => 'Plugin',
>>                  'root_oid' => '.1.3.6.1.4.1.38007.',
>>                  'full_name' => 'Plugin',
>>                  'full_oid' => '.1.3.6.1.4.1.38007.0.0.0'
>>                }, 'Plugin' );
>>
>> Here I've written a stripped down version that skips loading the plugins,
>> but instead creates the monitor as an object:
>>
>> #!/usr/bin/perl
>>
>> use common::sense;
>> use NetSNMP::OID     (':all');
>> use NetSNMP::agent     (':all');
>> use NetSNMP::ASN     (':all');
>>
>> our $agent = new NetSNMP::agent('dont_init_agent' => 1,
>>                 'dont_init_lib' => 1);
>>
>>
>> package Plugin;
>> use NetSNMP::ASN     (':all');
>>
>> sub new {
>>     my $obj_class       = shift;
>>     my $root_oid        = shift;
>>
>>     my $class = ref $obj_class || $obj_class;
>>     my ($name) = $class =~ /::Plugin::(.*)/;
>>
>>     my $self = {
>>         name        => 'Plugin',
>>         full_name   => $class,
>>         root_oid    => '.1.3.6.1.4.1.38007.',
>>         plugin_oid  => '',
>>         full_oid    => '',
>>         reg_oid     => '',
>>     };
>>
>>     bless $self, $class;
>>
>>     # create accessor methods for defined parameters
>>     for my $datum (keys %{$self}) {
>>         no strict "refs";
>>         *$datum = sub {
>>             shift; # XXX: ignore calling class/object
>>             $self->{$datum} = shift if @_;
>>             return $self->{$datum};
>>         };
>>     }
>>
>>     $self->set_oid();
>>
>>     return $self;
>> }
>>
>> sub set_plugin_oid {'0.0.0'};
>>
>> # Set the OID using plugin defined OID.
>> sub set_oid {
>>     my $self = shift;
>>     $self->plugin_oid($self->set_plugin_oid);
>>     $self->full_oid($self->root_oid . $self->plugin_oid);
>> }
>>
>> sub monitor {
>>     use Data::Dumper;
>>     print STDERR "Dumper: " . Dumper @_;
>>
>>     my ($class, $handler, $registration_info, $request_info, $requests) =
>> @_;
>>      my $request;
>>
>>     for($request = $requests; $request; $request = $request->next()) {
>>         print STDERR "In the handler";
>>         $request->setValue(ASN_OCTET_STR, "Test Successful");
>>     }
>>
>> }
>>
>> package main;
>>
>> my $plugin = Plugin->new();
>>
>> my $reg_oid = new NetSNMP::OID($plugin->full_oid);
>> $plugin->reg_oid($reg_oid);
>> print STDERR "reg_oid: " . $plugin->reg_oid . " Root: " .
>> $plugin->full_oid . "\n";
>>
>> $agent->register($plugin->name, $plugin->reg_oid, $plugin->monitor);
>> 1;
>> __END__
>>
>> Am I just totally barking up the wrong tree here?
>>
>> On Thu, Sep 15, 2011 at 2:16 AM, Dave Shield 
>> <[email protected]>wrote:
>>
>>> On 14 September 2011 18:30, Jon Jon <[email protected]>
>>> wrote:
>>> >     my ($handler, $registration_info, $request_info, $requests) = @_;
>>> >
>>> >     my $this_request = $request->next();
>>>
>>>
>>>
>>> > When run I get the following error:
>>> > Can't call method "next" on an undefined value at
>>> > /root/snmp_monitor/trunk/SNMPMonitor/Plugin/Atest.pm line 24.
>>>
>>> That sounds right.
>>> The handler routine is passed a variable that you have called
>>> 'requests'   (plural)
>>> but you are trying to dereference a variable called 'request'  (singular)
>>> This second variable has not been initialised, so will indeed be
>>> undefined.
>>>
>>> The normal code framework would look something like
>>>
>>>     my ($handler, $registration_info, $request_info, $requests) = @_;
>>>
>>>      for ( $this_request = $requests;   $this_request;
>>> $this_request=$this_request->next()) {
>>>         # do something with $this_request
>>>     }
>>>
>>> to handle the possibility of being passed several varbinds to process in
>>> one go.
>>>
>>> Your example code looks to be trying to set a value for the *second*
>>> varbind
>>> in the request list,  which will obviously fail if there is only one
>>> (the most common case)
>>>
>>> Dave
>>>
>>
>>
>
------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to