JMX

JMXメトリクスをFluent BitでElasticsearchへ連携

以下bashを作成

#!/bin/bash
curl -s -X POST http://localhost:8778/jolokia/ \
  -H 'Content-Type: application/json' \
  -d '[
    { "type": "read", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage" },
    { "type": "read", "mbean": "java.lang:type=MemoryPool,name=Metaspace", "attribute": "Usage" },
    { "type": "read", "mbean": "java.lang:type=Threading", "attribute": "ThreadCount" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=ThreadPool", "attribute": "currentThreadCount" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=ThreadPool", "attribute": "currentThreadsBusy" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=ThreadPool", "attribute": "connectionCount" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=GlobalRequestProcessor", "attribute": "requestCount" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=GlobalRequestProcessor", "attribute": "processingTime" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=GlobalRequestProcessor", "attribute": "errorCount" },
    { "type": "read", "mbean": "Catalina:name=\"http-nio-8080\",type=GlobalRequestProcessor", "attribute": "bytesReceived" },
    { "type": "read", "mbean": "java.lang:name=PS Scavenge,type=GarbageCollector", "attribute": "CollectionCount" },
    { "type": "read", "mbean": "java.lang:name=PS MarkSweep,type=GarbageCollector", "attribute": "CollectionCount" }
  ]' \
| jq -c '
  {
    timestamp: .[0].timestamp,
    heap_used: .[0].value.used,
    heap_max: .[0].value.max,
    metaspace_used: .[1].value.used,
    metaspace_max: .[1].value.max,
    thread_count: .[2].value,
    tomcat_threads_current: .[3].value,
    tomcat_threads_busy: .[4].value,
    tomcat_connections: .[5].value,
    tomcat_request_count: .[6].value,
    tomcat_processing_time: .[7].value,
    tomcat_error_count: .[8].value,
    tomcat_bytes_received: .[9].value,
    gc_scavenge_count: .[10].value,
    gc_marksweep_count: .[11].value
  }'
-- 実行権限の付与
chmod +x /opt/jmx-metrics/jmx_metrics.sh
実行結果例
[root@vm105 jmx-metrics]# /opt/jmx-metrics/jmx_metrics.sh | jq
{
  "timestamp": 1746806735.280342,
  "heap_used": 82441624,
  "heap_max": 954728448,
  "metaspace_used": 23855568,
  "thread_count": 25,
  "tomcat_threads_current": 10,
  "tomcat_threads_busy": 0,
  "tomcat_connections": 1,
  "tomcat_request_count": 72,
  "tomcat_processing_time": 79773,
  "tomcat_error_count": 1,
  "tomcat_bytes_received": 25622,
  "gc_scavenge_count": 2,
  "gc_marksweep_count": 1
}
[root@vm105 jmx-metrics]#
fluent-bit.conf の追記
[INPUT]
    Name              exec
    Tag               jmx.metrics
    Command           /opt/jmx-metrics/jmx_metrics.sh
    Interval_Sec      30
    Buf_Size          10240

[FILTER]
    Name              modify
    Match             jmx.metrics
    Add               source jmx

[OUTPUT]
    Name              es
    Match             jmx.metrics
    Host              192.168.56.105
    Port              9200
    Index             tomcat-jmx-metrics
    Suppress_Type_Name On
    Logstash_Format   On
    HTTP_User         elastic
    HTTP_Passwd       qW1uCDzzTBMNqx4y_0OW
    tls               On
    tls.verify        Off
スポンサーリンク
タイトルとURLをコピーしました