use of org.graylog2.gelfclient.transport.GelfTransport in project graylog2-server by Graylog2.
the class GelfOutputTest method testToGELFMessageWithValidStringLevel.
@Test
public void testToGELFMessageWithValidStringLevel() throws Exception {
final GelfTransport transport = mock(GelfTransport.class);
final GelfOutput gelfOutput = new GelfOutput(transport);
final DateTime now = DateTime.now(DateTimeZone.UTC);
final Message message = new Message("Test", "Source", now);
message.addField("level", "6");
final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
assertEquals(GelfMessageLevel.INFO, gelfMessage.getLevel());
}
use of org.graylog2.gelfclient.transport.GelfTransport in project graylog2-server by Graylog2.
the class GelfOutputTest method testToGELFMessageWithNullLevel.
@Test
public void testToGELFMessageWithNullLevel() throws Exception {
final GelfTransport transport = mock(GelfTransport.class);
final GelfOutput gelfOutput = new GelfOutput(transport);
final DateTime now = DateTime.now(DateTimeZone.UTC);
final Message message = new Message("Test", "Source", now);
message.addField("level", null);
final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
assertEquals(GelfMessageLevel.ALERT, gelfMessage.getLevel());
}
use of org.graylog2.gelfclient.transport.GelfTransport in project graylog2-server by Graylog2.
the class GelfOutput method buildTransport.
protected static GelfTransport buildTransport(final Configuration configuration) throws MessageOutputConfigurationException {
final String protocol = configuration.getString(CK_PROTOCOL);
final String hostname = configuration.getString(CK_HOSTNAME);
final int port = configuration.getInt(CK_PORT);
final int connectTimeout = configuration.getInt(CK_CONNECT_TIMEOUT, 1000);
final int reconnectDelay = configuration.getInt(CK_RECONNECT_DELAY, 500);
final boolean tcpKeepAlive = configuration.getBoolean(CK_TCP_KEEP_ALIVE, false);
final boolean tcpNoDelay = configuration.getBoolean(CK_TCP_NO_DELAY, false);
final boolean tlsVerificationEnabled = configuration.getBoolean(CK_TLS_VERIFICATION_ENABLED, false);
final String tlsTrustCertChain = configuration.getString(CK_TLS_TRUST_CERT_CHAIN);
final int queueSize = configuration.getInt(CK_QUEUE_SIZE, 512);
final int maxInflightSends = configuration.getInt(CK_MAX_INFLIGHT_SENDS, 512);
if (isNullOrEmpty(protocol) || isNullOrEmpty(hostname) || !configuration.intIsSet(CK_PORT)) {
throw new MessageOutputConfigurationException("Protocol and/or hostname missing!");
}
final GelfTransports transport;
final boolean tlsEnabled;
switch(protocol.toUpperCase(Locale.ENGLISH)) {
case "UDP":
transport = GelfTransports.UDP;
tlsEnabled = false;
break;
case "TCP":
transport = GelfTransports.TCP;
tlsEnabled = false;
break;
case "TCP+TLS":
transport = GelfTransports.TCP;
tlsEnabled = true;
break;
default:
throw new MessageOutputConfigurationException("Unknown protocol " + protocol);
}
final File tlsTrustCertChainFile;
if (tlsEnabled && !isNullOrEmpty(tlsTrustCertChain)) {
tlsTrustCertChainFile = new File(tlsTrustCertChain);
if (!tlsTrustCertChainFile.isFile() && !tlsTrustCertChainFile.canRead()) {
throw new MessageOutputConfigurationException("TLS trust certificate chain file cannot be read!");
}
} else {
tlsTrustCertChainFile = null;
}
final GelfConfiguration gelfConfiguration = new GelfConfiguration(hostname, port).transport(transport).connectTimeout(connectTimeout).reconnectDelay(reconnectDelay).tcpKeepAlive(tcpKeepAlive).tcpNoDelay(tcpNoDelay).queueSize(queueSize).maxInflightSends(maxInflightSends);
if (tlsEnabled) {
gelfConfiguration.enableTls();
if (tlsVerificationEnabled) {
gelfConfiguration.enableTlsCertVerification();
} else {
gelfConfiguration.disableTlsCertVerification();
}
if (tlsTrustCertChainFile != null) {
gelfConfiguration.tlsTrustCertChainFile(tlsTrustCertChainFile);
}
}
LOG.debug("Initializing GELF sender and connecting to {}://{}:{}", protocol, hostname, port);
try {
return GelfTransports.create(gelfConfiguration);
} catch (Exception e) {
final String error = "Error initializing " + GelfOutput.class;
LOG.error(error, e);
throw new MessageOutputConfigurationException(error);
}
}
use of org.graylog2.gelfclient.transport.GelfTransport in project graylog2-server by Graylog2.
the class GelfOutputTest method testToGELFMessageFullMessage.
@Test
public void testToGELFMessageFullMessage() throws Exception {
final GelfTransport transport = mock(GelfTransport.class);
final GelfOutput gelfOutput = new GelfOutput(transport);
final DateTime now = DateTime.now(DateTimeZone.UTC);
final Message message = new Message("Test", "Source", now);
message.addField(Message.FIELD_FULL_MESSAGE, "Full Message");
final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
assertEquals("Full Message", gelfMessage.getFullMessage());
}
use of org.graylog2.gelfclient.transport.GelfTransport in project graylog2-server by Graylog2.
the class GelfOutputTest method testToGELFMessageWithInvalidStringLevel.
@Test
public void testToGELFMessageWithInvalidStringLevel() throws Exception {
final GelfTransport transport = mock(GelfTransport.class);
final GelfOutput gelfOutput = new GelfOutput(transport);
final DateTime now = DateTime.now(DateTimeZone.UTC);
final Message message = new Message("Test", "Source", now);
message.addField("level", "BOOM");
final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
assertEquals(GelfMessageLevel.ALERT, gelfMessage.getLevel());
}
Aggregations