diff --git a/apache-recent b/apache-recent index ed1bb7e..05fbbb3 100644 --- a/apache-recent +++ b/apache-recent @@ -18,18 +18,34 @@ PATH_GROUPS = { } +def get_default_log_file(log_folder): + if log_folder is not None: + for candidate in ("other_vhosts_access.log", "access_log"): + log_file_path = os.path.join(log_folder, candidate) + if os.path.isfile(log_file_path): + return candidate + return None + + def get_log_line_timestamp(line): match = re.match('^.*\\[(\\d+\\/\\w+\\/\\d+:\\d.*)\\].*$', line) if match: try: - timestamp = datetime.strptime(match.group(1), "%d/%b/%Y:%H:%M:%S %z") - # Remove timezone (needed to avoid error while comparing with datetime.now()) - return timestamp.replace(tzinfo=None) + return datetime.strptime(match.group(1), "%d/%b/%Y:%H:%M:%S %z") except ValueError: pass return None +def detect_timezone(log_file): + with open(log_file, "r", encoding="utf-8") as f: + for line in f: + timestamp = get_log_line_timestamp(line) + if timestamp is not None: + return timestamp.tzinfo + return None + + def get_log_time_window(path): start_time = None end_time = None @@ -55,7 +71,7 @@ def get_log_line_path(line): def get_log_line_ip(line): - match = re.match('^.* (\\d+\\.\\d+\\.\\d+\\.\\d+) - - \\[.*$', line) + match = re.match('^.*(\\d+\\.\\d+\\.\\d+\\.\\d+) - - \\[.*$', line) if match: return match.group(1) return None @@ -83,8 +99,8 @@ def print_table(rows): print(" | ".join(cell.ljust(widths[i]) for i, cell in enumerate(row))) -def filter_logs(log_file, last_n_minutes): - now = datetime.now() +def filter_logs(log_file, last_n_minutes, timezone): + now = datetime.now(tz=timezone) total_count = 0 stats = { 'total': {}, @@ -126,6 +142,8 @@ for candidate in ("/var/log/apache2", "/var/log/httpd"): default_log_folder = candidate break +default_log_file = get_default_log_file(default_log_folder) + # Parse arguments parser = argparse.ArgumentParser() parser.add_argument( @@ -135,7 +153,7 @@ parser.add_argument( ) parser.add_argument( "--log-file", - default="other_vhosts_access.log", + default=default_log_file, help="Log file name", ) @@ -144,7 +162,13 @@ args = parser.parse_args() if args.log_folder is None: parser.error("Could not find Apache log folder. Please pass --log-folder explicitly.") -log_file_path = os.path.join(args.log_folder, args.log_file) +log_file = args.log_file +if log_file is None: + log_file = get_default_log_file(args.log_folder) + if log_file is None: + parser.error("Could not find Apache log file. Please pass --log-file explicitly.") + +log_file_path = os.path.join(args.log_folder, log_file) if not os.path.isfile(log_file_path): print(f"Could not find Apache log file in {log_file_path}", file=sys.stderr) @@ -166,8 +190,10 @@ else: print("Could not determine log time window.", file=sys.stderr) sys.exit(1) +timezone = detect_timezone(log_file_path) + print("\nStats for the last 10 minutes:\n") -filter_logs(log_file_path, 10) +filter_logs(log_file_path, 10, timezone) print("\nStats for the last 1 hour:\n") -filter_logs(log_file_path, 60) +filter_logs(log_file_path, 60, timezone)