[SNMP4J] Troubles with SET request

Frank Fock fock at agentpp.com
Wed Dec 17 20:11:51 CET 2014


Hello Krystian,

There are several errors (misunderstandings) in the code you posted.
Please completely rewrite it. I suggest using AgenPro to generate
your stub code from a MIB to get a consistent implementation for
the corresponding MIB specification.

The major issues are:
1. The tables have illegal OIDs (regarding SMI standard).
2. The constructor "MOTable" does not exists and it does not take 
several MOAccess values.
3. A table where rows can be created, needs at least one column wiht 
READ_CREATE max. access right.
4. By default, only the SnmpRowStatus column (implementation of the 
RowStatus TC) has a ready-to-use implementation for creating new rows.
5. Adding a row with the same index (OID) as an existing row (or 
previously added row) will replace that row!

Best regards,
Frank

Am 17.12.2014 10:35, schrieb Krystian Powójski:
> Hello Frank,
>
> I'm using your SNMP4J library to create my own SNMP agent. I read a 
> lot of information and forums how to use it in order to create a snmp 
> table. There is no problem when I'm using a SNMP Client sending GET 
> request to my agent, but during sending a SET request there is a 
> Exception:
> "Error 'Unable to create object' generated at: 
> 1.3.6.1.2.1.10.166.1.3.0.0.23 = 2223
>     at 
> org.snmp4j.agent.request.SnmpRequest$SnmpSubRequest.requestStatusChanged(SnmpRequest.java:614)
>     at 
> org.snmp4j.agent.request.RequestStatus.fireRequestStatusChanged(RequestStatus.java:89)
>     at 
> org.snmp4j.agent.request.RequestStatus.setErrorStatus(RequestStatus.java:52)
>     at 
> org.snmp4j.agent.CommandProcessor$SetHandler.prepare(CommandProcessor.java:790)
>     at 
> org.snmp4j.agent.CommandProcessor$SetHandler.processPdu(CommandProcessor.java:837)
>     at 
> org.snmp4j.agent.CommandProcessor$SetHandler.processPdu(CommandProcessor.java:755)
>     at 
> org.snmp4j.agent.CommandProcessor.processRequest(CommandProcessor.java:421)
>     at 
> org.snmp4j.agent.CommandProcessor.processRequest(CommandProcessor.java:383)
>     at 
> org.snmp4j.agent.CommandProcessor.dispatchCommand(CommandProcessor.java:339)
>     at 
> org.snmp4j.agent.CommandProcessor$Command.run(CommandProcessor.java:559)
>     at 
> org.snmp4j.agent.CommandProcessor.processPdu(CommandProcessor.java:162)
>     at 
> org.snmp4j.MessageDispatcherImpl.fireProcessPdu(MessageDispatcherImpl.java:675)
>     at 
> org.snmp4j.MessageDispatcherImpl.dispatchMessage(MessageDispatcherImpl.java:302)
>     at 
> org.snmp4j.MessageDispatcherImpl.processMessage(MessageDispatcherImpl.java:373)
>     at 
> org.snmp4j.MessageDispatcherImpl.processMessage(MessageDispatcherImpl.java:333)
>     at 
> org.snmp4j.transport.AbstractTransportMapping.fireProcessMessage(AbstractTransportMapping.java:76)
>     at 
> org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(DefaultUdpTransportMapping.java:414)
>     at java.lang.Thread.run(Unknown Source)".
>
> I'm quite desperate because I've already spent a few days and there is 
> no progress. Bellow I paste my code:
> import java.io.File;
> import java.io.IOException;
>
> import org.snmp4j.TransportMapping;
> import org.snmp4j.agent.BaseAgent;
> import org.snmp4j.agent.CommandProcessor;
> import org.snmp4j.agent.DuplicateRegistrationException;
> import org.snmp4j.agent.MOGroup;
> import org.snmp4j.agent.ManagedObject;
> import org.snmp4j.agent.mo.MOTableRow;
> import org.snmp4j.agent.mo.snmp.RowStatus;
> import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
> import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB.SnmpCommunityEntryRow;
> import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
> import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
> import org.snmp4j.agent.mo.snmp.StorageType;
> import org.snmp4j.agent.mo.snmp.VacmMIB;
> import org.snmp4j.agent.security.MutableVACM;
> import org.snmp4j.mp.MPv3;
> import org.snmp4j.security.SecurityLevel;
> import org.snmp4j.security.SecurityModel;
> import org.snmp4j.security.USM;
> import org.snmp4j.smi.Address;
> import org.snmp4j.smi.GenericAddress;
> import org.snmp4j.smi.Integer32;
> import org.snmp4j.smi.OID;
> import org.snmp4j.smi.OctetString;
> import org.snmp4j.smi.Variable;
> import org.snmp4j.transport.TransportMappings;
>
> public class SNMPAgent extends BaseAgent {
>
>     private String address;
>
>     /**
>      *
>      * @param address
>      * @throws IOException
>      */
>     public SNMPAgent(String address) 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;
>     }
>
>     /**
>      * Adds community to security name mappings needed for SNMPv1 and 
> SNMPv2c.
>      */
>     @Override
>     protected void addCommunities(SnmpCommunityMIB communityMIB) {
>         Variable[] com2sec = new Variable[] { new OctetString("public"),
>                 new OctetString("cpublic"), // security name
>                 getAgent().getContextEngineID(), // local engine ID
>                 new OctetString("public"), // default context name
>                 new OctetString("public"), // transport tag
>                 new Integer32(StorageType.nonVolatile), // storage type
>                 new Integer32(RowStatus.active) // row status
>         };
>         MOTableRow<?> row = 
> communityMIB.getSnmpCommunityEntry().createRow(
>                 new OctetString("public2public").toSubIndex(true), 
> com2sec);
> communityMIB.getSnmpCommunityEntry().addRow((SnmpCommunityEntryRow) row);
>
>     }
>
>     /**
>      * Adds initial notification targets and filters.
>      */
>     @Override
>     protected void addNotificationTargets(SnmpTargetMIB arg0,
>             SnmpNotificationMIB arg1) {
>         // TODO Auto-generated method stub
>
>     }
>
>     /**
>      * Adds all the necessary initial users to the USM.
>      */
>     @Override
>     protected void addUsmUser(USM arg0) {
>         // TODO Auto-generated method stub
>
>     }
>
>     /**
>      * Adds initial VACM configuration.
>      */
>     @Override
>      protected void addViews(VacmMIB vacm) {
>           vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new 
> OctetString(
>             "cpublic"), new OctetString("v1v2group"),
>             StorageType.nonVolatile);
>
>           vacm.addAccess(new OctetString("v1v2group"), new 
> OctetString("public"),
>             SecurityModel.SECURITY_MODEL_SNMPv2c, 
> SecurityLevel.NOAUTH_NOPRIV,
>             MutableVACM.VACM_MATCH_EXACT, new 
> OctetString("fullReadView"),
>             new OctetString("fullWriteView"), new OctetString(
>               "fullNotifyView"), StorageType.nonVolatile);
>
>           vacm.addViewTreeFamily(new OctetString("fullWriteView"), new 
> OID("1.3"),
>             new OctetString(), VacmMIB.vacmViewIncluded,
>             StorageType.volatile_);
>
>           vacm.addViewTreeFamily(new OctetString("fullReadView"), new 
> OID("1.3"),
>             new OctetString(), VacmMIB.vacmViewIncluded,
>             StorageType.volatile_);
>
>           vacm.addViewTreeFamily(new OctetString("fullNotifyView"), 
> new OID("1.3"),
>             new OctetString(), VacmMIB.vacmViewIncluded,
>             StorageType.volatile_);
>
>
>     }
>
>     /**
>      * 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));
>     }
>
> }
>
>
> And class where I create tables:
> import java.io.IOException;
>
> import org.apache.log4j.BasicConfigurator;
> import org.apache.log4j.Level;
> import org.apache.log4j.Logger;
> import org.snmp4j.PDU;
> import org.snmp4j.agent.MOAccess;
> import org.snmp4j.agent.mo.DefaultMOMutableRow2PC;
> import org.snmp4j.agent.mo.DefaultMOMutableRow2PCFactory;
> import org.snmp4j.agent.mo.DefaultMOMutableTableModel;
> import org.snmp4j.agent.mo.DefaultMOTable;
> import org.snmp4j.agent.mo.DefaultMOTableRow;
> import org.snmp4j.agent.mo.MOAccessImpl;
> import org.snmp4j.agent.mo.MOColumn;
> import org.snmp4j.agent.mo.MOMutableTableModel;
> import org.snmp4j.agent.mo.MOTableIndex;
> import org.snmp4j.agent.mo.MOTableModelEvent;
> import org.snmp4j.agent.mo.MOTableModelListener;
> import org.snmp4j.agent.mo.MOTableRow;
> import org.snmp4j.agent.mo.MOTableSubIndex;
> import org.snmp4j.log.Log4jLogFactory;
> import org.snmp4j.log.LogFactory;
> import org.snmp4j.smi.Integer32;
> import org.snmp4j.smi.OID;
> import org.snmp4j.smi.OctetString;
> import org.snmp4j.smi.SMIConstants;
> import org.snmp4j.smi.Variable;
>
> public class TestSnmpAgent {
>
>     static final OID rootOID = new OID(".1.3.6.1.2.1.10.166.1");
>
>     public static void main(String[] args) throws IOException {
>         BasicConfigurator.configure();
>         LogFactory.setLogFactory(new Log4jLogFactory());
>
>         TestSnmpAgent client = new TestSnmpAgent("udp:127.0.0.1/161");
>         client.init();
>
>         System.in.read();
>     }
>
>
>     SNMPAgent agent = null;
>     String address = null;
>
>     public TestSnmpAgent(String add) {
>         address = add;
>     }
>
>     private void init() throws IOException {
>         agent = new SNMPAgent("0.0.0.0/2001");
>         agent.start();
>
> //        PDU pdu = new PDU();
> //        pdu.setType(PDU.SET);
>
>         agent.unregisterManagedObject(agent.getSnmpv2MIB());
>
>         MOAccess[] moAccess1 = new MOAccess[]{ 
> MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_WRITE, 
> MOAccessImpl.ACCESS_READ_WRITE };
>         DefaultMOTable ifTable1 = new MOTable(new 
> OID(rootOID.toString() + ".1.0"), 3, moAccess1).create();
>         DefaultMOMutableTableModel model1 = 
> (DefaultMOMutableTableModel)ifTable1.getModel();
>
>         MOAccess[] moAccess2 = new MOAccess[]{ 
> MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_WRITE };
>         DefaultMOTable ifTable2 = new MOTable(new 
> OID(rootOID.toString() + ".2.0"), 2, moAccess2).create();
>         DefaultMOMutableTableModel model2 = 
> (DefaultMOMutableTableModel)ifTable2.getModel();
>
>         MOAccess[] moAccess3 = new MOAccess[]{ 
> MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_WRITE, 
> MOAccessImpl.ACCESS_READ_WRITE, MOAccessImpl.ACCESS_READ_WRITE};
>         DefaultMOTable ifTable3 = new MOTable(new 
> OID(rootOID.toString() + ".3.0"), 4, moAccess3).create();
>         DefaultMOMutableTableModel model3 = 
> (DefaultMOMutableTableModel)ifTable3.getModel();
>
>         MOAccess[] moAccess4 = new MOAccess[]{ 
> MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_WRITE, 
> MOAccessImpl.ACCESS_READ_WRITE};
>         DefaultMOTable ifTable4 = new MOTable(new 
> OID(rootOID.toString() + ".4.0"), 3, moAccess4).create();
>         DefaultMOMutableTableModel model4 = 
> (DefaultMOMutableTableModel)ifTable4.getModel();
>
>
>
>         //eonInputPortTable
>         DefaultMOMutableRow2PCFactory factory1 = new 
> DefaultMOMutableRow2PCFactory();
>         model1.setRowFactory(factory1);
>
>         MOTableRow row1 = factory1.createRow(new OID("0"), new 
> Variable[]{new Integer32(11), new Integer32(31),new Integer32(4)});
>         model1.addRow(row1);
>
>         MOTableRow row2 = factory1.createRow(new OID("1"), new 
> Variable[]{new Integer32(22), new Integer32(34),new Integer32(4)});
>         model1.addRow(row2);
>         MOTableRow row3 = factory1.createRow(new OID("2"), new 
> Variable[]{new Integer32(23), new Integer32(33),new Integer32(4)});
>         model1.addRow(row3);
>         MOTableRow row4 = factory1.createRow(new OID("3"), new 
> Variable[]{new Integer32(24), new Integer32(31),new Integer32(4)});
>         model1.addRow(row4);
>
>
> //        model1.addRow(new DefaultMOTableRow(new OID("0"),new 
> Variable[]{new Integer32(11), new Integer32(31)}));
> //        model1.addRow(new DefaultMOTableRow(new OID("1"),new 
> Variable[]{new Integer32(12), new Integer32(32)}));
> //        model1.addRow(new DefaultMOTableRow(new OID("2"),new 
> Variable[]{new Integer32(13), new Integer32(33)}));
> //        model1.addRow(new DefaultMOTableRow(new OID("3"),new 
> Variable[]{new Integer32(14), new Integer32(34)}));
> //        model1.setRowFactory(new DefaultMOMutableRow2PCFactory());
>
>         //eonOutputPortTable
> //        model2.addRow(new DefaultMOTableRow(new OID("0"), new 
> Variable[]{new Integer32(21), new Integer32(32)}));
> //        model2.addRow(new DefaultMOMutableRow2PC(new OID("1"), new 
> Variable[]{new Integer32(22), new Integer32(34)}));
> //        model2.addRow(new DefaultMOMutableRow2PC(new OID("2"), new 
> Variable[]{new Integer32(23), new Integer32(33)}));
> //        model2.addRow(new DefaultMOMutableRow2PC(new OID("3"), new 
> Variable[]{new Integer32(24), new Integer32(31)}));
> //        model2.setRowFactory(new DefaultMOMutableRow2PCFactory());
> //
> //        //eonSegmentTable
> //        model3.addRow(new DefaultMOMutableRow2PC(new OID("0"), new 
> Variable[]{new Integer32(31), new Integer32(4), new Integer32(1),new 
> Integer32(1)}));
> //        model3.addRow(new DefaultMOMutableRow2PC(new OID("1"), new 
> Variable[]{new Integer32(31), new Integer32(4), new Integer32(1),new 
> Integer32(1)}));
> //        model3.addRow(new DefaultMOMutableRow2PC(new OID("2"), new 
> Variable[]{new Integer32(31), new Integer32(4), new Integer32(1),new 
> Integer32(1)}));
> //        model3.setRowFactory(new DefaultMOMutableRow2PCFactory());
> //
> //        //eonXCTable
> //        model4.addRow(new DefaultMOMutableRow2PC(new OID("0"), new 
> Variable[]{new Integer32(1), new Integer32(11), new Integer32(21)}));
> //        model4.addRow(new DefaultMOMutableRow2PC(new OID("1"), new 
> Variable[]{new Integer32(1), new Integer32(13), new Integer32(22)}));
> //        model4.addRow(new DefaultMOMutableRow2PC(new OID("1"), new 
> Variable[]{new Integer32(1), new Integer32(11), new Integer32(21)}));
>
>
>         ifTable1.setVolatile(true);
>         agent.registerManagedObject(ifTable1);
> //        ifTable2.setVolatile(true);
> //        agent.registerManagedObject(ifTable2);
> //        ifTable3.setVolatile(true);
> //        agent.registerManagedObject(ifTable3);
> //        ifTable4.setVolatile(true);
> //        agent.registerManagedObject(ifTable4);
>
>
>
>
>
>
>     }
>
>
> }
>
>

-- 
---
AGENT++
Maximilian-Kolbe-Str. 10
73257 Koengen, Germany
https://agentpp.com
Phone: +49 7024 8688230
Fax:   +49 7024 8688231




More information about the SNMP4J mailing list