SNMP module to query net-snmp

M

milaus

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/SNMP-5.0301001/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
 
D

Dr.Ruud

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
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 address removed)
with an archive at
http://sourceforge.net/mailarchive/forum.php?forum_id=4959
 
M

milaus

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
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 address removed)
with an archive at
http://sourceforge.net/mailarchive/forum.php?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
 
D

Dr.Ruud

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" ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top