Skip to content

Instantly share code, notes, and snippets.

@nitredd
Created August 27, 2025 08:53
Show Gist options
  • Select an option

  • Save nitredd/2908de878bd4d559367914aa956299d7 to your computer and use it in GitHub Desktop.

Select an option

Save nitredd/2908de878bd4d559367914aa956299d7 to your computer and use it in GitHub Desktop.
This script returns the sum of bytes read, grouped by the driver, from the MongoDB log file
#!/bin/bash
# Author: Yuvi Herziger
set -euo pipefail
LOG_FILE="$1"
if [[ -z "$LOG_FILE" ]]; then
echo "Error: Log file path not provided." >&2
echo "Usage: $0 /path/to/logfile" >&2
exit 1
fi
if [[ ! -f "$LOG_FILE" ]]; then
echo "Error: File not found at '$LOG_FILE'" >&2
exit 1
fi
METADATA_FILE=$(mktemp)
SLOW_QUERIES_FILE=$(mktemp)
trap 'rm -f "$METADATA_FILE" "$SLOW_QUERIES_FILE"' EXIT
grep '"msg":"client metadata"' "$LOG_FILE" | \
jq -r '[.ctx, ((.attr.doc.driver.name // "N/A") + "|" + (.attr.doc.driver.version // "N/A"))] | @tsv' | \
sort -u -k1,1 > "$METADATA_FILE"
grep '"msg":"Slow query"' "$LOG_FILE" | \
jq -r '[.ctx, (.attr.storage.data.bytesRead // 0)] | @tsv' | \
sort -k1,1 > "$SLOW_QUERIES_FILE"
if ! [[ -s "$SLOW_QUERIES_FILE" ]]; then
echo "No slow queries were found in the log file." >&2
exit 0
fi
AGGREGATED_DATA=$(
join -t $'\t' -a 2 -e "Unknown" -o '1.2,2.2' "$METADATA_FILE" "$SLOW_QUERIES_FILE" | \
awk -F'\t' '{
bytes_read[$1] += $2
}
END {
for (driver in bytes_read) {
print driver "\t" bytes_read[driver]
}
}'
)
if [[ -z "$AGGREGATED_DATA" ]]; then
echo "No data could be successfully correlated." >&2
exit 0
fi
echo "$AGGREGATED_DATA" | sort -t $'\t' -k2,2rn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment