前提條件:已安裝好java環境,tomcat,influxdb和collectd。本文暫不提供以上內容的安裝步驟html
系統環境:centos7java
原理:開啓tomcat的jmx端口,使用collectd的collectd-fast-jmx插件收集jmx信息,collectd把數據存儲到infludb,再由grafana展現infludb的數據vim
cd /usr/local/tomcat/bin/centos
cp catalina.sh catalina.sh.baktomcat
vim catalina.shbash
增長:CATALINA_OPTS="$CATALINA_OPTS -Dfile.encoding=utf-8 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=127.0.0.1"session
重啓tomcatjvm
/etc/init.d/tomcat restartmaven
netstat -nltpcentos7
看到9999端口開啓就表示jmx開啓成功了
從http://www.mvnjar.com/com.e-gineering/collectd-fast-jmx/1.0.0/detail.html下載插件拷貝到機器上並重命名爲collectd-fast-jmx.jar
cd /tmp
wget http://maven.aliyun.com/nexus/content/groups/public/com/e-gineering/collectd-fast-jmx/1.0.0/collectd-fast-jmx-1.0.0.jar
cd /opt/collectd/share/collectd/java/
mv /tmp/collectd-fast-jmx-1.0.0.jar collectd-fast-jmx.jar
collectd插件的安裝比較簡單,下載jar包,在配置文件中添加相關配置就能夠了。collectd有豐富的插件可使用,免去了本身寫監控腳本的繁瑣。
cd /opt/collectd/etc
cp collectd.conf collectd.conf.bak
vim collectd.conf
增長以下內容:
LoadPlugin java <Plugin "java"> LoadPlugin "com.e_gineering.collectd.FastJMX" <Plugin "FastJMX"> MaxThreads 256 CollectInternal true <MBean "classes"> ObjectName "java.lang:type=ClassLoading" <Value "LoadedClassCount"> Type "gauge" InstancePrefix "loaded_classes" PluginName "JVM" </Value> </MBean> # Time spent by the JVM compiling or optimizing. <MBean "compilation"> ObjectName "java.lang:type=Compilation" <Value "TotalCompilationTime"> Type "total_time_in_ms" InstancePrefix "compilation_time" PluginName "JVM" </Value> </MBean> # Garbage collector information <MBean "garbage_collector"> ObjectName "java.lang:type=GarbageCollector,*" InstancePrefix "gc-" InstanceFrom "name" <Value "CollectionTime"> Type "total_time_in_ms" InstancePrefix "collection_time" PluginName "JVM" </Value> </MBean> <MBean "memory_pool"> ObjectName "java.lang:type=MemoryPool,*" InstancePrefix "memory_pool-" InstanceFrom "name" <Value> Type "memory" Table true Attribute "Usage" </Value> </MBean> <MBean "memory-heap"> ObjectName "java.lang:type=Memory" InstancePrefix "memory-heap" <Value> Type "memory" Table true Attribute "HeapMemoryUsage" </Value> </MBean> <MBean "memory-nonheap"> ObjectName "java.lang:type=Memory" InstancePrefix "memory-nonheap" <Value> Type "memory" Table true Attribute "NonHeapMemoryUsage" </Value> </MBean> <MBean "thread"> ObjectName "java.lang:type=Threading" InstancePrefix "threading" <Value> Type "gauge" Table false Attribute "ThreadCount" InstancePrefix "count" </Value> </MBean> <MBean "thread-daemon"> ObjectName "java.lang:type=Threading" InstancePrefix "threading" <Value> Type "gauge" Table false Attribute "DaemonThreadCount" InstancePrefix "count-daemon" </Value> </MBean> ### MBeans by Catalina / Tomcat ### # The global request processor (summary for each request processor) <MBean "catalina/global_request_processor"> ObjectName "Catalina:type=GlobalRequestProcessor,*" InstancePrefix "catalina_request_processor-" InstanceFrom "name" <Value> Type "io_octets" InstancePrefix "global" #InstanceFrom "" Table false Attribute "bytesReceived" Attribute "bytesSent" </Value> <Value> Type "total_requests" InstancePrefix "global" #InstanceFrom "" Table false Attribute "requestCount" </Value> <Value> Type "total_time_in_ms" InstancePrefix "global-processing" #InstanceFrom "" Table false Attribute "processingTime" </Value> </MBean> <MBean "catalina/threadpool"> ObjectName "Catalina:type=ThreadPool,*" InstancePrefix "catalina_threadpool-" InstanceFrom "name" <Value> Type "gauge" Table false Attribute "maxThreads" InstancePrefix "threadpool-maxThreads" </Value> <Value> Type "gauge" Table false Attribute "connectionCount" InstancePrefix "threadpool-connectionCount" </Value> </MBean> <MBean "tomcat_manager"> ObjectName "Catalina:type=Manager,context=*,host=*" InstanceFrom "context" <Value> Type "gauge" InstancePrefix "manager-active_sessions" Table false Attribute "activeSessions" </Value> </MBean> <MBean "jvm_runtime"> ObjectName "java.lang:type=Runtime" <Value> Type "counter" InstancePrefix "runtime-uptime" Table false Attribute "Uptime" </Value> </MBean> <MBean "jvm_system"> ObjectName "java.lang:type=OperatingSystem" <Value> Type "gauge" InstancePrefix "os-open_fd_count" Table false Attribute "OpenFileDescriptorCount" </Value> <Value> Type "counter" InstancePrefix "os-process_cpu_time" Table false Attribute "ProcessCpuTime" </Value> </MBean> <Connection> ServiceURL "service:jmx:rmi:///jndi/rmi://testserver:9999/jmxrmi" Host "testserver" IncludePortInHostname true Collect "classes" Collect "thread" Collect "thread-daemon" Collect "compilation" Collect "garbage_collector" Collect "memory_pool" Collect "memory-heap" Collect "memory-nonheap" Collect "catalina/global_request_processor" Collect "catalina/threadpool" Collect "jvm_system" Collect "jvm_runtime" Collect "tomcat_manager" </Connection> </Plugin> </Plugin>
校驗配置文件 /opt/collectd/sbin/collectd -t編輯完成後保存退出
沒問題的話就重啓collectd
ps -ef |grep collectd
kill -9 6669(這裏是進程號)
/opt/collectd/sbin/collectd
grafana面板下載地址:
https://grafana.com/dashboards/1503
能夠看出面板的編號是1503
接下來配置下grafana就能夠了
(1)在主頁選擇」dashboards"->"improt"進行模板導入
(2)輸入模板編號1503,再點擊load
(3)輸入面板名字,選擇數據源,點擊import
(4)而後就能夠在grafana上看到tomcat的JVM監控了,大功告成!