mirror of
				https://github.com/mail-in-a-box/mailinabox.git
				synced 2025-11-03 19:30:54 +00:00 
			
		
		
		
	This adds a new section to the admin panel called "Activity", that supplies charts, graphs and details about messages entering and leaving the host. A new daemon captures details of system mail activity by monitoring the /var/log/mail.log file, summarizing it into a sqllite database that's kept in user-data.
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import os, stat
 | 
						|
import sqlite3
 | 
						|
import logging
 | 
						|
import threading
 | 
						|
 | 
						|
from .DatabaseConnectionFactory import DatabaseConnectionFactory
 | 
						|
 | 
						|
log = logging.getLogger(__name__)
 | 
						|
 | 
						|
 | 
						|
class SqliteConnFactory(DatabaseConnectionFactory):
 | 
						|
    def __init__(self, db_path):
 | 
						|
        super(SqliteConnFactory, self).__init__()
 | 
						|
        log.debug('factory for %s', db_path)
 | 
						|
        self.db_path = db_path
 | 
						|
        self.db_basename = os.path.basename(db_path)
 | 
						|
        self.ensure_exists()
 | 
						|
 | 
						|
    def ensure_exists(self):
 | 
						|
        # create the parent directory and set its permissions
 | 
						|
        parent = os.path.dirname(self.db_path)
 | 
						|
        if parent != '' and not os.path.exists(parent):
 | 
						|
            os.makedirs(parent)
 | 
						|
            os.chmod(parent,
 | 
						|
                     stat.S_IRWXU |
 | 
						|
                     stat.S_IRGRP |
 | 
						|
                     stat.S_IXGRP |
 | 
						|
                     stat.S_IROTH |
 | 
						|
                     stat.S_IXOTH
 | 
						|
            )
 | 
						|
 | 
						|
        # if the database is new, create an empty file and set file
 | 
						|
        # permissions
 | 
						|
        if not os.path.exists(self.db_path):
 | 
						|
            log.debug('creating empty database: %s', self.db_basename)
 | 
						|
            with open(self.db_path, 'w') as fp:
 | 
						|
                pass
 | 
						|
 | 
						|
            os.chmod(self.db_path,
 | 
						|
                     stat.S_IRUSR |
 | 
						|
                     stat.S_IWUSR
 | 
						|
            )
 | 
						|
 | 
						|
    def connect(self):
 | 
						|
        log.debug('opening database %s', self.db_basename)
 | 
						|
        conn = sqlite3.connect(self.db_path)
 | 
						|
        conn.row_factory = sqlite3.Row
 | 
						|
        return conn
 | 
						|
 | 
						|
    def close(self, conn):
 | 
						|
        log.debug('closing database %s', self.db_basename)
 | 
						|
        conn.close()
 |