Search in sources :

Example 1 with TrapInformation

use of org.opennms.netmgt.snmp.TrapInformation in project opennms by OpenNMS.

the class TrapNotificationSerializationTest method writeTrapNotificationObject.

public boolean writeTrapNotificationObject(TrapInformation object) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
        objectOutputStream.writeObject(object);
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        TrapInformation notification = (TrapInformation) in.readObject();
    } catch (NotSerializableException e) {
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    return false;
}
Also used : NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) TrapInformation(org.opennms.netmgt.snmp.TrapInformation)

Example 2 with TrapInformation

use of org.opennms.netmgt.snmp.TrapInformation in project opennms by OpenNMS.

the class TrapNotificationSerializationTest method testsnmp4JV3Serialization.

@Test
public void testsnmp4JV3Serialization() throws UnknownHostException {
    // create instance of snmp4JV3Trap
    PDU snmp4JV3TrapPdu = new ScopedPDU();
    snmp4JV3TrapPdu.setType(PDU.TRAP);
    snmp4JV3TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV3Trap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(inetAddress, new String("public"), snmp4JV3TrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV3Trap));
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) 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 3 with TrapInformation

use of org.opennms.netmgt.snmp.TrapInformation 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 4 with TrapInformation

use of org.opennms.netmgt.snmp.TrapInformation 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 5 with TrapInformation

use of org.opennms.netmgt.snmp.TrapInformation in project opennms by OpenNMS.

the class TrapSinkModule method getAggregationPolicy.

@Override
public AggregationPolicy<TrapInformationWrapper, TrapLogDTO> getAggregationPolicy() {
    return new AggregationPolicy<TrapInformationWrapper, TrapLogDTO>() {

        @Override
        public int getCompletionSize() {
            return config.getBatchSize();
        }

        @Override
        public int getCompletionIntervalMs() {
            return config.getBatchIntervalMs();
        }

        @Override
        public Object key(TrapInformationWrapper message) {
            return message.getTrapInformation().getTrapAddress();
        }

        @Override
        public TrapLogDTO aggregate(TrapLogDTO oldBucket, TrapInformationWrapper newMessage) {
            final TrapInformation trapInfo = newMessage.getTrapInformation();
            if (oldBucket == null) {
                // no log created yet
                oldBucket = new TrapLogDTO(distPoller.getId(), distPoller.getLocation(), trapInfo.getTrapAddress());
            }
            final TrapDTO trapDTO = new TrapDTO(trapInfo);
            // include the raw message, if configured
            if (config.isIncludeRawMessage()) {
                byte[] rawMessage = convertToRawMessage(trapInfo);
                if (rawMessage != null) {
                    trapDTO.setRawMessage(convertToRawMessage(trapInfo));
                }
            }
            oldBucket.addMessage(trapDTO);
            return oldBucket;
        }
    };
}
Also used : AggregationPolicy(org.opennms.core.ipc.sink.api.AggregationPolicy) TrapInformation(org.opennms.netmgt.snmp.TrapInformation)

Aggregations

TrapInformation (org.opennms.netmgt.snmp.TrapInformation)7 Test (org.junit.Test)5 OID (org.snmp4j.smi.OID)5 OctetString (org.snmp4j.smi.OctetString)5 VariableBinding (org.snmp4j.smi.VariableBinding)5 PDU (org.snmp4j.PDU)3 Date (java.util.Date)2 PDUv1 (org.snmp4j.PDUv1)2 ScopedPDU (org.snmp4j.ScopedPDU)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 InetAddress (java.net.InetAddress)1 AggregationPolicy (org.opennms.core.ipc.sink.api.AggregationPolicy)1 Integer32 (org.snmp4j.smi.Integer32)1 IpAddress (org.snmp4j.smi.IpAddress)1