Hello all,

I have written the following test code to verify how to register a handler for 
an OID in a subagent using the AgentX protocol. Despite the API return success 
I never see the handler invoked for the OID, nor do I see a packet delivered to 
subagent.

I'm guessing I either I am utilizing the API incorrectly, or I am missing a 
critical step. I have poured through the documentation I can find online, with 
little success.

Also are there any books on writing subagents? Search of Amazon showed no 
obvious titles.

Thanks in advance, Marc

=== BEGIN CODE ===
#include <assert.h>

#include <signal.h>

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#define SUBAGENT_NAME "testrig"

static netsnmp_handler_registration *reginfo;
static int keep_running = 1;

void stop_server(int sig) 
{
    keep_running = 0;
}

int handler(netsnmp_mib_handler *handler, 
            netsnmp_handler_registration *reginfo, 
            netsnmp_agent_request_info *reqinfo, 
            netsnmp_request_info *requests)
{
    assert(0);
    puts("Handler called.");
    return -1;
}


int register_oid()
{
    oid oid1[] = { 1, 3, 6, 1, 2, 1, 27, 2 };
    int rc;

    reginfo = netsnmp_create_handler_registration("handler",
                                                  handler,
                                                  oid1,
                                                  OID_LENGTH(oid1),
                                                  HANDLER_CAN_RWRITE);
    if (!reginfo) {
        puts("Failed to register handler.");
        return 0;
    }

    rc = netsnmp_register_instance(reginfo);
    switch (rc) {
    case MIB_REGISTERED_OK:
        puts("Handler successfully registered.");
        break;
    case MIB_REGISTRATION_FAILED:
        puts("Handler registration failed.");
        return 0;
        break;
    case MIB_DUPLICATE_REGISTRATION:
        puts("Handler reigstration failed - duplicate.");
        return 0;
        break;
    }

    return 1;   
}

void unregister_oid() 
{
    int rc;
    rc = netsnmp_unregister_handler(reginfo);
    printf("unregister rc=%d\n", rc);
    reginfo = NULL;
}

void main_loop()
{
    int pktcount;
    while (keep_running) {
        pktcount = agent_check_and_process(1);
        printf("Processed %d packets.\n", pktcount);
    }
}

int main(int argc, char *argv[]) 
{
    int rc;

    snmp_enable_stderrlog();
    netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
    netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, 
"/tmp/agentx");
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 15);
    rc = init_agent(SUBAGENT_NAME);
    assert(rc == 0);
    puts("Connected as subagent.");

    if (!register_oid()) {
        puts("Failed to register oid.");
        exit(1);
    }
    keep_running = 1;
    signal(SIGTERM, stop_server);
    signal(SIGINT, stop_server);
    main_loop();
    unregister_oid();

    snmp_shutdown(SUBAGENT_NAME);
    return 0;
}
=== END CODE ===




------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to