Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/bin/
/jhealthagent.jar
/dist/
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</target>

<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" debug="on" source="1.7" target="1.7" />
<!-- javac srcdir="src" destdir="bin" classpathref="compile.path" debug="on" target="1.6" / -->
<copy todir="bin">
Expand Down
4 changes: 3 additions & 1 deletion sample/jhealth.properties
Original file line number Diff line number Diff line change
@@ -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
jhealth.delay=1000
# &-Separated IP-List of allowed Hosts
jhealth.ipList=127.0.0.1&10.11.12.13
50 changes: 36 additions & 14 deletions src/it/frech/jhealth/CollectorAcceptingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Long> lastValueMap = new java.util.Hashtable<String, Long>();

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
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/it/frech/jhealth/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
15 changes: 14 additions & 1 deletion src/it/frech/jhealth/JmxAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(",");
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -112,7 +125,7 @@ public void run() {
}
});

final CollectorAcceptingThread thread = new CollectorAcceptingThread();
final CollectorAcceptingThread thread = new CollectorAcceptingThread(permittedIPs);
thread.socket = serverSocket;
thread.start();

Expand Down