KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > jmx > example > JMXTestAgent


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - JMXTestAgent.java
4   _##
5   _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23
24 package org.snmp4j.agent.mo.jmx.example;
25
26 import java.io.*;
27
28 import org.snmp4j.*;
29 import org.snmp4j.agent.*;
30 import org.snmp4j.agent.mo.*;
31 import org.snmp4j.agent.mo.snmp.*;
32 import org.snmp4j.mp.*;
33 import org.snmp4j.security.*;
34 import org.snmp4j.smi.*;
35 import org.snmp4j.transport.*;
36 import org.snmp4j.agent.io.ImportModes;
37 import org.snmp4j.util.ThreadPool;
38 import org.snmp4j.log.LogFactory;
39 import org.snmp4j.agent.security.MutableVACM;
40 import org.snmp4j.agent.mo.jmx.mibs.JvmManagementMib;
41 import org.snmp4j.agent.mo.jmx.*;
42 import org.snmp4j.agent.mo.jmx.mibs.JvmManagementMibInst;
43 import org.snmp4j.log.LogAdapter;
44 import org.snmp4j.log.JavaLogFactory;
45
46 /**
47  * The <code>JMXTestAgent</code> is a sample SNMP agent implementation of all
48  * features (MIB implementations) provided by the SNMP4J-AgentJMX framework.
49  * For demonstration purposes, the agent implements the JVM-MANAGEMENT-MIB as
50  * far as this can be done without violating the SNMP standard. There are flaws
51  * in that MIB module that violate the SNMP standard which have been implemented
52  * in a standard conforming way.
53  *
54  * @author Frank Fock
55  * @version 1.0
56  */

57 public class JMXTestAgent extends BaseAgent {
58
59   // initialize Log4J logging
60
static {
61     LogFactory.setLogFactory(new JavaLogFactory());
62   }
63
64   private static final LogAdapter logger =
65       LogFactory.getLogger(JMXTestAgent.class);
66
67   protected String JavaDoc address;
68   private JvmManagementMib jvmManagementMIB;
69
70   /**
71    * Creates the test agent with a file to read and store the boot counter and
72    * a file to read and store its configuration.
73    *
74    * @param bootCounterFile
75    * a file containing the boot counter in serialized form (as expected by
76    * BaseAgent).
77    * @param configFile
78    * a configuration file with serialized management information.
79    * @throws IOException
80    * if the boot counter or config file cannot be read properly.
81    */

82   public JMXTestAgent(File bootCounterFile, File configFile) throws IOException {
83     super(bootCounterFile, configFile,
84           new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
85 // Alternatively: OctetString.fromHexString("00:00:00:00:00:00:02", ':');
86
agent.setThreadPool(ThreadPool.create("RequestPool", 4));
87   }
88
89   protected void registerManagedObjects() {
90   }
91
92   protected void addNotificationTargets(SnmpTargetMIB targetMIB,
93                                         SnmpNotificationMIB notificationMIB) {
94     targetMIB.addDefaultTDomains();
95
96     targetMIB.addTargetAddress(new OctetString("notification"),
97                                TransportDomains.transportDomainUdpIpv4,
98                                new OctetString(new UdpAddress("127.0.0.1/162").getValue()),
99                                200, 1,
100                                new OctetString("notify"),
101                                new OctetString("v2c"),
102                                StorageType.permanent);
103     targetMIB.addTargetParams(new OctetString("v2c"),
104                               MessageProcessingModel.MPv2c,
105                               SecurityModel.SECURITY_MODEL_SNMPv2c,
106                               new OctetString("public"),
107                               SecurityLevel.NOAUTH_NOPRIV,
108                               StorageType.permanent);
109     notificationMIB.addNotifyEntry(new OctetString("default"),
110                                    new OctetString("notify"),
111                                    SnmpNotificationMIB.SnmpNotifyTypeEnum.trap,
112                                    StorageType.permanent);
113   }
114
115   protected void addViews(VacmMIB vacm) {
116     vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv1,
117                   new OctetString("public"),
118                   new OctetString("v1v2group"),
119                   StorageType.nonVolatile);
120     vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c,
121                   new OctetString("public"),
122                   new OctetString("v1v2group"),
123                   StorageType.nonVolatile);
124     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
125                   new OctetString("SHADES"),
126                   new OctetString("v3group"),
127                   StorageType.nonVolatile);
128     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
129                   new OctetString("TEST"),
130                   new OctetString("v3test"),
131                   StorageType.nonVolatile);
132     vacm.addGroup(SecurityModel.SECURITY_MODEL_USM,
133                   new OctetString("SHA"),
134                   new OctetString("v3restricted"),
135                   StorageType.nonVolatile);
136
137     vacm.addAccess(new OctetString("v1v2group"), new OctetString(),
138                    SecurityModel.SECURITY_MODEL_ANY,
139                    SecurityLevel.NOAUTH_NOPRIV,
140                    MutableVACM.VACM_MATCH_EXACT,
141                    new OctetString("fullReadView"),
142                    new OctetString("fullWriteView"),
143                    new OctetString("fullNotifyView"),
144                    StorageType.nonVolatile);
145     vacm.addAccess(new OctetString("v3group"), new OctetString(),
146                    SecurityModel.SECURITY_MODEL_USM,
147                    SecurityLevel.AUTH_PRIV,
148                    MutableVACM.VACM_MATCH_EXACT,
149                    new OctetString("fullReadView"),
150                    new OctetString("fullWriteView"),
151                    new OctetString("fullNotifyView"),
152                    StorageType.nonVolatile);
153     vacm.addAccess(new OctetString("v3restricted"), new OctetString(),
154                    SecurityModel.SECURITY_MODEL_USM,
155                    SecurityLevel.AUTH_NOPRIV,
156                    MutableVACM.VACM_MATCH_EXACT,
157                    new OctetString("restrictedReadView"),
158                    new OctetString("restrictedWriteView"),
159                    new OctetString("restrictedNotifyView"),
160                    StorageType.nonVolatile);
161     vacm.addAccess(new OctetString("v3test"), new OctetString(),
162                    SecurityModel.SECURITY_MODEL_USM,
163                    SecurityLevel.AUTH_PRIV,
164                    MutableVACM.VACM_MATCH_EXACT,
165                    new OctetString("testReadView"),
166                    new OctetString("testWriteView"),
167                    new OctetString("testNotifyView"),
168                    StorageType.nonVolatile);
169
170     vacm.addViewTreeFamily(new OctetString("fullReadView"), new OID("1.3"),
171                            new OctetString(), VacmMIB.vacmViewIncluded,
172                            StorageType.nonVolatile);
173     vacm.addViewTreeFamily(new OctetString("fullWriteView"), new OID("1.3"),
174                            new OctetString(), VacmMIB.vacmViewIncluded,
175                            StorageType.nonVolatile);
176     vacm.addViewTreeFamily(new OctetString("fullNotifyView"), new OID("1.3"),
177                            new OctetString(), VacmMIB.vacmViewIncluded,
178                            StorageType.nonVolatile);
179
180     vacm.addViewTreeFamily(new OctetString("restrictedReadView"),
181                            new OID("1.3.6.1.2"),
182                            new OctetString(), VacmMIB.vacmViewIncluded,
183                            StorageType.nonVolatile);
184     vacm.addViewTreeFamily(new OctetString("restrictedWriteView"),
185                            new OID("1.3.6.1.2.1"),
186                            new OctetString(),
187                            VacmMIB.vacmViewIncluded,
188                            StorageType.nonVolatile);
189     vacm.addViewTreeFamily(new OctetString("restrictedNotifyView"),
190                            new OID("1.3.6.1.2"),
191                            new OctetString(), VacmMIB.vacmViewIncluded,
192                            StorageType.nonVolatile);
193
194     vacm.addViewTreeFamily(new OctetString("testReadView"),
195                            new OID("1.3.6.1.2"),
196                            new OctetString(), VacmMIB.vacmViewIncluded,
197                            StorageType.nonVolatile);
198     vacm.addViewTreeFamily(new OctetString("testReadView"),
199                            new OID("1.3.6.1.2.1.1"),
200                            new OctetString(), VacmMIB.vacmViewExcluded,
201                            StorageType.nonVolatile);
202     vacm.addViewTreeFamily(new OctetString("testWriteView"),
203                            new OID("1.3.6.1.2.1"),
204                            new OctetString(),
205                            VacmMIB.vacmViewIncluded,
206                            StorageType.nonVolatile);
207     vacm.addViewTreeFamily(new OctetString("testNotifyView"),
208                            new OID("1.3.6.1.2"),
209                            new OctetString(), VacmMIB.vacmViewIncluded,
210                            StorageType.nonVolatile);
211
212   }
213
214   protected void addUsmUser(USM usm) {
215     UsmUser user = new UsmUser(new OctetString("SHADES"),
216                                AuthSHA.ID,
217                                new OctetString("SHADESAuthPassword"),
218                                PrivDES.ID,
219                                new OctetString("SHADESPrivPassword"));
220     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
221     user = new UsmUser(new OctetString("TEST"),
222                                AuthSHA.ID,
223                                new OctetString("maplesyrup"),
224                                PrivDES.ID,
225                                new OctetString("maplesyrup"));
226     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
227     user = new UsmUser(new OctetString("SHA"),
228                                AuthSHA.ID,
229                                new OctetString("SHAAuthPassword"),
230                                null,
231                                null);
232     usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
233   }
234
235   protected void initTransportMappings() throws IOException {
236     transportMappings = new TransportMapping[1];
237     Address addr = GenericAddress.parse(address);
238     TransportMapping tm =
239         TransportMappings.getInstance().createTransportMapping(addr);
240     transportMappings[0] = tm;
241   }
242
243   public static void main(String JavaDoc[] args) {
244     String JavaDoc address;
245     if (args.length > 0) {
246       address = args[0];
247     }
248     else {
249       address = "0.0.0.0/161";
250     }
251     try {
252       JMXTestAgent testAgent1 =
253           new JMXTestAgent(new File("SNMP4JJMXTestAgentBC.cfg"),
254                            new File("SNMP4JJMXTestAgentConfig.cfg"));
255       testAgent1.address = address;
256       testAgent1.init();
257       testAgent1.loadConfig(ImportModes.UPDATE_CREATE);
258       testAgent1.addShutdownHook();
259       testAgent1.finishInit();
260       testAgent1.run();
261       while (true) {
262         try {
263           Thread.sleep(1000);
264         }
265         catch (InterruptedException JavaDoc ex1) {
266           break;
267         }
268       }
269     }
270     catch (IOException ex) {
271       ex.printStackTrace();
272     }
273
274   }
275
276   protected void unregisterManagedObjects() {
277     // here we should unregister those objects previously registered...
278
}
279
280   protected void addCommunities(SnmpCommunityMIB communityMIB) {
281     Variable[] com2sec = new Variable[] {
282         new OctetString("public"), // community name
283
new OctetString("public"), // security name
284
getAgent().getContextEngineID(), // local engine ID
285
new OctetString(), // default context name
286
new OctetString(), // transport tag
287
new Integer32(StorageType.nonVolatile), // storage type
288
new Integer32(RowStatus.active) // row status
289
};
290     MOTableRow row =
291         communityMIB.getSnmpCommunityEntry().createRow(
292           new OctetString("public2public").toSubIndex(true), com2sec);
293     communityMIB.getSnmpCommunityEntry().addRow(row);
294 // snmpCommunityMIB.setSourceAddressFiltering(true);
295
}
296
297   protected void registerSnmpMIBs() {
298     super.registerSnmpMIBs();
299     try {
300       jvmManagementMIB =
301           new JvmManagementMibInst(super.agent.getNotificationOriginator());
302       jvmManagementMIB.registerMOs(server, null);
303     }
304     catch (DuplicateRegistrationException ex) {
305       logger.error("Unable to register JVM-MANAGEMENT-MIB", ex);
306     }
307   }
308
309 }
310
Popular Tags