Java源码示例:org.snmp4j.smi.TimeTicks
示例1
public static void sendTrapV2(String port) throws IOException {
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Specify receiver
Address targetaddress = new UdpAddress("127.0.0.1/" + port);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetaddress);
// Send
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.send(trap, target, null, null);
snmp.close();
}
示例2
public static void sendTrapV1(String port) throws IOException {
TransportMapping<?> transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(new OctetString("public")));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress("127.0.0.1/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU trap = new PDUv1();
trap.setType(PDU.V1TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Send
Snmp snmp = new Snmp(transport);
snmp.send(trap, comtarget);
transport.close();
snmp.close();
}
示例3
@Override
public TimeTicks modify(final TimeTicks variable) {
TimeTicks timeTicks = new TimeTicks();
final long timeTicksInMilliseconds = variable.toMilliseconds();
final long upTime = (System.currentTimeMillis() - initTime) + timeTicksInMilliseconds;
timeTicks.fromMilliseconds(timeTicksInMilliseconds + upTime);
return timeTicks;
}
示例4
@Test
public void testModify() throws Exception {
final TimeTicksModifier timeTicksModifier = new TimeTicksModifier();
final TimeTicks timeTicks = new TimeTicks();
final long initialTimeTicks = timeTicks.getValue();
Thread.sleep(11L);
final TimeTicks modifiedVariable = timeTicksModifier.modify(timeTicks);
final long newTimeTicks = modifiedVariable.getValue();
assertNotEquals(newTimeTicks, initialTimeTicks);
}
示例5
private PDU createPDU(SnmpTrapInfo snmpTrapInfo) {
PDU trap = new PDU();
trap.setType(PDU.TRAP);
int alertType = snmpTrapInfo.getAlertType() + 1;
if (alertType > 0) {
long sysUpTimeTicks = ManagementFactory.getRuntimeMXBean().getUptime() / 10;
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks)));
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType)));
if (snmpTrapInfo.getDataCenterId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId())));
}
if (snmpTrapInfo.getPodId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId())));
}
if (snmpTrapInfo.getClusterId() != 0) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId())));
}
if (snmpTrapInfo.getMessage() != null) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage())));
} else {
throw new CloudRuntimeException(" What is the use of alert without message ");
}
if (snmpTrapInfo.getGenerationTime() != null) {
trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString())));
} else {
trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
}
} else {
throw new CloudRuntimeException(" Invalid alert Type ");
}
return trap;
}