以下bashを作成JMXメトリクスを取得するcurlをスクリプトにまとめます。
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 の追記
[SERVICE]
Flush 1
Daemon Off
Log_Level info
Parsers_File parsers.conf
# plugins_file plugins.conf
[INPUT]
Name exec
Tag jmx.metrics
Command /opt/jmx-metrics/jmx_metrics.sh
Interval_Sec 3
Buf_Size 10240
# パーサーを作成し
[FILTER]
Name parser
Match jmxmetrics
Key_Name exec
Parser jmx_json
[FILTER]
Name modify
Match jmx.metrics
Add source jmx
[OUTPUT]
Name stdout
Match *
Format json_lines
[OUTPUT]
Name pgsql
Match jmx.metrics
Host 192.168.56.105
Port 5432
User sooni
Password soopass
Database soodb
Table jmx_metrics
parsers.confへ以下追記
[PARSER]
Name jmx_json
Format json
CREATE TABLE jmx_metrics (
server_name text,
execution_time TIMESTAMP ,
metrics JSONB -- JMXメトリクスをJSONとして格納
);
SELECT
to_timestamp(((metrics->>'exec')::jsonb->>'timestamp')::double precision) AS formatted_time
,(metrics->>'exec')::jsonb->>'heap_used' AS heap_used
,(metrics->>'exec')::jsonb->>'heap_max' AS heap_max
,(metrics->>'exec')::jsonb->>'metaspace_used' AS metaspace_used
,(metrics->>'exec')::jsonb->>'tomcat_connections' AS tomcat_connections
FROM jmx_metrics;