转载

zabbix监控tomcat

编辑推荐:

本文主要介绍了监控环境搭建,监控数据采集以及解决思路等相关问题,希望对大家有所帮助。

本文来自于csdn,由火龙果软件Alice编辑、推荐。

1.前言:

我们的监控系统是zabbix,最近刚做完jvm还有tomcat的监控,需要吐槽以下内容:

zabbix官方提供的jvm和tomcat监控模版,存在item书写格式的坑,导致很多内容无法获取到监控。

网友写的zabbix监控java文章,99%就只有环境的搭建,没有zabbix item键值,所以环境监控上了,item键值不知道怎么写。

本文监控jvm内容如下:

内存使用状态:堆内存(Heap memory)和非堆内存(No Heap memory),包括已用值、最大值、已提交;

堆内存内存池:新生代(eden space),survivor space,老年代(old gen)的内存使用状态;

非堆内存内存池:代码缓存(Code cache),元空间(meta space),压缩类空间(compressed class space);

类加载:加载总数,已加载,已卸载。

Java线程:总开启线程,活动线程,线程峰值。

本文监控tomcat内容如下:

Tomcat请求数:包括每秒请求数,每秒出错数;

Tomcat网络流量统计:包括进流量统计,出流量统计;

Tomcat线程:包括最大线程数,当前线程数,当前繁忙线程数。

我没有在zabbix中添加垃圾回收(gc)的监控,而是在catalina配置里面添加gc日输入来给开发进行分析,设置方式如下。

CATALINA_OPTS=”-XX:ParallelGCThreads=4 -XX:+PrintGCDetails -Xloggc:日志存放路径”

监控效果如下:

zabbix监控tomcat

zabbix监控tomcat

zabbix监控tomcat

2.监控环境搭建

环境介绍:

[root@tomcat-01 ~]# /usr/local/tomcat/bin/version.sh
 Server version: Apache Tomcat/8.0.23
 Server built: May 19 2015 14:58:38 UTC
 Server number: 8.0.23.0
 OS Name: Linux
 OS Version: 2.6.32-573.22.1.el6.x86_64
 Architecture: amd64
 JVM Version: 1.8.0_65-b17
 JVM Vendor: Oracle Corporation

2.1.Server端配置

1、 Zabbix server端安装java jdk环境并开启javaGateway支持java监控。

javaGateway 启用方式如下:

Zabbix通过rpm包安装:只需要安装zabbix-java-gateway包即可。

Zabbix通过编译安装:在编译时需要加上—enable-java以支持jmx监控,如果之前没加需要重新编译。

2、修改zabbix-java-gateway配置文件

[root@zabbix ~]# vim /etc/zabbix/zabbix_java_gateway.conf
 LISTEN_IP="192.168.10.3"
 LISTEN_PORT=10052
 PID_FILE="/var/run/zabbix/zabbix_java.pid"
 START_POLLERS=5

修改zabbix-server配置文件

