Java Code Examples for org.snmp4j.PDU

The following examples show how to use org.snmp4j.PDU. These examples are extracted from open source projects. 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: localization_nifi   Author: wangrenlei   File: SNMPUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test for updating attributes of flow files with {@link PDU}
 */
@Test
public void validateUpdateFlowFileAttributes() {
    GetSNMP processor = new GetSNMP();
    ProcessSession processSession = new MockProcessSession(new SharedSessionState(processor, new AtomicLong()),
            processor);
    FlowFile sourceFlowFile = processSession.create();

    PDU pdu = new PDU();
    pdu.setErrorIndex(0);
    pdu.setErrorStatus(0);
    pdu.setType(4);

    FlowFile f2 = SNMPUtils.updateFlowFileAttributesWithPduProperties(pdu, sourceFlowFile,
            processSession);

    assertEquals("0", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "errorIndex"));
    assertEquals("0", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "errorStatus"));
    assertEquals("4", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "type"));
}
 
Example #2
Source Project: SuitAgent   Author: DevopsJK   File: SwitchCPUStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * walk的方式获取监控值
 * 通过获得的总值除数量的逻辑
 * @param session
 * @param oid
 * @return
 * @throws IOException
 */
private static CollectObject walkForSumDivCount(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    List<PDU> pduList = session.walk(oid);

    int count = pduList.size();
    long value = 0;

    for (PDU pdu : pduList) {
        value += Long.parseLong(SNMPHelper.getValueFromPDU(pdu));
    }

    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setValue(0 + "");
    if(count > 0){
        collectObject.setValue(String.valueOf(value / count));
    }
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #3
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getOldHuawei_Mem(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setTime(new Date());

    String memTotalOid = "1.3.6.1.4.1.2011.6.1.2.1.1.2";
    List<PDU> snmpMemTotal = session.walk(memTotalOid);

    String memFreeOid = "1.3.6.1.4.1.2011.6.1.2.1.1.3";
    List<PDU> snmpMemFree = session.walk(memFreeOid);

    if(snmpMemFree.isEmpty() || snmpMemTotal.isEmpty()){
        log.warn("{} No Such Object available on this agent at this OID",session);
        return null;
    }

    int memTotal = Integer.parseInt(SNMPHelper.getValueFromPDU(snmpMemTotal.get(0)));
    int memFree = Integer.parseInt(SNMPHelper.getValueFromPDU(snmpMemFree.get(0)));
    collectObject.setValue(String.valueOf(((float) memTotal - (float)memFree) / (float)memTotal));

    return collectObject;
}
 
Example #4
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
static CollectObject getH3CHWcpumem(String metricsName,SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();

    List<PDU> pduList = session.walk(oid);
    String value = "";
    for (PDU pdu : pduList) {
        if(pdu.get(0).getVariable().toLong() != 0){
            value = SNMPHelper.getValueFromPDU(pdu);
            break;
        }
    }
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setValue(value);
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #5
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getCisco_ASA_Mem(SNMPV3Session session, String oid) throws IOException {
    String memUsedOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.18";
    List<PDU> memUsedListPDU = session.walk(memUsedOid);
    String memFreeOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.20";
    List<PDU> memFreeListPDU = session.walk(memFreeOid);

    CollectObject collectObject = new CollectObject();
    collectObject.setMetrics(metricsName);
    collectObject.setSession(session);
    collectObject.setTime(new Date());

    if(SNMPHelper.isValidPDU(memFreeListPDU.get(0)) && SNMPHelper.isValidPDU(memUsedListPDU.get(0))){
        int memUsed = Integer.parseInt(SNMPHelper.getValueFromPDU(memUsedListPDU.get(0)));
        int memFree = Integer.parseInt(SNMPHelper.getValueFromPDU(memFreeListPDU.get(0)));
        if(memUsed+memFree != 0){
            collectObject.setValue(String.valueOf((double)memUsed / ((double)memUsed + (double)memFree)));
        }
    }

    return collectObject;
}
 
Example #6
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getCisco_IOS_XR_Mem(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setMetrics(metricsName);
    collectObject.setSession(session);
    collectObject.setTime(new Date());

    String cpuIndex = "1.3.6.1.4.1.9.9.109.1.1.1.1.2";
    PDU cpuIndexPDU = session.getNext(cpuIndex);
    if(cpuIndexPDU.get(0) != null){
        int index  = Integer.parseInt(SNMPHelper.getValueFromPDU(cpuIndexPDU));
        String memUsedOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.18." + index + ".1";
        String memFreeOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.20." + index + ".1";
        PDU memUsedPDU = session.get(memUsedOid);
        PDU memFreePDU = session.get(memFreeOid);
        setRatioValueHelper(memUsedPDU,memFreePDU,collectObject);
    }

    return collectObject;
}
 
Example #7
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 #8
Source Project: SuitAgent   Author: DevopsJK   File: SNMPV3Session.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取设备描述字符串
 *
 * @return
 * @throws IOException
 */
public String getSysDesc() throws IOException {
    String key = getInfoCacheKey() + CACHE_KEY_SYS_DESC;
    String sysDesc = (String) infoCache.get(key);
    if(sysDesc != null){
        return sysDesc;
    }
    PDU pdu = this.get(SNMPHelper.SYS_DESC_OID);
    if(pdu != null){
        sysDesc = pdu.get(0).getVariable().toString();
        infoCache.put(key,sysDesc);
        return sysDesc;
    }else {
        return "";
    }

}
 
Example #9
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchCPUStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * walk的方式获取监控值
 * 通过获得的总值除数量的逻辑
 * @param session
 * @param oid
 * @return
 * @throws IOException
 */
private static CollectObject walkForSumDivCount(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    List<PDU> pduList = session.walk(oid);

    int count = pduList.size();
    long value = 0;

    for (PDU pdu : pduList) {
        value += Long.parseLong(SNMPHelper.getValueFromPDU(pdu));
    }

    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setValue(0 + "");
    if(count > 0){
        collectObject.setValue(String.valueOf(value / count));
    }
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #10
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getOldHuawei_Mem(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setTime(new Date());

    String memTotalOid = "1.3.6.1.4.1.2011.6.1.2.1.1.2";
    List<PDU> snmpMemTotal = session.walk(memTotalOid);

    String memFreeOid = "1.3.6.1.4.1.2011.6.1.2.1.1.3";
    List<PDU> snmpMemFree = session.walk(memFreeOid);

    if(snmpMemFree.isEmpty() || snmpMemTotal.isEmpty()){
        log.warn("{} No Such Object available on this agent at this OID",session);
        return null;
    }

    int memTotal = Integer.parseInt(SNMPHelper.getValueFromPDU(snmpMemTotal.get(0)));
    int memFree = Integer.parseInt(SNMPHelper.getValueFromPDU(snmpMemFree.get(0)));
    collectObject.setValue(String.valueOf(((float) memTotal - (float)memFree) / (float)memTotal));

    return collectObject;
}
 
Example #11
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
static CollectObject getH3CHWcpumem(String metricsName,SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();

    List<PDU> pduList = session.walk(oid);
    String value = "";
    for (PDU pdu : pduList) {
        if(pdu.get(0).getVariable().toLong() != 0){
            value = SNMPHelper.getValueFromPDU(pdu);
            break;
        }
    }
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    collectObject.setValue(value);
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #12
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getCisco_ASA_Mem(SNMPV3Session session, String oid) throws IOException {
    String memUsedOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.18";
    List<PDU> memUsedListPDU = session.walk(memUsedOid);
    String memFreeOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.20";
    List<PDU> memFreeListPDU = session.walk(memFreeOid);

    CollectObject collectObject = new CollectObject();
    collectObject.setMetrics(metricsName);
    collectObject.setSession(session);
    collectObject.setTime(new Date());

    if(SNMPHelper.isValidPDU(memFreeListPDU.get(0)) && SNMPHelper.isValidPDU(memUsedListPDU.get(0))){
        int memUsed = Integer.parseInt(SNMPHelper.getValueFromPDU(memUsedListPDU.get(0)));
        int memFree = Integer.parseInt(SNMPHelper.getValueFromPDU(memFreeListPDU.get(0)));
        if(memUsed+memFree != 0){
            collectObject.setValue(String.valueOf((double)memUsed / ((double)memUsed + (double)memFree)));
        }
    }

    return collectObject;
}
 
Example #13
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 6 votes vote down vote up
private static CollectObject getCisco_IOS_XR_Mem(SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setMetrics(metricsName);
    collectObject.setSession(session);
    collectObject.setTime(new Date());

    String cpuIndex = "1.3.6.1.4.1.9.9.109.1.1.1.1.2";
    PDU cpuIndexPDU = session.getNext(cpuIndex);
    if(cpuIndexPDU.get(0) != null){
        int index  = Integer.parseInt(SNMPHelper.getValueFromPDU(cpuIndexPDU));
        String memUsedOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.18." + index + ".1";
        String memFreeOid = "1.3.6.1.4.1.9.9.221.1.1.1.1.20." + index + ".1";
        PDU memUsedPDU = session.get(memUsedOid);
        PDU memFreePDU = session.get(memFreeOid);
        setRatioValueHelper(memUsedPDU,memFreePDU,collectObject);
    }

    return collectObject;
}
 
Example #14
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 #15
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPV3Session.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取设备描述字符串
 *
 * @return
 * @throws IOException
 */
public String getSysDesc() throws IOException {
    String key = getInfoCacheKey() + cacheKey_sysDesc;
    String sysDesc = (String) infoCache.get(key);
    if(sysDesc != null){
        return sysDesc;
    }
    PDU pdu = this.get(SNMPHelper.sysDescOid);
    if(pdu != null){
        sysDesc = pdu.get(0).getVariable().toString();
        infoCache.put(key,sysDesc);
        return sysDesc;
    }else {
        return "";
    }

}
 
Example #16
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 #17
Source Project: nifi   Author: apache   File: SNMPUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test for updating attributes of flow files with {@link PDU}
 */
@Test
public void validateUpdateFlowFileAttributes() {
    GetSNMP processor = new GetSNMP();
    ProcessSession processSession = new MockProcessSession(new SharedSessionState(processor, new AtomicLong()),
            processor);
    FlowFile sourceFlowFile = processSession.create();

    PDU pdu = new PDU();
    pdu.setErrorIndex(0);
    pdu.setErrorStatus(0);
    pdu.setType(4);

    FlowFile f2 = SNMPUtils.updateFlowFileAttributesWithPduProperties(pdu, sourceFlowFile,
            processSession);

    assertEquals("0", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "errorIndex"));
    assertEquals("0", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "errorStatus"));
    assertEquals("4", f2.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + "type"));
}
 
Example #18
Source Project: localization_nifi   Author: wangrenlei   File: SNMPUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Updates {@link FlowFile} with attributes representing PDU properties
 * @param response PDU retried from SNMP Agent
 * @param flowFile instance of target {@link FlowFile}
 * @param processSession instance of {@link ProcessSession}
 * @return updated {@link FlowFile}
 */
public static FlowFile updateFlowFileAttributesWithPduProperties(PDU response, FlowFile flowFile, ProcessSession processSession) {
    if (response != null) {
        try {
            Method[] methods = PDU.class.getDeclaredMethods();
            Map<String, String> attributes = new HashMap<String, String>();
            for (Method method : methods) {
                if (Modifier.isPublic(method.getModifiers()) && (method.getParameterTypes().length == 0) && method.getName().startsWith("get")) {
                    String propertyName = extractPropertyNameFromMethod(method);
                    if (isValidSnmpPropertyName(propertyName)) {
                        Object amqpPropertyValue = method.invoke(response);
                        if (amqpPropertyValue != null) {
                            if (propertyName.equals(SNMP_PROP_PREFIX + "variableBindings") && (amqpPropertyValue instanceof Vector)) {
                                addGetOidValues(attributes, amqpPropertyValue);
                            } else {
                                attributes.put(propertyName, amqpPropertyValue.toString());
                            }
                        }
                    }
                }
            }
            flowFile = processSession.putAllAttributes(flowFile, attributes);
        } catch (Exception e) {
            logger.warn("Failed to update FlowFile with AMQP attributes", e);
        }
    }
    return flowFile;
}
 
Example #19
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 5 votes vote down vote up
static CollectObject getRuijiecpumem(String metricsName,SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    PDU pdu = session.get(oid);
    collectObject.setValue(SNMPHelper.getValueFromPDU(pdu));
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #20
Source Project: SuitAgent   Author: DevopsJK   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 使用率设值
 * @param usedPDU
 * @param freePDU
 * @param collectObject
 */
private static void setRatioValueHelper(PDU usedPDU, PDU freePDU, CollectObject collectObject){
    if(SNMPHelper.isValidPDU(usedPDU) && SNMPHelper.isValidPDU(freePDU)){
        int used = Integer.parseInt(SNMPHelper.getValueFromPDU(usedPDU));
        int free = Integer.parseInt(SNMPHelper.getValueFromPDU(freePDU));
        if (free + used != 0){
            collectObject.setValue(String.valueOf((float) used / ((float)used + (float)free)));
        }
    }
}
 
Example #21
Source Project: SuitAgent   Author: DevopsJK   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 判断传入的PDU是否带有可用的监控数据(PDU携带的第一个VariableBinding对象)
 * @param pdu
 * @return
 * true : 可用
 * false : 不可用
 */
public static boolean isValidPDU(PDU pdu){
    if(pdu == null){
        return false;
    }
    VariableBinding vb = pdu.get(0);
    if(vb == null){
        return false;
    }
    String vbResult = vb.toString();
    return !"noSuchInstance".equalsIgnoreCase(vbResult) &&
            !"noSuchObject".equalsIgnoreCase(vbResult) &&
            !"noNextInstance".equalsIgnoreCase(vbResult) &&
            !"endOfView".equalsIgnoreCase(vbResult);
}
 
Example #22
Source Project: SuitAgent   Author: DevopsJK   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 判断传入的PDU是否带有可用的监控数据
 * @param pdu
 * @param index
 * 指定的索引的VariableBinding对象
 * @return
 * true : 可用
 * false : 不可用
 */
public static boolean isValidPDU(PDU pdu,int index){
    if(pdu == null){
        return false;
    }
    VariableBinding vb = pdu.get(index);
    if(vb == null){
        return false;
    }
    String vbResult = vb.toString();
    return !"noSuchInstance".equalsIgnoreCase(vbResult) &&
            !"noSuchObject".equalsIgnoreCase(vbResult) &&
            !"noNextInstance".equalsIgnoreCase(vbResult) &&
            !"endOfView".equalsIgnoreCase(vbResult);
}
 
Example #23
Source Project: SuitAgent   Author: DevopsJK   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取传入PDU携带的MIB值(PDU携带的第一个VariableBinding对象)
 * @param pdu
 * @return
 * 若PDU无效返回空串
 */
public static String getValueFromPDU(PDU pdu){
    if(isValidPDU(pdu)){
        return pdu.get(0).getVariable().toString();
    }
    return "";
}
 
Example #24
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 #25
Source Project: mts   Author: ericsson-mts   File: MsgSnmp.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** This methods HAS TO be quick to execute for performance reason */
   @Override
public String toShortString() throws Exception 
   {
   	String ret = super.toShortString();
       ret += "\n";
       ret += "<MESSAGE type:" + PDU.getTypeString(pdu.getType()) + ", requestId:" + pdu.getRequestID();
       if(pdu.getType() != PDU.GETBULK)
           ret += ", errorStatus:" + pdu.getErrorStatusText() + ", errorIndex:" + pdu.getErrorIndex();
       else
           ret += ", nonRepeaters:" + pdu.getNonRepeaters() + ", maxRepetitions:" + pdu.getMaxRepetitions();
       ret += "/>";
	return ret;
}
 
Example #26
Source Project: mts   Author: ericsson-mts   File: MsgSnmp.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** 
   * Convert the message to XML document 
   */
  @Override
  public String toXml() throws Exception 
  {
  	String xml = "";
try {
	xml += "type: " + PDU.getTypeString(pdu.getType()) + ", requestId: " + pdu.getRequestID();
          if(pdu instanceof PDUv1 && (pdu.getType() == PDU.V1TRAP))//specific for TRAP in SNMPV1
              xml += ", enterprise: " + ((PDUv1)pdu).getEnterprise() + ", agentAddress: " + ((PDUv1)pdu).getAgentAddress() + 
                     ", genericTrap: " + ((PDUv1)pdu).getGenericTrap() + ", specificTrap: " + ((PDUv1)pdu).getSpecificTrap() +
                     ", timestamp: " + ((PDUv1)pdu).getTimestamp();
          else if(pdu.getType() != PDU.GETBULK)
          xml += ", errorStatus: " + pdu.getErrorStatusText() + ", errorIndex: " + pdu.getErrorIndex();
      else
          xml += ", nonRepeaters: " + pdu.getNonRepeaters() + ", maxRepetitions: " + pdu.getMaxRepetitions();

          xml += "\r\n";
          if(pdu.getVariableBindings().size() > 0)
          {
              xml += "VariableBinding: " + pdu.getVariableBindings().toString();
          }
	
} catch (Exception e) {
          GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL, e, "An error occured while logging the SMTP message : ", xml);
}
return xml;
  }
 
Example #27
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 5 votes vote down vote up
static CollectObject getRuijiecpumem(String metricsName,SNMPV3Session session, String oid) throws IOException {
    CollectObject collectObject = new CollectObject();
    collectObject.setSession(session);
    collectObject.setMetrics(metricsName);
    PDU pdu = session.get(oid);
    collectObject.setValue(SNMPHelper.getValueFromPDU(pdu));
    collectObject.setTime(new Date());

    return collectObject;
}
 
Example #28
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SwitchMemoryStatCollect.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 使用率设值
 * @param usedPDU
 * @param freePDU
 * @param collectObject
 */
private static void setRatioValueHelper(PDU usedPDU, PDU freePDU, CollectObject collectObject){
    if(SNMPHelper.isValidPDU(usedPDU) && SNMPHelper.isValidPDU(freePDU)){
        int used = Integer.parseInt(SNMPHelper.getValueFromPDU(usedPDU));
        int free = Integer.parseInt(SNMPHelper.getValueFromPDU(freePDU));
        if (free + used != 0){
            collectObject.setValue(String.valueOf((float) used / ((float)used + (float)free)));
        }
    }
}
 
Example #29
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 判断传入的PDU是否带有可用的监控数据(PDU携带的第一个VariableBinding对象)
 * @param pdu
 * @return
 * true : 可用
 * false : 不可用
 */
public static boolean isValidPDU(PDU pdu){
    if(pdu == null){
        return false;
    }
    VariableBinding vb = pdu.get(0);
    if(vb == null){
        return false;
    }
    String vbResult = vb.toString();
    return !"noSuchInstance".equalsIgnoreCase(vbResult) &&
            !"noSuchObject".equalsIgnoreCase(vbResult) &&
            !"noNextInstance".equalsIgnoreCase(vbResult) &&
            !"endOfView".equalsIgnoreCase(vbResult);
}
 
Example #30
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 判断传入的PDU是否带有可用的监控数据
 * @param pdu
 * @param index
 * 指定的索引的VariableBinding对象
 * @return
 * true : 可用
 * false : 不可用
 */
public static boolean isValidPDU(PDU pdu,int index){
    if(pdu == null){
        return false;
    }
    VariableBinding vb = pdu.get(index);
    if(vb == null){
        return false;
    }
    String vbResult = vb.toString();
    return !"noSuchInstance".equalsIgnoreCase(vbResult) &&
            !"noSuchObject".equalsIgnoreCase(vbResult) &&
            !"noNextInstance".equalsIgnoreCase(vbResult) &&
            !"endOfView".equalsIgnoreCase(vbResult);
}