本文整理汇总了Java中org.snmp4j.smi.OctetString类的典型用法代码示例。如果您正苦于以下问题:Java OctetString类的具体用法?Java OctetString怎么用?Java OctetString使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OctetString类属于org.snmp4j.smi包,在下文中一共展示了OctetString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUpTarget
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* Helper method that initializes the snmp target to listening mode. This
* method is explicitly for V1 and V2 messages. This method will create and
* set up the TransportMapping and target for SNMP V1 and V2. It creates a
* TransportMapping and puts it in the listening mode. Also Creates
* CommunityTarget object and sets SNMP target properties.
*
* @param communityName
* Community name of the target
* @param targetIP
* IP address of Target machine
* @param portNumber
* Port number
* @return The Target created
* @throws IOException
* IOException
*/
private Target setUpTarget( final String communityName, final String targetIP, final int portNumber )
throws IOException
{
final InetAddress inetAddress = InetAddress.getByName( targetIP );
final Address address = new UdpAddress( inetAddress, portNumber );
final OctetString community = new OctetString( communityName );
final TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
snmp.listen();
// Creating the communityTarget object and setting its properties
final CommunityTarget communityTarget = new CommunityTarget();
communityTarget.setCommunity( community );
// TODO Needs to check also for v2 messages
communityTarget.setVersion( SnmpConstants.version1 );
communityTarget.setAddress( address );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setRetries( SnmpManager.DEFAULT_RETRIES );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setTimeout( SnmpManager.DEFAULT_TIMEOUT );
return communityTarget;
}
示例2: main
import org.snmp4j.smi.OctetString; //导入依赖的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());
}
}
}
示例3: sendTrap
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* Send next-in-line trap from queue to to SNMP server
*/
public void sendTrap() {
String trapVal = trapQueue.poll();
if (trapVal == null) {
return;
}
try {
PDUv1 trapPdu = createTrapPDU(trapVal);
DefaultUdpTransportMapping tm = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(tm);
tm.listen();
OctetString community = new OctetString(SNMP_XAP_COMMUNITY);
Address add = GenericAddress.parse("udp" + ":" + snmpServerIP + "/" + snmpServerPort);
CommunityTarget target = new CommunityTarget(add, community);
target.setVersion(SnmpConstants.version1);
target.setRetries(0);
target.setTimeout(5000);
snmp.send(trapPdu, target);
}
catch (IOException e) {
e.printStackTrace();
}
}
示例4: sendPDU
import org.snmp4j.smi.OctetString; //导入依赖的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 <----");
}
示例5: getMib
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
public VariableBinding getMib(String strOid) {
OID oid = new OID(strOid);
DefaultMOContextScope scope =
new DefaultMOContextScope(
new OctetString(FILEMIB_CONTEXT),
oid, true, oid.nextPeer(), false
);
ManagedObject mo = server.lookup(new DefaultMOQuery(scope, false));
if (mo == null) {
return null;
}
if (mo instanceof MutableStaticMOGroup) {
return new VariableBinding(oid, ((MutableStaticMOGroup)mo).get(oid));
}
else {
return null;
}
}
示例6: walkMib
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
public List<VariableBinding> walkMib(String strOid, int count) {
OID oid = new OID(strOid);
DefaultMOContextScope scope
= new DefaultMOContextScope(
new OctetString(FILEMIB_CONTEXT),
oid, true, oid.nextPeer(), false
);
ManagedObject mo = server.lookup(new DefaultMOQuery(scope, false));
if (mo == null) {
return new ArrayList<VariableBinding>(0);
}
if (mo instanceof MutableStaticMOGroup) {
return ((MutableStaticMOGroup)mo).walk(oid, count);
}
else {
return new ArrayList<VariableBinding>(0);
}
}
示例7: getLocalDateAndTime
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* Converts an SNMP string representation into a {@link Date} object,
* and applies time zone conversion to provide the time on the local machine, ie PSM server.
*
* @param actAlarmDateAndTime MIB-II DateAndTime formatted. May optionally contain
* a timezone offset in 3 extra bytes
* @param sysInfoTimeZone Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
* @param swVersion Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
* @return adjusted {@link Date} or a simple conversion if other fields are null.
*/
public static Date getLocalDateAndTime(String actAlarmDateAndTime, String sysInfoTimeZone,
String swVersion) {
if (StringUtils.isBlank(actAlarmDateAndTime)) {
return null;
}
GregorianCalendar decodedDateAndTimeCal = btiMakeCalendar(OctetString.fromHexString(actAlarmDateAndTime));
if ((sysInfoTimeZone == null) || (swVersion == null)) {
return decodedDateAndTimeCal.getTime();
}
TimeZone javaTimeZone = getTimeZone();
decodedDateAndTimeCal.setTimeZone(javaTimeZone);
GregorianCalendar localTime = new GregorianCalendar();
localTime.setTimeInMillis(decodedDateAndTimeCal.getTimeInMillis());
return localTime.getTime();
}
示例8: btiMakeCalendar
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* This method is similar to SNMP4J approach with some fixes for the 11-bytes version (ie the one with timezone
* offset).
* <p>
* For original makeCalendar refer @see http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/DateAndTime.html
* <p>
* Creates a <code>GregorianCalendar</code> from a properly formatted SNMP4J DateAndTime <code>OctetString</code>.
*
* @param dateAndTimeValue an OctetString conforming to the DateAndTime TC.
* @return the corresponding <code>GregorianCalendar</code> instance.
*/
public static GregorianCalendar btiMakeCalendar(OctetString dateAndTimeValue) {
int year = (dateAndTimeValue.get(0) & 0xFF) * 256
+ (dateAndTimeValue.get(1) & 0xFF);
int month = (dateAndTimeValue.get(2) & 0xFF);
int date = (dateAndTimeValue.get(3) & 0xFF);
int hour = (dateAndTimeValue.get(4) & 0xFF);
int minute = (dateAndTimeValue.get(5) & 0xFF);
int second = (dateAndTimeValue.get(6) & 0xFF);
int deci = (dateAndTimeValue.get(7) & 0xFF);
GregorianCalendar gc =
new GregorianCalendar(year, month - 1, date, hour, minute, second);
gc.set(Calendar.MILLISECOND, deci * 100);
if (dateAndTimeValue.length() == 11) {
char directionOfOffset = (char) dateAndTimeValue.get(8);
int hoursOffset = directionOfOffset == '+'
? dateAndTimeValue.get(9) : -dateAndTimeValue.get(9);
org.joda.time.DateTimeZone offset =
org.joda.time.DateTimeZone.forOffsetHoursMinutes(hoursOffset, dateAndTimeValue.get(10));
org.joda.time.DateTime dt =
new org.joda.time.DateTime(year, month, date, hour, minute, second, offset);
return dt.toGregorianCalendar();
}
return gc;
}
示例9: getTarget
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
if("3".equals(this.version))return getTargetV3();
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
//logger.info("snmp version "+this.version+", community: "+this.community);
if(this.community == null || this.community.isEmpty())
target.setCommunity(new OctetString("public"));
else
target.setCommunity(new OctetString(this.community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(5000);
target.setVersion(this.getVersionInt());
return target;
}
示例10: getTargetV3
import org.snmp4j.smi.OctetString; //导入依赖的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;
}
示例11: createSNMP
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
/**
* Create SNMP.
*/
private void createSNMP() {
LOG.info("Creating SNMP...");
try {
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
snmp = new Snmp(transport);
// SNMP V3
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
LOG.debug("Snmp created.");
snmp.listen();
LOG.debug("Snmp listening...");
} catch (IOException e) {
throw new RuntimeException("Cannot create snmp!!!", e);
}
}
示例12: getDataType
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
public static SNMPDataType getDataType(Variable var){
SNMPDataType retVal = SNMPDataType.INT;
/*
* org.snmp4j.smi.Variable implementations
* AbstractVariable, BitString,
* Counter32, Counter64, Gauge32, Integer32, UnsignedInteger32,
* TimeTicks,
* OctetString,
* GenericAddress, IpAddress, Null, OID, Opaque, SMIAddress, SshAddress, TcpAddress, TlsAddress,
* TransportIpAddress, TsmSecurityParameters, UdpAddress, VariantVariable
* SNMPDataType: STRING,INT,LONG,FLOAT,TICKS,OCTIN,OCTOUT,COUNT, OTHER
*/
if(var instanceof TimeTicks) {
retVal = SNMPDataType.TICKS;
} else if(var instanceof Counter64 || var instanceof Counter32) {
retVal = SNMPDataType.COUNT;
} else if(var instanceof Integer32 || var instanceof UnsignedInteger32) {
retVal = SNMPDataType.COUNT;
} else if(var instanceof Gauge32) {
retVal = SNMPDataType.COUNT;
} else if(var instanceof OctetString) {
retVal = SNMPDataType.STRING;
}
return retVal;
}
示例13: snmpMessageToHumanReadable
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
static PDU snmpMessageToHumanReadable(String s) throws IOException {
final OctetString message = OctetString.fromHexString(s, ':');
final Address address = new UdpAddress();
final TransportMapping transportMapping = (address instanceof UdpAddress) ?
new DefaultUdpTransportMapping((UdpAddress) address)
: new DefaultTcpTransportMapping((TcpAddress) address);
final MessageDispatcher messageDispatcher = new
MessageDispatcherImpl();
SecurityProtocols.getInstance().addDefaultProtocols();
final SnmpMessageAnalyzer snmpMessageAnalyzer = new SnmpMessageAnalyzer();
final CommandResponder commandResponder = snmpMessageAnalyzer;
messageDispatcher.addCommandResponder(commandResponder);
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
messageDispatcher.addMessageProcessingModel(new MPv3());
messageDispatcher.processMessage(transportMapping, address, ByteBuffer.wrap(message.getValue()));
PDU result = snmpMessageAnalyzer.msg;
snmpMessageAnalyzer.msg = null;
return result;
}
示例14: TestRisingAlarm
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
@Ignore("Broken Test, lookup fails in RMON-MIB")
@Test
public void TestRisingAlarm() throws InterruptedException {
out.expectedMessageCount(1);
SmiObject notification = smiManager.findSmiObject("RMON-MIB","rmon.rmonEventsV2.risingAlarm");
assertNotNull("check for null SmiObject",notification);
OID oid = notification.getOID();
SendTrap sendTrap = SendTrapDefault();
sendTrap.addVariableBinding(new VariableBinding(oid,new OctetString("Rising Alarm")));
out.assertIsSatisfied();
SnmpTrap trap = getTrap();
assertNotNull(trap);
assertEquals("check trap name","risingAlarm",trap.getTrapName());
}
示例15: start
import org.snmp4j.smi.OctetString; //导入依赖的package包/类
@Override
public void start() {
// Initialize the connection to the external client
try {
snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress(bindAddress + "/" + bindPort));
target.setTimeout(3000); //3s
target.setRetries(1);
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
} catch (IOException ex) {
//
}
super.start();
}