This page describes how the SNMP4J-Agent MOFactory concept can be used to apply customised columnar objects to SNMP table implementations.
Tip | ||
---|---|---|
| ||
Although this article explains how to access SNMP request data while processing sub-requests, it is not recommended to do so without reasons.
|
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 theNotificationLogMibSnmp4jAgentTutorialMib.java
):Code Block language java @SuppressWarnings(value={"unchecked"}) private void createNlmConfigLogEntrycreateSnmp4jAgentTutorialFileTreeEUEntry(MOFactory moFactory) { // Index definition nlmConfigLogEntryIndexes snmp4jAgentTutorialFileTreeEUEntryIndexes = new MOTableSubIndex[] { moFactory.createSubIndex(oidNlmLogNameoidSnmp4jAgentTutorialFileTreeEUIndex, SMIConstants.SYNTAX_OCTETOBJECT_STRINGIDENTIFIER, 0, 32128) }; nlmConfigLogEntryIndexsnmp4jAgentTutorialFileTreeEUEntryIndex = moFactory.createIndex(nlmConfigLogEntryIndexessnmp4jAgentTutorialFileTreeEUEntryIndexes, false, new MOTableIndexValidator() { public boolean isValidIndex(OID index) { boolean isValidIndex = true; //--AgentGen BEGIN=snmp4jAgentTutorialFileTreeEUEntry::isValidIndex //--AgentGen END return isValidIndex; } }); // Columns MOColumn[] nlmConfigLogEntryColumnssnmp4jAgentTutorialFileTreeEUEntryColumns = new MOColumn[610]; nlmConfigLogEntryColumnssnmp4jAgentTutorialFileTreeEUEntryColumns[idxNlmConfigLogFilterNameidxSnmp4jAgentTutorialFileTreeEUPath] = new MOMutableColumnmoFactory.createColumn(colNlmConfigLogFilterNamecolSnmp4jAgentTutorialFileTreeEUPath, SMIConstants.SYNTAX_OCTET_STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATEONLY), new OctetString(new byte[] { })); ValueConstraint nlmConfigLogFilterNameVC = new ConstraintsImpl(); ((ConstraintsImpl)nlmConfigLogFilterNameVC).add(new Constraint(0L, 32L)); ((MOMutableColumn)nlmConfigLogEntryColumns[idxNlmConfigLogFilterName]). addMOValueValidationListener(new ValueConstraintValidator(nlmConfigLogFilterNameVC)); ((MOMutableColumn)nlmConfigLogEntryColumns[idxNlmConfigLogFilterName]). addMOValueValidationListener(new NlmConfigLogFilterNameValidator()); nlmConfigLogEntryColumns[idxNlmConfigLogEntryLimit] = new MOMutableColumn(colNlmConfigLogEntryLimit, 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_GAUGE32STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE), ONLY), tcModuleSNMPv2Tc, new UnsignedInteger32(0tcDefDateAndTime)); nlmConfigLogEntryColumnssnmp4jAgentTutorialFileTreeEUEntryColumns[idxNlmConfigLogAdminStatusidxSnmp4jAgentTutorialFileTreeEULastModified] = new Enumerated<Integer32>moFactory.createColumn(colNlmConfigLogAdminStatuscolSnmp4jAgentTutorialFileTreeEULastModified, SMIConstants.SYNTAX_OCTET_INTEGER32STRING, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE), ONLY), tcModuleSNMPv2Tc, new Integer32(1)tcDefDateAndTime); ValueConstraint nlmConfigLogAdminStatusVC snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEULastAccessed] = new EnumerationConstraint( moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEULastAccessed, new int[] { NlmConfigLogAdminStatusEnum.enabled, SMIConstants.SYNTAX_OCTET_STRING, NlmConfigLogAdminStatusEnum.disabled }); ((MOMutableColumn)nlmConfigLogEntryColumns[idxNlmConfigLogAdminStatus]). addMOValueValidationListener(new ValueConstraintValidator(nlmConfigLogAdminStatusVC)); nlmConfigLogEntryColumns[idxNlmConfigLogOperStatus] = moFactory.createColumn(colNlmConfigLogOperStatus, moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY), tcModuleSNMPv2Tc, tcDefDateAndTime); snmp4jAgentTutorialFileTreeEUEntryColumns[idxSnmp4jAgentTutorialFileTreeEUSizeInBytes] = moFactory.createColumn(colSnmp4jAgentTutorialFileTreeEUSizeInBytes, SMIConstants.SYNTAX_INTEGERGAUGE32, 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:Code Block language java 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())) { ... } } return (V) row.getValue(column); } } } public <V extends Variable> MOColumn<V> createColumn(int columnID, int syntax, MOAccess access, V defaultValue, boolean mutableInService, String tcModuleName, String textualConvention) { TextualConvention<V> tc = getTextualConvention(tcModuleName, textualConvention); if (tc != null) { // Here you might need to create a MOColumn proxy object to apply your interception code // to columns based on textual conventions too: return tc.createColumn(columnID, syntax, access, defaultValue, mutableInService); } return createColumn(columnID, syntax, access, defaultValue, mutableInService); } }
Now you need to use the created MOFactory with your instance(s) of the MIB module. Basically you have to change the MIB module creation to the following. In fact, there are several locations where this can be done when using AgenPro generated code:
1. When creating the MIB module (see below)
2. When creating the
Module
instance that encapsulates all MIB modules generated for an agent3. By overriding the
Agent.getFactory()
method and returning your customMOFactory
Code Block language java Snmp4jAgentTutorialMib snmp4jAgentTutorialMib = new Snmp4jAgentTutorialMib(myMOFactory);
- For GET type requests overriding the
MOColumn.getValue
method is sufficient. If you also want to control the processing of SET requests, you need also to override the following methods defined inMOMutableColumn
as needed:prepare
commit
undo
cleanup
Related articles
Content by Label | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...