Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > SNMP module to query net-snmp

Reply
Thread Tools

SNMP module to query net-snmp

 
 
milaus
Guest
Posts: n/a
 
      08-31-2006
Hi all,
I'm writing a perl script to query linux snmpd daemon (net-snmp) and I'm
using SNMP module "The Perl5 'SNMP' Extension Module for the Net-SNMP
SNMP package".
I tried to start with very simple script, using the examples as
starting-point
(http://search.cpan.org/src/HARDAKER/...1001/examples/)
But I can't figure out why my script doesn't work...

This are the script and its output:

#!/usr/bin/perl
use strict;
use warnings;
use SNMP;
my $host = '10.102.64.111';
my $community = 'public';
{
my $sess = new SNMP::Session (DestHost => $host,
Community => $community,
Retries => 1) || die "session doesn't work...\n";
my $oid = ".1.3.6.1.2.1.4.1.0";
my $cb = "3";
my ($result) = $sess->get(["$oid", "$cb"]) || 'get doesn\'t
work...';
print "result: $result\n";
}
### end of example-snmp.pl ###

[root@wi064111 ~]# ./example-snmp.pl
result: get doesn't work...

Instead, if I invoke 'snmpget' from the command line everything works
correctly ('snmpget' is the client side command of net-snmp package).

[root@wi064111 ~]# snmpget -c public -v 1 10.102.64.111
..1.3.6.1.2.1.4.1.0 IP-MIB::ipForwarding.0 = INTEGER: notForwarding(2)

Any ideas?
Thanks,
Marco
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      08-31-2006
milaus schreef:

> my ($result) = $sess->get(["$oid", "$cb"]) || 'get doesn\'t
> work...';


I checked the SNMP-documentation, and I don't think that ["$oid", "$cb"]
is one of the "Acceptable variable fomats".
Maybe you need to use SNMP::VarList. Read also `perldoc -q always`.


The "|| 'get doesn\'t work...'" looks strange, I would change that to

or die "get doesn't work: ". $sess->ErrorStr ;


While testing, I would also do an $SNMP::verbose = 1 or $SNMP::debugging
= 1 at the start.


In the trouble shooting section of `perldoc SNMP` it says:
"If you cannot resolve the problem you can post to
news:comp.lang.perl.modules or net-snmp-users(AT)lists.sourceforge.net
please give sufficient information to analyze the problem (OS type,
versions for OS/Perl/UCD/compiler, complete error output, etc.)"


So there is a mailing list http://www.velocityreviews.com/forums/(E-Mail Removed)
with an archive at
http://sourceforge.net/mailarchive/f...?forum_id=4959

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
 
 
 
milaus
Guest
Posts: n/a
 
      08-31-2006
Il Thu, 31 Aug 2006 14:47:39 +0200, Dr.Ruud ha scritto:

> milaus schreef:
>
>> my ($result) = $sess->get(["$oid", "$cb"]) || 'get doesn\'t
>> work...';

>
> I checked the SNMP-documentation, and I don't think that ["$oid", "$cb"]
> is one of the "Acceptable variable fomats".
> Maybe you need to use SNMP::VarList. Read also `perldoc -q always`.
>
>
> The "|| 'get doesn\'t work...'" looks strange, I would change that to
>
> or die "get doesn't work: ". $sess->ErrorStr ;
>
>
> While testing, I would also do an $SNMP::verbose = 1 or $SNMP::debugging
> = 1 at the start.
>
>
> In the trouble shooting section of `perldoc SNMP` it says:
> "If you cannot resolve the problem you can post to
> news:comp.lang.perl.modules or net-snmp-users(AT)lists.sourceforge.net
> please give sufficient information to analyze the problem (OS type,
> versions for OS/Perl/UCD/compiler, complete error output, etc.)"
>
>
> So there is a mailing list (E-Mail Removed)
> with an archive at
> http://sourceforge.net/mailarchive/f...?forum_id=4959


Thank You for your answer and advices,
I'm new with perl and I don't know a lot of thing and off course I should
spend more time reading documentation... but, as you can imagine, when
everything you read is new you can't catch any detail as if you were
already used with the topic.
Anyway, I solved the problem, the main point was that "Version" field has
to be specified when calling "new SNMP::Session(DestHost... ". But in
official CPAN snmp module example, Version field was not used!! This is
the example on CPAN:
...
$sess = new SNMP::Session(DestHost => localhost, Community => public);
...

Only to be complete, my working little example is:

[root@wi064111 ~]# cat example-snmp.pl
#!/usr/bin/perl
use strict;
use warnings;
use SNMP;
my $host = $ARGV[0];
my $community = $ARGV[1];
my $OID = $ARGV[2];
my $porta = $ARGV[3];
my $versione = '2c';
my $sess = new SNMP::Session (DestHost => $host, Community => $community, Version => '2c', RemotePort => $porta );
my $val = $sess->get($OID);
print $val . "\n";

And this is the output, looking for ipForwarding parameter:
[root@wi064111 ~]# ./example-snmp.pl wi064111 public 1.3.6.1.2.1.4.1
2

Or, quering JBOSS-4.0.4.GA snmp agent looking for VM freeMemory:
[root@wi064111 ~]# ./example-snmp.pl wi064111 q3lrcdjb 1.2.3.4.1.2 11212
90081120

Bye,
Marco
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      08-31-2006
milaus schreef:

> my $host = $ARGV[0];
> my $community = $ARGV[1];
> my $OID = $ARGV[2];
> my $porta = $ARGV[3];


Lined-up code is clearer:

my $host = $ARGV[0] ;
my $community = $ARGV[1] ;
my $OID = $ARGV[2] ;
my $porta = $ARGV[3] ;

Alternative:

my ($host, $community, $OID, $porta) = @ARGV ;


> my $versione = '2c';


You don't seem to be using that one.


> print $val . "\n";


Alternative:

print "$val\n" ;

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me to build an SNMP query ProgDario Cisco 2 05-06-2005 02:35 PM
SNMP query for installed DRAM davidabecker@yahoo.com Cisco 1 01-26-2005 09:06 AM
SNMP query - config change uzak Cisco 2 08-23-2004 08:32 AM
Re: snmp query for CAM count in Cisco 6509 sammis Cisco 0 04-23-2004 09:10 PM
SNMP - Cisco - SNMP news.easynews.com Cisco 0 03-04-2004 10:44 PM



Advertisments