org.snmp4j.smi.OID Java Examples

The following examples show how to use org.snmp4j.smi.OID. 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 Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: 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 Project: localization_nifi   Author: wangrenlei   File: SNMPUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Method to return the private protocol given the property
 * @param privProtocol property
 * @return protocol
 */
public static OID getPriv(String privProtocol) {
    switch (privProtocol) {
    case "DES":
        return PrivDES.ID;
    case "3DES":
        return Priv3DES.ID;
    case "AES128":
        return PrivAES128.ID;
    case "AES192":
        return PrivAES192.ID;
    case "AES256":
        return PrivAES256.ID;
    default:
        return null;
    }
}
 
Example #3
Source Project: SuitAgent   Author: DevopsJK   File: SNMPHelper.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) {
    boolean finished = false;
    if (pdu.getErrorStatus() != 0) {
        finished = true;
    } else if (vb.getOid() == null) {
        finished = true;
    } else if (vb.getOid().size() < targetOID.size()) {
        finished = true;
    } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) {
        finished = true;
    } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
        finished = true;
    } else if (vb.getOid().compareTo(targetOID) <= 0) {
        finished = true;
    }
    return finished;

}
 
Example #4
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPHelper.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) {
    boolean finished = false;
    if (pdu.getErrorStatus() != 0) {
        finished = true;
    } else if (vb.getOid() == null) {
        finished = true;
    } else if (vb.getOid().size() < targetOID.size()) {
        finished = true;
    } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) {
        finished = true;
    } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
        finished = true;
    } else if (vb.getOid().compareTo(targetOID) <= 0) {
        finished = true;
    }
    return finished;

}
 
Example #5
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve common system SNMP data
 * @return
 * @throws IOException
 */
public Map<String, String> querySysData() throws IOException 
{
	logger.fine("Query snmp for "+address);
	Map<String, String> resMap = null;
	 resMap = new java.util.LinkedHashMap<String, String>();
	 Map<OID, String> res = get(COMMON_SYS_OIDS);
	 if(res!=null)
	 {
		 for(Map.Entry<OID, String> e: res.entrySet())
		 {
			 if("noSuchObject".equalsIgnoreCase(e.getValue()))continue;
			 resMap.put(OID_NAME_MAP.get("."+e.getKey().toString()), e.getValue());
		 }
	 }
	 return resMap;
}
 
Example #6
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @return
 * @throws IOException
 */
public List<SNMPTriple> querySysData3() throws IOException 
{
	logger.fine("Query snmp system data for "+address);
	List<SNMPTriple> snmpList = new ArrayList<SNMPTriple>();
	 Map<OID, String> res = get(COMMON_SYS_OIDS);
	 if(res!=null)
	 {
		 for(Map.Entry<OID, String> e: res.entrySet())
		 {
			 if("noSuchObject".equalsIgnoreCase(e.getValue()))continue;
			 snmpList.add(new SNMPTriple(e.getKey().toString(), OID_NAME_MAP.get("."+e.getKey().toString()), e.getValue()));
		 }
	 }
	 return snmpList;
}
 
Example #7
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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");
}
 
Example #8
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 6 votes vote down vote up
private Target getTargetV3() {
	//logger.info("Use SNMP v3, "+this.privacyprotocol +"="+this.password+", "+this.privacyprotocol+"="+this.privacypassphrase);
	OID authOID = AuthMD5.ID;
	if("SHA".equals(this.authprotocol))
		authOID = AuthSHA.ID;
	OID privOID = PrivDES.ID;
	if(this.privacyprotocol == null || this.privacyprotocol.isEmpty())
		privOID = null;
	UsmUser user = new UsmUser(new OctetString(this.username),  
			authOID, new OctetString(this.password),  //auth
			privOID, this.privacypassphrase!=null?new OctetString(this.privacypassphrase):null); //enc
	snmp.getUSM().addUser(new OctetString(this.username), user);  
	Address targetAddress = GenericAddress.parse(address);
	UserTarget target = new UserTarget();
	target.setAddress(targetAddress);
	target.setRetries(2);
	target.setTimeout(1500);
	target.setVersion(this.getVersionInt());
	if(privOID != null)
		target.setSecurityLevel(SecurityLevel.AUTH_PRIV);  
	else
		target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV); 
	target.setSecurityName(new OctetString(this.username));
	return target;
}
 
