13. How do we get data into Graphite?
»telnet to port 2003
»Enter 3 values
metric.name value unixtime
14. Example in bash
#!/bin/bash
# Set this hostname
HOSTNAME=`/bin/hostname --short`
# Set Graphite host
GRAPHITE=graphite101
GRAPHITE_PORT=2003
# Get epoch
DATE=`/bin/date +%s`
# Collect some data
MY_DATA=`/usr/sbin/lsof -u tomcat | /usr/bin/wc -l`
# Send data to Graphite
echo "stats.${HOSTNAME}.file_handle.count ${MY_DATA} ${DATE}" | /usr/bin/nc $GRAPHITE $GRAPHITE_PORT
15. Example in Java
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
public class GraphiteLogger {
private static String GRAPHITE_HOST = "graphite101";
private static int GRAPHITE_PORT = 2003;
public static void logToGraphite(String metric, int value) throws Exception {
Long timeInSeconds = System.currentTimeMillis() / 1000;
Socket socket = new Socket(GRAPHITE_HOST, GRAPHITE_PORT);
try {
Writer writer = new OutputStreamWriter(socket.getOutputStream());
writer.write(metric + " " + value + " " + timeInSeconds + "n");
writer.flush();
writer.close();
} finally {
socket.close();
}
}
public static void main(String[]args) throws Exception {
GraphiteLogger.logToGraphite("stats.test.example.count", 100);
}
}