Authored by BuddyJack

Fix

@@ -22,10 +22,10 @@ public class JMXMonitor { @@ -22,10 +22,10 @@ public class JMXMonitor {
22 22
23 public static final Map<UUID, Connection> conMap = new LinkedHashMap<UUID, Connection>(); 23 public static final Map<UUID, Connection> conMap = new LinkedHashMap<UUID, Connection>();
24 24
25 - public static final String CONFIG_PATH = "/etc/metric_config.json"; 25 + //public static final String CONFIG_PATH = "/etc/metric_config.json";
26 26
27 - //public static final String CONFIG_PATH = "/Users/jack/Documents/metric/metric_config.json";  
28 - public String host = "127.0.0.1"; 27 + public static final String CONFIG_PATH = "/Users/jack/Documents/metric_config.json";
  28 + public String host = "localhost";
29 29
30 public List<Integer> ports = new ArrayList<Integer>(); 30 public List<Integer> ports = new ArrayList<Integer>();
31 31
@@ -66,6 +66,9 @@ public class JMXMonitor { @@ -66,6 +66,9 @@ public class JMXMonitor {
66 if (!javaObject.containsKey("host") || !javaObject.containsKey("ports")) 66 if (!javaObject.containsKey("host") || !javaObject.containsKey("ports"))
67 return false; 67 return false;
68 this.host = javaObject.getString("host"); 68 this.host = javaObject.getString("host");
  69 + if (StringUtils.equals("localhost", host)) {
  70 + this.host = getHostIp();
  71 + }
69 JSONArray array = javaObject.getJSONArray("ports"); 72 JSONArray array = javaObject.getJSONArray("ports");
70 for (Object obj : array) { 73 for (Object obj : array) {
71 if (obj instanceof Integer) 74 if (obj instanceof Integer)
@@ -109,6 +112,23 @@ public class JMXMonitor { @@ -109,6 +112,23 @@ public class JMXMonitor {
109 return 0; 112 return 0;
110 } 113 }
111 114
  115 + public static final String getHostIp() {
  116 + try {
  117 + for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
  118 + NetworkInterface networkInterface = interfaces.nextElement();
  119 + if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
  120 + continue;
  121 + }
  122 + Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
  123 + if (addresses.hasMoreElements()) {
  124 + return addresses.nextElement().getHostAddress();
  125 + }
  126 + }
  127 + } catch (Exception e) {
  128 + e.printStackTrace();
  129 + }
  130 + return null;
  131 + }
112 132
113 public static void main(String[] args) { 133 public static void main(String[] args) {
114 JMXMonitor jmxMonitor = new JMXMonitor(); 134 JMXMonitor jmxMonitor = new JMXMonitor();