diff --git a/log.py b/log.py index 86ee9c4..b3930f5 100644 --- a/log.py +++ b/log.py @@ -23,14 +23,17 @@ # SOFTWARE. import pyodbc +import mysql.connector import time -from config import logRetentionDays, maximumSQLAttempts +from odbcReferences import driver +from config import logRetentionDays, maximumSQLAttempts, loggingMode from datetime import datetime, timedelta class logsManager: def __init__(self, server, database, username, password): - self.conn_str = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password + + self.conn_str = 'DRIVER=' + driver + ';SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password def insertHost(self, hostname, ipAddress): @@ -102,7 +105,7 @@ class logsManager: currentAttempts = 1 self.insertHost(hostname, ipAddress) - self.deleteOldLogs("monutil_urllogs", "logTime") + self.deleteOldLogs("monutil_urlLogs", "logTime") while currentAttempts <= maximumSQLAttempts: @@ -137,7 +140,14 @@ class logsManager: conn = pyodbc.connect(self.conn_str) cursor = conn.cursor() - oldestLogQuery = f"SELECT TOP 1 {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC" + if loggingMode == 'mssql': + + oldestLogQuery = f"SELECT TOP 1 {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC" + + elif loggingMode == 'mariadb': + + oldestLogQuery = f"SELECT {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC LIMIT 1" + cursor.execute(oldestLogQuery) if cursor.rowcount > 0: diff --git a/odbcReferences.py b/odbcReferences.py new file mode 100644 index 0000000..d44a74e --- /dev/null +++ b/odbcReferences.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +# MIT License + +# Copyright (c) 2024 Thomas Williams - https://git.server.wales/thomas + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import config + +odbcMariaDB = '{MariaDB}' +odbcMSSQL = '{ODBC Driver 17 for SQL Server}' + +# DO NOT MODIFY ANYTHING BELOW THIS LINE + +if config.loggingMode == 'mariadb': + + driver = odbcMariaDB + +elif config.loggingMode == 'mssql': + + driver = odbcMSSQL