Tutorial for Visual xAgentBuilder for C++
Lesson 3: SNMP Agent
SNMP agent functionality is embedded in the BSNMPAgent class. To create a fully functional SNMP agent, all we have to do is to create object of BSNMPAgent class and call Start method. This method will create a few threads required for operation of the agent and return control to the main program. When user enters ‘q’ and exits CLI, Stop is invoked. Even if we forget to call Stop at the end, agent properly releases all allocated resources in the destructor.
int main(int argc, char*
argv[])
{
BTime tm;
BMibHandlerEx mh;
mh.Init(&tm, NULL);
BSNMPAgent snmpAgent(tm, &mh, NULL, NULL);
tInt32 err = snmpAgent.Init(eSNMPv2c, 161);
if (err != NDERR_NO_ERROR)
return err;
BCommunity& cmt = snmpAgent.CBSecSubsystem().Community();
cmt.Add(“public”, eMaxAccessReadOnly);
cmt.Add(“private”, eMaxAccessReadCreate);
BCLI cli( &mh );
cli.Register(&snmpAgent);
snmpAgent.Start();
cli.Stream().Write(HEADING);
cli.Start(“ndmp# “);
snmpAgent.Stop();
return 0;
}
BSnmpAgent’s constructor requires following parameters:
BSNMPAgent( BTime& tm,
IMIBHandler* pMibHndlr,
INonVolatile* pNV,
IAccessControl* pAccessControl,
ICallbackSnmpOriginator* pCbOrig)
The first one is the reference to the time object.
The second one is address of the object that implements IMIBHandler interface. In this case this is address of mh, which is instance of BMibHandlerEx class. SNMP agent will be using this interface to resolve all requests for the management variables.
The last three parameters for BSnmpAgent’s constructor can all be NULL.
The third parameter is pointer to the object that implements INonVolatile interface. INonVolatile represent interface that the underlaying SNMP engine uses to store non-volatile parameters like community strings, snmpEngineID, snmpEngineBoots, and all configuration tables. If not supplied, we have to programmatically initialize agent (as we’ll do in this lesson) and changes to the NV parameters won’t be saved.
The fourth parameter is pointer to the object that implements IAccessControl interface. Access Control for the requests is executed in the MIB handler. Why do we need access control here? The reason is notification originator. Notification originator (which is part of the SNMP agent) has to check access control for the variables that are part of the notification pdu.
The last, fifth, parameter is pointer to the object that implements ICallbackSnmpOriginator interface. This parameter is by default NULL, hence, it could be omitted. If provided, SNMP agent will call methods in this interface during processing of outgoing notification message.
As mentioned above, we didn’t provide non-volatile configuration for the agent so agent cannot initialize itself during startup. We have to do it programmatically. In this lesson we’ve specified that the agent should run as SNMPv2 agent, it should listen to port 162 for incoming requests and will accept request from two communities: public and private.
Agent created with the code shown above is fully functional SNMP agent. The agent internally implements following MIB subtrees (subtree is MIB node in the MIB’s naming hierarchy plus all of its subordinate elements), snmp (objects defined under mib-2 .snmp subtree), and from v3 MIBs: snmpFrameworkMIB, snmpMPDMIB, snmpTargetMIB , snmpNotificationMIB, snmpUsmMIB, and snmpVacmMIB.
Since the agent will be running in SNMPv2 mode, only ‘snmp’ will be registered with MIB handler.
Please build this project, start the agent and use Visual MIB Browser to inspect the agent’s MIB.
Please proceed to Lesson 4: HTTP topic.