org.snmp4j.util.TableEvent Java Examples

The following examples show how to use org.snmp4j.util.TableEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 8 votes vote down vote up
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;
}
 
Example #2
Source File: SnmpmanTest.java    From snmpman with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCommunityIndex() throws Exception {
    assertEquals(snmpman.getAgents().size(), 11);

    final String oid = "1.3.6.1.2.1.17.2.4";
    List<TableEvent> responses1 = getResponse(new OID(oid), PORT, "public@42");
    assertEquals(responses1.size(), 1);
    assertTrue(containsColumn(responses1, oid, "150"));

    List<TableEvent> responses2 = getResponse(new OID(oid), PORT, "public@9");
    assertEquals(responses2.size(), 1);
    assertTrue(containsColumn(responses2, oid, "120"));

    List<TableEvent> responses3 = getResponse(new OID(oid), PORT, COMMUNITY);
    assertEquals(responses3.size(), 1);
    assertTrue(containsColumn(responses3, oid, "0"));
}
 
Example #3
Source File: SnmpmanTest.java    From snmpman with Apache License 2.0 6 votes vote down vote up
@Test
public void testSnmpGetBulk() throws Exception {
    assertEquals(snmpman.getAgents().size(), 11);

    List<TableEvent> responses = getResponse(new OID("1.3.6.1.2.1"), 10000);
    assertEquals(responses.size(), 19);

    responses = getResponse(new OID("1.3.6.1.2.1.31"), 10000);
    assertEquals(responses.size(), 10);

    responses = getResponse(new OID(".1.3.6.1.2.1.2"), 10000);
    assertEquals(responses.size(), 7);

    responses = getResponse(new OID(".1.3"), 10010);
    assertEquals(responses.size(), 30);

    responses = getResponse(new OID(".1.0"), 10010);
    assertEquals(responses.size(), 8);
}
 
Example #4
Source File: SnmpmanIntegrationTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnmpGetBulk() throws Exception {
    List<TableEvent> responses = SnmpmanTest.getResponse(new OID("1.3.6.1.2.1"), 10000);
    assertEquals(responses.size(), 19);

    responses = SnmpmanTest.getResponse(new OID("1.3.6.1.2.1.31"), 10000);
    assertEquals(responses.size(), 10);

    responses = SnmpmanTest.getResponse(new OID(".1.3.6.1.2.1.2"), 10000);
    assertEquals(responses.size(), 7);
}
 
Example #5
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
void assertThatOidHasValue(OID oid, String expectedValue) throws Exception {
    List<TableEvent> responses1 = getResponse(oid, PORT);
    if (expectedValue.equals("null")) {
        assertEquals(Arrays.toString(responses1.get(0).getColumns()), expectedValue);
    } else {
        assertTrue(containsColumn(responses1, oid.toString(), expectedValue),
                "Table under OID=" + oid + " doesn't contain value=" + expectedValue);
    }
}
 
Example #6
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
public static boolean containsColumn(final List<TableEvent> responses, final String oid, final String result) {
    for (final TableEvent e : responses) {
        String columnsToString = Arrays.toString(e.getColumns());
        if (columnsToString.contains(oid) && columnsToString.contains("= " + result)) {
            return true;
        }
    }
    return false;
}
 
Example #7
Source File: SnmpmanTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifier() throws Exception {
    final String oid = "1.3.6.1.2.1.2.2.1.13";
    List<TableEvent> responses1 = SnmpmanTest.getResponse(new OID(oid), PORT, COMMUNITY);
    List<TableEvent> responses2 = SnmpmanTest.getResponse(new OID(oid), PORT, COMMUNITY);

    assertNotEquals(responses1.get(0).getColumns(), responses2.get(0).getColumns(),
            "repeated call should return a different result");
}
 
