diff --git a/.gitignore b/.gitignore index ae3c172..e7dd985 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /bin/ +/jhealthagent.jar +/dist/ diff --git a/build.xml b/build.xml index 7b8f73a..805aeb5 100644 --- a/build.xml +++ b/build.xml @@ -21,6 +21,7 @@ + diff --git a/sample/jhealth.properties b/sample/jhealth.properties index cd7454e..2079bc8 100644 --- a/sample/jhealth.properties +++ b/sample/jhealth.properties @@ -1,4 +1,6 @@ jhealth.format=$TIME{dd.MM.yyyy HH:mm:ss};$SYSPROP{jboss.node.name};$jhealth:type=YoungGC{count-2};$jhealth:type=TenuredGC{count-2};$java.lang:type=Threading{ThreadCount} jhealth.path=/tmp/health.log jhealth.port=5678 -jhealth.delay=1000 \ No newline at end of file +jhealth.delay=1000 +# &-Separated IP-List of allowed Hosts +jhealth.ipList=127.0.0.1&10.11.12.13 \ No newline at end of file diff --git a/src/it/frech/jhealth/CollectorAcceptingThread.java b/src/it/frech/jhealth/CollectorAcceptingThread.java index 648f671..be7f402 100644 --- a/src/it/frech/jhealth/CollectorAcceptingThread.java +++ b/src/it/frech/jhealth/CollectorAcceptingThread.java @@ -27,15 +27,20 @@ class CollectorAcceptingThread extends Thread { private boolean keepRunning; ServerSocket socket; + + String [] permittedIPs; // use a *synchronized* Map since multiple Threads may access it private Map lastValueMap = new java.util.Hashtable(); private MBeanServer mbeanServer; - public CollectorAcceptingThread() { + public CollectorAcceptingThread(String permittedIPs) { setName("Collector MBean Accept-Thread"); setDaemon(true); + if (permittedIPs != null && permittedIPs.length() > 0){ + this.permittedIPs = permittedIPs.split("&"); + } } @Override @@ -47,22 +52,39 @@ public void run() { } private void acceptRequests() { + try { + Socket reqSocket = socket.accept(); + if (isPermitted(reqSocket)){ + CollectorRequestHandlingThread thread = new CollectorRequestHandlingThread(reqSocket,lastValueMap, getMBeanServer()); + thread.setName("Collector request "+reqSocket.getRemoteSocketAddress()); + thread.setDaemon(true); + thread.start(); + }else{ + reqSocket.close(); + } + } catch (IOException e) { + // the socket could have been closed -> ok + }catch (SecurityException e) { + System.out.println(e.getMessage()); + } + } + + public boolean isPermitted(Socket socket) { + // default is allowed + boolean isPermitted = true; + String ipAddress = socket.getInetAddress().getHostAddress(); + // If a list of IPs is defined, check if the requesting IP is in there - - - try { - Socket reqSocket = socket.accept(); - CollectorRequestHandlingThread thread = new CollectorRequestHandlingThread(reqSocket,lastValueMap, getMBeanServer()); - thread.setName("Collector request "+reqSocket.getRemoteSocketAddress()); - thread.setDaemon(true); - thread.start(); - } catch (IOException e) { - // the socket could have been closed -> ok + if (permittedIPs != null && permittedIPs.length > 0) { + isPermitted = false; + for (String ip :permittedIPs){ + if (ipAddress.equals(ip)){ + isPermitted = true; + } + } } + return isPermitted; } - - - private MBeanServer getMBeanServer() { if (mbeanServer == null) { mbeanServer = ManagementFactory.getPlatformMBeanServer(); diff --git a/src/it/frech/jhealth/Constants.java b/src/it/frech/jhealth/Constants.java index 9e9ad1e..7a6a687 100644 --- a/src/it/frech/jhealth/Constants.java +++ b/src/it/frech/jhealth/Constants.java @@ -6,4 +6,5 @@ public class Constants { public final static String PORT_PROPERTY = "jhealth.port"; public final static String FORMAT_PROPERTY = "jhealth.format"; public final static String DELAY_PROPERTY = "jhealth.delay"; + public final static String IPLIST_PROPERTY = "jhealth.ipList"; } diff --git a/src/it/frech/jhealth/JmxAgent.java b/src/it/frech/jhealth/JmxAgent.java index 241eeb3..6b665d7 100644 --- a/src/it/frech/jhealth/JmxAgent.java +++ b/src/it/frech/jhealth/JmxAgent.java @@ -32,6 +32,7 @@ public static void premain(String agentArgs) { String path = null; int delay = 5000; String format = "$TIME{yyyy-MM-dd HH:mm:ss};minorGcCount=$jhealth:type=YoungGC{count-2};majorGcCount=$jhealth:type=TenuredGC{count-2};threadCount=$java.lang:type=Threading{ThreadCount}"; + String permittedIPs= null; String sysProp = System.getProperty(Constants.PORT_PROPERTY); if (sysProp != null) { @@ -43,6 +44,10 @@ public static void premain(String agentArgs) { path = sysProp; } + sysProp = System.getProperty(Constants.IPLIST_PROPERTY); + if (sysProp != null) { + permittedIPs = sysProp; + } if (agentArgs != null) { String[] args = agentArgs.split(","); @@ -70,6 +75,10 @@ public static void premain(String agentArgs) { if (p != null) { delay = Integer.parseInt(p); } + p = props.getProperty(Constants.IPLIST_PROPERTY); + if (p != null) { + permittedIPs = p; + } } catch (IOException e) { e.printStackTrace(System.err); } @@ -89,6 +98,10 @@ public static void premain(String agentArgs) { delay = Integer.parseInt(arg.substring(6)); continue; } + if (arg.startsWith("ipList=")) { + permittedIPs = arg.substring(7); + continue; + } if (arg.startsWith("config=")) { // skip, already processed continue; @@ -112,7 +125,7 @@ public void run() { } }); - final CollectorAcceptingThread thread = new CollectorAcceptingThread(); + final CollectorAcceptingThread thread = new CollectorAcceptingThread(permittedIPs); thread.socket = serverSocket; thread.start();