本文整理汇总了Java中org.snmp4j.smi.OID类的典型用法代码示例。如果您正苦于以下问题:Java OID类的具体用法?Java OID怎么用?Java OID使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OID类属于org.snmp4j.smi包,在下文中一共展示了OID类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.snmp4j.smi.OID; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress("23.23.52.11/161"));
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
VariableBinding[] array = {new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.7")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.10")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.19"))};
pdu.addAll(array);
//pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
System.out.println("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
System.out.println(vb.getVariable().toString());
}
} else {
System.out.println("Error:" + response.getErrorStatusText());
}
}
}
示例2: querySingleSNMPTableByOID
import org.snmp4j.smi.OID; //导入依赖的package包/类
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("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;
}
示例3: snmpGet
import org.snmp4j.smi.OID; //导入依赖的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;
}
示例4: snmpWalk
import org.snmp4j.smi.OID; //导入依赖的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;
}
示例5: checkWalkFinished
import org.snmp4j.smi.OID; //导入依赖的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;
}
示例6: getAddDropPort
import org.snmp4j.smi.OID; //导入依赖的package包/类
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;
}
示例7: poll
import org.snmp4j.smi.OID; //导入依赖的package包/类
@Override
protected int poll() throws Exception {
this.pdu.clear();
int type = this.getPduType(this.endpoint.getType());
this.pdu.setType(type);
// prepare the request items
for (OID oid : this.endpoint.getOids()) {
this.pdu.add(new VariableBinding(oid));
}
// send the request
snmp.send(pdu, target, null, this);
return 1;
}
示例8: convertPrivacyProtocol
import org.snmp4j.smi.OID; //导入依赖的package包/类
private OID convertPrivacyProtocol(String privacyProtocol) {
if (privacyProtocol == null) {
return null;
}
if ("DES".equals(privacyProtocol)) {
return PrivDES.ID;
} else if ("TRIDES".equals(privacyProtocol)) {
return Priv3DES.ID;
} else if ("AES128".equals(privacyProtocol)) {
return PrivAES128.ID;
} else if ("AES192".equals(privacyProtocol)) {
return PrivAES192.ID;
} else if ("AES256".equals(privacyProtocol)) {
return PrivAES256.ID;
} else {
throw new IllegalArgumentException("Unknown privacy protocol: " + privacyProtocol);
}
}
示例9: endOfTable
import org.snmp4j.smi.OID; //导入依赖的package包/类
private boolean endOfTable(int offset) {
if (response == null) {
throw new WouldBlockException();
}
int i = 0;
while (offset + i < response.size() && i < repeaters) {
OID oid = response.get(offset + i).getOid();
if (!oid.startsWith(requestedVarbinds[nonRepeaters + i].getOid())) {
return true;
}
i++;
}
if (i < repeaters) {
setNextOids(offset - repeaters);
throw new WouldBlockException();
}
return false;
}
示例10: privType
import org.snmp4j.smi.OID; //导入依赖的package包/类
private OID privType(SnmpV3Target v3Target) {
switch (v3Target.getPrivType()) {
case DES:
return PrivDES.ID;
case DES3:
return Priv3DES.ID;
case AES128:
return PrivAES128.ID;
case AES192:
return PrivAES192.ID;
case AES256:
return PrivAES256.ID;
default:
throw new IllegalArgumentException("unrecognized privacy type");
}
}
示例11: getIndexes
import org.snmp4j.smi.OID; //导入依赖的package包/类
@Override
public Varbind[] getIndexes() {
if (indexExtractor == null) {
return new Varbind[0];
}
IndexDescriptor[] descriptors = indexExtractor.extractIndexes(getOid());
Varbind[] varbinds = new Varbind[descriptors.length];
for (int i = 0; i < descriptors.length; i++) {
IndexDescriptor descriptor = descriptors[i];
Variable variable = AbstractVariable.createFromSyntax(
descriptor.getSyntax());
variable.fromSubIndex(new OID(descriptor.getEncoded()),
descriptor.isImplied());
VariableBinding vb = new VariableBinding(new OID(descriptor.getOid()),
variable);
varbinds[i] = varbindFactory.newVarbind(vb);
}
return varbinds;
}
示例12: querySysData
import org.snmp4j.smi.OID; //导入依赖的package包/类
/**
* 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;
}
示例13: querySysData3
import org.snmp4j.smi.OID; //导入依赖的package包/类
/**
*
* @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;
}
示例14: get
import org.snmp4j.smi.OID; //导入依赖的package包/类
/**
* 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");
}
示例15: getTargetV3
import org.snmp4j.smi.OID; //导入依赖的package包/类
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;
}