1 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 57 public class JMXTestAgent extends BaseAgent { 58 59 static { 61 LogFactory.setLogFactory(new JavaLogFactory()); 62 } 63 64 private static final LogAdapter logger = 65 LogFactory.getLogger(JMXTestAgent.class); 66 67 protected String address; 68 private JvmManagementMib jvmManagementMIB; 69 70 82 public JMXTestAgent(File bootCounterFile, File configFile) throws IOException { 83 super(bootCounterFile, configFile, 84 new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); 85 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 [] args) { 244 String 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 ex1) { 266 break; 267 } 268 } 269 } 270 catch (IOException ex) { 271 ex.printStackTrace(); 272 } 273 274 } 275 276 protected void unregisterManagedObjects() { 277 } 279 280 protected void addCommunities(SnmpCommunityMIB communityMIB) { 281 Variable[] com2sec = new Variable[] { 282 new OctetString("public"), new OctetString("public"), getAgent().getContextEngineID(), new OctetString(), new OctetString(), new Integer32(StorageType.nonVolatile), new Integer32(RowStatus.active) }; 290 MOTableRow row = 291 communityMIB.getSnmpCommunityEntry().createRow( 292 new OctetString("public2public").toSubIndex(true), com2sec); 293 communityMIB.getSnmpCommunityEntry().addRow(row); 294 } 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 |