use of org.snmp4j.TransportMapping in project mysql_perf_analyzer by yahoo.
the class SNMPClient method start.
/**
* Start the Snmp session. If you forget the listen() method you will not
* get any answers because the communication is asynchronous
* and the listen() method listens for answers.
* @throws IOException
*/
public void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
if (// add v3 support
"3".equals(this.version)) {
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
}
// Do not forget this line!
transport.listen();
}
use of org.snmp4j.TransportMapping in project pancm_project by xuwujing.
the class SnmpHelper method snmpSet.
public static void snmpSet(String ip, String community, PDU pdu) {
CommunityTarget target = createDefault(ip, community);
TransportMapping transport = null;
Snmp snmp = null;
List<String> result = new LinkedList<>();
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
sendRequest(snmp, pdu, target);
} catch (Exception e) {
log.error("SNMP walk Exception: ", e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
}
use of org.snmp4j.TransportMapping in project pentaho-kettle by pentaho.
the class JobEntrySNMPTrap method execute.
public Result execute(Result previousResult, int nr) {
Result result = previousResult;
result.setNrErrors(1);
result.setResult(false);
String servername = environmentSubstitute(serverName);
int nrPort = Const.toInt(environmentSubstitute("" + port), DEFAULT_PORT);
String Oid = environmentSubstitute(oid);
int timeOut = Const.toInt(environmentSubstitute("" + timeout), DEFAULT_TIME_OUT);
int retry = Const.toInt(environmentSubstitute("" + nrretry), 1);
String messageString = environmentSubstitute(message);
Snmp snmp = null;
try {
TransportMapping transMap = new DefaultUdpTransportMapping();
snmp = new Snmp(transMap);
UdpAddress udpAddress = new UdpAddress(InetAddress.getByName(servername), nrPort);
ResponseEvent response = null;
if (targettype.equals(target_type_Code[0])) {
// Community target
String community = environmentSubstitute(comString);
CommunityTarget target = new CommunityTarget();
PDUv1 pdu1 = new PDUv1();
transMap.listen();
target.setCommunity(new OctetString(community));
target.setVersion(SnmpConstants.version1);
target.setAddress(udpAddress);
if (target.getAddress().isValid()) {
if (log.isDebug()) {
logDebug("Valid IP address");
}
} else {
throw new KettleException("Invalid IP address");
}
target.setRetries(retry);
target.setTimeout(timeOut);
// create the PDU
pdu1.setGenericTrap(6);
pdu1.setSpecificTrap(PDUv1.ENTERPRISE_SPECIFIC);
pdu1.setEnterprise(new OID(Oid));
pdu1.add(new VariableBinding(new OID(Oid), new OctetString(messageString)));
response = snmp.send(pdu1, target);
} else {
// User target
String userName = environmentSubstitute(user);
String passPhrase = environmentSubstitute(passphrase);
String engineID = environmentSubstitute(engineid);
UserTarget usertarget = new UserTarget();
transMap.listen();
usertarget.setAddress(udpAddress);
if (usertarget.getAddress().isValid()) {
if (log.isDebug()) {
logDebug("Valid IP address");
}
} else {
throw new KettleException("Invalid IP address");
}
usertarget.setRetries(retry);
usertarget.setTimeout(timeOut);
usertarget.setVersion(SnmpConstants.version3);
usertarget.setSecurityLevel(SecurityLevel.AUTH_PRIV);
usertarget.setSecurityName(new OctetString("MD5DES"));
// Since we are using SNMPv3 we use authenticated users
// this is handled by the UsmUser and USM class
UsmUser uu = new UsmUser(new OctetString(userName), AuthMD5.ID, new OctetString(passPhrase), PrivDES.ID, new OctetString(passPhrase));
USM usm = snmp.getUSM();
if (usm == null) {
throw new KettleException("Null Usm");
} else {
usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
usm.addUser(new OctetString(userName), uu);
if (log.isDebug()) {
logDebug("Valid Usm");
}
}
// create the PDU
ScopedPDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID(Oid), new OctetString(messageString)));
pdu.setType(PDU.TRAP);
if (!Utils.isEmpty(engineID)) {
pdu.setContextEngineID(new OctetString(engineID));
}
// send the PDU
response = snmp.send(pdu, usertarget);
}
if (response != null) {
if (log.isDebug()) {
logDebug("Received response from: " + response.getPeerAddress() + response.toString());
}
}
result.setNrErrors(0);
result.setResult(true);
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobEntrySNMPTrap.ErrorGetting", e.getMessage()));
} finally {
try {
if (snmp != null) {
snmp.close();
}
} catch (Exception e) {
/* Ignore */
}
}
return result;
}
use of org.snmp4j.TransportMapping in project thingsboard by thingsboard.
the class SnmpDeviceSimulatorV3 method initTransportMappings.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void initTransportMappings() throws IOException {
transportMappings = new TransportMapping[2];
Address addr = GenericAddress.parse(address);
TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);
transportMappings[0] = tm;
transportMappings[1] = new DefaultTcpTransportMapping(new TcpAddress(address));
}
Aggregations