This page describes how the SNMP4J-Agent MOFactory concept can be used to apply customised columnar objects to SNMP table implementations.
Step-by-step guide
This guide assumes that you have generated your MIB instrumentation code with AgenPro, but you can do the same with manual written code too.
Generate the MIB module instrumentation code with AgenPro and activate factoryColumn property (set its value to yes) for your entire MIB module (or all affected tables). This will create the code to create
MOColumn
instances for read-only columns using theMOFactory
provided to your MIB module.
This step is important, because we will create our ownMOFactory
implementation to create our ownMOColumn
subclasses.
The generated code for each table will look like (sample taken from theNotificationLogMib.java
):@SuppressWarnings(value={"unchecked"}) private void createSnmp4jAgentTutorialFileTreeEUEntry(MOFactory moFactory) { // Index definition snmp4jAgentTutorialFileTreeEUEntryIndexes = new MOTableSubIndex[] { moFactory.createSubIndex(oidSnmp4jAgentTutorialFileTreeEUIndex, SMIConstants.SYNTAX_OBJECT_IDENTIFIER, 0, 128) }; snmp4jAgentTutorialFileTreeEUEntryIndex = moFactory.createIndex(snmp4jAgentTutorialFileTreeEUEntryIndexes, false, new MOTableIndexValidator() { public boolean isValidIndex(OID index) { boolean isValidIndex = true; //--AgentGen BEGIN=snmp4jAgentTutorialFileTreeEUEntry::isValidIndex //--AgentGen END return isValidIndex; } }); // Columns MOColumn[] snmp4jAgentTutorialFileTreeEUEntryColumns = new MOColumn[10]; snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEUPath] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEUPath, SMIConstants.SYNTAX_OCTET_STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), tcModuleSNMPv2Tc, tcDefDisplayString); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEUType] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEUType, SMIConstants.SYNTAX_INTEGER, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY)); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEUCreationTime] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEUCreationTime, SMIConstants.SYNTAX_OCTET_STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), tcModuleSNMPv2Tc, tcDefDateAndTime); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEULastModified] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEULastModified, SMIConstants.SYNTAX_OCTET_STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), tcModuleSNMPv2Tc, tcDefDateAndTime); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEULastAccessed] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEULastAccessed, SMIConstants.SYNTAX_OCTET_STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), tcModuleSNMPv2Tc, tcDefDateAndTime); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEUSizeInBytes] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEUSizeInBytes, SMIConstants.SYNTAX_GAUGE32, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY)); ... }
As you can see in the above code, the
moFactory
member of the generated MIB module class is used to create the actualMOColumn
instances. To create our custom MOColumn instances we need our own MOFactory instance first:myMOFactory = new DefaultMOFactory() { @Override public <V extends Variable> MOColumn<V> createColumn(int columnID, int syntax, MOAccess access, V defaultValue, boolean mutableInService) { return new MOMutableColumn<V>(columnID, syntax, access, defaultValue, mutableInService) { @Override public V getValue(MOTableRow row, int column, SubRequest subRequest) { if (subRequest.getScope() instanceof MOContextScope) { MOContextScope moContextScope = (MOContextScope)subRequest.getScope(); if ("myContext".equals(moContextScope.getContext())) { ... } } } } }