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