[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf
 JavaGateway=192.168.10.3
 JavaGatewayPort=10052
 StartJavaPollers=5

2.2.Tomcat服务器配置

1、下载catalina-jmx-remote.jar包,到tomcat安装目录下的lib目录

wget -O /usr/local/tomcat/lib/catalina-jmx-remote.jar http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.23/bin/extras/catalina-jmx-remote.jar

2、修改catalina.sh添加如下内容

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote
 -Dcom.sun.management.jmxremote.port =8090
 -Dcom.sun.management.jmxremote.ssl =false
 -Dcom.sun.management.jmxremote.authenticate =false"

3、重启tomcat

4、监控测试

(1)下载cmdline-jmxclient-0.10.3.jar文件,下载地址http://crawler.archive.org/cmdline-jmxclient/downloads.html

(2)本地执行如下命令查看tomcat的堆内存信息

[root@tomcat-01 ~]# java -jar /root/cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type =Memory HeapMemoryUsage
 11/04/2016 15:23:16 +0800 org.archive.jmx.Client HeapMemoryUsage:
 committed: 2146959360
 init: 2147483648
 max: 2146959360
 used: 407611808

3.监控数据采集

3.1.堆内存

Tomcat本地查看堆内存信息:

[root@tomcat-01 ~]# java -jar cmdline -jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type =Memory HeapMemoryUsage
 11/04/2016 15:36:58 +0800 org.archive.jmx.Client HeapMemoryUsage:
 committed: 2145910784
 init: 2147483648
 max: 2145910784
 used: 741540536

zabbix监控堆内存键值:

堆内存最大值: jmx["java.lang:type=Memory", "HeapMemoryUsage.max"]

已用堆内存: jmx["java.lang:type=Memory"," HeapMemoryUsage.used"]

已提交堆内存: jmx["java.lang:type=Memory", "HeapMemoryUsage.committed"]

一个完整的zabbix item填写方式如下,不同内容填写不同的键值即可:

zabbix监控tomcat

3.2.内存池eden space:

Tomcat本地查看eden space:

java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type =MemoryPool,name=PS/ Eden/ Space Usage

zabbix监控eden区域键值:

最大空间: jmx["java.lang:type=MemoryPool,name=PS Eden Space",Usage.max]

已用空间: jmx["java.lang:type=MemoryPool,name=PS Eden Space",Usage.used]

提交空间: jmx["java.lang:type=MemoryPool,name=PS Eden Space",Usage.committed]

3.3.内存池survivor space:

Tomcat本地查看Survivor space区域:

java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type=MemoryPool,name=PS/ Survivor/ Space Usage

zabbix监控Survivor 键值:

jmx["java.lang:type=MemoryPool, name=PS Survivor Space",Usage.committed]

jmx["java.lang:type=MemoryPool, name=PS Survivor Space",Usage.max]

jmx["java.lang:type=MemoryPool, name=PS Survivor Space",Usage.used]

3.4.内存池old gen:

Tomcat本地查看old gen区域使用:

java -jar cmdline-jmxclient -0.10.3.jar - 127.0.0.1:8090 java.lang:name =PS/ Old/ Gen,type=MemoryPool Usage

zabbix监控old gen键值:

jmx["java.lang:type=MemoryPool, name=PS Old Gen",Usage.committed] 
 jmx["java.lang:type=MemoryPool, name=PS Old Gen",Usage.max]
 jmx["java.lang:type=MemoryPool, name=PS Old Gen",Usage.used]

3.5.非堆内存:

Tomcat 本地查看非堆内存使用:

java -jar cmdline-jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type=Memory NonHeapMemoryUsag1
 zabbix监控非堆内存使用 
 1jmx["java.lang:type=Memory","NonHeapMemoryUsag.committed"]
 jmx["java.lang:type=Memory","NonHeapMemoryUsag.used"]

3.6.内存池meta space:

Tomcat 本地查看meta space区域使用:

java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type=MemoryPool,name=Metaspace Usage

zabbix监控mete space区域键值

jmx["java.lang:type=MemoryPool, name=Metaspace",Usage.committed]

jmx["java.lang:type=MemoryPool, name=Metaspace",Usage.used]

3.7.内存池code cache:

Tomcat 本地查看code cache区域使用:

java -jar cmdline- jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type=MemoryPool,name=Code/ Cache Usage

zabbix监控code cache区域使用:

jmx["java.lang:type=MemoryPool, name=Code Cache",Usage.committed]

jmx["java.lang:type=MemoryPool, name=Code Cache",Usage.max]

jmx["java.lang:type=MemoryPool, name=Code Cache",Usage.used]

3.8.内存池compressed class space:

Tomcat 本地查看compressed class space区域使用:

java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:type=MemoryPool,name=Compressed/ Class/ Space Usage

zabbix监控 compressed class space区域使用键值:

jmx["java.lang:type=MemoryPool, name=Compressed Class Space",Usage.committed]

jmx["java.lang:type=MemoryPool, name=Compressed Class Space",Usage.max]

jmx["java.lang:type=MemoryPool, name=Compressed Class Space",Usage.used]

3.9.类加载:

Tomcat本地查看类加载信息:

加载总数: java -jar cmdline-jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= ClassLoading TotalLoadedClassCoun
 已加载: java -jar cmdline-jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= ClassLoading LoadedClassCount
 已卸载: java -jar cmdline-jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= ClassLoading UnloadedClassCount

Zabbix监控类加载键值:

加载总数: jmx["java.lang:type= ClassLoading", "TotalLoadedClassCount"]

已加载: jmx["java.lang:type= ClassLoading", "LoadedClassCount"]

已卸载: jmx["java.lang:type= ClassLoading", "UnloadedClassCount"]

3.10.java线程:

tomcat本地查看java线程:

总开启线程: java -jar cmdline-jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= Threading TotalStartedThreadCount
 活动线程: java -jar cmdline- jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= Threading PeakThreadCount
 线程峰值: java -jar cmdline- jmxclient-0.10.3.jar controlRole:tomcat 127.0.0.1:8090 java.lang:type= Threading PeakThreadCount

Zabbix监控java线程键值:

总开启线程: jmx["java.lang:type= Threading"," TotalStartedThreadCount"]

活动线程: jmx["java.lang:type= Threading", "ThreadCount"]

线程峰值: jmx["java.lang:type= Threading", "PeakThreadCount"]

3.11.tomcat线程:

本地查看tomcat线程信息:

最大线程:java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 Catalina:name=/"http-nio-8080/",type= ThreadPool maxThreads
 当前线程:java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 Catalina:name= /"http-nio-8080/",type=ThreadPool currentThreadCount
 繁忙线程:java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 Catalina:name=/"http-nio-8080/",type= ThreadPool currentThreadsBusy

Zabbix监控tomcat线程键值:

最大线程:jmx["Catalina:type =ThreadPool,name =/"http-nio-8080/"",maxThreads]

当前线程:jmx["Catalina:type =ThreadPool,name =/"http-nio-8080/"",currentThreadCount]

繁忙线程 jmx["Catalina:type =ThreadPool,name =/"http-nio-8080/"",currentThreadsBusy]

3.12.网络流量:

Tomcat本地查看接收的字节:

接收的字节:java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 Catalina:name=/"http-nio-8080/",type =GlobalRequestProcessor bytesReceived

发送的字节:java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 Catalina:name = /"http-nio-8080/",type=GlobalRequestProcessor bytesSent

Zabbix监控tomcat接受字节键值:

接收的字节:jmx["Catalina:type =GlobalRequestProcessor,name =/"http-nio-8080/"",bytesReceived]

发送的字节:jmx["Catalina:type =GlobalRequestProcessor,name =/"http-nio-8080/"",bytesSent]

3.13.tomcat请求,出错请求:

tomcat本地查看tomcat请求数:

tomcat请求数: java -jar cmdline-jmxclient-0.10.3.jar - 192.168.10.46:8090 Catalina:name =/"http-nio-8080/",type=GlobalRequestProcessor requestCount

tomcat出错请求: java -jar cmdline-jmxclient-0.10.3.jar - 192.168.10.46:8090 Catalina:name =/"http-nio-8080/", type=GlobalRequestProcessor errorCount

zabbix监控tomcat请求数:

tomcat请求数: jmx["Catalina:type= GlobalRequestProcessor,name= /"http-nio-8080/"",requestCount]

tomcat出错请求: jmx["Catalina:type= GlobalRequestProcessor,name=/"http-nio-8080/"",errorCount]

4.关于网友问题的解答

最近有网友联系我说,看着我的博文搭建完了环境,获取不到数据,我帮助排查了一下,找到了问题所在,特此记录。

网友的报错如下:

# java -jar /root/cmdline- jmxclient-0.10.3.jar - 127.0.0.1:9080 java.lang:type= MemoryPool,name=PS/ Eden/ Space Usag
 11/11/2016 10:03:37 +0800
 org.archive.jmx.Client java.lang:name= PS Eden Space,type= MemoryPool is not a registered bean

4.1.解决思路

如果你使用命令行监控获取不到数据,那么就先使用jconsole看看有没有数据,如果jconsole有数据,那么就往下看,反之检查你的环境。

如果jconsole有数据,但是命令行没有数据,那么就是Mbean的Object Name或者属性有问题,网友都是复制我的,但是自己本地环境和我的环境不一样所以导致获取不到数据,查询自己的本地Mbean方法有两种,分别是图形和命令行,网友的报错说事自己的Eden Space空间有问题,那么我就贴出来查看本地内存池的Object Name和属性的方法。

(1)通过jconsole查看:

zabbix监控tomcat

(2)通过命令行查看:直接使用java -jar cmdline-jmxclient-0.10.3.jar – 127.0.0.1:8090命令可以获取所有的Mbean信息,输出太多这里就不贴输出结果了。我通过grep命令获取所有内存池的监控信息方式如下。

[root@tomcat-01 ~]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 | grep MemoryPool
 java.lang:name= Compressed Class Space,type=MemoryPool
 java.lang:name= Metaspace,type=MemoryPool
 java.lang:name= PS Old Gen,type=MemoryPool
 java.lang:name= PS Eden Space,type=MemoryPool
 java.lang:name= PS Survivor Space,type=MemoryPool
 java.lang:name= Code Cache,type=MemoryPool

然后我要获取Eden Space的所有属性信息方式如下:(一些特殊字符需要转义)

[root@tomcat-01 ~]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:name= PS/ Eden/ Space,type=MemoryPool
 Attributes:
 Usage: Usage (type=javax.management.openmbean.CompositeData)
 PeakUsage: PeakUsage (type=javax.management.openmbean.CompositeData)
 MemoryManagerNames: MemoryManagerNames (type=[Ljava.lang.String;)
 UsageThreshold: UsageThreshold (type=long)
 UsageThresholdExceeded: UsageThresholdExceeded (type=boolean)
 UsageThresholdCount: UsageThresholdCount (type=long)
 UsageThresholdSupported: UsageThresholdSupported (type=boolean)
 CollectionUsageThreshold: CollectionUsageThreshold (type=long)
 CollectionUsageThresholdExceeded: CollectionUsageThresholdExceeded (type=boolean)
 CollectionUsageThresholdCount: CollectionUsageThresholdCount (type=long)
 CollectionUsage: CollectionUsage (type=javax.management.openmbean.CompositeData)
 CollectionUsageThresholdSupported: CollectionUsageThresholdSupported (type=boolean)
 Valid: Valid (type=boolean)
 Name: Name (type=java.lang.String)
 Type: Type (type=java.lang.String)
 ObjectName: ObjectName (type=javax.management.ObjectName)
 Operations:
 resetPeakUsage: resetPeakUsage
 Parameters 0, return type=void

那么我们在查看Eden Space空间的使用信息。就看到已经出来数据了

[root@tomcat-01 ~]# java -jar cmdline-jmxclient-0.10.3.jar - 127.0.0.1:8090 java.lang:name=PS/ Eden/ Space,type=MemoryPool Usage
 12/03/2016 08:33:58 +0800 org.archive.jmx.Client Usage:
 committed: 712507392
 init: 537395200
 max: 712507392
 used: 396006304
原文  http://www.uml.org.cn/itil/202006292.asp?artid=23435
正文到此结束
Loading...