use of org.snmp4j.util.TableUtils in project mysql_perf_analyzer by yahoo.
the class SNMPClient method getProcessIndexes.
//1.3.6.1.2.1.25.4.2.1.2.
/**
* Query index for given process name. Note the parameter only provides 128 characters,
* so it could be difficult for us to differentiate each other if multi processes with same name exist.
* So we will return this list and use the sum from all processes for our metrics
* @param process
* @return
* @throws IOException
*/
private List<Integer> getProcessIndexes(String process) throws IOException {
List<Integer> indexes = new ArrayList<Integer>();
if (process == null || process.isEmpty())
return indexes;
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
logger.fine("Query " + this.address + " for process " + process);
@SuppressWarnings("unchecked") List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID("." + PROCESS_TABLE_OID) }, null, null);
for (TableEvent event : events) {
if (event.isError()) {
logger.warning("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 (process != null && !process.isEmpty() && !value.equalsIgnoreCase(process))
continue;
if (value != null) {
logger.fine("Find process OID entry: " + key);
int index = -1;
String[] strs = key.split("\\.");
try {
index = Integer.parseInt(strs[strs.length - 1]);
indexes.add(index);
} catch (Exception ex) {
}
}
}
}
return indexes;
}
use of org.snmp4j.util.TableUtils in project mysql_perf_analyzer by yahoo.
the class SNMPClient method querySingleSNMPTableByOID.
public List<SNMPTriple> querySingleSNMPTableByOID(String oid) throws IOException {
if (oid == null || oid.isEmpty())
return null;
if (!oid.startsWith("."))
oid = "." + oid;
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID(oid) }, null, null);
List<SNMPTriple> snmpList = new ArrayList<SNMPTriple>();
for (TableEvent event : events) {
if (event.isError()) {
logger.warning("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();
snmpList.add(new SNMPTriple(key, "", value));
}
}
return snmpList;
}
use of org.snmp4j.util.TableUtils in project opennms by OpenNMS.
the class MockAgentTest method testWalkSystem.
public void testWalkSystem() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
TableUtils walker = new TableUtils(snmp, new DefaultPDUFactory());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
//Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
// Implements snmp4j API
@SuppressWarnings("rawtypes") List results = walker.getTable(target, new OID[] { new OID("1.3.6.1.2.1.1") }, null, null);
assertNotNull(results);
assertFalse(results.isEmpty());
assertTrue(results.get(results.size() - 1) instanceof TableEvent);
TableEvent lastEvent = (TableEvent) results.get(results.size() - 1);
MockUtil.println("Status of lastEvent is " + lastEvent.getStatus());
assertEquals(TableEvent.STATUS_OK, lastEvent.getStatus());
}
use of org.snmp4j.util.TableUtils 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("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;
}
use of org.snmp4j.util.TableUtils 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("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;
}
Aggregations