【Prometheus】jmx_exporter监控java应用(tomcat/jar)

一、整合jmx_exporter+tomcat

1、下载jmx_exporter:

下载地址:https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.12.0/jmx_prometheus_javaagent-0.12.0.jar

2、配置jmx_exporter:

jmx_exporter config文件示例地址:
https://github.com/prometheus/jmx_exporter/tree/master/example_configs
对tomcat.yaml略作修改,增加metric结果如下:

---   
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
- pattern: 'Catalina<>(\w+):'
  name: tomcat_$3_total
  labels:
    port: "$2"
    protocol: "$1"
  help: Tomcat global $3
  type: COUNTER
- pattern: 'Catalina<>(requestCount|maxTime|processingTime|errorCount):'
  name: tomcat_servlet_$3_total
  labels:
    module: "$1"
    servlet: "$2"
  help: Tomcat servlet $3 total
  type: COUNTER
- pattern: 'Catalina<>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'
  name: tomcat_threadpool_$3
  labels:
    port: "$2"
    protocol: "$1"
  help: Tomcat threadpool $3
  type: GAUGE
- pattern: 'Catalina<>(processingTime|sessionCounter|rejectedSessions|expiredSessions):'
  name: tomcat_session_$3_total
  labels:
    context: "$2"
    host: "$1"
  help: Tomcat session $3 total
  type: COUNTER
- pattern: 'java.lang<>(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<>((?!process_cpu_time)\w+):'
  name: os_$1
  type: GAUGE
  attrNameSnakeCase: true  
 
3、配置tomcat

在tomcat bin目录下新增setenv.sh,内容如下,使 jmx_exporter 可以跟 tomcat 一起启动。

JAVA_OPTS="-javaagent:/usr/local/jmx/jmx_prometheus_javaagent-0.3.1.jar=30013:/usr/local/jmx/config.yaml"

二、整合jmx_exporter+jar包

1、下载jmx_exporter:

下载地址:https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.12.0/jmx_prometheus_javaagent-0.12.0.jar

2、配置jmx_exporter:

jmx_exporter config文件示例地址:
https://github.com/prometheus/jmx_exporter/tree/master/example_configs
对tomcat.yaml略作修改,并增加metric,

---   
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
- pattern: 'Tomcat<>(\w+):'
  name: tomcat_$3_total
  labels:
    port: "$2"
    protocol: "$1"
  help: Tomcat global $3
  type: COUNTER
- pattern: 'Tomcat<>(requestCount|maxTime|processingTime|errorCount):'
  name: tomcat_servlet_$3_total
  labels:
    module: "$1"
    servlet: "$2"
  help: Tomcat servlet $3 total
  type: COUNTER
- pattern: 'Tomcat<>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'
  name: tomcat_threadpool_$3
  labels:
    port: "$2"
    protocol: "$1"
  help: Tomcat threadpool $3
  type: GAUGE
- pattern: 'Tomcat<>(processingTime|sessionCounter|rejectedSessions|expiredSessions):'
  name: tomcat_session_$3_total
  labels:
    context: "$2"
    host: "$1"
  help: Tomcat session $3 total
  type: COUNTER
- pattern: 'java.lang<>(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<>((?!process_cpu_time)\w+):'
  name: os_$1
  type: GAUGE
  attrNameSnakeCase: true  
- pattern: ".*"
3、启动jar包

在jar包启动命令中添加启动参数如下,使jmx_exporter与jar包一同启动:

java  -javaagent:../jmx_prometheus_javaagent-0.12.0.jar=30014:jar.yml -Dspring.config.location=def.properties -Dserver.port=11130 -jar authority-admin-1.0.0-CLOUD-PROD.jar  

三、整合Prometheus+grafana

1、prometheus新增target

可通过静态配置或自动发现的方式新增 jmx_exporter的ip:port为target

// prometheus.yml 配置文件内容
 - job_name: 'tomcat'
    static_configs:
    - targets: ['localhost:30013']
      labels:
        appname: 'test'  
2、grafana配置

使用id为8563进行grafana配置。

你可能感兴趣的:(#,Prometheus系列)