diff --git a/README.md b/README.md index d4a3a98..5cf02fb 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,5 @@ Both CPU/RAM monitoring and URL monitoring can be set on their own monitoring pe - sqlServer - the address of the SQL server which to write the data. - sqlDatabase - the database to write the data. - sqlUsername - the username used to authenticate to the SQL server. -- sqlPassword - the password used to authenticate to the SQL server. +- sqlPassword - the password used to authenticate to the SQL server. +- logRetentionDays - the maximum age logs should be kept. diff --git a/config.py b/config.py index 8bf2bf4..7befe9e 100644 --- a/config.py +++ b/config.py @@ -4,7 +4,7 @@ hostMonitoringPeriod = 1 urlMonitoringPeriod = 10 -urls = [""] +urls = ["https://1.1.1.1"] urlTimeout = 10 maxWorkers = 4 forceNonPOSIXCPU = True @@ -13,3 +13,4 @@ sqlServer = '' sqlDatabase = '' sqlUsername = '' sqlPassword = '' +logRetentionDays = '90' diff --git a/log.py b/log.py index e48cafc..857defb 100644 --- a/log.py +++ b/log.py @@ -23,7 +23,8 @@ # SOFTWARE. import pyodbc -from datetime import datetime +from config import logRetentionDays +from datetime import datetime, timedelta class logsManager: @@ -56,6 +57,7 @@ class logsManager: try: self.insertHost(hostname, ipAddress) + self.deleteOldLogs("monutil_hostLogs", "logTime") conn = pyodbc.connect(self.conn_str) cursor = conn.cursor() @@ -73,6 +75,7 @@ class logsManager: try: self.insertHost(hostname, ipAddress) + self.deleteOldLogs("monutil_urlLogs", "logTime") conn = pyodbc.connect(self.conn_str) cursor = conn.cursor() @@ -85,3 +88,23 @@ class logsManager: except pyodbc.Error as ex: print("Error inserting data into monutil_urlLogs:", ex) + + def deleteOldLogs(self, tableName, logTimeColumn): + + try: + + conn = pyodbc.connect(self.conn_str) + cursor = conn.cursor() + + oldestLogQuery = f"SELECT TOP 1 {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC" + cursor.execute(oldestLogQuery) + oldestLogTime = cursor.fetchone()[0] + + if oldestLogTime is not None and oldestLogTime < datetime.now() - timedelta(days=int(logRetentionDays)): + + deleteQuery = f"DELETE FROM {tableName} WHERE {logTimeColumn} < ?" + cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDays))) + conn.commit() + except pyodbc.Error as ex: + + print("Error deleting old logs: {}".format(str(ex)))