Search in sources :

Example 1 with SyslogConnection

use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.

the class SyslogSinkConsumerNewSuspectIT method testUpdateInterfaceToNodeCache.

@Test
@Transactional
public void testUpdateInterfaceToNodeCache() throws Exception {
    // The cache has not been sync'd with the database yet
    assertEquals(0, m_cache.size());
    final Integer nodeId = m_databasePopulator.getNode1().getId();
    // One of the interfaces on node1
    final InetAddress addr = InetAddressUtils.addr("192.168.1.3");
    final byte[] bytes = ("<34>1 2010-08-19T22:14:15.000Z " + InetAddressUtils.str(addr) + " - - - - foo0: load test 0 on tty1\0").getBytes();
    final DatagramPacket pkt = new DatagramPacket(bytes, bytes.length, addr, SyslogClient.PORT);
    // Create a new SyslogConnection and call it to create the processed event
    Event e = dispatchAndCapture(new SyslogConnection(pkt, false));
    // The node is not present so nodeID should be blank
    Long foundid = e.getNodeid();
    LOG.debug("Found node ID: {}", foundid);
    assertTrue("Node ID was unexpectedly present: " + e.getNodeid(), foundid < 1);
    // Simulate a nodeGainedInterface event
    EventBuilder builder = new EventBuilder(EventConstants.NODE_GAINED_INTERFACE_EVENT_UEI, getClass().getSimpleName());
    builder.setNodeid(nodeId);
    builder.setInterface(addr);
    m_processor.handleNodeGainedInterface(builder.getEvent());
    // The entry was added to the cache
    assertEquals(1, m_cache.size());
    e = dispatchAndCapture(new SyslogConnection(pkt, false));
    // Assert that the event was associated with the node correctly
    foundid = e.getNodeid();
    LOG.debug("Found node ID: {}", foundid);
    assertEquals("Node ID was not present: " + e.getNodeid(), Long.valueOf(nodeId.longValue()), foundid);
    // Simulate an interfaceDeleted event
    builder = new EventBuilder(EventConstants.INTERFACE_DELETED_EVENT_UEI, getClass().getSimpleName());
    builder.setNodeid(nodeId);
    builder.setInterface(addr);
    m_processor.handleInterfaceDeleted(builder.getEvent());
    // The entry was removed from the cache
    assertEquals(0, m_cache.size());
    e = dispatchAndCapture(new SyslogConnection(pkt, false));
    // Assert that the event is no longer associated with the node
    assertTrue("Node ID was unexpectedly present: " + e.getNodeid(), e.getNodeid() < 1);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) DatagramPacket(java.net.DatagramPacket) Event(org.opennms.netmgt.xml.event.Event) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection) InetAddress(java.net.InetAddress) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SyslogConnection

use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.

the class SyslogdEventdLoadIT method testRfcSyslog.

@Test(timeout = 120000)
@Transactional
public void testRfcSyslog() throws Exception {
    loadSyslogConfiguration("/etc/syslogd-rfc-configuration.xml");
    startSyslogdGracefully();
    m_eventCounter.anticipate();
    InetAddress address = InetAddress.getLocalHost();
    // handle an invalid packet
    byte[] bytes = "<34>1 2010-08-19T22:14:15.000Z localhost - - - - foo0: load test 0 on tty1\0".getBytes(StandardCharsets.UTF_8);
    DatagramPacket pkt = new DatagramPacket(bytes, bytes.length, address, SyslogClient.PORT);
    SyslogMessageLogDTO messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
    m_syslogSinkConsumer.handleMessage(messageLog);
    // handle a valid packet
    bytes = "<34>1 2003-10-11T22:14:15.000Z plonk -ev/pts/8\0".getBytes();
    pkt = new DatagramPacket(bytes, bytes.length, address, SyslogClient.PORT);
    messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
    m_syslogSinkConsumer.handleMessage(messageLog);
    m_eventCounter.waitForFinish(120000);
    assertEquals(1, m_eventCounter.getCount());
}
Also used : SyslogMessageLogDTO(org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO) DatagramPacket(java.net.DatagramPacket) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection) InetAddress(java.net.InetAddress) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with SyslogConnection

use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.

the class SyslogdEventdLoadIT method testDefaultSyslogd.

@Test(timeout = 120000)
@Transactional
public void testDefaultSyslogd() throws Exception {
    startSyslogdGracefully();
    int eventCount = 100;
    List<Integer> foos = new ArrayList<Integer>();
    for (int i = 0; i < eventCount; i++) {
        int eventNum = Double.valueOf(Math.random() * 10000).intValue();
        foos.add(eventNum);
    }
    m_eventCounter.setAnticipated(eventCount);
    long start = System.currentTimeMillis();
    String testPduFormat = "2010-08-19 localhost foo%d: load test %d on tty1";
    SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DEBUG, addr("127.0.0.1"));
    for (int i = 0; i < eventCount; i++) {
        int foo = foos.get(i);
        DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, String.format(testPduFormat, foo, foo));
        SyslogMessageLogDTO messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
        m_syslogSinkConsumer.handleMessage(messageLog);
    }
    long mid = System.currentTimeMillis();
    m_eventCounter.waitForFinish(120000);
    long end = System.currentTimeMillis();
    final long total = (end - start);
    final double eventsPerSecond = (eventCount * 1000.0 / total);
    System.err.println(String.format("total time: %d, wait time: %d, events per second: %8.4f", total, (end - mid), eventsPerSecond));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SyslogMessageLogDTO(org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO) DatagramPacket(java.net.DatagramPacket) ArrayList(java.util.ArrayList) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with SyslogConnection

