Search in sources :

Example 46 with VariableBinding

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());
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) GenericAddress(org.snmp4j.smi.GenericAddress) ScopedPDU(org.snmp4j.ScopedPDU) CommandResponderEvent(org.snmp4j.CommandResponderEvent) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM) PrivAES192(org.snmp4j.security.PrivAES192) Snmp(org.snmp4j.Snmp) UserTarget(org.snmp4j.UserTarget) VariableBinding(org.snmp4j.smi.VariableBinding) CommandResponder(org.snmp4j.CommandResponder)

Example 47 with VariableBinding

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");
}
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)

Example 48 with VariableBinding

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

Example 49 with VariableBinding

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;
}
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 50 with VariableBinding

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

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)55 OID (org.snmp4j.smi.OID)40 PDU (org.snmp4j.PDU)33 OctetString (org.snmp4j.smi.OctetString)33 ScopedPDU (org.snmp4j.ScopedPDU)19 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 PDUv1 (org.snmp4j.PDUv1)10 Variable (org.snmp4j.smi.Variable)10 IOException (java.io.IOException)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 CommunityTarget (org.snmp4j.CommunityTarget)6 Integer32 (org.snmp4j.smi.Integer32)6 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6