SNMP module to query net-snmp

Discussion in 'Perl Misc' started by milaus, Aug 31, 2006.

  1. milaus

    milaus Guest

    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
    milaus, Aug 31, 2006
    #1
    1. Advertising

  2. milaus

    Dr.Ruud Guest

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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Aug 31, 2006
    #2
    1. Advertising

  3. milaus

    milaus Guest

    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
    > 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
    milaus, Aug 31, 2006
    #3
  4. milaus

    Dr.Ruud Guest

    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."
    Dr.Ruud, Aug 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    373
  2. Aldo Ceccarelli
    Replies:
    7
    Views:
    368
    Aldo Ceccarelli
    Apr 19, 2011
  3. Replies:
    1
    Views:
    134
    Fabian Pilkowski
    Apr 30, 2005
  4. milaus

    please helpme: Net-SNMP-5.2.0 module

    milaus, Aug 29, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    108
    Dr.Ruud
    Aug 29, 2006
  5. karthikd
    Replies:
    2
    Views:
    150
    karthikd
    Jun 18, 2009
Loading...

Share This Page