diff --git a/root/usr/bin/mailstats.py b/root/usr/bin/mailstats.py index 5f0614b..2b33a89 100644 --- a/root/usr/bin/mailstats.py +++ b/root/usr/bin/mailstats.py @@ -98,7 +98,7 @@ log_dir_path = "/var/log/mailstats" # Check if the directory exists, and create it if it doesn't if not os.path.exists(log_dir_path): os.makedirs(log_dir_path) -logging.basicConfig(level=logging.DEBUG, # Default level of messages to log +logging.basicConfig(level=logging.INFO, # Default level of messages to log format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(), # Log to console @@ -242,14 +242,22 @@ def get_logs_from_Journalctl(date='yesterday'): # Retrieve logs within the time range logs = [] + log_count = 0 + error_count = 0 for entry in j: - entry_timestamp = entry.get('__REALTIME_TIMESTAMP', None) - entry_microseconds = int(entry_timestamp.timestamp() * 1_000_000) - if entry_timestamp and since_microseconds <= entry_microseconds <= until_microseconds: - # takeout ASCII Escape sequences from the message - entry['MESSAGE'] = strip_ansi_codes(entry['MESSAGE']) - logs.append(entry) - + try: + entry_timestamp = entry.get('__REALTIME_TIMESTAMP', None) + entry_microseconds = int(entry_timestamp.timestamp() * 1_000_000) + if entry_timestamp and since_microseconds <= entry_microseconds <= until_microseconds: + log_count += 1 + # takeout ASCII Escape sequences from the message + entry['MESSAGE'] = strip_ansi_codes(entry['MESSAGE']) + logs.append(entry) + except Exception as e: + logging.warning(f"Error - log line: {log_count} {entry['_PID']} {entry['SYSLOG_IDENTIFIER']} : {e}") + error_count += 1 + if error_count: + logging.info(f"Had {error_count} errors on journal import - probably non character bytes") # Sort logs by __REALTIME_TIMESTAMP in ascending order sorted_logs = sorted(logs, key=lambda x: x.get("__REALTIME_TIMESTAMP", 0)) @@ -257,7 +265,7 @@ def get_logs_from_Journalctl(date='yesterday'): except Exception as e: logging.error(f"Unexpected error: {e}") - return {} + return {} def transform_to_dict(data, keys, iso_date): diff --git a/smeserver-mailstats.spec b/smeserver-mailstats.spec index 72ba727..526d592 100644 --- a/smeserver-mailstats.spec +++ b/smeserver-mailstats.spec @@ -6,7 +6,7 @@ Summary: Daily mail statistics for SME Server %define name smeserver-mailstats Name: %{name} %define version 11.1 -%define release 3 +%define release 4 Version: %{version} Release: %{release}%{?dist} License: GPL @@ -35,6 +35,9 @@ A script that via cron.d e-mails mail statistics to admin on a daily basis. See http://www.contribs.org/bugzilla/show_bug.cgi?id=819 %changelog +* Mon Sep 01 2025 Brian Read 11.1-4.sme +- More fixes for Journal bytes instead of characters [SME: 13117] + * Mon Sep 01 2025 Brian Read 11.1-3.sme - Sort out ASCII escape codes in return from journalctl API [SME: 13117] - Add in Status enabled t default for mailstats DB [SME: 13118]