log_summary.sh in Bash
A log triage in pure shell: buckets, top paths, the slow tail.
#!/usr/bin/env bash
# log_summary: status buckets, top paths and error lines from a log
set -euo pipefail
log=$(mktemp)
trap 'rm -f "$log"' EXIT
cat > "$log" <<'EOF'
GET /api/users 200 45
POST /api/login 401 12
GET /api/users 200 38
GET /static/app.js 200 5
POST /api/orders 500 220
GET /api/users 304 9
EOF
echo "== status buckets =="
awk '{ print substr($3, 1, 1) "xx" }' "$log" | sort | uniq -c
echo "== top paths =="
awk '{ print $2 }' "$log" | sort | uniq -c | sort -rn | head -3
echo "== slowest =="
sort -k4 -rn "$log" | head -1
errors=$(awk '$3 >= 500 { print $2 }' "$log")
echo "5xx on: ${errors:-none}"
How it works
awkbuckets statuses into 2xx/4xx/5xx with substr.sort | uniq -c | sort -rnranks the paths.sort -k4 -rnfinds the slowest request by column.
Keywords and builtins used here
echosettrap
The run, in numbers
- Lines
- 26
- Characters to type
- 617
- Tokens
- 85
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 64 seconds.
Step 1 of 2 in Encore, step 25 of 26 in Language basics.