use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.
the class SyslogdEventdLoadIT method testNGSyslog.
@Test(timeout = 120000)
@Transactional
public void testNGSyslog() throws Exception {
loadSyslogConfiguration("/etc/syslogd-syslogng-configuration.xml");
startSyslogdGracefully();
m_eventCounter.anticipate();
InetAddress address = InetAddress.getLocalHost();
// handle an invalid packet
byte[] bytes = "<34>main: 2010-08-19 localhost foo0: load test 0 on tty1\0".getBytes();
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>monkeysatemybrain!\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());
}
use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.
the class SyslogdIT 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 {
final EventBuilder expectedEventBldr = new EventBuilder(expectedUEI, "syslogd");
expectedEventBldr.setInterface(addr(expectedHost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(expectedLogMsg);
m_eventIpcManager.getEventAnticipator().anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, InetAddressUtils.ONE_TWENTY_SEVEN);
final DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, testPDU);
SyslogMessageLogDTO messageLog = m_syslogSinkModule.toMessageLog(new SyslogConnection(pkt, false));
m_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();
}
use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.
the class SyslogReceiverCamelNettyImpl method run.
/**
* The execution context.
*/
@Override
public void run() {
// Setup logging and create the dispatcher
super.run();
SimpleRegistry registry = new SimpleRegistry();
//Adding netty component to camel in order to resolve OSGi loading issues
NettyComponent nettyComponent = new NettyComponent();
m_camel = new DefaultCamelContext(registry);
// Set the context name so that it shows up nicely in JMX
//
// @see org.apache.camel.management.DefaultManagementNamingStrategy
//
//m_camel.setManagementName("org.opennms.features.events.syslog.listener");
m_camel.setName("syslogdListenerCamelNettyContext");
m_camel.setManagementNameStrategy(new DefaultManagementNameStrategy(m_camel, "#name#", null));
m_camel.addComponent("netty4", nettyComponent);
m_camel.getShutdownStrategy().setShutdownNowOnTimeout(true);
m_camel.getShutdownStrategy().setTimeout(15);
m_camel.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
try {
m_camel.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
String from = String.format("netty4:udp://%s:%d?sync=false&allowDefaultCodec=false&receiveBufferSize=%d&connectTimeout=%d", InetAddressUtils.str(m_host), m_port, Integer.MAX_VALUE, SOCKET_TIMEOUT);
from(from).routeId("syslogListen").process(new AsyncProcessor() {
@Override
public void process(Exchange exchange) throws Exception {
final ByteBuf buffer = exchange.getIn().getBody(ByteBuf.class);
// NettyConstants.NETTY_REMOTE_ADDRESS is a SocketAddress type but because
// we are listening on an InetAddress, it will always be of type InetAddressSocket
InetSocketAddress source = (InetSocketAddress) exchange.getIn().getHeader(NettyConstants.NETTY_REMOTE_ADDRESS);
// Synchronously invoke the dispatcher
m_dispatcher.send(new SyslogConnection(source, buffer.nioBuffer())).get();
}
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
final ByteBuf buffer = exchange.getIn().getBody(ByteBuf.class);
// NettyConstants.NETTY_REMOTE_ADDRESS is a SocketAddress type but because
// we are listening on an InetAddress, it will always be of type InetAddressSocket
InetSocketAddress source = (InetSocketAddress) exchange.getIn().getHeader(NettyConstants.NETTY_REMOTE_ADDRESS);
ByteBuffer bufferCopy = ByteBuffer.allocate(buffer.readableBytes());
buffer.getBytes(buffer.readerIndex(), bufferCopy);
m_dispatcher.send(new SyslogConnection(source, bufferCopy)).whenComplete((r, e) -> {
if (e != null) {
exchange.setException(e);
}
callback.done(false);
});
return false;
}
});
}
});
m_camel.start();
} catch (Throwable e) {
LOG.error("Could not configure Camel routes for syslog receiver", e);
}
}
use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.
the class SyslogReceiverJavaNetImpl method run.
/**
* The execution context.
*/
@Override
public void run() {
// Setup logging and create the dispatcher
super.run();
// get the context
m_context = Thread.currentThread();
// Create some metrics
Meter packetMeter = METRICS.meter(MetricRegistry.name(getClass(), "packets"));
Histogram packetSizeHistogram = METRICS.histogram(MetricRegistry.name(getClass(), "packetSize"));
if (m_stop) {
LOG.debug("Stop flag set before thread started, exiting");
return;
} else {
LOG.debug("Thread context started");
}
// allocate a buffer
final int length = 0xffff;
final byte[] buffer = new byte[length];
try {
LOG.debug("Creating syslog socket");
m_dgSock = new DatagramSocket(null);
} catch (SocketException e) {
LOG.warn("Could not create syslog socket: " + e.getMessage(), e);
return;
}
// if a socket is closed.
try {
LOG.debug("Setting socket timeout to {}ms", SOCKET_TIMEOUT);
m_dgSock.setSoTimeout(SOCKET_TIMEOUT);
} catch (SocketException e) {
LOG.warn("An I/O error occured while trying to set the socket timeout", e);
}
// also bound. This shouldn't have any effect at runtime.
try {
LOG.debug("Setting socket SO_REUSEADDR to true");
m_dgSock.setReuseAddress(true);
} catch (SocketException e) {
LOG.warn("An I/O error occured while trying to set SO_REUSEADDR", e);
}
// Increase the receive buffer for the socket
try {
LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
m_dgSock.setReceiveBufferSize(Integer.MAX_VALUE);
LOG.debug("Actual receive buffer size is {}", m_dgSock.getReceiveBufferSize());
} catch (SocketException e) {
LOG.info("Failed to set the receive buffer to {}", Integer.MAX_VALUE, e);
}
try {
LOG.debug("Opening datagram socket");
if (m_config.getListenAddress() != null) {
m_dgSock.bind(new InetSocketAddress(InetAddressUtils.addr(m_config.getListenAddress()), m_config.getSyslogPort()));
} else {
m_dgSock.bind(new InetSocketAddress(m_config.getSyslogPort()));
}
} catch (SocketException e) {
LOG.info("Failed to open datagram socket", e);
}
// set to avoid numerous tracing message
boolean ioInterrupted = false;
// Construct one mutable {@link DatagramPacket} that will be used for receiving syslog messages
DatagramPacket pkt = new DatagramPacket(buffer, length);
// now start processing incoming requests
while (!m_stop) {
if (m_context.isInterrupted()) {
LOG.debug("Thread context interrupted");
break;
}
try {
if (!ioInterrupted) {
LOG.debug("Waiting on a datagram to arrive");
}
m_dgSock.receive(pkt);
// Increment the packet counter
packetMeter.mark();
// Create a metric for the Syslog packet size
packetSizeHistogram.update(length);
final SyslogConnection connection = new SyslogConnection(pkt, true);
m_dispatcher.send(connection);
// reset the flag
ioInterrupted = false;
} catch (SocketTimeoutException e) {
ioInterrupted = true;
continue;
} catch (InterruptedIOException e) {
ioInterrupted = true;
continue;
} catch (IOException e) {
if (m_stop) {
// A SocketException can be thrown during normal shutdown so log as debug
LOG.debug("Shutting down the datagram receipt port: " + e.getMessage());
} else {
LOG.error("An I/O exception occured on the datagram receipt port, exiting", e);
}
break;
}
}
// end while status OK
LOG.debug("Thread context exiting");
}
use of org.opennms.netmgt.syslogd.api.SyslogConnection in project opennms by OpenNMS.
the class SyslogSinkModule method toMessageLog.
/**
* Used for testing.
*/
public SyslogMessageLogDTO toMessageLog(SyslogConnection... connections) {
final String systemId = distPollerDao.whoami().getId();
final String systemLocation = distPollerDao.whoami().getLocation();
if (connections.length < 1) {
throw new IllegalArgumentException("One or more connection are required.");
}
final SyslogMessageLogDTO messageLog = new SyslogMessageLogDTO(systemLocation, systemId, connections[0].getSource());
for (SyslogConnection connection : connections) {
final SyslogMessageDTO messageDTO = new SyslogMessageDTO(connection.getBuffer());
messageLog.getMessages().add(messageDTO);
}
return messageLog;
}
Aggregations