Example #8
Source File: SnmpmanIntegrationTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCommunityIndex() throws Exception {
    final String oid = "1.3.6.1.2.1.17.2.4";
    List<TableEvent> responses1 = SnmpmanTest.getResponse(new OID(oid), PORT, "public@42");
    assertEquals(responses1.size(), 1);
    assertTrue(SnmpmanTest.containsColumn(responses1, oid, "150"));

    List<TableEvent> responses2 = SnmpmanTest.getResponse(new OID(oid), PORT, "public@9");
    assertEquals(responses2.size(), 1);
    assertTrue(SnmpmanTest.containsColumn(responses2, oid, "120"));

    List<TableEvent> responses3 = SnmpmanTest.getResponse(new OID(oid), PORT, COMMUNITY);
    assertEquals(responses3.size(), 1);
    assertTrue(SnmpmanTest.containsColumn(responses3, oid, "0"));
}
 
Example #9
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 5 votes vote down vote up
private int getDiskIndex(String device) throws IOException {

      TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
      
      logger.fine("Query "+this.address+" for disk data: "+device);
       @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();
      	   if(value!=null && value.equals(device))
      	   {
      	       logger.fine("Find device OID entry: "+key);
      	         int index = -1;
      	         String[] strs = key.split("\\.");
      	         try
      	         {
      	        	 index = Integer.parseInt(strs[strs.length-1]);
      	         }catch(Exception ex){}
      	         return index;
      	   }
         }
       }
       return -1;
 }
 
Example #10
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 5 votes vote down vote up
/**
  * 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;
}
 
Example #11
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 5 votes vote down vote up
private Map<Integer, String> getNetIfIndexes(String device) throws IOException {
  Map<Integer, String> ifMaps = new HashMap<Integer, 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_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();
     	   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){}
     	   }
        }
      }
      return ifMaps;
}
 
Example #12
Source File: PolatisFlowRuleProgrammable.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<FlowEntry> getFlowEntries() {
    List<TableEvent> events;
    DeviceId deviceId = handler().data().deviceId();
    ImmutableList.Builder<FlowEntry> connectionsBuilder = ImmutableList.builder();

    try {
        OID[] columnOIDs = {new OID(PORT_PATCH_OID)};
        events = getTable(handler(), columnOIDs);
    } catch (IOException e) {
        log.error("Error reading ports table for device {} exception {}", deviceId, e);
        return connectionsBuilder.build();
    }

    if (events == null) {
        log.error("Error reading ports table for device {}", deviceId);
        return connectionsBuilder.build();
    }

    for (TableEvent event : events) {
        if (event == null) {
            log.error("Error reading event for device {}", deviceId);
            continue;
        }
        VariableBinding[] columns = event.getColumns();
        if (columns == null) {
            log.error("Error reading columns for device {}", deviceId);
            continue;
        }

        VariableBinding patchColumn = columns[0];
        if (patchColumn == null) {
            continue;
        }

        int port = event.getIndex().last();
        int patch = patchColumn.getVariable().toInt();
        if (patch == 0) {
            continue;
        }

        FlowRule flowRule = PolatisOpticalUtility.toFlowRule(this,
            PortNumber.portNumber(port), PortNumber.portNumber(patch));
        connectionsBuilder.add(new DefaultFlowEntry(flowRule, FlowEntry.FlowEntryState.ADDED));
    }

    return connectionsBuilder.build();
}
 
Example #13
Source File: PolatisPowerConfig.java    From onos with Apache License 2.0 4 votes vote down vote up
private List<PortNumber> acquirePorts() {
    List<TableEvent> events;
    DeviceId deviceId = handler().data().deviceId();
    List<PortNumber> ports = new ArrayList<>();

    try {
        OID[] columnOIDs = {new OID(OPM_POWER_OID)};
        events = getTable(handler(), columnOIDs);
    } catch (IOException e) {
        log.error("Error reading opm power table for device {} exception {}", deviceId, e);
        return ports;
    }

    if (events == null) {
        log.error("Error reading opm power table for device {}", deviceId);
        return ports;
    }

    for (TableEvent event : events) {
        if (event == null) {
            log.error("Error reading event for device {}", deviceId);
            continue;
        }
        VariableBinding[] columns = event.getColumns();
        if (columns == null) {
            log.error("Error reading columns for device {}", deviceId);
            continue;
        }

        VariableBinding opmColumn = columns[0];
        if (opmColumn == null) {
            continue;
        }

        int port = event.getIndex().last();
        int opm = opmColumn.getVariable().toInt();
        if (opm == 0) {
            continue;
        }

        ports.add(PortNumber.portNumber(port));
    }

    return ports;
}
 
Example #14
Source File: PolatisDeviceDescription.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Discovers port details, for Polatis Snmp device.
 *
 * @return port list
 */
