Search in sources :

Example 1 with DefaultPDUFactory

use of org.snmp4j.util.DefaultPDUFactory 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());
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) InetAddress(java.net.InetAddress) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) Target(org.snmp4j.Target) CommunityTarget(org.snmp4j.CommunityTarget) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) Snmp(org.snmp4j.Snmp) List(java.util.List) TableUtils(org.snmp4j.util.TableUtils) CommunityTarget(org.snmp4j.CommunityTarget)

Example 2 with DefaultPDUFactory

use of org.snmp4j.util.DefaultPDUFactory 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 3 with DefaultPDUFactory

use of org.snmp4j.util.DefaultPDUFactory 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(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();
            snmpList.add(new SNMPTriple(key, "", value));
        }
    }
    return snmpList;
}
Also used : DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) ArrayList(java.util.ArrayList) TableUtils(org.snmp4j.util.TableUtils) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 4 with DefaultPDUFactory

use of org.snmp4j.util.DefaultPDUFactory in project nifi by apache.

the class SNMPGetter method walk.

/**
 * Perform a SNMP walk and returns the list of {@link TreeEvent}
 * @return the list of {@link TreeEvent}
 */
public List<TreeEvent> walk() {
    TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
    @SuppressWarnings("unchecked") List<TreeEvent> events = treeUtils.getSubtree(this.target, this.oid);
    return events;
}
Also used : TreeEvent(org.snmp4j.util.TreeEvent) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TreeUtils(org.snmp4j.util.TreeUtils)

Example 5 with DefaultPDUFactory

use of org.snmp4j.util.DefaultPDUFactory 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)

Aggregations

DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)8 OID (org.snmp4j.smi.OID)7 OctetString (org.snmp4j.smi.OctetString)7 TableEvent (org.snmp4j.util.TableEvent)7 TableUtils (org.snmp4j.util.TableUtils)7 VariableBinding (org.snmp4j.smi.VariableBinding)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)2 InetAddress (java.net.InetAddress)1 Map (java.util.Map)1 CommunityTarget (org.snmp4j.CommunityTarget)1 Snmp (org.snmp4j.Snmp)1 Target (org.snmp4j.Target)1 Address (org.snmp4j.smi.Address)1 UdpAddress (org.snmp4j.smi.UdpAddress)1 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)1 TreeEvent (org.snmp4j.util.TreeEvent)1