* Thu Sep 04 2025 Brian Read <brianr@koozali.org> 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 [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]
This commit is contained in:
16
root/etc/e-smith/db/configuration/migrate/80DBPass
Normal file
16
root/etc/e-smith/db/configuration/migrate/80DBPass
Normal file
@@ -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 ""
|
||||
}
|
24
root/etc/e-smith/templates/etc/e-smith/sql/init/99mailstats
Normal file
24
root/etc/e-smith/templates/etc/e-smith/sql/init/99mailstats
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
my $db = $mailstats{DBName} || 'mailstats';
|
||||
my $user = $mailstats{DBUser} || 'mailstats_rw';
|
||||
my $pass = $mailstats{DBPass} || 'changeme';
|
||||
$OUT .= <<END
|
||||
#! /bin/sh
|
||||
if [ -d /var/lib/mysql/mailstats ]; then
|
||||
exit
|
||||
fi
|
||||
/usr/bin/mariadb <<EOF
|
||||
CREATE DATABASE $db DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
|
||||
USE $db;
|
||||
CREATE TABLE IF NOT EXISTS SummaryLogs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
Date DATE,
|
||||
Hour INT,
|
||||
logData TEXT
|
||||
);
|
||||
CREATE USER $user@localhost IDENTIFIED BY '$pass';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON $db.* TO $user@localhost;
|
||||
FLUSH PRIVILEGES;
|
||||
EOF
|
||||
END
|
||||
}
|
@@ -1,97 +0,0 @@
|
||||
CREATE DATABASE IF NOT EXISTS `mailstats`;
|
||||
|
||||
USE `mailstats`;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `ColumnStats` (
|
||||
`ColumnStatsid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`timeid` int(11) NOT NULL default '0',
|
||||
`descr` varchar(20) NOT NULL default '',
|
||||
`count` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`ColumnStatsid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `JunkMailStats` (
|
||||
`JunkMailstatsid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`user` varchar(12) NOT NULL default '',
|
||||
`count` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) default NULL,
|
||||
PRIMARY KEY (`JunkMailstatsid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `SARules` (
|
||||
`SARulesid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`rule` varchar(50) NOT NULL default '',
|
||||
`count` bigint(20) NOT NULL default '0',
|
||||
`totalhits` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`SARulesid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `SAscores` (
|
||||
`SAscoresid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`acceptedcount` bigint(20) NOT NULL default '0',
|
||||
`rejectedcount` bigint(20) NOT NULL default '0',
|
||||
`hamcount` bigint(20) NOT NULL default '0',
|
||||
`acceptedscore` decimal(20,2) NOT NULL default '0.00',
|
||||
`rejectedscore` decimal(20,2) NOT NULL default '0.00',
|
||||
`hamscore` decimal(20,2) NOT NULL default '0.00',
|
||||
`totalsmtp` bigint(20) NOT NULL default '0',
|
||||
`totalrecip` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`SAscoresid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `VirusStats` (
|
||||
`VirusStatsid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`descr` varchar(40) NOT NULL default '',
|
||||
`count` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`VirusStatsid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `date` (
|
||||
`dateid` int(11) NOT NULL auto_increment,
|
||||
`date` date NOT NULL default '0000-00-00',
|
||||
PRIMARY KEY (`dateid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `domains` (
|
||||
`domainsid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`domain` varchar(40) NOT NULL default '',
|
||||
`type` varchar(10) NOT NULL default '',
|
||||
`total` bigint(20) NOT NULL default '0',
|
||||
`denied` bigint(20) NOT NULL default '0',
|
||||
`xfererr` bigint(20) NOT NULL default '0',
|
||||
`accept` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`domainsid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `qpsmtpdcodes` (
|
||||
`qpsmtpdcodesid` int(11) NOT NULL auto_increment,
|
||||
`dateid` int(11) NOT NULL default '0',
|
||||
`reason` varchar(40) NOT NULL default '',
|
||||
`count` bigint(20) NOT NULL default '0',
|
||||
`servername` varchar(30) NOT NULL default '',
|
||||
PRIMARY KEY (`qpsmtpdcodesid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `time` (
|
||||
`timeid` int(11) NOT NULL auto_increment,
|
||||
`time` time NOT NULL default '00:00:00',
|
||||
PRIMARY KEY (`timeid`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
|
||||
|
||||
CREATE USER 'mailstats'@'localhost' IDENTIFIED BY 'mailstats';
|
||||
GRANT ALL PRIVILEGES ON mailstats.* TO 'mailstats'@'localhost';
|
||||
FLUSH PRIVILEGES;
|
||||
|
24
root/etc/e-smith/templates/etc/mailstats/db.php/10DBDetails
Normal file
24
root/etc/e-smith/templates/etc/mailstats/db.php/10DBDetails
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
# Load SME::ConfigDB to read values from DB
|
||||
my $cdb = esmith::ConfigDB->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";
|
||||
<?php
|
||||
return [
|
||||
'host' => '$dbhost',
|
||||
'user' => '$dbuser',
|
||||
'pass' => '$dbpass',
|
||||
'name' => '$dbname',
|
||||
];
|
||||
END
|
||||
}
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
@@ -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
|
||||
|
15
root/usr/bin/runallmailstats.sh
Executable file
15
root/usr/bin/runallmailstats.sh
Executable file
@@ -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
|
Reference in New Issue
Block a user