Last active
July 25, 2024 08:52
-
-
Save sunny4381/4b83184d3aa47b69a18c9321f95ddbe3 to your computer and use it in GitHub Desktop.
jqコマンドを利用してmongodbのlogからslow queryを抽出して集計するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# slow query のみ抽出 & 整形 | |
head -500 /var/log/mongodb/mongod.log |\ | |
jq -c 'select(.msg == "Slow query" and .attr.type == "command") | { collection: (.attr.command.find // .attr.command.aggregate // .attr.command.mapReduce), durationMillis: .attr.durationMillis, count: 1 } | select(.collection != null)' \ | |
> work-0 | |
# slow query の集計 | |
cat work-0 |\ | |
jq -s -c 'group_by(.collection) | .[] | reduce .[] as {$collection,$durationMillis,$count} (null; .collection = $collection | .durationMillis += $durationMillis | .count += $count)' \ | |
> work-1 | |
# 遅い順 Top 5 | |
cat work-1 | jq -s -c 'sort_by(.durationMillis) | reverse | .[0:5] | .[]' | |
# 件数の多い順 Top 5 | |
cat work-1 | jq -s -c 'sort_by(.count) | reverse | .[0:5] | .[]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment