use of org.snmp4j.smi.VariableBinding in project LogHub by fbacchella.
the class SNMPTrapGeneratorClient2 method sendSnmpV3Trap.
/**
* Sends the v3 trap
*/
private static void sendSnmpV3Trap() {
try {
Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES192());
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID, new OctetString("UserName"), PrivAES128.ID, new OctetString("UserName")));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(11500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("MD5DES"));
// Create PDU for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));
// Send the PDU
snmp.send(pdu, target);
System.out.println("Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
snmp.addCommandResponder(new CommandResponder() {
public void processPdu(CommandResponderEvent arg0) {
System.out.println(arg0);
}
});
snmp.close();
} catch (Exception e) {
System.err.println("Error in Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method get.
/**
* This method is capable of handling multiple OIDs
* @param oids
* @return
* @throws IOException
*/
public Map<OID, String> get(OID[] oids) throws IOException {
PDU pdu = createPDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if (event != null) {
PDU pdu2 = event.getResponse();
VariableBinding[] binds = pdu2 != null ? event.getResponse().toArray() : null;
if (binds != null) {
Map<OID, String> res = new LinkedHashMap<OID, String>(binds.length);
for (VariableBinding b : binds) res.put(b.getOid(), b.getVariable().toString());
return res;
} else
return null;
}
throw new RuntimeException("GET timed out");
}
use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getNetIfData.
public Map<String, List<SNMPTriple>> getNetIfData(String device) throws IOException {
Map<Integer, String> ifMaps = new HashMap<Integer, String>();
Map<String, List<SNMPTriple>> resMap = new HashMap<String, List<SNMPTriple>>();
Map<String, String> res = new HashMap<String, 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_OID) }, null, null);
for (TableEvent event : events) {
if (event.isError()) {
logger.warning(this.address + ": SNMP event error: " + event.getErrorMessage() + ", already returned: " + ifMaps);
continue;
// throw new RuntimeException(event.getErrorMessage());
}
for (VariableBinding vb : event.getColumns()) {
String key = vb.getOid().toString();
String value = vb.getVariable().toString();
res.put(key, value);
if (key.startsWith(IF_TABLE_DEVICE_OID + ".")) {
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) {
}
}
}
}
// for var
}
for (Map.Entry<Integer, String> entry : ifMaps.entrySet()) {
int index = entry.getKey();
String ifName = entry.getValue();
// ignore the case with no incoming and no outgoing traffic
if ("0".equals(res.get(IF_TABLE_OID + ".6." + index)) && "0".equals(res.get(IF_TABLE_OID + ".10." + index)))
continue;
resMap.put(ifName, new ArrayList<SNMPTriple>(IF_TABLE_ENTRIES.length));
for (int i = 1; i < IF_TABLE_ENTRIES.length; i++) {
if (IF_TABLE_ENTRIES[i].length() == 0)
continue;
resMap.get(ifName).add(new SNMPTriple("." + IF_TABLE_OID + "." + i + "." + index, IF_TABLE_ENTRIES[i], res.get(IF_TABLE_OID + "." + i + "." + index)));
}
}
return resMap;
}
use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getMultiDiskData.
public Map<String, List<SNMPTriple>> getMultiDiskData() throws IOException {
Map<String, List<SNMPTriple>> resMap = new HashMap<String, List<SNMPTriple>>();
Map<Integer, String> indexes = this.getDiskIndexes();
if (indexes == null || indexes.size() == 0)
return resMap;
logger.fine("Query disk stats");
PDU pdu = createPDU();
int reqSize = 0;
for (Map.Entry<Integer, String> entry : indexes.entrySet()) {
for (int i = 1; i < DISK_TABLE_ENTRIES.length; i++) {
if (DISK_TABLE_ENTRIES[i].length() == 0)
continue;
reqSize++;
pdu.add(new VariableBinding(new OID("." + DISK_TABLE_OID + "." + i + "." + entry.getKey())));
}
}
pdu.setType(PDU.GET);
Map<String, String> res = new HashMap<String, String>(13);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if (event != null) {
PDU resp = event.getResponse();
if (resp == null) {
logger.info(this.address + ": Unexpected snmp response: " + event + ", request size " + reqSize);
return resMap;
}
VariableBinding[] binds = resp.toArray();
for (VariableBinding b : binds) res.put(b.getOid().toString(), b.getVariable().toString());
// logger.info(res.toString());
}
for (Map.Entry<Integer, String> entry : indexes.entrySet()) {
List<SNMPTriple> resList = new ArrayList<SNMPTriple>(res.size());
for (int i = 1; i < DISK_TABLE_ENTRIES.length; i++) {
if (DISK_TABLE_ENTRIES[i].length() == 0)
continue;
resList.add(new SNMPTriple("." + DISK_TABLE_OID + "." + i + "." + entry.getKey(), DISK_TABLE_ENTRIES[i], res.get(DISK_TABLE_OID + "." + i + "." + entry.getKey())));
}
resMap.put(entry.getValue(), resList);
}
return resMap;
}
use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getDiskIndexes.
private Map<Integer, String> getDiskIndexes() throws IOException {
Map<Integer, String> diskIndexes = new HashMap<Integer, String>();
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
logger.fine("Query " + this.address + " for disk oids");
@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();
// ignore dm disk
if (value == null || value.isEmpty() || value.startsWith("dm-"))
continue;
// ignore dm disk
if (value.startsWith("ram") || value.startsWith("loop"))
continue;
char c = value.charAt(value.length() - 1);
if (c >= '0' && c <= '9') {
if (value.startsWith("sd")) {
if (value.length() > 2) {
char d = value.charAt(2);
if (d >= 'a' && d <= 'z')
continue;
}
}
}
logger.fine("Find device OID entry: " + key);
int index = -1;
String[] strs = key.split("\\.");
try {
index = Integer.parseInt(strs[strs.length - 1]);
diskIndexes.put(index, value);
} catch (Exception ex) {
}
}
}
return diskIndexes;
}
Aggregations