All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.inputs.raw.RawProcessor Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/**
 * This file is part of Graylog2.
 *
 * Graylog2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog2.  If not, see .
 */
package org.graylog2.inputs.raw;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.buffers.Buffer;
import org.graylog2.plugin.buffers.BufferOutOfCapacityException;
import org.graylog2.plugin.configuration.Configuration;
import org.graylog2.plugin.inputs.MessageInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

import static com.codahale.metrics.MetricRegistry.name;

/**
 * @author Lennart Koopmann 
 */
public class RawProcessor {

    private static final Logger LOG = LoggerFactory.getLogger(RawProcessor.class);
    private final Buffer processBuffer;
    private final Configuration config;

    private final MessageInput sourceInput;

    private final Meter incomingMessages;
    private final Meter failures;
    private final Meter incompleteMessages;
    private final Meter processedMessages;
    private final Timer parseTime;

    public RawProcessor(MetricRegistry metricRegistry,
                        Buffer processBuffer,
                        Configuration config,
                        MessageInput sourceInput) {
        this.processBuffer = processBuffer;
        this.config = config;

        this.sourceInput = sourceInput;

        String metricName = sourceInput.getUniqueReadableId();
        this.incomingMessages = metricRegistry.meter(name(metricName, "incomingMessages"));
        this.failures = metricRegistry.meter(name(metricName, "failures"));
        this.processedMessages = metricRegistry.meter(name(metricName, "processedMessages"));
        this.incompleteMessages = metricRegistry.meter(name(metricName, "incompleteMessages"));
        this.parseTime = metricRegistry.timer(name(metricName, "parseTime"));
    }

    public void messageReceived(String msg, InetAddress remoteAddress) throws BufferOutOfCapacityException {
        incomingMessages.mark();

        // Convert to LogMessage
        Message lm;
        try {
            lm = new Message(msg, parseSource(msg, remoteAddress), Tools.iso8601());
        } catch (Exception e) {
            failures.mark();
            LOG.error("Could not parse raw message. Not further handling.", e);
            return;
        }

        if (!lm.isComplete()) {
            incompleteMessages.mark();
            LOG.debug("Skipping incomplete message.");
            return;
        }

        // Add to process buffer.
        LOG.debug("Adding received raw message <{}> to process buffer: {}", lm.getId(), lm);
        processedMessages.mark();
        processBuffer.insertCached(lm, sourceInput);
    }

    private String parseSource(String msg, InetAddress remoteAddress) {
        if (config.stringIsSet(RawInputBase.CK_OVERRIDE_SOURCE)) {
            return config.getString(RawInputBase.CK_OVERRIDE_SOURCE);
        }

        return remoteAddress.getCanonicalHostName();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy