001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - TestAgent.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent.test;
022:
023: import java.io.*;
024:
025: import org.apache.log4j.*;
026: import org.snmp4j.*;
027: import org.snmp4j.agent.*;
028: import org.snmp4j.agent.mo.*;
029: import org.snmp4j.agent.mo.snmp.*;
030: import org.snmp4j.mp.*;
031: import org.snmp4j.security.*;
032: import org.snmp4j.smi.*;
033: import org.snmp4j.transport.*;
034: import org.snmp4j.agent.io.ImportModes;
035: import org.snmp4j.util.ThreadPool;
036: import org.snmp4j.log.Log4jLogFactory;
037: import org.snmp4j.log.LogFactory;
038: import org.snmp4j.agent.mo.snmp4j.example.Snmp4jHeartbeatMib;
039: import org.snmp4j.agent.security.MutableVACM;
040: import org.snmp4j.agent.mo.ext.AgentppSimulationMib;
041:
042: /**
043: * The <code>TestAgent</code> is a sample SNMP agent implementation of all
044: * features (MIB implementations) provided by the SNMP4J-Agent framework.
045: * The <code>TestAgent</code> extends the <code>BaseAgent</code> which provides
046: * a framework for custom agent implementations through hook methods. Those
047: * abstract hook methods need to be implemented by extending the
048: * <code>BaseAgent</code>.
049: * <p>
050: * This IF-MIB implementation part of this test agent, is instrumentation as
051: * a simulation MIB. Thus, by changing the agentppSimMode
052: * (1.3.6.1.4.1.4976.2.1.1.0) from 'oper(1)' to 'config(2)' any object of the
053: * IF-MIB is writable and even creatable (columnar objects) via SNMP. Check it
054: * out!
055: *
056: * @author Frank Fock
057: * @version 1.0
058: */
059: public class TestAgent extends BaseAgent {
060:
061: // initialize Log4J logging
062: static {
063: LogFactory.setLogFactory(new Log4jLogFactory());
064: }
065:
066: protected String address;
067: private Snmp4jHeartbeatMib heartbeatMIB;
068: private IfMib ifMIB;
069: private AgentppSimulationMib agentppSimulationMIB;
070:
071: /**
072: * Creates the test agent with a file to read and store the boot counter and
073: * a file to read and store its configuration.
074: *
075: * @param bootCounterFile
076: * a file containing the boot counter in serialized form (as expected by
077: * BaseAgent).
078: * @param configFile
079: * a configuration file with serialized management information.
080: * @throws IOException
081: * if the boot counter or config file cannot be read properly.
082: */
083: public TestAgent(File bootCounterFile, File configFile)
084: throws IOException {
085: super (bootCounterFile, configFile, new CommandProcessor(
086: new OctetString(MPv3.createLocalEngineID())));
087: // Alternatively: OctetString.fromHexString("00:00:00:00:00:00:02", ':');
088: agent.setThreadPool(ThreadPool.create("RequestPool", 4));
089: }
090:
091: protected void registerManagedObjects() {
092: try {
093: // server.register(createStaticIfTable(), null);
094: agentppSimulationMIB.registerMOs(server, null);
095: ifMIB.registerMOs(server, new OctetString("public"));
096: heartbeatMIB.registerMOs(server, null);
097: } catch (DuplicateRegistrationException ex) {
098: ex.printStackTrace();
099: }
100: }
101:
102: protected void addNotificationTargets(SnmpTargetMIB targetMIB,
103: SnmpNotificationMIB notificationMIB) {
104: targetMIB.addDefaultTDomains();
105:
106: targetMIB.addTargetAddress(new OctetString("notification"),
107: TransportDomains.transportDomainUdpIpv4,
108: new OctetString(new UdpAddress("127.0.0.1/162")
109: .getValue()), 200, 1,
110: new OctetString("notify"), new OctetString("v2c"),
111: StorageType.permanent);
112: targetMIB.addTargetParams(new OctetString("v2c"),
113: MessageProcessingModel.MPv3,
114: SecurityModel.SECURITY_MODEL_USM, new OctetString(
115: "SHADES"), SecurityLevel.AUTH_PRIV,
116: StorageType.permanent);
117: notificationMIB.addNotifyEntry(new OctetString("default"),
118: new OctetString("notify"),
119: SnmpNotificationMIB.SnmpNotifyTypeEnum.inform,
120: StorageType.permanent);
121: }
122:
123: protected void addViews(VacmMIB vacm) {
124: vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv1,
125: new OctetString("cpublic"),
126: new OctetString("v1v2group"), StorageType.nonVolatile);
127: vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c,
128: new OctetString("cpublic"),
129: new OctetString("v1v2group"), StorageType.nonVolatile);
130: vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
131: new OctetString("SHADES"), new OctetString("v3group"),
132: StorageType.nonVolatile);
133: vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
134: new OctetString("TEST"), new OctetString("v3test"),
135: StorageType.nonVolatile);
136: vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
137: new OctetString("SHA"),
138: new OctetString("v3restricted"),
139: StorageType.nonVolatile);
140:
141: vacm.addAccess(new OctetString("v1v2group"), new OctetString(
142: "public"), SecurityModel.SECURITY_MODEL_ANY,
143: SecurityLevel.NOAUTH_NOPRIV,
144: MutableVACM.VACM_MATCH_EXACT, new OctetString(
145: "fullReadView"), new OctetString(
146: "fullWriteView"), new OctetString(
147: "fullNotifyView"), StorageType.nonVolatile);
148: vacm.addAccess(new OctetString("v3group"), new OctetString(),
149: SecurityModel.SECURITY_MODEL_USM,
150: SecurityLevel.AUTH_PRIV, MutableVACM.VACM_MATCH_EXACT,
151: new OctetString("fullReadView"), new OctetString(
152: "fullWriteView"), new OctetString(
153: "fullNotifyView"), StorageType.nonVolatile);
154: vacm.addAccess(new OctetString("v3restricted"),
155: new OctetString(), SecurityModel.SECURITY_MODEL_USM,
156: SecurityLevel.AUTH_NOPRIV,
157: MutableVACM.VACM_MATCH_EXACT, new OctetString(
158: "restrictedReadView"), new OctetString(
159: "restrictedWriteView"), new OctetString(
160: "restrictedNotifyView"),
161: StorageType.nonVolatile);
162: vacm.addAccess(new OctetString("v3test"), new OctetString(),
163: SecurityModel.SECURITY_MODEL_USM,
164: SecurityLevel.AUTH_PRIV, MutableVACM.VACM_MATCH_EXACT,
165: new OctetString("testReadView"), new OctetString(
166: "testWriteView"), new OctetString(
167: "testNotifyView"), StorageType.nonVolatile);
168:
169: vacm.addViewTreeFamily(new OctetString("fullReadView"),
170: new OID("1.3"), new OctetString(),
171: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
172: vacm.addViewTreeFamily(new OctetString("fullWriteView"),
173: new OID("1.3"), new OctetString(),
174: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
175: vacm.addViewTreeFamily(new OctetString("fullNotifyView"),
176: new OID("1.3"), new OctetString(),
177: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
178:
179: vacm.addViewTreeFamily(new OctetString("restrictedReadView"),
180: new OID("1.3.6.1.2"), new OctetString(),
181: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
182: vacm.addViewTreeFamily(new OctetString("restrictedWriteView"),
183: new OID("1.3.6.1.2.1"), new OctetString(),
184: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
185: vacm.addViewTreeFamily(new OctetString("restrictedNotifyView"),
186: new OID("1.3.6.1.2"), new OctetString(),
187: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
188:
189: vacm.addViewTreeFamily(new OctetString("testReadView"),
190: new OID("1.3.6.1.2"), new OctetString(),
191: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
192: vacm.addViewTreeFamily(new OctetString("testReadView"),
193: new OID("1.3.6.1.2.1.1"), new OctetString(),
194: VacmMIB.vacmViewExcluded, StorageType.nonVolatile);
195: vacm.addViewTreeFamily(new OctetString("testWriteView"),
196: new OID("1.3.6.1.2.1"), new OctetString(),
197: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
198: vacm.addViewTreeFamily(new OctetString("testNotifyView"),
199: new OID("1.3.6.1.2"), new OctetString(),
200: VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
201:
202: }
203:
204: protected void addUsmUser(USM usm) {
205: UsmUser user = new UsmUser(new OctetString("SHADES"),
206: AuthSHA.ID, new OctetString("SHADESAuthPassword"),
207: PrivDES.ID, new OctetString("SHADESPrivPassword"));
208: // usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
209: usm.addUser(user.getSecurityName(), null, user);
210: user = new UsmUser(new OctetString("TEST"), AuthSHA.ID,
211: new OctetString("maplesyrup"), PrivDES.ID,
212: new OctetString("maplesyrup"));
213: usm.addUser(user.getSecurityName(), usm.getLocalEngineID(),
214: user);
215: user = new UsmUser(new OctetString("SHA"), AuthSHA.ID,
216: new OctetString("SHAAuthPassword"), null, null);
217: usm.addUser(user.getSecurityName(), usm.getLocalEngineID(),
218: user);
219: }
220:
221: /* This code illustrates how a table can be created and filled with static
222: data:
223:
224: private static DefaultMOTable createStaticIfTable() {
225: MOTableSubIndex[] subIndexes =
226: new MOTableSubIndex[] { new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER) };
227: MOTableIndex indexDef = new MOTableIndex(subIndexes, false);
228: MOColumn[] columns = new MOColumn[8];
229: int c = 0;
230: columns[c++] =
231: new MOColumn(c, SMIConstants.SYNTAX_INTEGER,
232: MOAccessImpl.ACCESS_READ_ONLY); // ifIndex
233: columns[c++] =
234: new MOColumn(c, SMIConstants.SYNTAX_OCTET_STRING,
235: MOAccessImpl.ACCESS_READ_ONLY);// ifDescr
236: columns[c++] =
237: new MOColumn(c, SMIConstants.SYNTAX_INTEGER,
238: MOAccessImpl.ACCESS_READ_ONLY); // ifType
239: columns[c++] =
240: new MOColumn(c, SMIConstants.SYNTAX_INTEGER,
241: MOAccessImpl.ACCESS_READ_ONLY); // ifMtu
242: columns[c++] =
243: new MOColumn(c, SMIConstants.SYNTAX_GAUGE32,
244: MOAccessImpl.ACCESS_READ_ONLY); // ifSpeed
245: columns[c++] =
246: new MOColumn(c, SMIConstants.SYNTAX_OCTET_STRING,
247: MOAccessImpl.ACCESS_READ_ONLY);// ifPhysAddress
248: columns[c++] =
249: new MOMutableColumn(c, SMIConstants.SYNTAX_INTEGER, // ifAdminStatus
250: MOAccessImpl.ACCESS_READ_WRITE, null);
251: columns[c++] =
252: new MOColumn(c, SMIConstants.SYNTAX_INTEGER,
253: MOAccessImpl.ACCESS_READ_ONLY); // ifOperStatus
254:
255: DefaultMOTable ifTable =
256: new DefaultMOTable(new OID("1.3.6.1.2.1.2.2.1"), indexDef, columns);
257: MOMutableTableModel model = (MOMutableTableModel) ifTable.getModel();
258: Variable[] rowValues1 = new Variable[] {
259: new Integer32(1),
260: new OctetString("eth0"),
261: new Integer32(6),
262: new Integer32(1500),
263: new Gauge32(100000000),
264: new OctetString("00:00:00:00:01"),
265: new Integer32(1),
266: new Integer32(1)
267: };
268: Variable[] rowValues2 = new Variable[] {
269: new Integer32(2),
270: new OctetString("loopback"),
271: new Integer32(24),
272: new Integer32(1500),
273: new Gauge32(10000000),
274: new OctetString("00:00:00:00:02"),
275: new Integer32(1),
276: new Integer32(1)
277: };
278: model.addRow(new DefaultMOMutableRow2PC(new OID("1"), rowValues1));
279: model.addRow(new DefaultMOMutableRow2PC(new OID("2"), rowValues2));
280: ifTable.setVolatile(true);
281: return ifTable;
282: }
283: */
284: protected void initTransportMappings() throws IOException {
285: transportMappings = new TransportMapping[1];
286: Address addr = GenericAddress.parse(address);
287: TransportMapping tm = TransportMappings.getInstance()
288: .createTransportMapping(addr);
289: transportMappings[0] = tm;
290: }
291:
292: public static void main(String[] args) {
293: String address;
294: if (args.length > 0) {
295: address = args[0];
296: } else {
297: address = "0.0.0.0/161";
298: }
299: BasicConfigurator.configure();
300: try {
301: TestAgent testAgent1 = new TestAgent(new File(
302: "SNMP4JTestAgentBC.cfg"), new File(
303: "SNMP4JTestAgentConfig.cfg"));
304: testAgent1.address = address;
305: testAgent1.init();
306: testAgent1.loadConfig(ImportModes.REPLACE_CREATE);
307: testAgent1.addShutdownHook();
308: testAgent1.getServer()
309: .addContext(new OctetString("public"));
310: testAgent1.finishInit();
311: testAgent1.run();
312: testAgent1.sendColdStartNotification();
313: while (true) {
314: try {
315: Thread.sleep(1000);
316: } catch (InterruptedException ex1) {
317: break;
318: }
319: }
320: } catch (IOException ex) {
321: ex.printStackTrace();
322: }
323:
324: }
325:
326: protected void unregisterManagedObjects() {
327: // here we should unregister those objects previously registered...
328: }
329:
330: protected void addCommunities(SnmpCommunityMIB communityMIB) {
331: Variable[] com2sec = new Variable[] {
332: new OctetString("public"), // community name
333: new OctetString("cpublic"), // security name
334: getAgent().getContextEngineID(), // local engine ID
335: new OctetString("public"), // default context name
336: new OctetString(), // transport tag
337: new Integer32(StorageType.nonVolatile), // storage type
338: new Integer32(RowStatus.active) // row status
339: };
340: MOTableRow row = communityMIB.getSnmpCommunityEntry()
341: .createRow(
342: new OctetString("public2public")
343: .toSubIndex(true), com2sec);
344: communityMIB.getSnmpCommunityEntry().addRow(row);
345: // snmpCommunityMIB.setSourceAddressFiltering(true);
346: }
347:
348: protected void registerSnmpMIBs() {
349: heartbeatMIB = new Snmp4jHeartbeatMib(super
350: .getNotificationOriginator(), new OctetString(),
351: super .snmpv2MIB.getSysUpTime());
352: ifMIB = new IfMib();
353: agentppSimulationMIB = new AgentppSimulationMib();
354: super.registerSnmpMIBs();
355: }
356: }
|