Updated deletion of old logs
This commit is contained in:
parent
0ebd1abdd7
commit
743bb3a6ec
1 changed files with 37 additions and 7 deletions
40
log.py
40
log.py
|
@ -77,7 +77,7 @@ class logsManager:
|
|||
currentAttempts = 1
|
||||
|
||||
self.insertHost(hostname, ipAddress)
|
||||
self.deleteOldLogs("monutil_hostlogs", "logTime")
|
||||
self.deleteOldLogs("hostlogs", "logTime", logRetentionDaysHost)
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
|
@ -183,7 +183,7 @@ class logsManager:
|
|||
currentAttempts = 1
|
||||
|
||||
self.insertHost(hostname, ipAddress)
|
||||
self.deleteOldLogs("monutil_hostlogs", "logTime")
|
||||
self.deleteOldLogs("iplogs", "logTime", logRetentionDaysIPBlock)
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
|
@ -279,7 +279,7 @@ class logsManager:
|
|||
currentAttempts = 1
|
||||
|
||||
self.insertHost(hostname, ipAddress)
|
||||
self.deleteOldLogs("monutil_urlLogs", "logTime")
|
||||
self.deleteOldLogs("urllogs", "logTime", logRetentionDaysURL)
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
|
@ -303,7 +303,7 @@ class logsManager:
|
|||
|
||||
time.sleep(1)
|
||||
|
||||
def deleteOldLogs(self, tableName, logTimeColumn):
|
||||
def deleteOldLogs(self, logType, logTimeColumn, logRetentionDays):
|
||||
|
||||
currentAttempts = 1
|
||||
|
||||
|
@ -311,6 +311,13 @@ class logsManager:
|
|||
|
||||
try:
|
||||
|
||||
if logType == 'hostlogs':
|
||||
tableName = 'monutil_hostLogs'
|
||||
elif logType == 'urllogs':
|
||||
tableName = 'monutil_urlLogs'
|
||||
elif logType == 'iplogs':
|
||||
tableName == 'monutil_ipblock'
|
||||
|
||||
conn = pyodbc.connect(self.conn_str)
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
@ -329,8 +336,31 @@ class logsManager:
|
|||
|
||||
if cursor.rowcount > 0 and oldestLogTime < datetime.now() - timedelta(days=int(logRetentionDays)):
|
||||
|
||||
if logType == 'hostlogs':
|
||||
|
||||
deleteQuery = f"DELETE FROM {tableName} WHERE {logTimeColumn} < ?"
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDays)))
|
||||
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDaysHost)))
|
||||
conn.commit()
|
||||
|
||||
elif logType == 'urllogs':
|
||||
|
||||
deleteQuery = f"DELETE FROM {tableName} WHERE {logTimeColumn} < ?"
|
||||
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDaysURL)))
|
||||
conn.commit()
|
||||
|
||||
elif logType == 'iplogs':
|
||||
|
||||
deleteQuery = f"DELETE FROM monutil_geoip WHERE logID IN (
|
||||
SELECT logID FROM monutil_ipblock WHERE {logTimeColumn} < ?)"
|
||||
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDaysIPBlock)))
|
||||
conn.commit()
|
||||
|
||||
deleteQuery = f"DELETE FROM monutil_ipblock WHERE {logTimeColumn} < ?"
|
||||
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDaysIPBlock)))
|
||||
conn.commit()
|
||||
|
||||
break
|
||||
|
|
Loading…
Reference in a new issue