進入tomcat的bin目錄編輯shell
vi catalina.sh
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=XXXXX -Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=jmxremote.password -Dcom.sun.management.jmxremote.access.file=jmxremote.access"
也可以把這段話加入JAVA_OPTS
注意-Djava.rmi.server.hostname=XXXXX 一定要加。網上很多資料沒有寫這段,我想是因爲本地連接不需要。
-Dcom.sun.management.jmxremote.authenticate=true 也可以改成false,這樣可以省去認證環節。
mxremote.password和jmxremote.access 是認證需要的文件,可以從java_home/jre/lib/management 裏考一份到tomcat的bin下面
編輯 mxremote.password設置密碼,例如
monitorRole 123
controlRole 123
然後在本地機器打開java_home/bin/jconsole
選擇遠程連接
輸入 XXXXX:8888
用戶名:controlRole
密碼:123
連進去試試看,如果不成功可以查看下8888端口是否打開
netstat -anp|grep 8888
這條命令也可以查看佔用8888端口的進程號。
接着可以用jconsole打開圖形界面來觀察數據,注意MBean這個tab。
裏面的屬性對應這zabbix的item,很多人說zabbix集成的tomcat模板有問題。
其實就是模板裏設置的item的Key與下圖的objectName不一致。例如下面這個例子,模板中的name我記得原來好像是“http-8080”
zabbix要想正確讀取jmx傳來的數據,還需要配置zabbix_java_gateway。
vi zabbix_java_gateway.conf
# This is a configuration file for Zabbix Java Gateway.
# It is sourced by startup.sh and shutdown.sh scripts.
### Option: zabbix.listenIP
# IP address to listen on.
#
# Mandatory: no
# Default:
LISTEN_IP="127.0.0.1"
### Option: zabbix.listenPort
# Port to listen on.
#
# Mandatory: no
# Range: 1024-32767
# Default:
LISTEN_PORT=10052
### Option: zabbix.pidFile
# Name of PID file.
# If omitted, Zabbix Java Gateway is started as a console application.
#
# Mandatory: no
# Default:
# PID_FILE=
PID_FILE="/var/run/zabbix/zabbix_java.pid"
LogFile=/mnt/erp/log/zabbix/zabbix_java_gateway.log
### Option: zabbix.startPollers
# Number of worker threads to start.
#
# Mandatory: no
# Range: 1-1000
# Default:
START_POLLERS=5
保存後,啓動gateway即可
/etc/init.d/zabbix-java-gateway
成功之後,去zabbix的web頁面配置下
確認到jmx的標記綠了,說明成功了
當然,配置失敗的可能性會很大。
失敗的情況下可以用一下命令調試。
zabbix_get -s XXX.XXX.XXX.XX -p 1234 -k jmx["org.dozer.jmx:type=ThreadPool,name=\"http-bio-8080\"",currentThreadCount]
或者
比如說,["]需要轉義。