Monitoring Remote Java Applications with VisualVM
Hey guys, the blog updates have slowed since I found Minecraft. Fairly addictive. Just a quick one today.
To monitor a remote Java application with VisualVM, simply use jstatd
Start jstatd on the remote machine, make a connection from VisualVM to the remote machine and we can then monitor all running Java applications.
If you try starting jstatd without specifying a policy you may get the following error.
1
2
3
4
5
6
7
8
9
10
| $ ./jstatd Could not create remote object access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write) java.security.AccessControlException: access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:342) at java.security.AccessController.checkPermission(AccessController.java:553) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at java.lang.System.setProperty(System.java:744) at sun.tools.jstatd.Jstatd.main(Jstatd.java:139) |
Create a security policy file if one does not already exist called ‘jstatd.all.policy’ (same location as jstatd).
Add the following to the file:
1 2 3 4 5 | grant codebase "file:${java.home}/../lib/tools.jar" { permission java.security.AllPermission; }; |
Then simply run jstatd with:
1 | $ ./jstatd -J-Djava.security.policy=jstatd.all.policy & |
You can test the connection using the following command
1 | $ jps -l -m -v rmi://localhost |
If you’re experiencing problems make sure you run jstatd with the same user as the java processes.
It could be that the ports used my jstatd are blocked by your firewall. jstatd uses two ports, the once specified and a random port. Check the ports by running the following command
1 2 3 | $ netstat -nap | grep jstatd tcp 0 0 :::47232 :::* LISTEN 23185/jstatd tcp 0 0 :::1099 :::* LISTEN 23185/jstatd |
EDIT:
You can find the log files for visualvm here
Linux
[userdir]/.visualvm/[version]/var/log/messages.log
Windows
C:\Users\[username]\AppData\Roaming\.visualvm\[version]\var\log\messages.log
Run the visualVM in windows, and create the host name, type the ip or the hostname, then it's ok.
eg:
jstatd -J-Djava.rmi.server.hostname=cnbjlnx016 -J-Dcom.sun.management.jmxremote.port=8888 -J-Dcom.sun.management.jmxremote.ssl=false -J-Dcom.sun.management.jmxremote.authenticate=false -J-Djava.security.policy=jstatd.all.policy &
---------------------
连接到远程主机
要从远程应用程序中检索数据,需要在远程 JVM 上运行 jstatd 实用程序。有关如何启动 jstatd 的更多信息 请参见 jstatd - Virtual Machine jstat Daemon(jstatd - 虚拟机 jstat 守护进程)。无法对远程主机上运行的应用程序进行性能分析。
1需要在被监控的服务器上面,通过jstatd来启动RMI服务。新建个文件jstatd.all.policy 文件内容如下:
grant codebase "file:${java.home}/../lib/tools.jar" {
permission java.security.AllPermission;
};
2启动jstatd
jstatd -J-Djava.security.policy=jstatd.all.policy &
上面启动的RMI服务的端口是1099。启动通过:netstat -an|grep 8888可以查看端口是否已经被监听
3连接远程主机
- 右键单击“应用程序”窗口中的“远程”节点,然后选择“添加远程主机”。
- 在“添加远程主机”对话框中,键入远程计算机的主机名或 IP 地址。
- (可选)键入远程主机的显示名称。此名称将显示在“应用程序”窗口中。如果没有输入显示名称,则在“应用程序”窗口中使用主机名标识远程主机。
- 单击“确定”。