use of org.snmp4j.smi.TimeTicks in project camel by apache.
the class TrapTest method testSendReceiveTraps.
@Test
public void testSendReceiveTraps() throws Exception {
// Create a trap PDU
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
// put your uptime here
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 it
LOG.info("Sending pdu " + trap);
Endpoint endpoint = context.getEndpoint("direct:snmptrap");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(trap);
Producer producer = endpoint.createProducer();
producer.process(exchange);
synchronized (this) {
Thread.sleep(1000);
}
// If all goes right it should come here
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.assertIsSatisfied();
List<Exchange> exchanges = mock.getExchanges();
SnmpMessage msg = (SnmpMessage) exchanges.get(0).getIn();
PDU receivedTrap = msg.getSnmpMessage();
Assert.assertEquals(trap, receivedTrap);
if (LOG.isInfoEnabled()) {
LOG.info("Received SNMP TRAP:");
Vector<? extends VariableBinding> variableBindings = receivedTrap.getVariableBindings();
for (VariableBinding vb : variableBindings) {
LOG.info(" " + vb.toString());
}
}
}
use of org.snmp4j.smi.TimeTicks in project opennms by OpenNMS.
the class TrapDTOMapperTest method object2dtoTest.
@Test
public void object2dtoTest() throws UnknownHostException {
long testStartTime = new Date().getTime();
PDU snmp4JV2cTrapPdu = new PDU();
snmp4JV2cTrapPdu.setType(PDU.TRAP);
OID oid = new OID(".1.3.6.1.2.1.1.3.0");
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress("127.0.0.1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Trap v1 msg-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("Trap v1 msg-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.1"), new OctetString("Trap v1 msg-3")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.733.6.3.18.1.5.0"), new Integer32(1)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new Null()));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.1"), new Null(128)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.2"), new Null(129)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.3"), new Null(130)));
TrapInformation snmp4JV2cTrap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(InetAddressUtils.ONE_TWENTY_SEVEN, "public", snmp4JV2cTrapPdu);
TrapDTO trapDto = new TrapDTO(snmp4JV2cTrap);
System.out.println("trapDto is : " + trapDto);
System.out.println("trapDto.getBody() is : " + trapDto.getRawMessage());
System.out.println("trapDto.getCommunity() is : " + trapDto.getCommunity());
assertEquals(".1.3.6.1.2.1.1.3", trapDto.getTrapIdentity().getEnterpriseId());
assertEquals(6, trapDto.getTrapIdentity().getGeneric());
assertEquals(0, trapDto.getTrapIdentity().getSpecific());
assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
assertEquals("public", trapDto.getCommunity());
assertEquals(5000, trapDto.getTimestamp());
// Trap and agent address are identical with SNMPv2
assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
assertEquals("v2", trapDto.getVersion());
// Make sure that the message was created after the start of the test
assertTrue(trapDto.getCreationTime() >= testStartTime);
}
use of org.snmp4j.smi.TimeTicks in project opennms by OpenNMS.
the class Snmp4JDummyTransportTest method makePdu.
private static final PDU makePdu() {
PDU snmp4JV2cTrapPdu = new PDUv1();
OID oid = new OID(".1.3.6.1.2.1.1.3.0");
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(oid)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress("127.0.0.1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Trap v1 msg-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("Trap v1 msg-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.1"), new OctetString("Trap v1 msg-3")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.733.6.3.18.1.5.0"), new Integer32(1)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new Null()));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.1"), new Null(128)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.2"), new Null(129)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.3"), new Null(130)));
snmp4JV2cTrapPdu.setType(PDU.V1TRAP);
return snmp4JV2cTrapPdu;
}
use of org.snmp4j.smi.TimeTicks in project opennms by OpenNMS.
the class MockAgentTest method setUp.
@Override
protected void setUp() throws Exception {
MockUtil.println("------------ Begin Test " + getName() + " --------------------------");
MockLogAppender.setupLogging();
m_network = new MockNetwork();
m_network.setCriticalService("ICMP");
m_network.addNode(1, "Router");
m_network.addInterface("192.168.1.1");
m_network.addService("ICMP");
m_network.addService("SMTP");
m_network.addInterface("192.168.1.2");
m_network.addService("ICMP");
m_network.addService("SMTP");
m_network.addNode(2, "Server");
m_network.addInterface("192.168.1.3");
m_network.addService("ICMP");
m_network.addService("HTTP");
m_network.addInterface("192.168.1.2");
MapSubAgent systemGroup = new MapSubAgent("1.3.6.1.2.1.1");
systemGroup.put("1.0", new OctetString("MockAgent!"));
systemGroup.put("2.0", new OID("1.3.6.1.4.1.5813.1"));
systemGroup.put("3.0", new TimeTicks(1234));
systemGroup.put("4.0", new OctetString("Mr. Personality"));
systemGroup.put("5.0", new OctetString("mockhost"));
systemGroup.put("6.0", new OctetString("Wouldn't you like to know"));
// what happened to 7.0?
MapSubAgent interfaces = new MapSubAgent("1.3.6.1.2.1.2");
interfaces.put("1.0", new Integer32(2));
try {
m_proxy = new MockProxy(9161);
} catch (Throwable t) {
throw new Exception("Could not start MockProxy on 9161: " + t, t);
}
MockAgent agent = new MockAgent();
agent.addSubAgent(systemGroup);
agent.addSubAgent(interfaces);
m_proxy.addAgent(agent);
}
use of org.snmp4j.smi.TimeTicks in project opennms by OpenNMS.
the class PropertiesBackedManagedObject method getVariableFromValueString.
/**
* <p>getVariableFromValueString</p>
*
* @param oidStr a {@link java.lang.String} object.
* @param valStr a {@link java.lang.String} object.
* @return a {@link org.snmp4j.smi.Variable} object.
* @throws SnmpErrorStatusException
*/
private Variable getVariableFromValueString(String oidStr, String valStr) throws SnmpErrorStatusException {
Variable newVar;
if (valStr.startsWith("Wrong Type")) {
String newVal = valStr.replaceFirst("Wrong Type \\(should be .*\\): ", "");
s_log.error("Bad Mib walk has value: '" + valStr + "' using '" + newVal + "'");
valStr = newVal;
}
if ("\"\"".equals(valStr)) {
newVar = new Null();
} else {
String moTypeStr = valStr.substring(0, valStr.indexOf(':'));
String moValStr = valStr.substring(valStr.indexOf(':') + 2);
try {
if (moTypeStr.equals("STRING")) {
if (moValStr.startsWith("\"") && moValStr.endsWith("\"")) {
moValStr = moValStr.substring(1, moValStr.length() - 1);
}
newVar = new OctetString(moValStr);
} else if (moTypeStr.equals("Hex-STRING")) {
newVar = OctetString.fromHexString(moValStr.trim().replace(' ', ':'));
} else if (moTypeStr.equals("INTEGER")) {
newVar = new Integer32(Integer.parseInt(moValStr));
} else if (moTypeStr.equals("Gauge32")) {
newVar = new Gauge32(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Counter32")) {
// a 32 bit counter can be > 2 ^ 31, which is > INTEGER_MAX
newVar = new Counter32(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Counter64")) {
newVar = new Counter64(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Timeticks")) {
Integer ticksInt = Integer.parseInt(moValStr.substring(moValStr.indexOf('(') + 1, moValStr.indexOf(')')));
newVar = new TimeTicks(ticksInt);
} else if (moTypeStr.equals("OID")) {
newVar = new OID(moValStr);
} else if (moTypeStr.equals("IpAddress")) {
newVar = new IpAddress(moValStr.trim());
} else if (moTypeStr.equals("Network Address")) {
newVar = OctetString.fromHexString(moValStr.trim());
} else if (moTypeStr.equals("Responder")) {
newVar = handleDynamicVariable(oidStr, moValStr);
} else {
//newVar = new OctetString(moValStr);
throw new IllegalArgumentException("Unrecognized SNMP Type " + moTypeStr);
}
} catch (SnmpErrorStatusException e) {
throw e;
} catch (Throwable t) {
throw new UndeclaredThrowableException(t, "Could not convert value '" + moValStr + "' of type '" + moTypeStr + "' to SNMP object for OID " + oidStr);
}
}
return newVar;
}
Aggregations