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

2024-04-12 05:48

本文主要是介绍JVM监控:python脚本JMX获取JVM状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 jmxqueryDownloading https://files.pythonhosted.org/packages/93/02/b079e4df07f4c23bc5759742c488673008327dcd14713b519a96c7cc3e94/jmxquery-0.5.0.tar.gz
Installing collected packages: jmxqueryRunning setup.py install for jmxquery: startedRunning 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, JMXQueryjmxConnection = 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

 

这篇关于JVM监控:python脚本JMX获取JVM状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896272

相关文章

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它