Permalink
Browse files

Send correct server hostname as cassandra host

  • Loading branch information...
1 parent ceffd78 commit 08162610e795859c04ff9580754006abb4b0a2d1 Jake Luciani committed Oct 3, 2012
Showing with 36 additions and 1 deletion.
  1. +36 −1 src/main/java/com/bluemountain/RiemannCassandraClient.java
@@ -1,8 +1,15 @@
package com.bluemountain;
import java.io.PrintWriter;
+import java.net.Inet4Address;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
@@ -60,14 +67,42 @@ public RiemannCassandraClient(String riemannHost, Integer riemannPort, String ca
this.jmxUsername = jmxUsername;
this.jmxPassword = jmxPassword;
- protoEvent = Event.newBuilder().setHost(cassandraHost).addTags("cassandra").setState("ok").setTtl(5).build();
+ protoEvent = Event.newBuilder().setHost(pickBestHostname(cassandraHost)).addTags("cassandra").setState("ok").setTtl(5).build();
riemannClient = new RiemannClient(new InetSocketAddress(riemannHost, riemannPort));
if (!reconnectJMX())
System.err.println(String.format("Unable to connect to Cassandra JMX (%s:%d) will continue to try silently....", cassandraHost, cassandraJmxPort));
}
+ private String pickBestHostname(String cassandraHost)
+ {
+ try {
+ InetAddress cassandraAddr = InetAddress.getByName(cassandraHost);
+
+ if (!cassandraAddr.isLoopbackAddress())
+ return cassandraAddr.getCanonicalHostName();
+
+ //Pick first non local ip with a hostname
+ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
+ for (NetworkInterface netint : Collections.list(nets))
+ {
+ Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
+ for (InetAddress inetAddress : Collections.list(inetAddresses)) {
+ if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
+ return inetAddress.getCanonicalHostName();
+ }
+ }
+
+ } catch (UnknownHostException e) {
+ throw new RuntimeException("Unknown host", e);
+ } catch (SocketException e) {
+ throw new RuntimeException("Error getting network info", e);
+ }
+
+ return cassandraHost;
+ }
+
private synchronized boolean reconnectJMX() {
try {

0 comments on commit 0816261

Please sign in to comment.