Example #9
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * For test SNMP purpose. Used to check if individual SNMP entry is supported
 * @param oid
 * @return
 * @throws IOException 
 */
public List<SNMPTriple> querySingleSNMPEntryByOID(String oid) throws IOException
{
 if(oid == null || oid.isEmpty())return null;
 if(!oid.startsWith("."))oid = "."+oid;
 List<SNMPTriple> snmpList = new ArrayList<SNMPTriple>();
Map<OID, String> res = get(new OID[]{new OID(oid)});
if(res!=null)
{
 for(Map.Entry<OID, String> e: res.entrySet())
 {
	 //if("noSuchObject".equalsIgnoreCase(e.getValue()))continue;
	 snmpList.add(new SNMPTriple(e.getKey().toString(), "", e.getValue()));
 }
}
return snmpList;
}
 
Example #10
Source Project: nifi   Author: apache   File: SNMPUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Method to return the private protocol given the property
 * @param privProtocol property
 * @return protocol
 */
public static OID getPriv(String privProtocol) {
    switch (privProtocol) {
    case "DES":
        return PrivDES.ID;
    case "3DES":
        return Priv3DES.ID;
    case "AES128":
        return PrivAES128.ID;
    case "AES192":
        return PrivAES192.ID;
    case "AES256":
        return PrivAES256.ID;
    default:
        return null;
    }
}
 
Example #11
Source Project: onos   Author: opennetworkinglab   File: PolatisOpticalUtility.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Transforms a flow FlowRule object to a variable binding.
 * @param rule FlowRule object
 * @param delete whether it is a delete or edit request
 * @return variable binding
 */
public static VariableBinding fromFlowRule(FlowRule rule, boolean delete) {
    Set<Criterion> criterions = rule.selector().criteria();
    PortNumber inPort = criterions.stream()
            .filter(c -> c instanceof PortCriterion)
            .map(c -> ((PortCriterion) c).port())
            .findAny()
            .orElse(null);
    long input = inPort.toLong();
    List<Instruction> instructions = rule.treatment().immediate();
    PortNumber outPort = instructions.stream()
            .filter(c -> c instanceof Instructions.OutputInstruction)
            .map(c -> ((Instructions.OutputInstruction) c).port())
            .findAny()
            .orElse(null);
    long output = outPort.toLong();
    OID oid = new OID(PORT_PATCH_OID + "." + input);
    Variable var = new UnsignedInteger32(delete ? 0 : output);
    return new VariableBinding(oid, var);
}
 
Example #12
Source Project: onos   Author: opennetworkinglab   File: PolatisPowerConfig.java    License: Apache License 2.0 6 votes vote down vote up
private boolean setPortTargetPower(PortNumber port, long power) {
    log.debug("Set port{} target power...", port);
    List<VariableBinding> vbs = new ArrayList<>();

    OID voaStateOid = new OID(VOA_STATE_OID + "." + port.toLong());
    Variable voaStateVar = new UnsignedInteger32(VOA_STATE_ABSOLUTE);
    VariableBinding voaStateVb = new VariableBinding(voaStateOid, voaStateVar);
    vbs.add(voaStateVb);

    OID voaLevelOid = new OID(VOA_LEVEL_OID + "." + port.toLong());
    Variable voaLevelVar = new UnsignedInteger32(power);
    VariableBinding voaLevelVb = new VariableBinding(voaLevelOid, voaLevelVar);
    vbs.add(voaLevelVb);

    DeviceId deviceId = handler().data().deviceId();
    try {
        set(handler(), vbs);
    } catch (IOException e) {
        log.error("Error writing ports table for device {} exception {}", deviceId, e);
        return false;
    }
    return true;
}
 
