Closes issue #15

This commit is contained in:
Thomas Williams 2024-07-04 14:48:22 +01:00
parent df0f19949a
commit a677ce90ba
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
3 changed files with 28 additions and 3 deletions

View file

@ -16,3 +16,4 @@ Both CPU/RAM monitoring and URL monitoring can be set on their own monitoring pe
- sqlDatabase - the database to write the data. - sqlDatabase - the database to write the data.
- sqlUsername - the username used to authenticate to the SQL server. - 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.

View file

@ -4,7 +4,7 @@
hostMonitoringPeriod = 1 hostMonitoringPeriod = 1
urlMonitoringPeriod = 10 urlMonitoringPeriod = 10
urls = [""] urls = ["https://1.1.1.1"]
urlTimeout = 10 urlTimeout = 10
maxWorkers = 4 maxWorkers = 4
forceNonPOSIXCPU = True forceNonPOSIXCPU = True
@ -13,3 +13,4 @@ sqlServer = ''
sqlDatabase = '' sqlDatabase = ''
sqlUsername = '' sqlUsername = ''
sqlPassword = '' sqlPassword = ''
logRetentionDays = '90'

25
log.py
View file

@ -23,7 +23,8 @@
# SOFTWARE. # SOFTWARE.
import pyodbc import pyodbc
from datetime import datetime from config import logRetentionDays
from datetime import datetime, timedelta
class logsManager: class logsManager:
@ -56,6 +57,7 @@ class logsManager:
try: try:
self.insertHost(hostname, ipAddress) self.insertHost(hostname, ipAddress)
self.deleteOldLogs("monutil_hostLogs", "logTime")
conn = pyodbc.connect(self.conn_str) conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor() cursor = conn.cursor()
@ -73,6 +75,7 @@ class logsManager:
try: try:
self.insertHost(hostname, ipAddress) self.insertHost(hostname, ipAddress)
self.deleteOldLogs("monutil_urlLogs", "logTime")
conn = pyodbc.connect(self.conn_str) conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor() cursor = conn.cursor()
@ -85,3 +88,23 @@ class logsManager:
except pyodbc.Error as ex: except pyodbc.Error as ex:
print("Error inserting data into monutil_urlLogs:", 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)))