@Override
public List<PortDescription> discoverPortDetails() {
    List<PortDescription> ports = Lists.newArrayList();
    List<TableEvent> events;
    DeviceId deviceId = handler().data().deviceId();

    try {
        OID[] columnOIDs = {new OID(PORT_CURRENT_STATE_OID)};
        events = getTable(handler(), columnOIDs);
    } catch (IOException e) {
        log.error("Error reading ports table for device {} exception {}", deviceId, e);
        return ports;
    }

    if (events == null) {
        log.error("Error reading ports table for device {}", deviceId);
        return ports;
    }

    for (TableEvent event : events) {
        if (event == null) {
            log.error("Error reading event for device {}", deviceId);
            continue;
        }
        VariableBinding[] columns = event.getColumns();
        if (columns == null) {
            log.error("Error reading columns for device {} event {}", deviceId, event);
            continue;
        }

        VariableBinding portColumn = columns[0];
        if (portColumn == null) {
            continue;
        }

        int port = event.getIndex().last();
        boolean enabled = (portColumn.getVariable().toInt() == 1);
        PortNumber portNumber = PortNumber.portNumber(port);
        DefaultAnnotations annotations = DefaultAnnotations.builder().build();
        double opticalBand = Spectrum.O_BAND_MIN.asGHz() - Spectrum.L_BAND_MAX.asGHz();
        Frequency opticalGrid = Frequency.ofGHz(opticalBand / POLATIS_NUM_OF_WAVELENGTHS);
        PortDescription p = omsPortDescription(portNumber,
                enabled,
                Spectrum.O_BAND_MIN,
                Spectrum.L_BAND_MAX,
                opticalGrid,
                annotations);
        ports.add(p);
    }

    return ports;
}
 
Example #15
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 4 votes vote down vote up
public static List<TableEvent> getResponse(final OID query, int port, final String community) throws Exception {
    final Address targetAddress = GenericAddress.parse(String.format("127.0.0.1/%d", port));
    final Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();

    final CommunityTarget target = getCommunityTarget(community, targetAddress);

    // creating PDU
    final PDUFactory pduFactory = new DefaultPDUFactory(PDU.GETBULK);
    final TableUtils utils = new TableUtils(snmp, pduFactory);

    return utils.getTable(target, new OID[]{query}, null, null);
}
 
Example #16
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 4 votes vote down vote up
public static List<TableEvent> getResponse(final OID query, int port) throws Exception {
    return getResponse(query, port, COMMUNITY);
}
 
Example #17
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 4 votes vote down vote up
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();
      	   if(value == null || value.isEmpty() || value.startsWith("dm-"))continue;//ignore dm disk
      	   if(value.startsWith("ram") || value.startsWith("loop") )continue;//ignore dm disk
      	   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;
 }
 
Example #18
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves a table.
 *
 * @param handler parent driver handler
 * @param columnOIDs column oid object identifiers
 * @return the table
 * @throws IOException if unable to retrieve the object value
 */
public static List<TableEvent> getTable(DriverHandler handler, OID[] columnOIDs) throws IOException {
    Snmp session = getSession(handler);
    CommunityTarget target = getTarget(handler);
    TableUtils tableUtils = new TableUtils(session, new DefaultPDUFactory());
    return tableUtils.getTable(target, columnOIDs, null, null);
}