本文整理汇总了Java中org.snmp4j.CommunityTarget类的典型用法代码示例。如果您正苦于以下问题:Java CommunityTarget类的具体用法?Java CommunityTarget怎么用?Java CommunityTarget使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CommunityTarget类属于org.snmp4j包,在下文中一共展示了CommunityTarget类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUpTarget
import org.snmp4j.CommunityTarget; //导入依赖的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: main
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress("23.23.52.11/161"));
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
VariableBinding[] array = {new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.7")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.10")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.19"))};
pdu.addAll(array);
//pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
System.out.println("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
System.out.println(vb.getVariable().toString());
}
} else {
System.out.println("Error:" + response.getErrorStatusText());
}
}
}
示例3: sendTrap
import org.snmp4j.CommunityTarget; //导入依赖的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();
}
}
示例4: getTarget
import org.snmp4j.CommunityTarget; //导入依赖的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;
}
示例5: start
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
@Override
public void start() {
// Initialize the connection to the external client
try {
snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress(bindAddress + "/" + bindPort));
target.setTimeout(3000); //3s
target.setRetries(1);
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
} catch (IOException ex) {
//
}
super.start();
}
示例6: sendTrap
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
protected void sendTrap(String message) throws Exception {
PDU pdu = new PDU();
pdu.setType(PDU.TRAP);
add(pdu, alertName, message);
add(pdu, alertSeverity, Severity.medium);
add(pdu, alertSource, "rhq.org");
CommunityTarget target = new CommunityTarget();
target.setCommunity(community);
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress(address, port));
target.setTimeout(1000);
target.setRetries(2);
try {
snmp.send(pdu, target);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
示例7: sendTrapV2
import org.snmp4j.CommunityTarget; //导入依赖的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();
}
示例8: getTarget
import org.snmp4j.CommunityTarget; //导入依赖的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;
}
示例9: createDevice
import org.snmp4j.CommunityTarget; //导入依赖的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);
}
示例10: newTarget
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
@Override
public Target newTarget(SnmpTarget target) {
if (!(target instanceof SnmpV2cTarget)) return null;
CommunityTarget communityTarget = new CommunityTarget();
communityTarget.setVersion(SnmpConstants.version2c);
String community = ((SnmpV2cTarget) target).getCommunity();
communityTarget.setCommunity(new OctetString(community));
return communityTarget;
}
示例11: createDefault
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
/**
* 创建对象communityTarget,用于返回target
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public static CommunityTarget createDefault(String ip, String community) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip
+ "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);
target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
target.setRetries(DEFAULT_RETRY);
return target;
}
示例12: sendPDU
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
private void sendPDU(CommunityTarget target, PDU pdu) {
try {
snmp.send(pdu, target, null, this);
} catch (IOException e) {
logger.error("Error sending PDU", e);
}
}
示例13: createDefault
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
/**
* Create communityTarget.
*
* @param ip
* @param community
* @return CommunityTarget
*/
private static CommunityTarget createDefault(String ip, String community) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip
+ "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);
target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
target.setRetries(DEFAULT_RETRY);
return target;
}
示例14: createDefault
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
/**
* Create communityTarget.
*
* @param ip
* @param community
* @return CommunityTarget
*/
private static CommunityTarget createDefault(String ip, String community) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);
target.setVersion(DEFAULT_VERSION);
target.setRetries(DEFAULT_RETRY);
target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
return target;
}
示例15: getTarget
import org.snmp4j.CommunityTarget; //导入依赖的package包/类
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
return target;
}