See www.zabbix.com for the official Zabbix site.
This article is intended to explain how to properly configure Zabbix with JMX monitoring.
How RMI works?
First of all you need to know how RMI (Remote Method Invocation) works. Please, read this post with attention: http://www.netcluesoft.com/rmi-through-a-firewall.html.
How Zabbix monitors JMX?
- Zabbix connects to Java gateway on TCP port 10052 (default port), which can be configured in /etc/zabbix/zabbix_java_gateway.conf (if you have installed it through apt-get). The parameter is LISTEN_PORT
- Java gateway will try to access JMX on remote host using TCP port 12345 (default), which needs to be set on remote host using this Java command line argument "-Dcom.sun.management.jmxremote.port". Normally it is configured in JAVA_OPTS environment variable.
- The remote host will answer to zabbix-java-gateway suggesting to it to use another random port to access RMI information. This occurs because RMI was implemented to use RPC (Remote Procedure Call). This is the problematic step when you use a firewall between zabbix-java-gateway and the remote host running Java, because you don't know which TCP port will be negotiated.
How to solve this problem?
- On the remote machine, add these two parameters in JAVA_OPTS along with the other JMX options: -Djava.rmi.server.hostname=$REMOTE_HOST_IP_ADDR -Dcom.sun.management.jmxremote.rmi.port=12346.
- The first one will help to solve the below problem, as explained in details here. $REMOTE_HOST_IP_ADDR is the IP address that was configured in "JMX interfaces" of host definition in Zabbix web interface.
com.zabbix.gateway.ZabbixException: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused
- The second parameter will tell Java to use only that defined port to make RMI connections. So, when Java on remote host answers to a zabbix-java-getway JMX request, it will not try to negotiate a random TCP port. It will tell zabbix-java-gateway to connect to 12346 instead.
Note: The second parameter is supposed to be supported since Java 7u4, although there are reports of error messages when both ports are set to the same value. See http://hirt.se/blog/?p=289 for more detail.
- Go into your firewall and open these two ports: 12345 (for JMX) and 12346 (for RMI)
- Restart you Java application on remote host
- Download this script and make a test with a JMX item in Zabbix server's shell
zabbix_get_jmx 127.0.0.1 10052 $REMOTE_HOST_IP_ADDR 12345 'jmx[\"java.lang:type=Memory\",HeapMemoryUsage.max]'
- You must see an answer like this: