[SNMP4J] SNMP4J Agent cannot be discovered (Netwrok Discovery)

Denis Ioan d.ioan at next-italia.it
Thu Sep 17 11:14:06 CEST 2015


Hi to everybody, 
   I'm new to the SNMP protocol but I had to implement an Agent in v3.
Using SNMP4J I've implemented and all works fine except the agent cannot
be discovered automatically from the Network Discovery functions.
I guess I forgot to insert something in the configuration. I post here
the Agent class and the way I use it in the software:

public class SNMPAgent extends BaseAgent {
    
    private final String GROUP_NAME = "v3group";
     
    private String address;
    private String baseOID;
    private String securityName; // User
    private String SHAPassword;
    private String AES128Passphrase;  
    private String trapIPAddress;
    private String trapPort;
        
    /**
     *
     * @param address
     * @throws IOException
     */
    public SNMPAgent(String address, String baseOID, 
            String securityName, String SHAPassword, String
AES128Passphrase, String trapIPAddress, String trapPort) throws
IOException {

            /**
             * Creates a base agent with boot-counter, config file, and
a
             * CommandProcessor for processing SNMP requests.
Parameters:
             * "bootCounterFile" - a file with serialized boot-counter
information
             * (read/write). If the file does not exist it is created on
shutdown of
             * the agent. "configFile" - a file with serialized
configuration
             * information (read/write). If the file does not exist it
is created on
             * shutdown of the agent. "commandProcessor" - the
CommandProcessor
             * instance that handles the SNMP requests.
             */
            super(new File("conf.agent"), new File("bootCounter.agent"),
                            new CommandProcessor(
                                            new
OctetString(MPv3.createLocalEngineID())));
            
            this.address = address;
            this.baseOID = baseOID;
            this.securityName = securityName;
            this.SHAPassword = SHAPassword;
            this.AES128Passphrase = AES128Passphrase;
            this.trapIPAddress = trapIPAddress;
            this.trapPort = trapPort;  
            
    }

    /**
     * Adds community to security name mappings needed for SNMPv1 and
SNMPv2c.
     */
    @Override
    protected void addCommunities(SnmpCommunityMIB communityMIB) {
        
    }

    /**
     * Adds initial notification targets and filters.
     */
    @Override
    protected void addNotificationTargets(final SnmpTargetMIB targetMIB,
        final SnmpNotificationMIB notificationMIB) {
    }

    /**
     * Adds all the necessary initial users to the USM.
     */
    @Override
    protected void addUsmUser(USM usm) 
    {
        UsmUser user = new UsmUser(
                        new OctetString(securityName),         
                        AuthSHA.ID,
                        new OctetString(SHAPassword), 
                        PrivAES128.ID,           
                        new OctetString(AES128Passphrase)  
                        );

        usm.addUser(user.getSecurityName(), null, user);
    }

    /**
     * Adds initial VACM configuration.
     */
    @Override
    protected void addViews(VacmMIB vacm) 
    {		                
        vacm.addGroup(
                SecurityModel.SECURITY_MODEL_USM, 
                new OctetString(securityName),   
                new OctetString(GROUP_NAME),   
                StorageType.nonVolatile       
                );

        vacm.addAccess(
                new OctetString(GROUP_NAME), 
                new OctetString(),          
                SecurityModel.SECURITY_MODEL_USM,  
                SecurityLevel.AUTH_PRIV,    
                MutableVACM.VACM_MATCH_EXACT,  
                new OctetString("fullReadView"), 
                new OctetString("fullWriteView"), 
                new OctetString("fullNotifyView"), 
                StorageType.nonVolatile          
                );

        vacm.addViewTreeFamily(
                new OctetString("fullReadView"), 
                new OID(baseOID),               
                new OctetString(),             
                VacmMIB.vacmViewIncluded,      
                StorageType.nonVolatile       
                );

        vacm.addViewTreeFamily(
                new OctetString("fullWriteView"),  
                new OID(baseOID),                 
                new OctetString(),               
                VacmMIB.vacmViewIncluded,        
                StorageType.nonVolatile          
                );
        
        vacm.addViewTreeFamily(new OctetString("fullReadView"), new
OID("1.3"),
				new OctetString(), VacmMIB.vacmViewIncluded,
				StorageType.nonVolatile);
    }

    /**
     * Unregister the basic MIB modules from the agent's MOServer.
     */
    @Override
    protected void unregisterManagedObjects() {
            // TODO Auto-generated method stub

    }

    /**
     * Register additional managed objects at the agent's server.
     */
    @Override
    protected void registerManagedObjects() {
            // TODO Auto-generated method stub

    }

    protected void initTransportMappings() throws IOException {
            transportMappings = new TransportMapping[1];
            Address addr = GenericAddress.parse(address);
            TransportMapping tm = TransportMappings.getInstance()
                            .createTransportMapping(addr);
            transportMappings[0] = tm;
    }

    /**
     * Start method invokes some initialization methods needed to start
the
     * agent
     *
     * @throws IOException
     */
    public void start() throws IOException {

            init();
            // This method reads some old config from a file and causes
            // unexpected behavior.
            // loadConfig(ImportModes.REPLACE_CREATE);
            addShutdownHook();
            //getServer().addContext(new OctetString("public"));
            finishInit();
            run();
            sendColdStartNotification();
    }

    /**
     * Clients can register the MO they need
     */
    public void registerManagedObject(ManagedObject mo) {
            try {
                    server.register(mo, null);
            } catch (DuplicateRegistrationException ex) {
                    throw new RuntimeException(ex);
            }
    }

    public void unregisterManagedObject(MOGroup moGroup) {
            moGroup.unregisterMOs(server, getContext(moGroup));
    }

}

I user the Agent in this way:


agent = new SNMPAgent(
                    LOCALHOST + '/' + configuration.getAgentSnmpPort(),
                    configuration.getAgentBaseOID(),
                    configuration.getAgentSnmpUser(),
                    configuration.getAgentSHAPassword(),
                    configuration.getAgentAES128PassPhrase(),
                    configuration.getTrapDestIPAddress(),
                    configuration.getTrapPort());

            agent.start();

// Since BaseAgent registers some mibs by default we need to unregister
	// one before we register our own sysDescr. Normally you would
	// override that method and register the mibs that you need
	agent.unregisterManagedObject(agent.getSnmpv2MIB());
 
	// Register a system description, use one from you product environment
	// to test with

agent.registerManagedObject(MOCreator.createReadOnly(sysDescr,SYS_DESCRIPTION));

agent.registerManagedObject(MOCreator.createReadOnly(sysObjectID, new
OID(configuration.getAgentBaseOID())));

agent.registerManagedObject(MOCreator.createReadOnly(sysContact,SYS_CONTACT));

agent.registerManagedObject(MOCreator.createReadOnly(sysName,SYS_NAME));

agent.registerManagedObject(MOCreator.createReadOnly(sysLocation,SYS_LOCATION));

agent.registerManagedObject(MOCreator.createReadOnly(sysServices,SYS_SERVICES));
        
        MOSSysUpTime = MOCreator.createReadWrite(sysUpTime, new
TimeTicks((long)0));
        agent.registerManagedObject(MOSSysUpTime);


I thank you in advance everybody will answer me.
Denis






More information about the SNMP4J mailing list