Search in sources :

Example 31 with OctetString

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

the class TrapDTOMapperTest method object2dtoTestV1.

@Test
public void object2dtoTestV1() throws UnknownHostException {
    long testStartTime = new Date().getTime();
    PDUv1 snmp4JV1TrapPdu = new PDUv1();
    snmp4JV1TrapPdu.setType(PDU.V1TRAP);
    snmp4JV1TrapPdu.setEnterprise(new OID(".1.3.6.1.6.3.1.1.4.1.0"));
    snmp4JV1TrapPdu.setGenericTrap(10);
    snmp4JV1TrapPdu.setSpecificTrap(0);
    snmp4JV1TrapPdu.setTimestamp(5000);
    snmp4JV1TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
    ;
    TrapInformation snmp4JV1Trap = new Snmp4JTrapNotifier.Snmp4JV1TrapInformation(inetAddress, "public", snmp4JV1TrapPdu);
    TrapDTO trapDto = new TrapDTO(snmp4JV1Trap);
    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.6.3.1.1.4.1.0", trapDto.getTrapIdentity().getEnterpriseId());
    assertEquals(10, trapDto.getTrapIdentity().getGeneric());
    assertEquals(0, trapDto.getTrapIdentity().getSpecific());
    assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
    assertEquals("public", trapDto.getCommunity());
    assertEquals(5000, trapDto.getTimestamp());
    // This is the "default" value from SNMP4J that indicates that the trap has not been forwarded
    assertEquals("v1", trapDto.getVersion());
    // Make sure that the message was created after the start of the test
    assertTrue(trapDto.getCreationTime() >= testStartTime);
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) InetAddress(java.net.InetAddress) Date(java.util.Date) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 32 with OctetString

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

the class TrapNotificationSerializationTest method testsnmp4JV1Serialization.

@Test
public void testsnmp4JV1Serialization() throws UnknownHostException {
    // create instance of Snmp4JV1
    PDUv1 snmp4JV1TrapPdu = new PDUv1();
    snmp4JV1TrapPdu.setType(PDU.V1TRAP);
    snmp4JV1TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV1Trap = new Snmp4JTrapNotifier.Snmp4JV1TrapInformation(inetAddress, new String("public"), snmp4JV1TrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV1Trap));
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDUv1(org.snmp4j.PDUv1) 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 33 with OctetString

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

the class Snmp4jTrapReceiverIT method testTrapReceiverWithoutOpenNMS.

/*
     * IMPORTANT:
     *
     * The sentence <code>snmp.getUSM().addUser(...)</code>, is the only requirement
     * in order to properly process SNMPv3 traps.
     * 
     * This is related with the credentials that should be created for Trapd in order
     * to properly authenticate and/or decode SNMPv3 traps in OpenNMS.
     * 
     * This is a user that should be configured (or should be used) by the external
     * devices to send SNMPv3 Traps to OpenNMS.
     * 
     * The SNMPv3 users should be configured in trapd-configuration.xml
     */
@Test
public void testTrapReceiverWithoutOpenNMS() throws Exception {
    final Snmp4JStrategy strategy = new Snmp4JStrategy();
    assertEquals(0, m_trapCount);
    LOG.debug("SNMP4J: Register for Traps");
    DefaultUdpTransportMapping transportMapping = null;
    Snmp snmp = null;
    try {
        // Set socket option SO_REUSEADDR so that we can bind to the port even if it
        // has recently been closed by passing 'true' as the second argument here.
        transportMapping = new DefaultUdpTransportMapping(new UdpAddress(9162), true);
        snmp = new Snmp(transportMapping);
        snmp.addCommandResponder(this);
        snmp.getUSM().addUser(new OctetString("opennmsUser"), new UsmUser(new OctetString("opennmsUser"), AuthMD5.ID, new OctetString("0p3nNMSv3"), PrivDES.ID, new OctetString("0p3nNMSv3")));
        snmp.listen();
        sendTraps(strategy, SnmpConfiguration.AUTH_PRIV);
        await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
    } finally {
        LOG.debug("SNMP4J: Unregister for Traps");
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                LOG.debug("Failed to close Snmp object: {}", snmp, e);
            }
        }
        if (transportMapping != null) {
            try {
                transportMapping.close();
            } catch (final IOException e) {
                LOG.debug("Failed to close transport mapping: {}", transportMapping, e);
            }
        }
    }
    LOG.debug("SNMP4J: Checking Trap status");
    assertEquals(2, m_trapCount);
    snmp.getUSM().removeAllUsers();
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) UsmUser(org.snmp4j.security.UsmUser) Test(org.junit.Test)

Example 34 with OctetString

use of org.snmp4j.smi.OctetString 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;
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) Integer32(org.snmp4j.smi.Integer32) Null(org.snmp4j.smi.Null) TimeTicks(org.snmp4j.smi.TimeTicks) IpAddress(org.snmp4j.smi.IpAddress) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 35 with OctetString

use of org.snmp4j.smi.OctetString 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);
}
Also used : OctetString(org.snmp4j.smi.OctetString) Integer32(org.snmp4j.smi.Integer32) TimeTicks(org.snmp4j.smi.TimeTicks) OID(org.snmp4j.smi.OID) IOException(java.io.IOException)

Aggregations

OctetString (org.snmp4j.smi.OctetString)42 PDU (org.snmp4j.PDU)18 OID (org.snmp4j.smi.OID)18 Snmp (org.snmp4j.Snmp)15 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)14 CommunityTarget (org.snmp4j.CommunityTarget)12 UdpAddress (org.snmp4j.smi.UdpAddress)12 VariableBinding (org.snmp4j.smi.VariableBinding)12 ScopedPDU (org.snmp4j.ScopedPDU)11 USM (org.snmp4j.security.USM)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 UsmUser (org.snmp4j.security.UsmUser)8 Integer32 (org.snmp4j.smi.Integer32)8 ResponseEvent (org.snmp4j.event.ResponseEvent)7 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Address (org.snmp4j.smi.Address)5 IpAddress (org.snmp4j.smi.IpAddress)5 TimeTicks (org.snmp4j.smi.TimeTicks)5 InetAddress (java.net.InetAddress)4