使用jmx_agent监控java程序
1.下载jmx_prometheus_javaagent.jar
从https://github.com/prometheus/jmx_exporter/releases下载最新版本的jmx_prometheus_javaagent.jar
2.创建一个名为jmx_exporter_config.yml的文件
用于配置jmx_prometheus_javaagent.jar的监控参数。该文件应该包含要暴露的MBean的名称,以及它们要被暴露的端口号。
---
lowercaseOutputName: true
rules:
- pattern: 'java.lang<type=MemoryPool,name=([^,]+)><>(Usage|PeakUsage)\.(used|committed|max)'
name: jvm_memory_pool_$1_$2_$3_bytes
type: GAUGE
- pattern: 'java.lang<type=MemoryPool,name=([^,]+)><>CollectionUsage\.(used|committed)'
name: jvm_memory_pool_$1_collection_usage_$2_bytes
type: GAUGE
- pattern: 'java.lang<type=GarbageCollector,name=([^,]+)><>CollectionTime'
name: jvm_gc_collection_seconds_total
help: Time spent in a given JVM garbage collector in seconds.
labels:
collector: '$1'
type: COUNTER
- pattern: 'java.lang<type=GarbageCollector,name=([^,]+)><>CollectionCount'
name: jvm_gc_collection_count_total
help: Number of collections that have occurred for a given JVM garbage collector.
labels:
collector: '$1'
type: COUNTER
- pattern: 'java.lang<type=Memory><>HeapMemoryUsage\.(used|committed|max)'
name: jvm_memory_heap_$1_bytes
type: GAUGE
- pattern: 'java.lang<type=Memory><>NonHeapMemoryUsage\.(used|committed|max)'
name: jvm_memory_non_heap_$1_bytes
type: GAUGE
- pattern: 'java.lang<type=Threading><>ThreadCount'
name: jvm_threads_live
type: GAUGE
- pattern: 'java.lang<type=Threading><>DaemonThreadCount'
name: jvm_threads_daemon
type: GAUGE
- pattern: 'java.lang<type=OperatingSystem><>(\w+)'
name: jvm_$1
type: GAUGE
- pattern: 'org.apache.cassandra.metrics<type=(\w+), name=(\w+)><>(\w+)'
name: cassandra_$1_$2_$3
type: GAUGE
- pattern: 'org.apache.cassandra.metrics<type=(\w+), name=(\w+)><>(Count|MeanRate|OneMinuteRate|FiveMinuteRate|FifteenMinuteRate)'
name: cassandra_$1_$2_$3
type: COUNTER
unit: events/second
- pattern: 'org.apache.cassandra.metrics<type=(\w+), name=(\w+)><>(Min|Max|Mean|95thPercentile)'
name: cassandra_$1_$2_$3
type: GAUGE
unit: milliseconds
- pattern: 'org.apache.cassandra.metrics<type=(\w+), name=(\w+)><>.*'
name: cassandra_$1_$2
type: GAUGE
- pattern: 'java.lang<type=OperatingSystem><>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:'
name: os_$1_bytes
type: GAUGE
attrNameSnakeCase: true
- pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):'
name: os_$1
type: GAUGE
attrNameSnakeCase: true
3.使用agent方式启动java应用程序的同时启动jmx
java -javaagent:/path/to/jmx_prometheus_javaagent.jar=8080:/path/to/jmx_exporter_config.yml -jar myapp.jar
4.配置Prometheus.yml,并重启prometheus
scrape_configs:
- job_name: 'myapp'
static_configs:
- targets: ['localhost:8080']