Search in sources :

Example 21 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getProcessData.

public List<SNMPTriple> getProcessData(String processName) throws IOException {
    List<SNMPTriple> resList = new ArrayList<SNMPTriple>();
    List<Integer> prIndexes = this.getProcessIndexes(processName);
    if (prIndexes == null || prIndexes.size() == 0)
        return resList;
    logger.fine("Query process stats");
    PDU pdu = createPDU();
    for (Integer idx : prIndexes) {
        for (int i = 1; i < PROCESS_PERF_TABLE_ENTRIES.length; i++) {
            if (PROCESS_PERF_TABLE_ENTRIES[i].length() == 0)
                continue;
            pdu.add(new VariableBinding(new OID("." + PROCESS_PERF_TABLE_OID + "." + i + "." + idx)));
        // logger.info("Adding " + "."+PROCESS_PERF_TABLE_OID+"."+i+"."+idx);
        }
    }
    pdu.setType(PDU.GET);
    Map<String, String> res = new HashMap<String, String>(prIndexes.size() * 2);
    ResponseEvent event = snmp.send(pdu, getTarget(), null);
    if (event != null) {
        VariableBinding[] binds = event.getResponse().toArray();
        for (VariableBinding b : binds) {
            res.put(b.getOid().toString(), b.getVariable().toString());
        // logger.info(b.getOid().toString() +", "+ b.getVariable().toString());
        }
    }
    // logger.info("result: "+res);
    for (int i = 1; i < PROCESS_PERF_TABLE_ENTRIES.length; i++) {
        if (PROCESS_PERF_TABLE_ENTRIES[i].length() == 0)
            continue;
        BigDecimal data = new BigDecimal(0);
        for (Integer idx : prIndexes) {
            data = data.add(new BigDecimal(res.get(PROCESS_PERF_TABLE_OID + "." + i + "." + idx)));
        }
        resList.add(new SNMPTriple("", PROCESS_PERF_TABLE_ENTRIES[i], data.toString()));
    }
    return resList;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) BigDecimal(java.math.BigDecimal) ResponseEvent(org.snmp4j.event.ResponseEvent) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 22 with VariableBinding

use of org.snmp4j.smi.VariableBinding 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(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 (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;
}
Also used : ArrayList(java.util.ArrayList) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) IOException(java.io.IOException) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) TableUtils(org.snmp4j.util.TableUtils) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 23 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getEvent.

public ResponseEvent getEvent(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) {
        return event;
    }
    throw new RuntimeException("GET timed out");
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ResponseEvent(org.snmp4j.event.ResponseEvent) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 24 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getDiskData.

public List<SNMPTriple> getDiskData(String device) throws IOException {
    int index = this.getDiskIndex(device);
    if (index < 0) {
        return new ArrayList<SNMPTriple>();
    }
    logger.fine("Query disk stats for " + index);
    PDU pdu = createPDU();
    for (int i = 1; i < DISK_TABLE_ENTRIES.length; i++) {
        if (DISK_TABLE_ENTRIES[i].length() == 0)
            continue;
        pdu.add(new VariableBinding(new OID("." + DISK_TABLE_OID + "." + i + "." + index)));
    }
    pdu.setType(PDU.GET);
    Map<String, String> res = new HashMap<String, String>(13);
    ResponseEvent event = snmp.send(pdu, getTarget(), null);
    if (event != null) {
        VariableBinding[] binds = event.getResponse().toArray();
        for (VariableBinding b : binds) res.put(b.getOid().toString(), b.getVariable().toString());
    // logger.info(res.toString());
    }
    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 + "." + index, DISK_TABLE_ENTRIES[i], res.get(DISK_TABLE_OID + "." + i + "." + index)));
    }
    return resList;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ResponseEvent(org.snmp4j.event.ResponseEvent) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 25 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getNetIfData3.

public Map<String, List<SNMPTriple>> getNetIfData3(String device) throws IOException {
    Map<String, List<SNMPTriple>> resMap = new HashMap<String, List<SNMPTriple>>();
    Map<Integer, String> indexMap = this.getNetIfIndexes(device);
    if (indexMap == null || indexMap.size() == 0) {
        logger.warning("Cannot find network interfaces ");
        return resMap;
    }
    logger.fine("Query net if stats for network");
    PDU pdu = createPDU();
    for (Map.Entry<Integer, String> entry : indexMap.entrySet()) for (int i = 1; i < IF_TABLE_ENTRIES.length; i++) {
        if (IF_TABLE_ENTRIES[i].length() == 0)
            continue;
        pdu.add(new VariableBinding(new OID("." + IF_TABLE_OID + "." + i + "." + entry.getKey())));
    }
    pdu.setType(PDU.GET);
    Map<String, String> res = new HashMap<String, String>(IF_TABLE_ENTRIES.length * indexMap.size());
    ResponseEvent event = snmp.send(pdu, getTarget(), null);
    if (event != null) {
        VariableBinding[] binds = event.getResponse().toArray();
        for (VariableBinding b : binds) res.put(b.getOid().toString(), b.getVariable().toString());
    // logger.info(res.toString());
    }
    for (Map.Entry<Integer, String> entry : indexMap.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;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) ArrayList(java.util.ArrayList) List(java.util.List) ResponseEvent(org.snmp4j.event.ResponseEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) VariableBinding(org.snmp4j.smi.VariableBinding)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)52 OID (org.snmp4j.smi.OID)37 OctetString (org.snmp4j.smi.OctetString)31 PDU (org.snmp4j.PDU)30 ScopedPDU (org.snmp4j.ScopedPDU)17 Test (org.junit.Test)11 PDUv1 (org.snmp4j.PDUv1)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 Variable (org.snmp4j.smi.Variable)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Map (java.util.Map)4