Search in sources :

Example 11 with ResponseEvent

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

the class BrocadeMibIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) throws IOException {
    PDU response;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    target.setTimeout(5000);
    TransportMapping<UdpAddress> transport = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    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) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 12 with ResponseEvent

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

the class LLDPMibIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) throws IOException {
    PDU response;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    if (m_timeout > 0) {
        target.setTimeout(m_timeout);
    } else {
        target.setTimeout(5000);
    }
    TransportMapping<UdpAddress> transport = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    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) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 13 with ResponseEvent

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

the class LLDPMibIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) throws Exception {
    PDU response;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(version);
    if (m_timeout > 0) {
        target.setTimeout(m_timeout);
    }
    TransportMapping<UdpAddress> transport = null;
    try {
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    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) CommunityTarget(org.snmp4j.CommunityTarget)

Example 14 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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("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;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) 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 15 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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");
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ResponseEvent(org.snmp4j.event.ResponseEvent) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) LinkedHashMap(java.util.LinkedHashMap)

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