From cb1991739e4185321c4196cf123e05d0e5cb3428 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 10 Feb 2022 15:46:47 +0000 Subject: [PATCH 1/4] scripts/apache-log-rate: Determine highest message rate from apache logs --- scripts/apache-log-rate | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 scripts/apache-log-rate diff --git a/scripts/apache-log-rate b/scripts/apache-log-rate new file mode 100755 index 0000000..d5e9106 --- /dev/null +++ b/scripts/apache-log-rate @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +# vim: wrapmargin=0 textwidth=0 smarttab expandtab tabstop=2 shiftwidth=2 +"""Process Apache access.log lines to find highest rate of /upload/.""" + +import argparse +import datetime +import dateutil.parser +import fileinput +import re + + +def process_log_file( + input_file: str ='-', + request_text: str = '/upload/', + window_size: int = 1, +) -> None: + """ + Process the indicated log file to determine peak rate of interesting lines. + + :param input_file: Name of input file, `-` for stdin + :param request_text: The text that denotes an interesting line + :param window_size: Time, in seconds, for the window to assess + """ + print(f'With:\n\tinput_file: "{input_file}"\n\trequest_text: "{request_text}"') + with fileinput.FileInput(files=(input_file)) as f: + apache_re = re.compile(r'^(?P[.:0-9a-fA-F]{3,39}) - - \[(?P[^\]]+)\] (?P.*' + request_text + '.*)$') + apache_datetime_re = re.compile( + r'^(?P[0-9]{2})/(?P[^/]{3})/(?P[0-9]{4}):(?P