在工作上其他的同事問我如何去查出 Java 程式中的 Thread ??
經由 google 可以查到 透過 JVM 的 SNMP 機制並結合 cacti 可做到這樣的監控功能.
Java snmp 的設定方式請參考下列 URL
http://docs.oracle.com/javase/6/docs/technotes/guides/management/snmp.html
重要詞彙:
Java Virtual Machine (JVM)
Java Management Extensions (JMX)
Remote Method Invocation (RMI)
以下為本 LAB 的設定方式:
[root@centos management]# pwd
/usr/java/jre/lib/management
[root@centos management]# ls -la
total 40
drwxr-xr-x 2 root root 4096 Dec 28 23:27 .
drwxr-xr-x 18 root root 4096 Dec 28 21:55 ..
-rw-r--r-- 1 root root 3998 Nov 14 18:19 jmxremote.access
-r--r--r-- 1 root root 2856 Nov 14 18:19 jmxremote.password.template
-rw-r--r-- 1 root root 14097 Nov 14 18:19 management.properties
-r-------- 1 root root 3355 Dec 28 23:16 snmp.acl
-r--r--r-- 1 root root 3376 Nov 14 18:19 snmp.acl.template
[root@centos management]# cat snmp.acl
acl = {
{
communities = public
access = read-only
managers = localhost
}
}
下方的 java profile 設定分為三段
(1)啟用 jvm snmp 使用設定
(2)設定 jvm RAM 使用設定
(3)啟用 jmx 使用設定 (透過 jconsole 去 monitor jvm )
[root@centos profile.d]# pwd
/etc/profile.d
[root@centos profile.d]# cat java.sh
JAVA_HOME=/usr/java
ANT_HOME=/usr/local/ant
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:/usr/java/jre/lib/management-agent.jar:/usr/local/ant/lib/ant.jar
PATH=$JAVA_HOME/bin:$PATH:$ANT_HOME/bin
export JAVA_HOME CLASSPATH PATH ANT_HOME
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.snmp.port=1610 -Dcom.sun.management.snmp.acl.file=/usr/java/jre/lib/management/snmp.acl -Dcom.sun.management.snmp.interface=0.0.0.0 -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.100.101 -Dcom.sun.management.jmxremote.port=9012 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
檢查 jvm snmp & jmx 是否正常 Listen
[root@centos snmp_queries]# netstat -anp | grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1477/java
tcp 0 0 :::50023 :::* LISTEN 1477/java
tcp 0 0 :::8009 :::* LISTEN 1477/java
tcp 0 0 :::8080 :::* LISTEN 1477/java
tcp 0 0 :::49043 :::* LISTEN 1477/java
tcp 0 0 :::9012 :::* LISTEN 1477/java
udp 0 0 :::1610 :::* 1477/java
驗證 snmp 的服務是否正常
[root@centos snmp_queries]#snmpwalk -v2c -c public localhost system
SNMPv2-MIB::sysDescr.0 = STRING: Linux centos 2.6.32-279.19.1.el6.i686 #1 SMP Wed Dec 19 04:30:58 UTC 2012 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (117485) 0:19:34.85
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: centos
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (6) 0:00:00.06
SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDMIBObjects.3.1.1
SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.6 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (5) 0:00:00.05
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (6) 0:00:00.06
[root@centos snmp_queries]# snmpget -v2c -c public localhost SNMPv2-MIB::sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux centos 2.6.32-279.19.1.el6.i686 #1 SMP Wed Dec 19 04:30:58 UTC 2012 i686
[root@centos snmp_queries]#
JVM SNMP MIB 參考的 URL
http://ipmsupport.solarwinds.com/mibs/JVM-MANAGEMENT-MIB/oids.aspx
http://support.ipmonitor.com/mibs/JVM-MANAGEMENT-MIB/oids.aspx
JVM Thread Count MIB
1.3.6.1.4.1.42.2.145.3.163.1.1.3.1.0
透過使用 snmp proxy 的方式
[root@centos snmp_queries]# snmpget -v2c -c public localhost 1.3.6.1.4.1.42.2.145.3.163.1.1.3.1.0
SNMPv2-SMI::enterprises.42.2.145.3.163.1.1.3.1.0 = Gauge32: 19
vi /etc/snmp/snmpd.conf
mibs +JVM-MANAGEMENT-MIB
proxy -v 2c -c public localhost:1610 .1.3.6.1.4.1.42
修改必須重啟 snmpd
不透過使用 snmp proxy 的方式
[root@centos snmp_queries]
#snmpget -v2c -c public localhost:1610 1.3.6.1.4.1.42.2.145.3.163.1.1.3.1.0
SNMPv2-SMI::enterprises.42.2.145.3.163.1.1.3.1.0 = Gauge32: 19
需修改 Data Templates snmp port 為 1610
Another JVM Monitoring template
http://forums.cacti.net/post-112268.html
修改 Data Templates snmp port 為 1610
jconsole test demo
jconsole test demo
cacti monitor 與 jconsole 實際運作時的對照如下