diff --git a/root/usr/bin/mailstats.py b/root/usr/bin/mailstats.py index 3dcba25..9c56839 100644 --- a/root/usr/bin/mailstats.py +++ b/root/usr/bin/mailstats.py @@ -6,16 +6,17 @@ # # Mailstats # -# optional arguments: -# -h, --help show this help message and exit -# -d DATE, --date DATE Specify a valid date (yyyy-mm-dd) for the analysis -# -ef EMAILFILE, --emailfile EMAILFILE -# Save an html file of the email sent (y/N) -# -tf TEXTFILE, --textfile TEXTFILE -# Save a txt file of the html page (y/N) +# Optional arguments: +# -h, --help show this help message and exit +# -d DATE, --date DATE Specify a valid date (yyyy-mm-dd) for the analysis +# -ef EMAILFILE, --emailfile EMAILFILE +# Save an html file of the email sent (y/N) +# -tf TEXTFILE, --textfile TEXTFILE +# Save a txt file of the html page (y/N) +# --version show program's version number and exit # # -# Re-written in python from Mailstats.pl (Perl) to conform to SME11 / Postfix / qpsmtpd log formats +# (June 2024 - bjr) Re-written in Python from Mailstats.pl (Perl) to conform to SME11 / Postfix / qpsmtpd log formats # and html output added # # Todo: @@ -24,7 +25,7 @@ # 4. Percent char causes sort to fail - look at adding it in the template # 5. Chase disparity in counts betweeen old mailstats and this # 6. Count emails delivered over ports 25/587/465 (SMTPS?) -# 7. use tmp for txt file and copy to html dir if required. +# 7. Incorporate the (rpm) build date into the version # # Future: # 1. Write summary line for each transaction to DB and link to it through cell in main table @@ -54,6 +55,9 @@ import argparse import tempfile Mailstats_version = '1.2' +build_date_time = "__BUILD_DATE_TIME__" +if build_date_time == "__BUILD_DATE_TIME__": + build_date_time = "Unknown" script_dir = os.path.dirname(os.path.abspath(__file__)) data_file_path = script_dir+'/../..' #back to the top @@ -453,41 +457,41 @@ def render_sub_table(table_title,table_headers,found_values): return rendered_html -def get_spamassassin_version(): - """ - Get the installed SpamAssassin version. +# def get_spamassassin_version(): + # """ + # Get the installed SpamAssassin version. - Returns: - str: Version number of SpamAssassin if installed, otherwise an error message. - """ - try: - result = subprocess.run(['spamassassin', '--version'], capture_output=True, text=True) - if result.returncode == 0: - version_line = result.stdout.split('\n')[0] - version = version_line.split()[1] - return version - else: - return "SpamAssassin is not installed or an error occurred." - except Exception as e: - return f"Error: {e}" + # Returns: + # str: Version number of SpamAssassin if installed, otherwise an error message. + # """ + # try: + # result = subprocess.run(['spamassassin', '--version'], capture_output=True, text=True) + # if result.returncode == 0: + # version_line = result.stdout.split('\n')[0] + # version = version_line.split()[1] + # return version + # else: + # return "SpamAssassin is not installed or an error occurred." + # except Exception as e: + # return f"Error: {e}" -def get_clamav_version(): - """ - Get the installed ClamAV version. +# def get_clamav_version(): + # """ + # Get the installed ClamAV version. - Returns: - str: Version number of ClamAV if installed, otherwise an error message. - """ - try: - result = subprocess.run(['clamscan', '--version'], capture_output=True, text=True) - if result.returncode == 0: - version_line = result.stdout.split('\n')[0] - version = version_line.split()[1] - return version - else: - return "ClamAV is not installed or an error occurred." - except Exception as e: - return f"Error: {e}" + # Returns: + # str: Version number of ClamAV if installed, otherwise an error message. + # """ + # try: + # result = subprocess.run(['clamscan', '--version'], capture_output=True, text=True) + # if result.returncode == 0: + # version_line = result.stdout.split('\n')[0] + # version = version_line.split()[1] + # return version + # else: + # return "ClamAV is not installed or an error occurred." + # except Exception as e: + # return f"Error: {e}" def read_html_from_file(filepath): """ @@ -646,7 +650,9 @@ if __name__ == "__main__": parser.add_argument('-d', '--date', help='Specify a valid date (yyyy-mm-dd) for the analysis', default=formatted_yesterday) parser.add_argument('-ef', '--emailfile', help='Save an html file of the email sent (y/N)', default='n') parser.add_argument('-tf', '--textfile', help='Save a txt file of the html page (y/N)', default='n') + parser.add_argument('--version', action='version', version='%(prog)s '+Mailstats_version+" built on "+build_date_time) args = parser.parse_args() + analysis_date = args.date # and check its format is valid try: @@ -660,8 +666,19 @@ if __name__ == "__main__": anaysis_date_obj = datetime.strptime(analysis_date, '%Y-%m-%d') noemailfile = args.emailfile.lower() == 'n' notextfile = args.textfile.lower() == 'n' - isThonny = is_running_under_thonny() + + hello_string = "Mailstats:"+Mailstats_version+' for '+DomainName+" at "+formatted_datetime+" for "+analysis_date + print(hello_string) + version_string = "Chameleon:"+chameleon_version+" Python:"+python_version + if isThonny: + version_string = version_string + "...under Thonny" + if args.version: + print(f"{Mailstats_version} {version_string}") + quit() + + print(version_string) + #E-Smith Config DBs if isThonny: db_dir = "/home/brianr/SME11Build/GITFiles/smecontribs/smeserver-mailstats/" @@ -691,8 +708,8 @@ if __name__ == "__main__": EMailSMTPUser = get_value(ConfigDB,"mailstats","EmailUser") #None = default => no authenticatioon needed EMailSMTPPassword = get_value(ConfigDB,"mailstats","EmailPassword") - spamassassin_version = get_spamassassin_version() - clamav_version = get_clamav_version() + #spamassassin_version = get_spamassassin_version() + #clamav_version = get_clamav_version() FetchmailIP = '127.0.0.200'; #Apparent Ip address of fetchmail deliveries WebmailIP = '127.0.0.1'; #Apparent Ip of Webmail sender @@ -701,12 +718,6 @@ if __name__ == "__main__": MAILMAN = "bounces"; #sender when mailman sending when orig is localhost DMARCDomain="dmarc"; #Pattern to recognised DMARC sent emails (this not very reliable, as the email address could be anything) DMARCOkPattern="dmarc: pass"; #Pattern to use to detect DMARC approval - hello_string = "Mailstats:"+Mailstats_version+' for '+DomainName+" at "+formatted_datetime+" for "+analysis_date - print(hello_string) - version_string = "Chameleon:"+chameleon_version+" Python:"+python_version - if isThonny: - version_string = version_string + "...under Thonny" - print(version_string) num_hours = 25 # Represents hours from 0 to 23 - adds extra one for column totals and another for percentages @@ -757,18 +768,18 @@ if __name__ == "__main__": qpcodes_pattern = re.compile(r".*(\(.*\)).*'") i = 0; sorted_len= len(sorted_log_dict) - # Initial call to print the progress bar #unless none to show + spamavg = 0; + spamqueuedcount = 0 + hamcount = 0 + hamavg = 0 + rejectspamcount = 0 + rejectspamavg = 0 + DMARCSendCount = 0 + totalexamined = 0 if sorted_len > 0: - spamavg = 0; - spamqueuedcount = 0 - hamcount = 0 - hamavg = 0 - rejectspamcount = 0 - rejectspamavg = 0 - DMARCSendCount = 0 - totalexamined = 0 if isThonny: + # Initial call to print the progress bar print_progress_bar(0, sorted_len, prefix='Progress:', suffix='Complete', length=50) for timestamp, data in sorted_log_dict.items(): i += 1 diff --git a/smeserver-mailstats.spec b/smeserver-mailstats.spec index 6fdaa3f..a2d95db 100644 --- a/smeserver-mailstats.spec +++ b/smeserver-mailstats.spec @@ -1,17 +1,31 @@ -# $Id: smeserver-mailstats.spec,v 1.8 2023/02/15 09:40:09 brianr Exp $ +# $Id: smeserver-mailstats.spec,v 1.7 2021/04/02 11:11:44 brianr Exp $ # Authority: brianread # Name: Brian Read Summary: Daily mail statistics for SME Server %define name smeserver-mailstats Name: %{name} -%define version 1.2 -%define release 1 +%define version 1.1 +%define release 18 Version: %{version} Release: %{release}%{?dist} License: GPL Group: SME/addon -Source: %{name}-%{version}.tar.xz +Source: %{name}-%{version}.tgz +Patch0: smeserver-mailstats-1.1-random-cron_and_spamfilter-stats-7-update.patch +Patch1: smeserver-mailstats-1.1.bz8957.Create_the_mailstats_user.patch +Patch2: smeserver-mailstats-1.1.bz8656.Spamassassin_scores_and_tags.patch +Patch3: smeserver-mailstats-1.0-628.patch +Patch4: smeserver-mailstats-1.1.bz9588.qpsmtpd0_96compatible.patch +Patch5: smeserver-mailstats-1.1.bz9717-9716email_specific_stats.patch +Patch6: smeserver-mailstats-1.1.bz9888.geoip.patch +Patch7: smeserver-mailstats-1.1.bz10858_10327.cleanup_and_email_truncate.patch +Patch8: smeserver-mailstats-1.1-uninitialised-variable-on-no-data +Patch9: smeserver-mailstats-1.1-Clear-up-uninitialsed-use-of-lc.patch +Patch10:smeserver-mailstats-1.1-Add-Update-event-to-createlinks.patch +Patch11:smeserver-mailstats-1.1-Take-out-dot-in-cron-file-and-also-re-direct-errors-to-syslog.patch +Patch12:smeserver-mailstats-1.1-bz12327-Add-in-imap-authorisation-log-string.patch +Patch13:smeserver-mailstats-1.1-Pull-in-python-rewrite-from-sme11-dev.patch BuildRoot: /var/tmp/%{name}-%{version}-%{release}-buildroot BuildArchitectures: noarch @@ -19,22 +33,19 @@ Requires: smeserver-release => 9.0 Requires: qpsmtpd >= 0.96 BuildRequires: e-smith-devtools >= 1.13.1-03 Requires: perl-Switch +BuildRequires: python36 +Requires: python36 +Requires: html2text +Requires: python3-chameleon +AutoReqProv: no %description 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 -* Tue May 28 2024 Brian Read 1.2-1.sme -- Rewrite in python and conform to new postfix SME11 log format [SME: ] - - -* Sun Jul 09 2023 cvs2git.sh aka Brian Read 1.1-18.sme -- Roll up patches and move to git repo [SME: 12338] - -* Sun Jul 09 2023 BogusDateBot -- Eliminated rpmbuild "bogus date" warnings due to inconsistent weekday, - by assuming the date is correct and changing the weekday. +* Fri Jun 07 2024 Brian Read 1.1-18.sme +- Pull in python re-write from SME11 dev [SME: ] * Wed Feb 15 2023 Brian Read 1.1-17.sme - Add-in-imap-authorisation-log-string [SME: 12327] @@ -105,6 +116,20 @@ See http://www.contribs.org/bugzilla/show_bug.cgi?id=819 %prep %setup +%patch0 -p1 +%patch1 -p1 +%patch2 -p1 +%patch3 -p1 +%patch4 -p1 +%patch5 -p1 +%patch6 -p1 +%patch7 -p1 +%patch8 -p1 +%patch9 -p1 +%patch10 -p1 +%patch11 -p1 +%patch12 -p1 +%patch13 -p1 %build perl createlinks @@ -113,11 +138,19 @@ perl createlinks /bin/rm -rf $RPM_BUILD_ROOT (cd root ; /usr/bin/find . -depth -print | /bin/cpio -dump $RPM_BUILD_ROOT) chmod +x $RPM_BUILD_ROOT/usr/bin/runmailstats.sh +# Define the placeholder and generate the current date and time +now=$(date +"%Y-%m-%d %H:%M:%S") + +# Replace the placeholder in the Python program located at %{BUILDROOT}/usr/bin +sed -i "s|__BUILD_DATE_TIME__|$now|" $RPM_BUILD_ROOT/usr/bin/mailstats.py + + /bin/rm -f %{name}-%{version}-filelist -/sbin/e-smith/genfilelist $RPM_BUILD_ROOT > %{name}-%{version}-filelist +/sbin/e-smith/genfilelist $RPM_BUILD_ROOT | grep -v "\.pyc" | grep -v "\.pyo" > %{name}-%{version}-filelist %clean /bin/rm -rf $RPM_BUILD_ROOT %files -f %{name}-%{version}-filelist %defattr(-,root,root) +