本文整理汇总了Java中org.snmp4j.PDU类的典型用法代码示例。如果您正苦于以下问题:Java PDU类的具体用法?Java PDU怎么用?Java PDU使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PDU类属于org.snmp4j包,在下文中一共展示了PDU类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendPDU
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* 向接收器发送Trap 信息
*
* @throws IOException
*/
public void sendPDU() throws IOException {
PDU pdu = new PDU();
pdu.add(new VariableBinding(
new OID(".1.3.6.1.2.1.1.1.0"),
new OctetString("SNMP Trap Test.see more:http://www.micmiu.com")));
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(
new UnsignedInteger32(System.currentTimeMillis() / 1000)
.getValue())));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(
".1.3.6.1.6.3.1.1.4.3")));
// 向Agent发送PDU
pdu.setType(PDU.TRAP);
snmp.send(pdu, target);
System.out.println("----> Trap Send END <----");
}
示例2: processResponse
import org.snmp4j.PDU; //导入依赖的package包/类
private void processResponse(PDU response) {
try {
if (log().isDebugEnabled()) {
log().debug("Received a tracker PDU of type "+PDU.getTypeString(response.getType())+" from "+getAddress()+" of size "+response.size()+", errorStatus = "+response.getErrorStatus()+", errorStatusText = "+response.getErrorStatusText()+", errorIndex = "+response.getErrorIndex());
}
if (response.getType() == PDU.REPORT) {
handleAuthError("A REPORT PDU was returned from the agent. This is most likely an authentication problem. Please check the config");
} else {
if (!processErrors(response.getErrorStatus(), response.getErrorIndex())) {
if (response.size() == 0) { // NMS-6484
handleError("A PDU with no errors and 0 varbinds was returned from the agent at " + getAddress() + ". This seems to be related with a broken SNMP agent.");
} else {
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
SnmpObjId receivedOid = SnmpObjId.get(vb.getOid().getValue());
SnmpValue val = new Snmp4JValue(vb.getVariable());
Snmp4JWalker.this.processResponse(receivedOid, val);
}
}
}
buildAndSendNextPdu();
}
} catch (Throwable e) {
handleFatalError(e);
}
}
示例3: processResponse
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* TODO: Merge this logic with {@link Snmp4JWalker.Snmp4JResponseListener#processResponse(PDU response)}
*/
private static SnmpValue[] processResponse(Snmp4JAgentConfig agentConfig, ResponseEvent responseEvent) throws IOException {
SnmpValue[] retvalues = { null };
if (responseEvent.getResponse() == null) {
log().warn("processResponse: Timeout. Agent: "+agentConfig);
} else if (responseEvent.getError() != null) {
log().warn("processResponse: Error during get operation. Error: "+responseEvent.getError().getLocalizedMessage(), responseEvent.getError());
} else if (responseEvent.getResponse().getType() == PDU.REPORT) {
log().warn("processResponse: Error during get operation. Report returned with varbinds: "+responseEvent.getResponse().getVariableBindings());
} else if (responseEvent.getResponse().getVariableBindings().size() < 1) {
log().warn("processResponse: Received PDU with 0 varbinds.");
} else if (responseEvent.getResponse().get(0).getSyntax() == SMIConstants.SYNTAX_NULL) {
log().info("processResponse: Null value returned in varbind: " + responseEvent.getResponse().get(0));
} else {
retvalues = convertResponseToValues(responseEvent);
if (log().isDebugEnabled()) {
log().debug("processResponse: SNMP operation successful, value: "+Arrays.toString(retvalues));
}
}
return retvalues;
}
示例4: walkForSumDivCount
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* 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;
}
示例5: getOldHuawei_Mem
import org.snmp4j.PDU; //导入依赖的package包/类
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;
}
示例6: getH3CHWcpumem
import org.snmp4j.PDU; //导入依赖的package包/类
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;
}
示例7: getCisco_ASA_Mem
import org.snmp4j.PDU; //导入依赖的package包/类
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;
}
示例8: getCisco_IOS_XR_Mem
import org.snmp4j.PDU; //导入依赖的package包/类
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;
}
示例9: snmpGet
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* 获取指定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;
}
示例10: snmpWalk
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* walk方式获取指定的oid value
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static List<PDU> snmpWalk(Snmp snmp, Target target, String oid) throws IOException {
List<PDU> pduList = new ArrayList<>();
ScopedPDU pdu = new ScopedPDU();
OID targetOID = new OID(oid);
pdu.add(new VariableBinding(targetOID));
boolean finished = false;
while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);
PDU response = respEvent.getResponse();
if (null == response) {
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
pduList.add(response);
// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
}
}
return pduList;
}
示例11: checkWalkFinished
import org.snmp4j.PDU; //导入依赖的package包/类
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;
}
示例12: getSysDesc
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* 获取设备描述字符串
*
* @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 "";
}
}
示例13: getSysDesc
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* 获取设备描述字符串
*
* @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 "";
}
}
示例14: onResponse
import org.snmp4j.PDU; //导入依赖的package包/类
public void onResponse(ResponseEvent event) {
// Always cancel async request when response has been received
// otherwise a memory leak is created! Not canceling a request
// immediately can be useful when sending a request to a broadcast address.
((Snmp)event.getSource()).cancel(event.getRequest(), this);
// check for valid response
if (event.getRequest() == null || event.getResponse() == null) {
// ignore null requests/responses
LOG.debug("Received invalid SNMP event. Request: " + event.getRequest() + " / Response: " + event.getResponse());
return;
}
PDU pdu = event.getResponse();
processPDU(pdu);
}
示例15: validateResponse
import org.snmp4j.PDU; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void validateResponse(ResponseEvent event) {
super.validateResponse(event);
PDU response = event.getResponse();
final int responseSize = response.size();
if (responseSize <= nonRepeaters) {
throw new TruncatedResponseException(
"response contains no repeaters; too many non-repeaters?");
}
if (nonRepeaters + repeaters >= responseSize) {
if (context.getConfig().isWalkAllowsTruncatedRepetition()) {
this.repeaters = responseSize - nonRepeaters;
}
else {
throw new TruncatedResponseException(
"response contains partial first repetition; "
+ "set walkAllowsTruncatedRepetition if you wish to allow it");
}
}
}