use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.

the class SyslogSinkModule method getAggregationPolicy.

@Override
public AggregationPolicy<SyslogConnection, SyslogMessageLogDTO> getAggregationPolicy() {
    final String systemId = distPollerDao.whoami().getId();
    final String systemLocation = distPollerDao.whoami().getLocation();
    return new AggregationPolicy<SyslogConnection, SyslogMessageLogDTO>() {

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

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

        @Override
        public Object key(SyslogConnection syslogConnection) {
            return syslogConnection.getSource();
        }

        @Override
        public SyslogMessageLogDTO aggregate(SyslogMessageLogDTO oldLog, SyslogConnection connection) {
            if (oldLog == null) {
                oldLog = new SyslogMessageLogDTO(systemLocation, systemId, connection.getSource());
            }
            SyslogMessageDTO messageDTO = new SyslogMessageDTO(connection.getBuffer());
            oldLog.getMessages().add(messageDTO);
            return oldLog;
        }
    };
}
Also used : SyslogMessageLogDTO(org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO) SyslogMessageDTO(org.opennms.netmgt.syslogd.api.SyslogMessageDTO) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection) AggregationPolicy(org.opennms.core.ipc.sink.api.AggregationPolicy)

Example 5 with SyslogConnection

use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.

the class Nms4335IT method doMessageTest.

/**
     * Send a raw syslog message and expect a given event as a result
     * 
     * @param testPDU The raw syslog message as it would appear on the wire (just the UDP payload)
     * @param expectedHost The host from which the event should be resolved as originating
     * @param expectedUEI The expected UEI of the resulting event
     * @param expectedLogMsg The expected contents of the logmsg for the resulting event 
     * 
     * @throws UnknownHostException 
     * @throws InterruptedException 
     * @throws ExecutionException 
     */
private List<Event> doMessageTest(String testPDU, String expectedHost, String expectedUEI, String expectedLogMsg) throws UnknownHostException, InterruptedException, ExecutionException {
    SyslogdTestUtils.startSyslogdGracefully(m_syslogd);
    final EventBuilder expectedEventBldr = new EventBuilder(expectedUEI, "syslogd");
    expectedEventBldr.setInterface(addr(expectedHost));
    expectedEventBldr.setLogDest("logndisplay");
    expectedEventBldr.setLogMessage(expectedLogMsg);
    m_eventIpcManager.getEventAnticipator().anticipateEvent(expectedEventBldr.getEvent());
    final SyslogSinkConsumer syslogSinkConsumer = new SyslogSinkConsumer(new MetricRegistry());
    syslogSinkConsumer.setDistPollerDao(m_distPollerDao);
    syslogSinkConsumer.setSyslogdConfig(m_config);
    syslogSinkConsumer.setEventForwarder(m_eventIpcManager);
    final SyslogSinkModule syslogSinkModule = syslogSinkConsumer.getModule();
    final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr("127.0.0.1"));
    final DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, testPDU);
    final SyslogMessageLogDTO messageLog = syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
    syslogSinkConsumer.handleMessage(messageLog);
    m_eventIpcManager.getEventAnticipator().verifyAnticipated(5000, 0, 0, 0, 0);
    final Event receivedEvent = m_eventIpcManager.getEventAnticipator().getAnticipatedEventsReceived().get(0);
    assertEquals("Log messages do not match", expectedLogMsg, receivedEvent.getLogmsg().getContent());
    return m_eventIpcManager.getEventAnticipator().getAnticipatedEventsReceived();
}
Also used : SyslogMessageLogDTO(org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) MetricRegistry(com.codahale.metrics.MetricRegistry) DatagramPacket(java.net.DatagramPacket) Event(org.opennms.netmgt.xml.event.Event) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection)

Aggregations

SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)15 DatagramPacket (java.net.DatagramPacket)11 SyslogMessageLogDTO (org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO)10 Test (org.junit.Test)9 Transactional (org.springframework.transaction.annotation.Transactional)8 InetAddress (java.net.InetAddress)6 Event (org.opennms.netmgt.xml.event.Event)4 ArrayList (java.util.ArrayList)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 InetSocketAddress (java.net.InetSocketAddress)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SyslogMessageDTO (org.opennms.netmgt.syslogd.api.SyslogMessageDTO)2 Histogram (com.codahale.metrics.Histogram)1 Meter (com.codahale.metrics.Meter)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 DatagramSocket (java.net.DatagramSocket)1 SocketException (java.net.SocketException)1