本文整理汇总了Java中org.snmp4j.smi.Address类的典型用法代码示例。如果您正苦于以下问题:Java Address类的具体用法?Java Address怎么用?Java Address使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Address类属于org.snmp4j.smi包,在下文中一共展示了Address类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUpTarget
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* Helper method that initializes the snmp target to listening mode. This
* method is explicitly for V1 and V2 messages. This method will create and
* set up the TransportMapping and target for SNMP V1 and V2. It creates a
* TransportMapping and puts it in the listening mode. Also Creates
* CommunityTarget object and sets SNMP target properties.
*
* @param communityName
* Community name of the target
* @param targetIP
* IP address of Target machine
* @param portNumber
* Port number
* @return The Target created
* @throws IOException
* IOException
*/
private Target setUpTarget( final String communityName, final String targetIP, final int portNumber )
throws IOException
{
final InetAddress inetAddress = InetAddress.getByName( targetIP );
final Address address = new UdpAddress( inetAddress, portNumber );
final OctetString community = new OctetString( communityName );
final TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
snmp.listen();
// Creating the communityTarget object and setting its properties
final CommunityTarget communityTarget = new CommunityTarget();
communityTarget.setCommunity( community );
// TODO Needs to check also for v2 messages
communityTarget.setVersion( SnmpConstants.version1 );
communityTarget.setAddress( address );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setRetries( SnmpManager.DEFAULT_RETRIES );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setTimeout( SnmpManager.DEFAULT_TIMEOUT );
return communityTarget;
}
示例2: sendTrap
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* Send next-in-line trap from queue to to SNMP server
*/
public void sendTrap() {
String trapVal = trapQueue.poll();
if (trapVal == null) {
return;
}
try {
PDUv1 trapPdu = createTrapPDU(trapVal);
DefaultUdpTransportMapping tm = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(tm);
tm.listen();
OctetString community = new OctetString(SNMP_XAP_COMMUNITY);
Address add = GenericAddress.parse("udp" + ":" + snmpServerIP + "/" + snmpServerPort);
CommunityTarget target = new CommunityTarget(add, community);
target.setVersion(SnmpConstants.version1);
target.setRetries(0);
target.setTimeout(5000);
snmp.send(trapPdu, target);
}
catch (IOException e) {
e.printStackTrace();
}
}
示例3: newListener
import org.snmp4j.smi.Address; //导入依赖的package包/类
@Override
public SnmpListener newListener(String address, int port, Mib mib) {
lock.lock();
try {
Address listenAddress = address == null ?
new UdpAddress(port) : createAddress(address, port);
Snmp4jListener listener = new Snmp4jListener(getSnmp(),
listenAddress, new Snmp4jNotificationEventFactory(
new SimpleVarbindFactory(mib)), this);
listener.open();
refs.put(listener, listener);
return listener;
}
finally {
lock.unlock();
}
}
示例4: processPdu
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* Will be called whenever a {@link PDU} is received on the given port
* specified in the listen() method. It extracts a {@link Variable}
* according to the configured OID prefix and sends its value to the event
* bus.
*/
public void processPdu(CommandResponderEvent event) {
Address addr = event.getPeerAddress();
if (addr == null) {
return;
}
String s = addr.toString().split("/")[0];
if (s == null) {
logger.error("TRAP: failed to translate address {}", addr);
dispatchPdu(addr, event.getPDU());
} else {
// Need to change the port to 161, which is what the bindings are configured for since
// at least some SNMP devices send traps from a random port number. Otherwise the trap
// won't be found as the address check will fail. It feels like there should be a better
// way to do this!!!
Address address = GenericAddress.parse("udp:" + s + "/161");
dispatchPdu(address, event.getPDU());
}
}
示例5: getTarget
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
if("3".equals(this.version))return getTargetV3();
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
//logger.info("snmp version "+this.version+", community: "+this.community);
if(this.community == null || this.community.isEmpty())
target.setCommunity(new OctetString("public"));
else
target.setCommunity(new OctetString(this.community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(5000);
target.setVersion(this.getVersionInt());
return target;
}
示例6: getTargetV3
import org.snmp4j.smi.Address; //导入依赖的package包/类
private Target getTargetV3() {
//logger.info("Use SNMP v3, "+this.privacyprotocol +"="+this.password+", "+this.privacyprotocol+"="+this.privacypassphrase);
OID authOID = AuthMD5.ID;
if("SHA".equals(this.authprotocol))
authOID = AuthSHA.ID;
OID privOID = PrivDES.ID;
if(this.privacyprotocol == null || this.privacyprotocol.isEmpty())
privOID = null;
UsmUser user = new UsmUser(new OctetString(this.username),
authOID, new OctetString(this.password), //auth
privOID, this.privacypassphrase!=null?new OctetString(this.privacypassphrase):null); //enc
snmp.getUSM().addUser(new OctetString(this.username), user);
Address targetAddress = GenericAddress.parse(address);
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(this.getVersionInt());
if(privOID != null)
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
else
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
target.setSecurityName(new OctetString(this.username));
return target;
}
示例7: snmpMessageToHumanReadable
import org.snmp4j.smi.Address; //导入依赖的package包/类
static PDU snmpMessageToHumanReadable(String s) throws IOException {
final OctetString message = OctetString.fromHexString(s, ':');
final Address address = new UdpAddress();
final TransportMapping transportMapping = (address instanceof UdpAddress) ?
new DefaultUdpTransportMapping((UdpAddress) address)
: new DefaultTcpTransportMapping((TcpAddress) address);
final MessageDispatcher messageDispatcher = new
MessageDispatcherImpl();
SecurityProtocols.getInstance().addDefaultProtocols();
final SnmpMessageAnalyzer snmpMessageAnalyzer = new SnmpMessageAnalyzer();
final CommandResponder commandResponder = snmpMessageAnalyzer;
messageDispatcher.addCommandResponder(commandResponder);
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
messageDispatcher.addMessageProcessingModel(new MPv3());
messageDispatcher.processMessage(transportMapping, address, ByteBuffer.wrap(message.getValue()));
PDU result = snmpMessageAnalyzer.msg;
snmpMessageAnalyzer.msg = null;
return result;
}
示例8: sendTrapV2
import org.snmp4j.smi.Address; //导入依赖的package包/类
public static void sendTrapV2(String port) throws IOException {
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Specify receiver
Address targetaddress = new UdpAddress("127.0.0.1/" + port);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetaddress);
// Send
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.send(trap, target, null, null);
snmp.close();
}
示例9: startUp
import org.snmp4j.smi.Address; //导入依赖的package包/类
@Override
public void startUp() throws IOException {
log.info("Snmp Trap Receiver Start");
log.info("listened on " + Configure.getInstance().getUdpTrapIpPort());
ThreadPool pool = ThreadPool.create(Const.THREAD_POOL_NAME, Const.AGENT_THREAD_NUM);
MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(pool, new MessageDispatcherImpl());
Address listenAddress = GenericAddress.parse(Configure.getInstance().getUdpTrapIpPort());
TransportMapping transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
// ����SNMP������ʹ�俪ʼ����
Snmp snmp = new Snmp(dispatcher, transport);
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.listen();
snmp.addCommandResponder(new CommandResponderImpl());
}
示例10: processPdu
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* Will be called whenever a {@link PDU} is received on the given port
* specified in the listen() method. It extracts a {@link Variable}
* according to the configured OID prefix and sends its value to the event
* bus.
*/
@Override
public void processPdu(CommandResponderEvent event) {
Address addr = event.getPeerAddress();
if (addr == null) {
return;
}
String s = addr.toString().split("/")[0];
if (s == null) {
logger.error("TRAP: failed to translate address {}", addr);
dispatchPdu(addr, event.getPDU());
} else {
// Need to change the port to 161, which is what the bindings are configured for since
// at least some SNMP devices send traps from a random port number. Otherwise the trap
// won't be found as the address check will fail. It feels like there should be a better
// way to do this!!!
Address address = GenericAddress.parse("udp:" + s + "/161");
dispatchPdu(address, event.getPDU());
}
}
示例11: getSNMPv3Target
import org.snmp4j.smi.Address; //导入依赖的package包/类
/**
* This method returns a Target, which contains information about where the data should be fetched and how.
*
* @return
*/
private Target getSNMPv3Target() {
Address targetAddress = GenericAddress.parse(address);
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setVersion(SnmpConstants.version3); // SnmpConstants.version3
target.setRetries(2);
target.setTimeout(2500);
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV); // SecurityLevel.AUTH_NOPRIV
target.setSecurityName(new OctetString(ver3Username));
return target;
}
示例12: getTarget
import org.snmp4j.smi.Address; //导入依赖的package包/类
private Target getTarget() {
Address targetAddress = GenericAddress.parse("udp:" + snmpHost + "/" + snmpPort);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion("1".equals(version) ? SnmpConstants.version1 : SnmpConstants.version2c);
return target;
}
示例13: createDevice
import org.snmp4j.smi.Address; //导入依赖的package包/类
private void createDevice(String ipAddress, int port) throws IOException {
Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
snmp = new Snmp(transport);
// setting up target
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(3);
target.setTimeout(1000 * 3);
target.setVersion(SnmpConstants.version2c);
target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU);
}
示例14: createAddress
import org.snmp4j.smi.Address; //导入依赖的package包/类
private static Address createAddress(String address, int port) {
Assert.notNull(address, "address is required");
StringBuilder sb = new StringBuilder();
sb.append("udp:");
sb.append(address);
sb.append("/").append(port);
return GenericAddress.parse(sb.toString());
}
示例15: newTarget
import org.snmp4j.smi.Address; //导入依赖的package包/类
private SnmpTarget newTarget(CommandResponderEvent event) {
SnmpTargetBase target = newTarget(event.getMessageProcessingModel());
Address address = event.getPeerAddress();
String uri = address.toString();
int i = uri.indexOf('/');
if (i == -1) i = uri.length();
target.setAddress(uri.substring(0, i));
return target;
}