Search in sources :

Example 1 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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 2 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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 3 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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)

Example 4 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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 5 with ResponseEvent

use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.

the class MockSnmpAgentIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) {
    PDU response = null;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
    target.setVersion(version);
    TransportMapping<UdpAddress> transport = null;
    Snmp snmp = null;
    try {
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        if (transport != null) {
            try {
                transport.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget)

Aggregations

ResponseEvent (org.snmp4j.event.ResponseEvent)15 PDU (org.snmp4j.PDU)13 ScopedPDU (org.snmp4j.ScopedPDU)12 OctetString (org.snmp4j.smi.OctetString)12 Snmp (org.snmp4j.Snmp)9 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)8 OID (org.snmp4j.smi.OID)7 UdpAddress (org.snmp4j.smi.UdpAddress)7 VariableBinding (org.snmp4j.smi.VariableBinding)7 LinkedHashMap (java.util.LinkedHashMap)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CommunityTarget (org.snmp4j.CommunityTarget)4 IOException (java.io.IOException)3 UserTarget (org.snmp4j.UserTarget)3 USM (org.snmp4j.security.USM)3 UsmUser (org.snmp4j.security.UsmUser)3 List (java.util.List)2 Map (java.util.Map)2 BigDecimal (java.math.BigDecimal)1