使用Jmxtrans的理由:
JMX(Java Management Extensions娘荡,即Java管理擴(kuò)展)是一個(gè)為應(yīng)用程序、設(shè)備驶沼、系統(tǒng)等植入管理功能的框架炮沐。JMX可以跨越一系列異構(gòu)操作系統(tǒng)平臺、系統(tǒng)體系結(jié)構(gòu)和網(wǎng)絡(luò)傳輸協(xié)議回怜,靈活的開發(fā)無縫集成的系統(tǒng)大年、網(wǎng)絡(luò)和服務(wù)管理應(yīng)用。利用JMX可以對外暴露jvm內(nèi)部的一些指標(biāo)鹉戚,但是要獲取那些jvm的內(nèi)部信息鲜戒,就還需要自己寫java程序調(diào)用jmx接口去獲取數(shù)據(jù),并按照某種格式發(fā)送到其他地方(如監(jiān)控程序Graphite抹凳,Zabbix,時(shí)序數(shù)據(jù)庫Influxdb等)伦腐。這時(shí)jmxtrans就派上用場了赢底,jmxtrans的作用是自動(dòng)去jvm中獲取所需要的jmx數(shù)據(jù),并按照某種格式(json文件配置格式)輸出到其他應(yīng)用程序柏蘑。
如果我們要監(jiān)控Kafka幸冻、Kettle等一些開源的應(yīng)用程序,市面上很難找到一款現(xiàn)成的監(jiān)控平臺咳焚,通過Jmxtrans + Influxdb + Grafana我們就可以輕松的打造屬于自己的監(jiān)控平臺(包括Java的JMX監(jiān)控)洽损。
Jmxtrans的使用:
1、安裝
Jmxtrans 安裝特別簡單(安裝前需要先安裝好JDK并配置JAVA_HOME環(huán)境變量)革半,從官網(wǎng)下載安裝包碑定,根據(jù)平臺不同,可以選擇對應(yīng)的版本又官,例如:rpm延刘、Debian、tar.gz六敬,下載地址:http://central.maven.org/maven2/org/jmxtrans/jmxtrans/
2碘赖、指定監(jiān)控指標(biāo)獲取
Jmxtrans啟動(dòng)需要有監(jiān)控配置文件,默認(rèn)目錄是/var/lib/jmxtrans/,格式是json格式普泡,可以任意創(chuàng)建播掷,在啟動(dòng)時(shí)指定調(diào)用。
我們以influxdb推送為例撼班,json中配置的第一層是要監(jiān)控的服務(wù)端:
{
? "servers" : [ {
? "port" : "2099",
? "host" : "172.16.1.30",
? "queries" : [........這是監(jiān)控指標(biāo)項(xiàng)........
? ]
}
而queries就是我們要寫的具體監(jiān)控指標(biāo)叮趴,按json格式列出多個(gè)指標(biāo)項(xiàng),要監(jiān)控的指標(biāo)可以通過jconsole.exe工具(JDK自帶的工具)獲取权烧,我們以Kafka為例眯亦,在Kafka的啟動(dòng)文件配置KAFKA_HEAP_OPTS,開啟JMX端口般码,及jmx remote遠(yuǎn)程連接授權(quán)妻率。
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
配置完后啟動(dòng)kafka,如:nohup bin/kafka-server-start.sh config/server.properties >>kafka.log & 板祝,啟動(dòng)后就可以在本地用jconsole.exe遠(yuǎn)程獲取要監(jiān)控的MBean:
我們可以指向需要監(jiān)控的指標(biāo)項(xiàng)宫静,以kafka為例,監(jiān)控消息消費(fèi)端FetchConsumer的RemoteTime:
?然后再指向我們想監(jiān)控的具體指標(biāo)(屬性及值):
這些值都是在實(shí)時(shí)變化的券时,而我們通過jmxtrans就是要把這些值采集下來孤里,發(fā)送給監(jiān)控平臺或是監(jiān)控?cái)?shù)據(jù)庫。針對這項(xiàng)指標(biāo)的監(jiān)控按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對應(yīng)的就是上圖的ObjectName是辕,attr對應(yīng)的是要監(jiān)控收集的屬性琅轧,resultAlias是監(jiān)控指標(biāo)名(相當(dāng)于對應(yīng)的是influxdb的measurement表),監(jiān)控?cái)?shù)據(jù)發(fā)送到influxdb端的效果如下(RemoteTimeMs):
?對于JSON格式文中的outputWriters對應(yīng)的是要將監(jiān)控指標(biāo)輸入到對應(yīng)平臺的信息虏等,@class是對應(yīng)平臺的Writer類,influxdb對應(yīng)“com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory”适肠,而Graphite對應(yīng)的是 “com.googlecode.jmxtrans.model.output.GraphiteWriterFactory”霍衫。
另外強(qiáng)調(diào)一下就是tags屬性,對應(yīng)的是influxdb?measurement的tagKeys侯养、tagValues敦跌,比如除了要監(jiān)控FetchConsumer消息消費(fèi)的RemoteTimeMs,還要監(jiān)控消息生產(chǎn)Produce的RemoteTimeMs逛揩,這兩個(gè)都可以放在一個(gè)measurement(表)中柠傍,就是用tag進(jìn)行區(qū)分。如:
{
"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"}
} ]
}
對應(yīng)的influxdb中如下展示:
因此我們可得出?database息尺、resultAlias携兵、tags、attr分別組成了influxdb的database搂誉、measurement徐紧、points、tagKeys、tagValues的數(shù)據(jù)元素并级,我們可以根據(jù)自己的需要去靈活配置拂檩。
3、其他常用指標(biāo)獲取
以上是我們針對Kafka的指標(biāo)舉例嘲碧,而JVM的通用指我們也是必須獲取的稻励,就是系統(tǒng)和線程的指標(biāo):
以下我把這部分常用指標(biāo)寫成了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、最后啟用監(jiān)控
我們在啟動(dòng)Jmxtrans時(shí)需要指定我們配好的json:
cd /usr/share/jmxtrans/bin
./jmxtrans start kafka.json
通過日志查看監(jiān)控進(jìn)程是否正常:tail -f -n100 /var/log/jmxtrans/jmxtrans.log?
然后我們在influxdb端就能看到監(jiān)控產(chǎn)生的數(shù)據(jù)愈涩,配上Grafana望抽,就形成了高大上的監(jiān)控平臺了:
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)摘請注明出處: https://blog.csdn.net/smooth00/article/details/90399528