Last modified by Frank Fock on 2024/05/25 12:27

Hide last authors
Frank Fock 1.1 1 Reading this article is recommended if you want to use SNMP4J-Agent Manged Objects (MIB) objects like MOScalar and MOTable with multiple SNMPv3 contexts in your agent.
2
3 == Background ==
4
5 In SNMPv3 the concept of using //contexts// is generally used to separate MIB data into different virtual MIBs named by the context name. As a consequence, a basic requirement for the MIB instrumentation is, that the data content is separated by the contexts. With SNMP4J-Agent you have basically three options to realize this requirement:
6
7 1. (((
8 For each context create a separate ManagedObject instance and register it with the target context using MOServer.register.
9
10 {{code}}
11 MOScalar myScalarObjAContext1 = new MyScalarObjA(..);
12 MOScalar myScalarObjAContext2 = new MyScalarObjA(..);
13
14 MOServer server = ..;
15 server.register(myScalarObjAContext1, new OctetString("context1"));
16 server.register(myScalarObjAContext2, new OctetString("context2"));
17 {{/code}}
18 )))
19 1. (((
20 Use the same ManagedObject instance for all target contexts by calling MOServer.register repeatedly for each context with the same instance. The single instance is then responsible to deliver and update its content (MIB data) based on the context information given in the ManagedObject.get, .next, .prepare, .commit, .undo, and .cleanup SubRequest parameter in the MOScope (in fact it is then a MOContextScope) member.
21
22 {{code}}
23 OctetString context1 = new OctetString("context1");
24 OctetString context2 = new OctetString("context2");
25
26 MOScalar myScalarObjA = new MyScalarObjA(..) {
27 public void get(SubRequest request) {
28 if (request.getScope() instanceof MOContextScope) {
29 MOContextScope scope = (MOContextScope)request.getScope();
30 if (context1.equals(scope.getContext()) {
31 // return value from context1
32 ..
33 }
34 else if (context2.equals(scope.getContext()) {
35 // return value from context2
36 ..
37 }
38 else {
39 // cannot be reached
40 }
41 }
42 }
43 }
44
45 MOServer server = ..;
46 server.register(myScalarObjA, context1);
47 server.register(myScalarObjA, context2));
48 {{/code}}
49 )))
50 1. Any combination of the above two approaches will work too.
51
52 == Step-by-step guide ==
53
54 1. Let AgenPro generate the MIB object stub and agent main code as described in: [[Generate code for SNMP4J-Agent with AgenPro>>url:https://doc.snmp.app/display/SNMP4J/Generate+code+for+SNMP4J-Agent+with+AgenPro]].
55 1. (((
56 Edit the Agent.java file (or modify the AgenPro template) as follows:1. (((
57 Define the context values you want to support in the agent:
58
59 {{code}}
60 //|:AgenPro|=members
61 OctetString context1 = new OctetString("ctx1");
62 OctetString context2 = new OctetString("ctx2");
63 //|AgenPro:|
64 {{/code}}
65 )))
66 1. (((
67 Add the contexts to the MOServer instance(s) you use in the agent:
68
69 {{code language="java"}}
70 public static void main(String[] args) {
71 //|:AgenPro|=main
72 server.addContext(context1);
73 server.addContext(context2);
74 defaultMain(args);
75 //|AgenPro:|
76 }
77 {{/code}}
78 )))
79 1. (((
80 Register your MIB object instances for each context separately (as explained above in the //Background// section):
81
82 {{code}}
83 protected void registerMIBs()
84 {
85 //|:AgenPro|=registerBefore
86 try {
87 // Create a new instance per generated MIB module (you can use different MOFactory instances per context if needed):
88 Snmp4jAgentTutorialMib tutorialMibCtx1 = new Snmp4jAgentTutorialMib(getFactory());
89 tutorialMibCtx1.registerMOs(server, context1);
90 Snmp4jAgentTutorialMib tutorialMibCtx2 = new Snmp4jAgentTutorialMib(getFactory());
91 tutorialMibCtx2.registerMOs(server, context2);
92 } catch (DuplicateRegistrationException drex) {
93 logger.error("Duplicate registration: "+drex.getMessage()+"."+
94 " MIB object registration may be incomplete!", drex);
95 }
96 //|AgenPro:|
97 ..
98 }
99 {{/code}}
100 )))
101 )))
102 1. (((
103 Configure the SNMPv3 View-based Access Control Model (VACM) MIB to allow SNMPv3 (and/or SNMPv1/2vc) users to access the new non-default contexts:
104
105 {{code}}
106 public void run() {
107 // initialize agent before registering our own modules
108 agent.initialize();
109 // this requires sysUpTime to be available.
110 registerMIBs();
111 // add proxy forwarder
112 agent.setupProxyForwarder();
113 // now continue agent setup and launch it.
114 agent.run();
115
116 //|:AgenPro|=run
117 VacmMIB vacm = agent.getVacmMIB();
118 vacm.addAccess(new OctetString("v3group"), context1,
119 SecurityModel.SECURITY_MODEL_ANY,
120 SecurityLevel.AUTH_PRIV,
121 MutableVACM.VACM_MATCH_EXACT,
122 new OctetString("unrestrictedReadView"),
123 new OctetString("unrestrictedWriteView"),
124 new OctetString("unrestrictedNotifyView"),
125 StorageType.nonVolatile);
126 vacm.addAccess(new OctetString("v3group"), context2,
127 SecurityModel.SECURITY_MODEL_ANY,
128 SecurityLevel.AUTH_PRIV,
129 MutableVACM.VACM_MATCH_EXACT,
130 new OctetString("unrestrictedReadView"),
131 new OctetString("unrestrictedWriteView"),
132 new OctetString("unrestrictedNotifyView"),
133 StorageType.nonVolatile);
134 //|AgenPro:|
135 }
136 {{/code}}
137 )))