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

相关文章

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功