diff --git a/createlinks b/createlinks index 65db52d..21b40fc 100644 --- a/createlinks +++ b/createlinks @@ -6,8 +6,9 @@ $event = 'smeserver-mailstats-update'; #see the /etc/systemd/system-preset/49-koozali.preset should be present for systemd integration on all you yum update event foreach my $file (qw( - /etc/systemd/system-preset/49-koozali.preset - /etc/e-smith/sql/init/99smeserver-mailstats.sql + /etc/systemd/system-preset/49-koozali.preset + /etc/mailstats/db.php + /etc/e-smith/sql/init/99mailstats /etc/httpd/conf/httpd.conf )) { @@ -20,7 +21,7 @@ event_link('systemd-reload', $event, '50'); #event_link('action', $event, '30'); #services we need to restart safe_symlink('restart', "root/etc/e-smith/events/$event/services2adjust/httpd-e-smith"); +safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/mysql.init");; #and Server Mmanager panel link #panel_link('somefunction', 'manager'); - -templates2events("/etc/e-smith/sql/init/99smeserver-mailstats.sql", "post-upgrade"); +#templates2events("/etc/e-smith/sql/init/99smeserver-mailstats.sql", "post-upgrade"); \ No newline at end of file diff --git a/root/etc/e-smith/db/configuration/migrate/80DBPass b/root/etc/e-smith/db/configuration/migrate/80DBPass new file mode 100644 index 0000000..e0d16e4 --- /dev/null +++ b/root/etc/e-smith/db/configuration/migrate/80DBPass @@ -0,0 +1,16 @@ +{ + use MIME::Base64 qw(encode_base64); + + my $rec = $DB->get('mailstats') || $DB->new_record('mailstats', {type => 'report'}); + + my $pw = $rec->prop('DBPass'); + return "" if $pw; + + my $length = shift || 16; + + my @chars = ('A'..'Z', 'a'..'z', 0..9, qw(! @ $ % ^ & * ? _ - + =)); + $pw = ''; + $pw .= $chars[rand @chars] for 1..$length; + $rec->set_prop('DBPass', $pw); + return "" +} diff --git a/root/etc/e-smith/templates.metadata/etc/e-smith/sql/init/99smeserver-mailstats.sql b/root/etc/e-smith/templates.metadata/etc/e-smith/sql/init/99mailstats similarity index 100% rename from root/etc/e-smith/templates.metadata/etc/e-smith/sql/init/99smeserver-mailstats.sql rename to root/etc/e-smith/templates.metadata/etc/e-smith/sql/init/99mailstats diff --git a/root/etc/e-smith/templates/etc/e-smith/sql/init/99mailstats b/root/etc/e-smith/templates/etc/e-smith/sql/init/99mailstats new file mode 100644 index 0000000..9966216 --- /dev/null +++ b/root/etc/e-smith/templates/etc/e-smith/sql/init/99mailstats @@ -0,0 +1,24 @@ +{ + my $db = $mailstats{DBName} || 'mailstats'; + my $user = $mailstats{DBUser} || 'mailstats_rw'; + my $pass = $mailstats{DBPass} || 'changeme'; + $OUT .= <open() || die "Cannot open configuration DB\n"; + + # Get the fragment (report database definition) + my $report = $cdb->get('mailstats'); + + my $dbhost = $report->prop('DBHost') || 'localhost'; + my $dbport = $report->prop('DBPort') || '3306'; + my $dbuser = $report->prop('DBUser') || 'mailstats_rw'; + # Assume password is stored in a property 'DBPass' + my $dbpass = $report->prop('DBPass') || 'changeme'; + my $dbname = $report->key || 'mailstats'; + + $OUT = <<"END"; + '$dbhost', + 'user' => '$dbuser', + 'pass' => '$dbpass', + 'name' => '$dbname', +]; +END +} diff --git a/root/opt/mailstats/html/ShowDetailedLogs.php b/root/opt/mailstats/html/ShowDetailedLogs.php index 10b3bd2..d168c53 100644 --- a/root/opt/mailstats/html/ShowDetailedLogs.php +++ b/root/opt/mailstats/html/ShowDetailedLogs.php @@ -24,7 +24,9 @@ $dbname = getenv('MAILSTATS_DB_NAME') ?: ''; if ($username === '' || $password === '' || $dbname === '') { $cfgPath = '/etc/mailstats/db.php'; // optional fallback config file if (is_readable($cfgPath)) { - $cfg = include $cfgPath; + ob_start(); + $cfg = include $cfgPath; + ob_end_clean(); $servername = $cfg['host'] ?? $servername; $username = $cfg['user'] ?? $username; $password = $cfg['pass'] ?? $password; diff --git a/root/opt/mailstats/html/showSummaryLogs.php b/root/opt/mailstats/html/showSummaryLogs.php index bf8e052..2de77ba 100644 --- a/root/opt/mailstats/html/showSummaryLogs.php +++ b/root/opt/mailstats/html/showSummaryLogs.php @@ -26,7 +26,9 @@ $dbname = getenv('MAILSTATS_DB_NAME') ?: ''; if ($username === '' || $password === '' || $dbname === '') { $cfgPath = '/etc/mailstats/db.php'; if (is_readable($cfgPath)) { - $cfg = include $cfgPath; + ob_start(); + $cfg = include $cfgPath; + ob_end_clean(); $servername = $cfg['host'] ?? $servername ?: 'localhost'; $username = $cfg['user'] ?? $username; $password = $cfg['pass'] ?? $password; diff --git a/root/usr/bin/mailstats.py b/root/usr/bin/mailstats.py index b47fcf2..fc63e00 100644 --- a/root/usr/bin/mailstats.py +++ b/root/usr/bin/mailstats.py @@ -111,7 +111,7 @@ except ImportError: logging.warning("Matplotlib is not installed - no graphs") enable_graphs = False; -Mailstats_version = '1.2' +Mailstats_version = '1.3' build_date_time = "2024-06-18 12:03:40OURCE" build_date_time = build_date_time[:19] #Take out crap that sneaks in. @@ -123,7 +123,6 @@ data_file_path = script_dir+'/../..' #back to the top now = datetime.now() yesterday = now - timedelta(days=1) formatted_yesterday = yesterday.strftime("%Y-%m-%d") -#html_page_path = data_file_path+"/home/e-smith/files/ibays/mesdb/html/mailstats/" html_page_dir = data_file_path+"/opt/mailstats/html/" template_dir = data_file_path+"/opt/mailstats/templates/" logs_dir = data_file_path+"/opt/mailstats/logs/" @@ -1374,7 +1373,7 @@ if __name__ == "__main__": count_records_to_db = 0; # Db save control - saveData = get_value(ConfigDB,"mailstats","SaveDataToMySQL","no") == 'yes' or forceDbSave + saveData = get_value(ConfigDB,"mailstats","SaveDataToMySQL","yes") == 'yes' or forceDbSave logging.debug(f"Save Mailstats to DB set:{saveData} ") if saveData: # Database config retrieval diff --git a/root/usr/bin/runallmailstats.sh b/root/usr/bin/runallmailstats.sh new file mode 100755 index 0000000..ff773a4 --- /dev/null +++ b/root/usr/bin/runallmailstats.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Extract the earliest date from the journalctl header for qpsmtpd service +earliest_date=$(journalctl -u qpsmtpd | head -n 1 | sed -n 's/.*Logs begin at [A-Za-z]* \([0-9-]*\).*/\1/p') + +# Get yesterday's date +yesterday=$(date -d 'yesterday' +%F) + +current_date="$earliest_date" + +# Loop from earliest date to yesterday +while [[ "$current_date" < "$yesterday" || "$current_date" == "$yesterday" ]]; do + runmailstats.sh "$current_date" + current_date=$(date -I -d "$current_date + 1 day") +done diff --git a/smeserver-mailstats.spec b/smeserver-mailstats.spec index 680df28..8ae5e55 100644 --- a/smeserver-mailstats.spec +++ b/smeserver-mailstats.spec @@ -92,8 +92,9 @@ usermod -aG systemd-journal www %changelog * Thu Sep 04 2025 Brian Read 11.1-6.sme - Add favicon to mailstats table, summary and detailed pages [SME: 13121] -- Bring DB config reading for mailstats itself inline with php summary and detailed logs - using /etc/mailstats/db.php -- Remove DB config fields from the SM2 config panel +- Bring DB config reading for mailstats itself inline with php summary and detailed logs - using /etc/mailstats/db.php [SME: 13121] +- Remove DB config fields from the SM2 config panel {sme: 13121] +- Arrange for password to be generated and mailstats user to be set with limited permissions [SME: 13121] * Tue Sep 02 2025 Brian Read 11.1-5.sme - Speed up Journal access [SME: 13121]