Search in sources :

Example 1 with GelfConfiguration

use of org.graylog2.gelfclient.GelfConfiguration 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);
    }
}
Also used : GelfTransports(org.graylog2.gelfclient.GelfTransports) GelfConfiguration(org.graylog2.gelfclient.GelfConfiguration) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException) File(java.io.File) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException)

Aggregations

File (java.io.File)1 GelfConfiguration (org.graylog2.gelfclient.GelfConfiguration)1 GelfTransports (org.graylog2.gelfclient.GelfTransports)1 MessageOutputConfigurationException (org.graylog2.plugin.outputs.MessageOutputConfigurationException)1