JVM监控:python脚本JMX获取JVM状态

5 篇文章 1 订阅
3 篇文章 0 订阅

python脚本JMX获取JVM状态

 

主要原理:

        主要通过python库jmxquery,jmxquery是通过python脚本调用它里面的JAR包的JAVA方法连接JMX端口,以获取JVM的状态。

       jmxquery github:

             https://github.com/dgildeh/JMXQuery/tree/master/python

 

安装jmxquery:

$ pip install jmxquery
Collecting jmxquery
  Downloading https://files.pythonhosted.org/packages/93/02/b079e4df07f4c23bc5759742c488673008327dcd14713b519a96c7cc3e94/jmxquery-0.5.0.tar.gz
Installing collected packages: jmxquery
  Running setup.py install for jmxquery: started
    Running setup.py install for jmxquery: finished with status 'done'
Successfully installed jmxquery-0.5.0
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

 

 PYTHON脚本:

       只要指定JMX地址即可。

from jmxquery import JMXConnection, JMXQuery

jmxConnection = JMXConnection("service:jmx:rmi:///jndi/rmi://127.0.0.1:8889/jmxrmi")

## memory
type_str = "Memory"
beanName_str = "java.lang:type={0}".format(type_str)
for atrributeName in ["HeapMemoryUsage","NonHeapMemoryUsage"]:
		jmxQuery = [
		    JMXQuery(mBeanName=beanName_str,
		            attribute=atrributeName,     
		            metric_name="Type:{type}_{attribute}_{attributeKey}")]
		metrics = jmxConnection.query(jmxQuery)
		for metric in metrics:
		    print(f"{metric.to_query_string()} ({metric.value_type}) = {metric.value}") 
    

## memoryPool  
type_str = "MemoryPool"
for name in ["Code Cache","Metaspace","Compressed Class Space","Par Eden Space","Par Survivor Space","PS Eden Space","PS Old Gen","PS Perm Gen","PS Survivor Space","CMS Old Gen","CMS Perm Gen","Perm Gen"]: 
		beanName_str = "java.lang:type={0},name={1}".format(type_str, name)
		jmxQuery = [
		    JMXQuery(mBeanName=beanName_str,
		            attribute="Usage",  
		            metric_name="Type:{type}_{attribute}_{attributeKey}")]
		metrics = jmxConnection.query(jmxQuery)
		for metric in metrics:
		    print(f"{metric.to_query_string()} ({metric.value_type}) = {metric.value}")


## GarbageCollector 其中时间单位是s
type_str = "GarbageCollector"
for name in ["Copy","MarkSweepCompact","PS Scavenge","ConcurrentMarkSweep","ParNew","PS MarkSweep"]:
		beanName_str = "java.lang:type={0},name={1}".format(type_str, name)
		for atrributeName in ["CollectionTime","CollectionCount"]:
				jmxQuery = [
				    JMXQuery(mBeanName=beanName_str,
				            attribute=atrributeName,  
				            metric_name="Type:{type}_{attribute}")]
				metrics = jmxConnection.query(jmxQuery)
				for metric in metrics:
				    print(f"{metric.to_query_string()} ({metric.value_type}) = {metric.value}")

      
## Threading
jmxQuery = [
    JMXQuery(mBeanName="java.lang:type=Threading",
            attribute="ThreadCount",  
            metric_name="Type:{type}_{attribute}")]
metrics = jmxConnection.query(jmxQuery)
for metric in metrics:
    print(f"{metric.to_query_string()} ({metric.value_type}) = {metric.value}")

 

运行输出:

Administrator@USER-TJY MINGW64 /f/work-project/jvm-monitor/doc/weblogic (master)
$ python jmx.py
Type:Memory_HeapMemoryUsage_committed==java.lang:type=Memory/HeapMemoryUsage/committed (Long) = 1820327936
Type:Memory_HeapMemoryUsage_init==java.lang:type=Memory/HeapMemoryUsage/init (Long) = 266338304
Type:Memory_HeapMemoryUsage_max==java.lang:type=Memory/HeapMemoryUsage/max (Long) = 3787980800
Type:Memory_HeapMemoryUsage_used==java.lang:type=Memory/HeapMemoryUsage/used (Long) = 877956624
Type:Memory_NonHeapMemoryUsage_committed==java.lang:type=Memory/NonHeapMemoryUsage/committed (Long) = 155746304
Type:Memory_NonHeapMemoryUsage_init==java.lang:type=Memory/NonHeapMemoryUsage/init (Long) = 2555904
Type:Memory_NonHeapMemoryUsage_max==java.lang:type=Memory/NonHeapMemoryUsage/max (Long) = -1
Type:Memory_NonHeapMemoryUsage_used==java.lang:type=Memory/NonHeapMemoryUsage/used (Long) = 151725664
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=Code Cache/Usage/committed (Long) = 45088768
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=Code Cache/Usage/init (Long) = 2555904
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=Code Cache/Usage/max (Long) = 251658240
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=Code Cache/Usage/used (Long) = 44772480
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=Metaspace/Usage/committed (Long) = 98484224
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=Metaspace/Usage/init (Long) = 0
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=Metaspace/Usage/max (Long) = -1
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=Metaspace/Usage/used (Long) = 95575976
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=Compressed Class Space/Usage/committed (Long) = 12238848
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=Compressed Class Space/Usage/init (Long) = 0
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=Compressed Class Space/Usage/max (Long) = 1073741824
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=Compressed Class Space/Usage/used (Long) = 11446056
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=PS Eden Space/Usage/committed (Long) = 1417150464
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=PS Eden Space/Usage/init (Long) = 66584576
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=PS Eden Space/Usage/max (Long) = 1417150464
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=PS Eden Space/Usage/used (Long) = 580131288
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=PS Old Gen/Usage/committed (Long) = 401604608
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=PS Old Gen/Usage/init (Long) = 177733632
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=PS Old Gen/Usage/max (Long) = 2841116672
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=PS Old Gen/Usage/used (Long) = 308996216
Type:MemoryPool_Usage_committed==java.lang:type=MemoryPool,name=PS Survivor Space/Usage/committed (Long) = 1572864
Type:MemoryPool_Usage_init==java.lang:type=MemoryPool,name=PS Survivor Space/Usage/init (Long) = 11010048
Type:MemoryPool_Usage_max==java.lang:type=MemoryPool,name=PS Survivor Space/Usage/max (Long) = 1572864
Type:MemoryPool_Usage_used==java.lang:type=MemoryPool,name=PS Survivor Space/Usage/used (Long) = 1277952
Type:GarbageCollector_CollectionTime==java.lang:type=GarbageCollector,name=PS Scavenge/CollectionTime (Long) = 46496
Type:GarbageCollector_CollectionCount==java.lang:type=GarbageCollector,name=PS Scavenge/CollectionCount (Long) = 6871
Type:GarbageCollector_CollectionTime==java.lang:type=GarbageCollector,name=PS MarkSweep/CollectionTime (Long) = 8398
Type:GarbageCollector_CollectionCount==java.lang:type=GarbageCollector,name=PS MarkSweep/CollectionCount (Long) = 5
Type:Threading_ThreadCount==java.lang:type=Threading/ThreadCount (Integer) = 114

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值