Last modified by Admin on 2024/05/25 10:37

Hide last authors
Admin 12.1 1 \\
2
3 The integration of SNMP4J-SMI in SNMP4J is very easy and complete. If you create an {{code language="none"}}OID{{/code}} or {{code language="none"}}Variable{{/code}} value from a {{code language="none"}}String{{/code}} value or converting them using the {{code language="none"}}toString(){{/code}} or {{code language="none"}}format(){{/code}} methods, SNMP4J-SMI will parse and format the values as human readable object names and value combinations on-the-fly.
4
5 {{tip title="SNMP4J Maven Repository"}}
6 To include SNMP4J-SMI-PRO in your Maven build, you need to add the SNMP4J Maven repository to your .m2/settings.xml file as shown below.
7 {{/tip}}
8
9 (% class="auto-cursor-target" %)
10 \\
11
12 {{code title="Maven Repository Definition"}}
13 <repositories>
14 <repository>
15 <id>snmp4j</id>
16 <name>SNMP4J Releases</name>
17 <releases>
18 <enabled>true</enabled>
19 <updatePolicy>always</updatePolicy>
20 <checksumPolicy>warn</checksumPolicy>
21 </releases>
22 <url>https://snmp.app/dist/release</url>
23 <layout>default</layout>
24 </repository>
25 </repositories>
26 {{/code}}
27
28 (% class="auto-cursor-target" %)
29 \\
30
31 {{code title="Maven Dependency for pom.xml "}}
32 <dependency>
33 <groupId>org.snmp4j.smi</groupId>
34 <artifactId>snmp4j-smi-pro</artifactId>
35 <version>1.9.13</version>
36   <classifier>jar-with-dependencies</classifier>
37 </dependency>
38 {{/code}}
39
40 === Integrating SNMP4J-SMI into SNMP4J ===
41
42 The code needed at minimum to use SNMP4J with SNMP4J-SMI OID and Variable value formatting and parsing support is:
43
44 {{code}}
45 static {
46 SmiManager smiManager = new SmiManager("myLicenseKey", new File("myEmptyDirectory"));
47 SNMP4JSettings.setOIDTextFormat(smiManager);
48 SNMP4JSettings.setVariableTextFormat(smiManager);
49 // If you need to disable full index formatting, then choose a different format below and uncomment the line:
50 // smiManager.setOidFormat(OIDFormat.ObjectNameAndDecodedIndex4RoundTrip);
51 }
52
53 {{/code}}
54
55 === Compiling MIB specification text files ===
56
57 Sample code to compile textual MIB specifications into the SNMP4J-SMI repository directory:
58
59 {{code}}
60 File f = new File(fname);
61 if (f.exists() && f.canRead()) {
62 File[] files;
63 if (f.isDirectory()) {
64 files = f.listFiles();
65 }
66 else {
67 files = new File[1];
68 files[0] = f;
69 }
70 // Compile MIB files without monitor (null), load them directly into the MIB repository in memory,
71 // update existant MIB modules, and do not ignore syntax errors:
72 List<CompilationResult> results = smiManager.compile(files, null, true, true, false);
73 int ok = 0;
74 for (CompilationResult result : results) {
75 if (result.getSmiErrorList() == null) {
76 ok += result.getModuleNames().size();
77 }
78 }
79 out.println("" + f + " contains ");
80 out.println(" " + ok + " syntactically correct MIB modules and");
81 out.println(" " + (results.size() - ok) +
82 " MIB modules with errors.");
83 String lastFile = null;
84 for (CompilationResult result : results) {
85 List<SmiError> smiErrors = result.getSmiErrorList();
86 String n = result.getFileName();
87 n = n.substring(n.lastIndexOf('/') + 1);
88 if ((lastFile == null) || (!lastFile.equals(n))) {
89 out.println("------ " + n + " ------");
90 lastFile = n;
91 }
92 if (smiErrors != null) {
93 for (int j = 0; j < smiErrors.size(); j++) {
94 SmiError error = smiErrors.get(j);
95 String txt = n + " #" + (j + 1) + ": " + error.getMessage();
96 out.println(txt);
97 }
98 }
99 else {
100 String txt = n + ": " + "OK";
101 out.println(txt);
102 }
103 }
104 }
105 else {
106 out.println("Cannot access MIB file '" + fname + "'");
107 }
108 {{/code}}
109
110 If you specify more than one MIB file, the MIB modules in those files will be automatically processed in the order of their import dependencies This avoids manual sorting or syntax errors because of unresolved IMPORTs.
111
112 === Loading MIB Modules ===
113
114 Sample code to load (already compiled) a MIB module from the MIB repository directory:
115
116 {{code}}
117 smiManager.loadModule("SNMP-VIEW-BASED-ACM-MIB");
118 smiManager.loadModule("SNMPv2-SMI");
119 {{/code}}
120
121 The SmiManager will also load all MIB objects that are imported by the specified modules! You do not have to implement this logic yourself
122
123 === Formatting and Parsing Examples for Object Identifiers (OIDs) ===
124
125 {{code language="java"}}
126 // The classic use case, formatting OIDs using (last) object name and numeric (index) suffix:
127 OID testPrivOID = new OID("1.3.6.1.4.1.4976");
128 assertEquals("enterprises.4976", testPrivOID.toString());
129
130 OID vacmAccessContextMatch = new OID("1.3.6.1.6.3.16.1.4.1.4.7.118.51.103.114.111.117.112.0.3.1");
131 assertEquals("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'", vacmAccessContextMatch.toString());
132  
133 // If the selected OID format supports round-trip processing like this SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip format
134 // OID.toString() and OID.format() will return the same value. Otherwise, OID.toString() will render the OID in the next less formatting
135 // OID format that supports parsing:
136 assertEquals("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'", vacmAccessContextMatch.format());
137  
138 // Parsing of textual OIDs is supported out-of-the-box:
139 assertEquals(vacmAccessContextMatch, new OID("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'"));
140
141 {{/code}}
142
143 === Formatting and Parsing Examples for Variable Bindings (VBs) ===
144
145 {{code language="java"}}
146 // store current format
147 SmiManager.OctetStringDefaultFormat defaultFormat = smiManager.getOctetStringDisplayHint();
148 // set the format to use MIB defined DISPLAY-HINT for OCTET STRING types
149 smiManager.setOctetStringDisplayHint(SmiManager.OctetStringDefaultFormat.MIB);
150
151 VariableBinding vbEnum = new VariableBinding(new OID("ifAdminStatus.4"), "down(2)");
152 assertEquals(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,2,2,1,7,4 }), new Integer32(2)), vbEnum);
153 assertEquals("down(2)", vbEnum.toValueString());
154
155 VariableBinding vbDateAndTime = new VariableBinding(new OID("nlmLogDateAndTime.1"),"2015-10-13,12:45:53.8,+2:0");
156 assertEquals(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,92,1,3,1,1,3,1 }), OctetString.fromHexString("07:df:0a:0d:0c:2d:35:08:2b:02:00")), vbDateAndTime);
157  
158 // restore previous format
159 smiManager.setOctetStringDisplayHint(defaultFormat); 
160 {{/code}}
161
162 (% class="auto-cursor-target" %)
163 \\