http://www.perlmonks.org?node_id=1007607

MacScissor has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks! I have the following script:
#!/usr/bin/perl use strict; use warnings; use Net::SNMP; my ($session, $error) = Net::SNMP->session(Hostname => '172.25.0.5', Community => 'testcommunity', port => 161) or die "Session: $!\015\012"; my $ram = $session->get_request("1.3.6.1.2.1.25.2.2.0") + or die "get_request ram: $!\015\012"; my $uptime = $session->get_request("1.3.6.1.2.1.25.1.1.0") +or die "get_request uptime: $!\015\012"; my $drive1 = $session->get_request("1.3.6.1.2.1.25.2.3.1.6.1") + or die "get_request drive1: $!\015\012"; my $processes = $session->get_request("1.3.6.1.2.1.25.1.6.0") + or die "get_request processes: $!\015\12"; $session->close; print "RAM: ".$ram->{"1.3.6.1.2.1.25.2.2.0"}."\015\12"; print "Uptime: ".$uptime->{"1.3.6.1.2.1.25.1.1.0"}."\015\012"; print "Drive 1: ".$drive1->{"1.3.6.1.2.1.25.2.3.1.6.1"}."\015\012"; print "Processes: ".$processes->{"1.3.6.1.2.1.25.1.6.0"}."\015\012";
The script is executed under linux, the target host is a windows machine. There are no errors, but the output is just
RAM: Uptime: Drive 1: Processes:
When I try the same script under windows (with target host "localhost") the values are returned correctly. Where is my mistake? Thanks in advance!

Replies are listed 'Best First'.
Re: snmp script with perl
by blue_cowdawg (Monsignor) on Dec 06, 2012 at 15:54 UTC

    Check the community settings and ACL settings for the snmpd on the target host. I've seen that trip folks up before.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: snmp script with perl
by greengaroo (Hermit) on Dec 06, 2012 at 15:38 UTC

    Try use Data::Dumper; then do print Dumper $ram, $uptime, $drive1, $processes;

    Try that first to see if your structures have any content at all, maybe this will help.

    Testing never proves the absence of faults, it only shows their presence.
      Hello and thanks for your reply! I added the lines to the code, now the output is
      Aktive Prozesse: $VAR1 = { '1.3.6.1.2.1.25.2.2.0' => '' }; $VAR2 = { '1.3.6.1.2.1.25.1.1.0' => '' }; $VAR3 = { '1.3.6.1.2.1.25.2.3.1.6.1' => '' }; $VAR4 = { '1.3.6.1.2.1.25.1.6.0' => '' };

        OK your hashref values are empty so that explains why your output is empty. Also if you print the string "\015\12", remember that special characters such as \0 and \1 are interpreted in double quotes, so if you really want to print that string, put it in single quotes.

        Testing never proves the absence of faults, it only shows their presence.
Re: snmp script with perl
by RichardK (Parson) on Dec 06, 2012 at 16:08 UTC

    have you got the same version of Net::SNMP on both machines ?

    Your get_request() call doesn't look like the one in the Net::SNMP pod, so maybe some version problem?

    Does error() return anything useful?

    updated! I really meant get_request not get_session, sorry about that.

Re: snmp script with perl
by FloydATC (Deacon) on Dec 06, 2012 at 19:26 UTC
    Use the snmpwalk tool to verify that the SNMP daemon returns anything for those OIDs. The standard SNMP daemon shipped with Windows is missing a lot of important metrics. Consider installing another one, like http://www.snmp-informant.com/

    -- Time flies when you don't know what you're doing