Closes issue 16

This commit is contained in:
Thomas Williams 2024-07-06 15:47:41 +01:00
parent a5bb0bbe3b
commit 40ce856084
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
2 changed files with 52 additions and 4 deletions

18
log.py
View file

@ -23,14 +23,17 @@
# SOFTWARE. # SOFTWARE.
import pyodbc import pyodbc
import mysql.connector
import time import time
from config import logRetentionDays, maximumSQLAttempts from odbcReferences import driver
from config import logRetentionDays, maximumSQLAttempts, loggingMode
from datetime import datetime, timedelta from datetime import datetime, timedelta
class logsManager: class logsManager:
def __init__(self, server, database, username, password): 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): def insertHost(self, hostname, ipAddress):
@ -102,7 +105,7 @@ class logsManager:
currentAttempts = 1 currentAttempts = 1
self.insertHost(hostname, ipAddress) self.insertHost(hostname, ipAddress)
self.deleteOldLogs("monutil_urllogs", "logTime") self.deleteOldLogs("monutil_urlLogs", "logTime")
while currentAttempts <= maximumSQLAttempts: while currentAttempts <= maximumSQLAttempts:
@ -137,7 +140,14 @@ class logsManager:
conn = pyodbc.connect(self.conn_str) conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor() 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) cursor.execute(oldestLogQuery)
if cursor.rowcount > 0: if cursor.rowcount > 0:

38
odbcReferences.py Normal file
View file

@ -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