Skip to content
Browse files

first working, very basic transmission of counter values to graphite/…

…carbon
  • Loading branch information...
1 parent 99dfc87 commit 5243c13bb41117523718148d6736319204ea8ff6 @lennartkoopmann lennartkoopmann committed May 8, 2012
View
4 misc/graylog2.conf
@@ -62,3 +62,7 @@ enable_tokenizer_filter = true
# Forwarders
# Timeout in seconds for each connection and read of Logg.ly API when forwarding messages. Default: 3
forwarder_loggly_timeout = 3
+
+# Additional modules
+#graphite_carbon_host = 127.0.0.1
+#graphite_carbon_udp_port = 2003
View
14 src/main/java/org/graylog2/Configuration.java
@@ -142,6 +142,12 @@
@Parameter(value = "enable_tokenizer_filter", required = true)
private boolean enableTokenizerFilter = true;
+ @Parameter(value = "graphite_carbon_host", required = false)
+ private String graphiteCarbonHost = "127.0.0.1";
+
+ @Parameter(value = "graphite_carbon_udp_port", validator = InetPortValidator.class, required = false)
+ private int graphiteCarbonUdpPort = 2003;
+
public int getSyslogListenPort() {
return syslogListenPort;
}
@@ -297,6 +303,14 @@ public boolean isEnableTokenizerFilter() {
return enableTokenizerFilter;
}
+ public String getGraphiteCarbonHost() {
+ return graphiteCarbonHost;
+ }
+
+ public int getGraphiteCarbonUdpPort() {
+ return graphiteCarbonUdpPort;
+ }
+
@ValidatorMethod
public void validate() throws ValidationException {
View
47 src/main/java/org/graylog2/periodical/GraphiteWriterThread.java
@@ -20,33 +20,41 @@
package org.graylog2.periodical;
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
import org.apache.log4j.Logger;
import org.graylog2.GraylogServer;
import org.graylog2.MessageCounter;
+import org.graylog2.Tools;
/**
* GraphiteWriterThread.java: 08.05.2012 16:13:29
*
- * Describe me.
- *
* @author Lennart Koopmann <lennart@socketfeed.com>
*/
public class GraphiteWriterThread implements Runnable {
private static final Logger LOG = Logger.getLogger(GraphiteWriterThread.class);
- public static final String GRAPHITE_CARBON_HOST = "127.0.0.1";
- public static final int GRAPHITE_CARBON_PORT = 2003;
-
public static final String COUNTER_NAME = "graphitecounter";
public static final int INITIAL_DELAY = 0;
public static final int PERIOD = 1;
private final GraylogServer graylogServer;
+ String carbonHost;
+ int carbonPort;
+
public GraphiteWriterThread(GraylogServer graylogServer) {
this.graylogServer = graylogServer;
+
+ carbonHost = graylogServer.getConfiguration().getGraphiteCarbonHost();
+ carbonPort = graylogServer.getConfiguration().getGraphiteCarbonUdpPort();
}
@Override
@@ -58,13 +66,38 @@ public void run() {
MessageCounter counter = this.graylogServer.getMessageCounterManager().get(COUNTER_NAME);
try {
-System.out.println("Overall count: " + counter.getTotalCount());
- LOG.debug("Sent message counts to Graphite at <" + GRAPHITE_CARBON_HOST + ":" + GRAPHITE_CARBON_PORT + ">.");
+ String val = "graylog2.messagecounts.total " + counter.getTotalCount() + " " + Tools.getUTCTimestamp();
+ send(val.getBytes());
+ LOG.debug("Sent message counts to Graphite at <" + carbonHost + ":" + carbonPort + ">.");
} catch (Exception e) {
LOG.warn("Error in GraphiteWriterThread: " + e.getMessage(), e);
} finally {
counter.resetAllCounts();
}
}
+ private boolean send(byte[] what) {
+ DatagramSocket sock = null;
+
+ try {
+ sock = new DatagramSocket();
+ InetAddress destination = InetAddress.getByName(carbonHost);
+
+ DatagramPacket pkg = new DatagramPacket(what, what.length, destination, carbonPort);
+ sock.send(pkg);
+ } catch (SocketException e) {
+ LOG.error("Could not send data to Graphite", e);
+ } catch (UnknownHostException e) {
+ LOG.error("Could not send data to Graphite (Unknown host: <" + carbonHost + ">)", e);
+ } catch (IOException e) {
+ LOG.error("Could not send data to Graphite (IO error):", e);
+ } finally {
+ if (sock != null) {
+ sock.close();
+ }
+ }
+
+ return true;
+ }
+
}

0 comments on commit 5243c13

Please sign in to comment.
Something went wrong with that request. Please try again.