Example #13
Source Project: onos   Author: opennetworkinglab   File: LumentumSdnRoadmFlowRuleProgrammable.java    License: Apache License 2.0 6 votes vote down vote up
private PortNumber getAddDropPort(int channel, boolean isAddPort) {
    OID oid = new OID(CTRL_CHANNEL_ADD_DROP_PORT_INDEX + (isAddPort ? "1" : "2"));

    for (TreeEvent event : snmp.get(oid)) {
        if (event == null) {
            return null;
        }

        VariableBinding[] varBindings = event.getVariableBindings();

        for (VariableBinding varBinding : varBindings) {
            if (varBinding.getOid().last() == channel) {
                int port = varBinding.getVariable().toInt();
                if (!isAddPort) {
                    port += DROP_PORT_OFFSET;
                }
                return PortNumber.portNumber(port);

            }
        }

    }

    return null;
}
 
Example #14
Source Project: snmpman   Author: 1and1   File: WildcardOID.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new instance of this class.
 *
 * @param oid the {@code OID} that potentially contains a wildcard
 * @throws NullPointerException     if the specified {@code OID} is null
 * @throws IllegalArgumentException if the specified {@code OID} contains more than one wildcard character
 */
public WildcardOID(final String oid) {
    Preconditions.checkNotNull(oid, "oid may not be null");

    final Matcher matcher = WILDCARD_OID_PATTERN.matcher(oid);
    if (matcher.matches()) {
        this.startsWith = new OID(matcher.group(1));
        if (matcher.group(5).isEmpty()) {
            this.endsWith = null;
        } else {
            this.endsWith = new OID(matcher.group(5));
        }
    } else {
        throw new IllegalArgumentException("specified oid \"" + oid + "\" is not a valid wildcard");
    }
}
 
Example #15
Source Project: snmpman   Author: 1and1   File: SnmpmanTest.java    License: 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 #16
Source Project: snmpman   Author: 1and1   File: SnmpmanTest.java    License: 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, "[email protected]");
    assertEquals(responses1.size(), 1);
    assertTrue(containsColumn(responses1, oid, "150"));

    List<TableEvent> responses2 = getResponse(new OID(oid), PORT, "[email protected]");
    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 #17
Source Project: snmpman   Author: 1and1   File: ModifierPropertiesTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParsing() throws Exception {
    final AgentConfiguration.DeviceFactory deviceFactory = new AgentConfiguration.DeviceFactory();
    final Device device = deviceFactory.getDevice(new File("src/test/resources/configuration/cisco.yaml"));

    assertFalse(device.getModifiers().isEmpty());

    boolean counter64found = false;
    for (final Modifier modifier : device.getModifiers()) {
        // counter64
        if (modifier.isApplicable(new OID(".1.3.6.1.2.1.31.1.1.1.11.1"))) {
            final Field variableModifierField = Modifier.class.getDeclaredField("modifier");
            variableModifierField.setAccessible(true);

            final Counter64Modifier counter64Modifier = (Counter64Modifier) variableModifierField.get(modifier);
            assertEquals(counter64Modifier.getMaximum(), UnsignedLong.valueOf(new BigInteger("1844674407370955161")));
            counter64found = true;
        }
    }
    
    if (!counter64found) {
        fail("no modifier for unsigned long found");
    }
}
 
Example #18
Source Project: localization_nifi   Author: wangrenlei   File: SNMPUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Method to return the authentication protocol given the property
 * @param authProtocol property
 * @return protocol
 */
public static OID getAuth(String authProtocol) {
    switch (authProtocol) {
    case "SHA":
        return AuthSHA.ID;
    case "MD5":
        return AuthMD5.ID;
    default:
        return null;
    }
}
 
Example #19
Source Project: SuitAgent   Author: DevopsJK   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定OID的 get
 *
 * @param snmp
 * @param target
 * @param oid
 * @return
 * @throws IOException
 */
public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException {
    ScopedPDU pdu = new ScopedPDU();
    pdu.setType(PDU.GET);
    pdu.add(new VariableBinding(new OID(oid)));

    ResponseEvent responseEvent = snmp.send(pdu, target);
    PDU response = responseEvent.getResponse();
    if(response == null){
        log.warn("response null - error:{} peerAddress:{} source:{} request:{}",
                responseEvent.getError(),
                responseEvent.getPeerAddress(),
                responseEvent.getSource(),
                responseEvent.getRequest());
    }
    return response;
}
 
Example #20
Source Project: SuitAgent   Author: DevopsJK   File: SNMPV3Session.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定的认证算法
 * @param privType
 * @return
 * @throws AgentArgumentException
 */
private OID getPrivProtocol(String privType) throws AgentArgumentException {

    if (privType == null
            || "none".equalsIgnoreCase(privType)
            || privType.length() == 0) {
        return null;
    }

    switch (privType) {
        case "DES":
            return PrivDES.ID;
        case "3DES":
            return Priv3DES.ID;
        case "AES128":
        case "AES-128":
        case "AES":
            return PrivAES128.ID;
        case "AES192":
        case "AES-192":
            return PrivAES192.ID;
        case "AES256":
        case "AES-256":
            return PrivAES256.ID;
        default:
            throw new AgentArgumentException("Privacy protocol " + privType + " not supported");
    }
}
 
Example #21
Source Project: SuitAgent   Author: DevopsJK   File: SNMPV3Session.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定的加密算法
 * @param authMethod
 * @return
 * @throws AgentArgumentException
 */
private OID getAuthProtocol(String authMethod) throws AgentArgumentException {
    if (authMethod == null
            || "none".equalsIgnoreCase(authMethod)
            || authMethod.length() == 0) {
        return null;
    } else if ("md5".equalsIgnoreCase(authMethod)) {
        return AuthMD5.ID;
    } else if ("sha".equalsIgnoreCase(authMethod)) {
        return AuthSHA.ID;
    } else {
        throw new AgentArgumentException("unknown authentication protocol: " + authMethod);
    }
}
 
Example #22
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定OID的 get
 *
 * @param snmp
 * @param target
 * @param oid
 * @return
 * @throws IOException
 */
public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException {
    ScopedPDU pdu = new ScopedPDU();
    pdu.setType(PDU.GET);
    pdu.add(new VariableBinding(new OID(oid)));

    ResponseEvent responseEvent = snmp.send(pdu, target);
    PDU response = responseEvent.getResponse();
    if(response == null){
        log.warn("response null - error:{} peerAddress:{} source:{} request:{}",
                responseEvent.getError(),
                responseEvent.getPeerAddress(),
                responseEvent.getSource(),
                responseEvent.getRequest());
    }
    return response;
}
 
Example #23
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPV3Session.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定的认证算法
 * @param privType
 * @return
 * @throws AgentArgumentException
 */
private OID getPrivProtocol(String privType) throws AgentArgumentException {

    if (privType == null
            || privType.equalsIgnoreCase("none")
            || privType.length() == 0) {
        return null;
    }

    switch (privType) {
        case "DES":
            return PrivDES.ID;
        case "3DES":
            return Priv3DES.ID;
        case "AES128":
        case "AES-128":
        case "AES":
            return PrivAES128.ID;
        case "AES192":
        case "AES-192":
            return PrivAES192.ID;
        case "AES256":
        case "AES-256":
            return PrivAES256.ID;
        default:
            throw new AgentArgumentException("Privacy protocol " + privType + " not supported");
    }
}
 
Example #24
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPV3Session.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定的加密算法
 * @param authMethod
 * @return
 * @throws AgentArgumentException
 */
private OID getAuthProtocol(String authMethod) throws AgentArgumentException {
    if (authMethod == null
            || authMethod.equalsIgnoreCase("none")
            || authMethod.length() == 0) {
        return null;
    } else if (authMethod.equalsIgnoreCase("md5")) {
        return AuthMD5.ID;
    } else if (authMethod.equalsIgnoreCase("sha")) {
        return AuthSHA.ID;
    } else {
        throw new AgentArgumentException("unknown authentication protocol: " + authMethod);
    }
}
 
Example #25
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Method which takes a single OID and returns the response from the agent as a String.
 * @param oid
 * @return
 * @throws IOException
 */
public String getAsString(OID oid) throws IOException {
	ResponseEvent res = getEvent(new OID[] { oid });
	if(res!=null)
		return res.getResponse().get(0).getVariable().toString();
	return null;
}
 
Example #26
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #27
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 5 votes vote down vote up
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;
 }
 
Example #28
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: 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 #29
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: 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 #30
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: 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;
}