Search in sources :

Example 6 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class TrapNotificationSerializationTest method testsnmp4JV2cSerialization.

@Test
public void testsnmp4JV2cSerialization() throws UnknownHostException {
    // create instance of snmp4JV2cTrap
    PDU snmp4JV2cTrapPdu = new PDU();
    snmp4JV2cTrapPdu.setType(PDU.TRAP);
    snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV2cTrap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(inetAddress, new String("public"), snmp4JV2cTrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV2cTrap));
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 7 with VariableBinding

use of org.snmp4j.smi.VariableBinding 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);
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) Null(org.snmp4j.smi.Null) TimeTicks(org.snmp4j.smi.TimeTicks) OID(org.snmp4j.smi.OID) Date(java.util.Date) Integer32(org.snmp4j.smi.Integer32) IpAddress(org.snmp4j.smi.IpAddress) VariableBinding(org.snmp4j.smi.VariableBinding) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 8 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class Snmp4JV2TrapBuilder method addVarBind.

@Override
public void addVarBind(SnmpObjId name, SnmpValue value) {
    OID oid = new OID(name.getIds());
    Variable val = ((Snmp4JValue) value).getVariable();
    m_pdu.add(new VariableBinding(oid, val));
}
Also used : Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 9 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class Snmp4JStrategyIT method testPreparePduWithValues.

@Test
public void testPreparePduWithValues() throws Exception {
    SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0"), SnmpObjId.get(".1.3.5.1.1.4.0") };
    SnmpValue[] values = new SnmpValue[] { snmpValue("foo"), snmpValue("bar") };
    PDU pdu = m_strategy.buildPdu(new Snmp4JAgentConfig(getAgentConfig()), PDU.SET, oids, values);
    assertNotNull("PDU should not be null", pdu);
    assertEquals("PDU variable bindings size", oids.length, pdu.getVariableBindings().size());
    for (int i = 0; i < oids.length; i++) {
        VariableBinding vb = pdu.get(i);
        assertEquals("PDU variable binding " + i + " OID", oids[i].toString(), "." + vb.getOid().toString());
        assertEquals("PDU variable binding " + i + " syntax", vb.getSyntax(), SMIConstants.SYNTAX_OCTET_STRING);
        assertEquals("PDU variable binding " + i + " variable syntax", vb.getVariable().getSyntax(), SMIConstants.SYNTAX_OCTET_STRING);
        assertEquals("PDU variable binding " + i + " variable value", vb.getVariable().toString(), values[i].toString());
    }
}
Also used : PDU(org.snmp4j.PDU) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) VariableBinding(org.snmp4j.smi.VariableBinding) Test(org.junit.Test)

Example 10 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class Snmp4JStrategyIT method testPreparePduWithNoValues.

@Test
public void testPreparePduWithNoValues() throws Exception {
    SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0"), SnmpObjId.get(".1.3.5.1.1.4.0") };
    SnmpValue[] values = null;
    PDU pdu = m_strategy.buildPdu(new Snmp4JAgentConfig(getAgentConfig()), PDU.SET, oids, values);
    assertNotNull("PDU should not be null", pdu);
    assertEquals("PDU variable bindings size", oids.length, pdu.getVariableBindings().size());
    for (int i = 0; i < oids.length; i++) {
        VariableBinding vb = pdu.get(i);
        assertEquals("PDU variable binding " + i + " OID", oids[i].toString(), "." + vb.getOid().toString());
        assertEquals("PDU variable binding " + i + " syntax", vb.getSyntax(), SMIConstants.SYNTAX_NULL);
        assertEquals("PDU variable binding " + i + " variable syntax", vb.getVariable().getSyntax(), SMIConstants.SYNTAX_NULL);
    }
}
Also used : PDU(org.snmp4j.PDU) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) VariableBinding(org.snmp4j.smi.VariableBinding) Test(org.junit.Test)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)52 OID (org.snmp4j.smi.OID)37 OctetString (org.snmp4j.smi.OctetString)31 PDU (org.snmp4j.PDU)30 ScopedPDU (org.snmp4j.ScopedPDU)17 Test (org.junit.Test)11 PDUv1 (org.snmp4j.PDUv1)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 Variable (org.snmp4j.smi.Variable)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Map (java.util.Map)4