使用Jmxtrans的理由:
JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。利用JMX可以对外暴露jvm内部的一些指标,但是要获取那些jvm的内部信息,就还需要自己写java程序调用jmx接口去获取数据,并按照某种格式发送到其他地方(如监控程序Graphite,Zabbix,时序数据库Influxdb等)。这时jmxtrans就派上用场了,jmxtrans的作用是自动去jvm中获取所需要的jmx数据,并按照某种格式(json文件配置格式)输出到其他应用程序。
如果我们要监控Kafka、Kettle等一些开源的应用程序,市面上很难找到一款现成的监控平台,通过Jmxtrans + Influxdb + Grafana我们就可以轻松的打造属于自己的监控平台(包括Java的JMX监控)。
Jmxtrans的使用:
1、安装
Jmxtrans 安装特别简单(安装前需要先安装好JDK并配置JAVA_HOME环境变量),从官网下载安装包,根据平台不同,可以选择对应的版本,例如:rpm、Debian、tar.gz,下载地址:http://central.maven.org/maven2/org/jmxtrans/jmxtrans/
2、指定监控指标获取
Jmxtrans启动需要有监控配置文件,默认目录是/var/lib/jmxtrans/,格式是json格式,可以任意创建,在启动时指定调用。
我们以influxdb推送为例,json中配置的第一层是要监控的服务端:
{
"servers" : [ {
"port" : "2099",
"host" : "172.16.1.30",
"queries" : [........这是监控指标项........
]
}
而queries就是我们要写的具体监控指标,按json格式列出多个指标项,要监控的指标可以通过jconsole.exe工具(JDK自带的工具)获取,我们以Kafka为例,在Kafka的启动文件配置KAFKA_HEAP_OPTS,开启JMX端口,及jmx remote远程连接授权。
if [ "x$KAFKA_HEAP_OPTS" = "x" ]; then
export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"
export JMX_PORT="2099"
export KAFKA_HEAP_OPTS="-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=172.16.1.30 "$KAFKA_HEAP_OPTS
fi
配置完后启动kafka,如:nohup bin/kafka-server-start.sh config/server.properties >>kafka.log & ,启动后就可以在本地用jconsole.exe远程获取要监控的MBean:
我们可以指向需要监控的指标项,以kafka为例,监控消息消费端FetchConsumer的RemoteTime:
然后再指向我们想监控的具体指标(属性及值):
这些值都是在实时变化的,而我们通过jmxtrans就是要把这些值采集下来,发送给监控平台或是监控数据库。针对这项指标的监控按JSON格式,编辑如下:
{
"obj" : "kafka.network:type=RequestMetrics,name=RemoteTimeMs,request=FetchConsumer",
"attr" : [ "Count","Min","Mean","Max","95thPercentile","StdDev" ],
"resultAlias":"RemoteTimeMs",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "FetchConsumer"}
} ]
}
可以看出obj对应的就是上图的ObjectName,attr对应的是要监控收集的属性,resultAlias是监控指标名(相当于对应的是influxdb的measurement表),监控数据发送到influxdb端的效果如下(RemoteTimeMs):
对于JSON格式文中的outputWriters对应的是要将监控指标输入到对应平台的信息,@class是对应平台的Writer类,influxdb对应“com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory”,而Graphite对应的是 “com.googlecode.jmxtrans.model.output.GraphiteWriterFactory”。
另外强调一下就是tags属性,对应的是influxdb measurement的tagKeys、tagValues,比如除了要监控FetchConsumer消息消费的RemoteTimeMs,还要监控消息生产Produce的RemoteTimeMs,这两个都可以放在一个measurement(表)中,就是用tag进行区分。如:
{
"obj" : "kafka.network:type=RequestMetrics,name=RemoteTimeMs,request=Produce",
"attr" : [ "Count","Min","Mean","Max","95thPercentile","StdDev" ],
"resultAlias":"RemoteTimeMs",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "Produce"}
} ]
}
对应的influxdb中如下展示:
因此我们可得出 database、resultAlias、tags、attr分别组成了influxdb的database、measurement、points、tagKeys、tagValues的数据元素,我们可以根据自己的需要去灵活配置。
3、其他常用指标获取
以上是我们针对Kafka的指标举例,而JVM的通用指我们也是必须获取的,就是系统和线程的指标:
以下我把这部分常用指标写成了Json:
{
"servers" : [ {
"port" : "2099",
"host" : "172.16.1.30",
"queries" : [ {
"obj" : "java.lang:type=OperatingSystem",
"attr" : [ "CommittedVirtualMemorySize", "TotalPhysicalMemorySize", "FreePhysicalMemorySize", "TotalSwapSpaceSize", "FreeSwapSpaceSize", "ProcessCpuLoad", "ProcessCpuTime", "SystemCpuLoad", "AvailableProcessors", "Name", "SystemLoadAverage" ],
"resultAlias":"OperatingSystem",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "OperatingSystem"}
} ]
},
{
"obj" : "java.lang:type=Runtime",
"attr" : [ "Uptime", "Name", "StartTime" ],
"resultAlias":"Runtime",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "Runtime"}
} ]
},
{
"obj" : "java.lang:type=Memory",
"attr" : [ "HeapMemoryUsage", "NonHeapMemoryUsage" ],
"resultAlias":"MemoryUsage",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "MemoryUsage"}
} ]
},
{
"obj" : "java.lang:type=GarbageCollector,name=*",
"attr" : [ "CollectionCount","CollectionTime" ],
"resultAlias":"GC",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "GC"}
} ]
},
{
"obj" : "java.lang:type=Threading",
"attr" : [ "PeakThreadCount","ThreadCount" ],
"resultAlias":"Thread",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://172.17.2.130:8086/",
"username" : "admin",
"password" : "admin",
"database" : "kafka",
"tags" : {"application" : "Thread"}
} ]
}
]
} ]
}
4、最后启用监控
我们在启动Jmxtrans时需要指定我们配好的json:
cd /usr/share/jmxtrans/bin
./jmxtrans start kafka.json
通过日志查看监控进程是否正常:tail -f -n100 /var/log/jmxtrans/jmxtrans.log
然后我们在influxdb端就能看到监控产生的数据,配上Grafana,就形成了高大上的监控平台了:
版权声明:本文为博主原创文章,转摘请注明出处: https://blog.csdn.net/smooth00/article/details/90399528