Skip to content

Commit

Permalink
Merge pull request #63 from joschi/SERVER-27
Browse files Browse the repository at this point in the history
Added configuration option to set listening address for GELF service
  • Loading branch information
Lennart Koopmann committed Dec 5, 2011
2 parents 476aa28 + 105a979 commit c2597ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions misc/graylog2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mongodb_threads_allowed_to_block_multiplier = 5

# Graylog Extended Log Format (GELF)
use_gelf = true
gelf_listen_address = 0.0.0.0
gelf_listen_port = 12201

# Drools Rule File (Use to rewrite incoming log messages)
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/graylog2/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class Configuration {
@Parameter(value = "use_gelf", required = true)
private boolean useGELF = false;

@Parameter(value = "gelf_listen_address")
private String gelfListenAddress = "0.0.0.0";

@Parameter(value = "gelf_listen_port", required = true, validator = InetPortValidator.class)
private int gelfListenPort = 12201;

Expand Down Expand Up @@ -206,6 +209,10 @@ public boolean isUseGELF() {
return useGELF;
}

public String getGelfListenAddress() {
return gelfListenAddress;
}

public int getGelfListenPort() {
return gelfListenPort;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/graylog2/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@
import org.graylog2.messagehandlers.gelf.ChunkedGELFClientManager;
import org.graylog2.messagehandlers.gelf.GELFMainThread;
import org.graylog2.messagehandlers.syslog.SyslogServerThread;
import org.graylog2.messagequeue.MessageQueue;
import org.graylog2.messagequeue.MessageQueueFlusher;
import org.graylog2.periodical.BulkIndexerThread;
import org.graylog2.periodical.ChunkedGELFClientManagerThread;
import org.graylog2.periodical.HostCounterCacheWriterThread;
import org.graylog2.periodical.MessageCountWriterThread;
import org.graylog2.periodical.MessageRetentionThread;
import org.graylog2.periodical.ServerValueWriterThread;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.graylog2.messagequeue.MessageQueue;
import org.graylog2.messagequeue.MessageQueueFlusher;
import org.graylog2.periodical.BulkIndexerThread;
import org.graylog2.periodical.MessageRetentionThread;
import org.graylog2.periodical.ServerValueWriterThread;

/**
* Main class of Graylog2.
Expand Down Expand Up @@ -167,7 +168,7 @@ public static void main(String[] args) {

// Start GELF threads
if (configuration.isUseGELF()) {
initializeGELFThreads(configuration.getGelfListenPort(), scheduler);
initializeGELFThreads(configuration.getGelfListenAddress(), configuration.getGelfListenPort(), scheduler);
}

// Initialize AMQP Broker if enabled
Expand Down Expand Up @@ -230,8 +231,8 @@ private static void initializeMessageRetentionThread(ScheduledExecutorService sc
LOG.info("Retention time management active.");
}

private static void initializeGELFThreads(int gelfPort, ScheduledExecutorService scheduler) {
GELFMainThread gelfThread = new GELFMainThread(gelfPort);
private static void initializeGELFThreads(String gelfAddress, int gelfPort, ScheduledExecutorService scheduler) {
GELFMainThread gelfThread = new GELFMainThread(new InetSocketAddress(gelfAddress, gelfPort));
gelfThread.start();

scheduler.scheduleAtFixedRate(new ChunkedGELFClientManagerThread(ChunkedGELFClientManager.getInstance()), ChunkedGELFClientManagerThread.INITIAL_DELAY, ChunkedGELFClientManagerThread.PERIOD, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.log4j.Logger;

import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -37,26 +38,26 @@ public class GELFMainThread extends Thread {

private static final Logger LOG = Logger.getLogger(GELFMainThread.class);

private int port = 0;
private InetSocketAddress socketAddress;

private ExecutorService threadPool = Executors.newCachedThreadPool();

/**
* Thread responsible for listening for GELF messages.
*
* @param port The TCP port to listen on
* @param socketAddress The {@link InetSocketAddress} to bind to
*/
public GELFMainThread(int port) {
this.port = port;
public GELFMainThread(InetSocketAddress socketAddress) {
this.socketAddress = socketAddress;
}

/**
* Run the thread. Runs forever!
*/
@Override public void run() {
GELFServer server = new GELFServer();
if (!server.create(this.port)) {
throw new RuntimeException("Could not start GELF server. Do you have permissions to listen on UDP port " + this.port + "?");
if (!server.create(socketAddress)) {
throw new RuntimeException("Could not start GELF server. Do you have permissions to bind to " + socketAddress + "?");
}

// Run forever.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
* GELFThread.java: Jun 23, 2010 6:58:07 PM
Expand All @@ -47,17 +48,19 @@ public class GELFServer {
/**
* Create the UDP socket.
*
* @param port The port to listen on.
* @param socketAddress The {@link InetSocketAddress} to bind to
* @return boolean
*/
public boolean create(int port) {
public boolean create(InetSocketAddress socketAddress) {
try {
this.serverSocket = new DatagramSocket(port);
this.serverSocket = new DatagramSocket(socketAddress);
} catch(IOException e) {
LOG.fatal("Could not create ServerSocket in GELFServer::create(): " + e.getMessage(), e);
return false;
}

LOG.info("Started GELF server on " + socketAddress);

return true;
}

Expand Down

0 comments on commit c2597ca

Please sign in to comment.