8

I've been working on a custom SNMP Mib and I've come up against a wall while trying to get an agent to return the proper data.

MIB (validated by running smilint -l 6):

IDB-MIB DEFINITIONS ::= BEGIN

IMPORTS
        MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises
            FROM SNMPv2-SMI
        MODULE-COMPLIANCE, OBJECT-GROUP FROM SNMPv2-CONF;

idb MODULE-IDENTITY
    LAST-UPDATED   "201307300000Z" -- Midnight 30 July 2013
    ORGANIZATION "*********"
    CONTACT-INFO "email: *******"
    DESCRIPTION "description"
    REVISION "201307300000Z" -- Midnight 29 July 2013
    DESCRIPTION "First Draft"
::= { enterprises 42134 }

iDBCompliance MODULE-COMPLIANCE
    STATUS current
    DESCRIPTION
        "Compliance statement for iDB"
    MODULE
        GROUP testGroup
        DESCRIPTION
            "This group is a test group"
::= {idb 1}

test2 OBJECT-TYPE
 SYNTAX         Integer32
 UNITS          "tests"
 MAX-ACCESS     read-write
 STATUS         current
 DESCRIPTION
        "A test object"
 DEFVAL { 5 }
 ::= { idb 3 }

testGroup OBJECT-GROUP
      OBJECTS {
          test2
      }
      STATUS current
      DESCRIPTION "all test objects"
::= { idb 2 }

END

Agent file:

#!/usr/bin/perl

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

sub myhandler {
    my  ($handler, $registration_info, $request_info, $requests) = @_;
    print "Handling request\n";
    for ($request = $requests; $request; $request = $request->next()) {
        #
        #  Work through the list of varbinds
        #
        my $oid = $request->getOID();
        print "Got request for oid $oi\n";
        if ($request_info->getMode() == MODE_GET) {
            if ($oid == new NetSNMP::OID($rootOID . ".3")) {
                $request->setValue(ASN_INTEGER, 2);
            }
        }
    }
}

{
    $subagent = 0;

    print "Running new agent\n";
    my $rootOID = ".1.3.6.1.4.1.42134";
    my $regoid = new NetSNMP::OID($rootOID);
    if (!$agent) {
        $agent = new NetSNMP::agent('Name'=>'my_agent_name','AgentX' => 1);
        $subagent = 1;
        print "Starting subagent\n";
    }
    print "Registering agent\n";
    $agent->register("my_agent_name", $regoid, \&myhandler);
    print "Agent registered\n";

    if ($subagent) {
        $SIG{'INT'} = \&shut_it_down;
        $SIG{'QUIT'} = \&shut_it_down;
        $running = 1;

        while ($running) {
            $agent->agent_check_and_process(1);
        }

        $agent->shutdown();
    }
}

sub shut_it_down() {
    $running = 0;
    print "Shutting down agent\n";
}

When I run the agent I get the following:

Running new agent
Starting subagent!
Registering agent with oid idb
Agent registered

So I know that much is working. However when I run the following command:

snmpget -v 1 -c mycommunity localhost:161 test2.0

I get this error message:

Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: IDB-MIB::test2.0

I know from snmptranslate that the mib file is set correctly. I have even looked through the debug for snmpget (using -DALL) to make sure that the mib is being loaded and parsed correctly.

So my question is: Why is my subagent not being passed the request?

Update:

I've been told by @EhevuTov that my MIB file is not valid, however smilint does not report any issues and running snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3.0 does report the NAME of the object (IDB-MIB::test2.0) correctly, but does not find any data for it.

I am getting IDB-MIB::test2 = No Such Object available on this agent at this OID, which makes me think that my agent is not registering properly, however it's not throwing any errors.

Update 2:

I've been fiddling around with the agent code a bit. Based on the CPAN documentation (http://metacpan.org/pod/NetSNMP::agent), it looks like the $agent->register function call is supposed to return 0 if successful. So I checked the return code and got this:

Agent registered. Result: NetSNMP::agent::netsnmp_handler_registration=SCALAR(0x201e688)

Printing it out using Data::Dumper results in:

$VAR1 = bless( do{\(my $o = 34434624)}, 'NetSNMP::agent::netsnmp_handler_registration' );

I vaguely understand what bless does, but even so, I have no idea what this result is supposed to mean. So I'm starting to think that the agent is wrong somehow. Does anyone know how to debug these agents? Is there somewhere I can look to see if it's getting loaded properly into the master snmpd?

szabgab
  • 6,202
  • 11
  • 50
  • 64
  • It doesn't look like you have a proper MIB. – EhevuTov Aug 09 '13 at 20:27
  • Ah, typo. Fixed that and it still doesn't seem to be working (I ran it through a mib checker, http://www.simpleweb.org/ietf/mibs/validate/, to make sure there were no other issues) –  Aug 09 '13 at 20:30
  • 2
    If you're using a *nix OS, then I suggest installing `libsmi`, which is a library and software suite, and use it's `smilint` MIB checker, too. I write an .sh script that just checks it for me whenever I change/write to the new MIB. – EhevuTov Aug 09 '13 at 20:34
  • Thanks for that, very helpful. It showed me another error in my MIB, which I have also fixed. But even after that (and a restart of snmpd just in case), the agent is still not getting called. Note that the MIB is not showing any further issues. –  Aug 09 '13 at 20:48
  • What happens when you run `snmpget -v 1 -c mycommunity localhost:161 test2` or: `snmpget -v 1 -c mycommunity localhost:161 test2.0.1` – EhevuTov Aug 09 '13 at 21:29
  • Also, could you edit your MIB with a MIB that passes `smilint`? I'm trying to run this test now. – EhevuTov Aug 09 '13 at 21:36
  • Both of those commands give the same response: `(noSuchName) There is no such variable name in this MIB.`. And I've already updated the MIB in the problem description to one that passes smilint. Are you seeing smilint errors? –  Aug 09 '13 at 21:40
  • Yeah, I'm seeing a lot of errors, actually. For one, you're trying ot use SNMPv2 in your SMI syntax, but your snmpget is using `-v 1` – EhevuTov Aug 09 '13 at 21:43
  • Can you show me how you're running the smilint command? When I run it on this MIB I see no errors. What flags are you using? –  Aug 09 '13 at 21:44
  • `smilint IDB-MIB.mib` – EhevuTov Aug 09 '13 at 22:37
  • That's odd... I ran `smilint -l 6 IDB-MIB.mib` and didn't get any errors at all. Do you think it will start working if I use `-v 2c` as the version? –  Aug 09 '13 at 22:39
  • Also, it's not even finishing parsing the MIB before it returns errors. The net-snmp smi parser returns errors before it even starts to send the SNMP PDU's over the network. I even tried using `-v 2c` and got similar errors. On the good news, I think you're close. Just need to tidy up your MIB, I think. Try accessing the OID through snmp get without the MIB name but instead the coded OID. – EhevuTov Aug 09 '13 at 22:40
  • Running `snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3.0` just gives me this: `IDB-MIB::test2.0 = No Such Object available on this agent at this OID`. The fact that it was able to figure out the MIB name based on the OID tells me that it's at least parsing SOME of the MIB properly. –  Aug 09 '13 at 22:45
  • When the OIDs come back with an noSuchName error, replace the OIDs with the ones from the original set. – hwnd Aug 12 '13 at 16:57
  • @hwnd: I don't understand what you mean by the ones from the original set. What is the original set? How do I "replace the OIDs"? –  Aug 13 '13 at 16:31
  • @EhevuTov I'm still unable to find out what is wrong with my MIB-smilint checks pass without any problems. I've verified that I'm using the most up to date smilint binary as well. Any idea why I can't get these errors to show up? –  Aug 13 '13 at 17:07
  • @Bucky24 hrm, I don't know. It's odd. I'm glad you got it working though. – EhevuTov Aug 14 '13 at 03:23

1 Answers1

2

And I've solved the problem. It wasn't with the MIB, it was with the agent (which I had THOUGHT was working fine the whole time so I never bothered to check it).

I'd been running the agent stand-alone, because it seemed like it was working fine (never threw any errors when registering the handler). Apparently though, it needs to be run directly by snmpd.

I moved it to a directory that snmpd can access (because also apparently snmpd can't run scripts from /root, even though it's running as root), and added these lines in snmpd.conf:

perl print "\nRunning agents now\n";
perl do "/usr/share/snmp/agent.pl" || print "Problem running agent script: $!\n";
perl print "Agents run\n";

Note that these two lines were already present:

disablePerl false
perlInitFile /usr/share/snmp/snmp_perl.pl

I can now run the snmpget command and get the expected response.

> snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3
IDB-MIB::test2 = INTEGER: 2 tests