Skip to content

Commit c715c1c

Browse files
authored
Merge pull request #10873 from cdrini/feature/obfi-range
Add experimental obfi_range command
2 parents 8b3f45d + 66754b5 commit c715c1c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

scripts/obfi.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ obfi() {
1818
echo " obfi 'tac' | ... - reads the logs in reverse"
1919
echo " obfi_previous_minute | ... - reads the logs from the previous minute"
2020
echo " obfi_walk_logs <days> | ... - walks the gzip logs for the last <days> days"
21+
echo " obfi_range <start> <end> | ... - ALPHA: reads the logs from the given range."
22+
echo " Only works for today's logs (access.log). Expects"
23+
echo " timestamps in milliseconds, eg from Grafana URLs."
2124
echo ""
2225
echo "From here, you can filter the logs with grep as normal, or the obfi grep commands."
2326
echo "These commands use grep, so support grep options like -i, -v, etc. See grep --help."
@@ -114,6 +117,48 @@ except BrokenPipeError:
114117
'
115118
}
116119

120+
obfi_range() {
121+
# Iterate over the logs and print the logs from the given range
122+
if [[ -z "$1" || -z "$2" ]]; then
123+
echo "Usage: obfi_range <start> <end>"
124+
echo "Example: obfi_range 1748502382753 1748503464280"
125+
echo "Timestamps eg from grafana URLs"
126+
return 1
127+
fi
128+
129+
START=$1
130+
END=$2
131+
132+
obfi tac | python3 -c "
133+
import sys
134+
from datetime import datetime, timezone
135+
start = datetime.fromtimestamp($START / 1000, tz=timezone.utc)
136+
end = datetime.fromtimestamp($END / 1000, tz=timezone.utc)
137+
138+
print(f'Start: {start:%d/%b/%Y:%H:%M:%S %z}, End: {end:%d/%b/%Y:%H:%M:%S %z}', file=sys.stderr)
139+
140+
started = False
141+
buffer = 25 # Lines to read after mismatch
142+
try:
143+
for line in sys.stdin:
144+
# Extract the date from the line
145+
# Get the date part, e.g. '18/Mar/2025:11:20:36 +0000'
146+
date_str = ' '.join(line.split(' ', 5)[3:5])[1:-1]
147+
log_date = datetime.strptime(date_str, '%d/%b/%Y:%H:%M:%S %z')
148+
149+
if start <= log_date <= end:
150+
started = True
151+
buffer = 25
152+
sys.stdout.write(line)
153+
elif started:
154+
buffer -= 1
155+
if buffer == 0:
156+
break
157+
except BrokenPipeError:
158+
pass
159+
"
160+
}
161+
117162
###############################################################
118163
# Aggregation commands
119164
###############################################################

0 commit comments

Comments
 (0)