private PDU sendRequestV1V2(PDU pdu, int version) throws Exception { PDU response; CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort())); target.setVersion(version); if (m_timeout > 0) { target.setTimeout(m_timeout); } TransportMapping<UdpAddress> transport = null; try { transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); transport.listen(); ResponseEvent e = snmp.send(pdu, target); response = e.getResponse(); } finally { if (transport != null) { transport.close(); } } return response; }
private PDU sendRequestV3(PDU pdu) throws IOException { PDU response; OctetString userId = new OctetString("opennmsUser"); OctetString pw = new OctetString("0p3nNMSv3"); UserTarget target = new UserTarget(); target.setSecurityLevel(SecurityLevel.AUTH_PRIV); target.setSecurityName(userId); target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort())); target.setVersion(SnmpConstants.version3); if (m_timeout > 0) { target.setTimeout(m_timeout); } else { target.setTimeout(5000); } TransportMapping<UdpAddress> transport = null; try { USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw); snmp.getUSM().addUser(userId, user); transport.listen(); ResponseEvent e = snmp.send(pdu, target); response = e.getResponse(); } finally { if (transport != null) { transport.close(); } } return response; }
// @Override public void stop() throws IOException { transport.close(); }
/** * Starts the <code>MockSnmpAgent</code> running. Meant to be called from the <code>start</code> * method of class <code>Thread</code>, but could also be used to bring up a standalone mock * agent. * * @see org.snmp4j.agent.BaseAgent#run() * @author Jeff Gehlbach */ @Override public void run() { s_log.info("MockSnmpAgent: Initializing SNMP Agent"); try { init(); s_log.info("MockSnmpAgent: Finished 'init' loading config"); loadConfig(ImportModes.UPDATE_CREATE); s_log.info("MockSnmpAgent: finished 'loadConfig' adding shutdown hook"); addShutdownHook(); s_log.info("MockSnmpAgent: finished 'addShutdownHook' finishing init"); finishInit(); s_log.info("MockSnmpAgent: finished 'finishInit' running agent"); super.run(); s_log.info("MockSnmpAgent: finished running Agent - setting running to true"); m_running.set(true); } catch (final BindException e) { s_log.error( String.format( "MockSnmpAgent: Unable to bind to %s. You probably specified an invalid address or a port < 1024 and are not running as root. Exception: %s", m_address.get(), e), e); } catch (final Throwable t) { s_log.error("MockSnmpAgent: An error occurred while initializing: " + t, t); t.printStackTrace(); } boolean interrupted = false; s_log.info( "MockSnmpAgent: Initialization Complete processing message until agent is shutdown."); while (m_running.get()) { try { Thread.sleep(10); // fast, Fast, FAST, *FAST*!!! } catch (final InterruptedException e) { interrupted = true; break; } } s_log.info("MockSnmpAgent: Shutdown called stopping agent."); for (final TransportMapping transportMapping : transportMappings) { try { if (transportMapping != null) { transportMapping.close(); } } catch (final IOException t) { s_log.error( "MockSnmpAgent: an error occurred while closing the transport mapping " + transportMapping + ": " + t, t); } } m_stopped.set(true); s_log.info("MockSnmpAgent: Agent is no longer running."); if (interrupted) { Thread.currentThread().interrupt(); } }