use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getDiskIndex.
private int getDiskIndex(String device) throws IOException {
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
logger.fine("Query " + this.address + " for disk data: " + device);
@SuppressWarnings("unchecked") List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID("." + DISK_TABLE_DEVICE_OID) }, null, null);
for (TableEvent event : events) {
if (event.isError()) {
logger.warning(this.address + ": SNMP event error: " + event.getErrorMessage());
continue;
// throw new RuntimeException(event.getErrorMessage());
}
for (VariableBinding vb : event.getColumns()) {
String key = vb.getOid().toString();
String value = vb.getVariable().toString();
if (value != null && value.equals(device)) {
logger.fine("Find device OID entry: " + key);
int index = -1;
String[] strs = key.split("\\.");
try {
index = Integer.parseInt(strs[strs.length - 1]);
} catch (Exception ex) {
}
return index;
}
}
}
return -1;
}
use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getNetIfIndexes.
private Map<Integer, String> getNetIfIndexes(String device) throws IOException {
Map<Integer, String> ifMaps = new HashMap<Integer, String>();
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
logger.fine("Query " + this.address + " for network interface, excluding lo");
@SuppressWarnings("unchecked") List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID("." + IF_TABLE_DEVICE_OID) }, null, null);
for (TableEvent event : events) {
if (event.isError()) {
logger.warning(this.address + ": SNMP event error: " + event.getErrorMessage());
continue;
// throw new RuntimeException(event.getErrorMessage());
}
for (VariableBinding vb : event.getColumns()) {
String key = vb.getOid().toString();
String value = vb.getVariable().toString();
if (device != null && !device.isEmpty() && !value.equalsIgnoreCase(device))
continue;
if (value != null && !value.equalsIgnoreCase("lo")) {
logger.fine("Find device OID entry: " + key);
int index = -1;
String[] strs = key.split("\\.");
try {
index = Integer.parseInt(strs[strs.length - 1]);
ifMaps.put(index, value);
} catch (Exception ex) {
}
}
}
}
return ifMaps;
}
use of org.snmp4j.smi.VariableBinding in project cloudstack by apache.
the class SnmpHelper method createPDU.
private PDU createPDU(SnmpTrapInfo snmpTrapInfo) {
PDU trap = new PDU();
trap.setType(PDU.TRAP);
int alertType = snmpTrapInfo.getAlertType() + 1;
if (alertType > 0) {
long sysUpTimeTicks = ManagementFactory.getRuntimeMXBean().getUptime() / 10;
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks)));
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType)));
if (snmpTrapInfo.getDataCenterId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId())));
}
if (snmpTrapInfo.getPodId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId())));
}
if (snmpTrapInfo.getClusterId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId())));
}
if (snmpTrapInfo.getMessage() != null) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage())));
} else {
throw new CloudRuntimeException(" What is the use of alert without message ");
}
if (snmpTrapInfo.getGenerationTime() != null) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString())));
} else {
trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
}
} else {
throw new CloudRuntimeException(" Invalid alert Type ");
}
return trap;
}
use of org.snmp4j.smi.VariableBinding 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.smi.VariableBinding in project thingsboard by thingsboard.
the class PduService method createPdu.
public PDU createPdu(DeviceSessionContext sessionContext, SnmpCommunicationConfig communicationConfig, Map<String, String> values) {
PDU pdu = setUpPdu(sessionContext);
pdu.setType(communicationConfig.getMethod().getCode());
pdu.addAll(communicationConfig.getAllMappings().stream().filter(mapping -> values.isEmpty() || values.containsKey(mapping.getKey())).map(mapping -> Optional.ofNullable(values.get(mapping.getKey())).map(value -> {
Variable variable = toSnmpVariable(value, mapping.getDataType());
return new VariableBinding(new OID(mapping.getOid()), variable);
}).orElseGet(() -> new VariableBinding(new OID(mapping.getOid())))).collect(Collectors.toList()));
return pdu;
}
Aggregations