This closes issue20

This commit is contained in:
Thomas Williams 2024-07-05 13:42:28 +01:00
parent 29546b6280
commit 26d2d9b168
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
2 changed files with 64 additions and 51 deletions

View file

@ -17,4 +17,9 @@ Both CPU/RAM monitoring and URL monitoring can be set on their own monitoring pe
- **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. - **logRetentionDays** - the maximum age logs should be kept.
- **maximumSQLAttempts** - the maximum number of attempts to try certain SQL operations - **maximumSQLAttempts** - the maximum number of attempts to try certain SQL operations.
- **hostMonitorStartTime** - the start time which the host monitor should start at the earliest
- **hostMonitorEndTime** - the end time which the host monitor should shut down.
- **urlMonitorStartTime** - the start time which the url monitor should start at the earliest
- **urlMonitorEndTime** - the end time which the url monitor whould shut down.

18
main.py
View file

@ -88,13 +88,13 @@ def getNonPOSIXCPUAverage():
return avgLoad return avgLoad
def monitorHost(stop_event): def monitorHost(stop_event):
while time.strftime("%H:%M:%S") <= config.hostMonitorStartTime:
time.sleep(1) # This block is important to ensure the thread sleeps until the start time is reached. Else the thread wont start if the script is started before the start time
nonPOSIXCPUStarted = False nonPOSIXCPUStarted = False
while not (stop_event.is_set()):
while not (stop_event.is_set()) and (time.strftime("%H:%M:%S") >= config.hostMonitorStartTime and time.strftime("%H:%M:%S") <= config.hostMonitorEndTime): while not (stop_event.is_set()) and (time.strftime("%H:%M:%S") >= config.hostMonitorStartTime and time.strftime("%H:%M:%S") <= config.hostMonitorEndTime):
if os.name != 'posix' or config.forceNonPOSIXCPU: if os.name != 'posix' or config.forceNonPOSIXCPU:
@ -113,6 +113,7 @@ def monitorHost(stop_event):
loadavg = round((load1/os.cpu_count()) * 100, 2) loadavg = round((load1/os.cpu_count()) * 100, 2)
memory = psutil.virtual_memory().percent memory = psutil.virtual_memory().percent
logHostLog(socket.gethostname(), datetime.now(), loadavg, memory) logHostLog(socket.gethostname(), datetime.now(), loadavg, memory)
print("CPU %: " + str(loadavg)) print("CPU %: " + str(loadavg))
@ -121,10 +122,11 @@ def monitorHost(stop_event):
time.sleep(config.hostMonitoringPeriod) time.sleep(config.hostMonitoringPeriod)
time.sleep(1)
def monitorUrls(stop_event): def monitorUrls(stop_event):
while time.strftime("%H:%M:%S") <= config.urlMonitorStartTime: while not (stop_event.is_set()):
time.sleep(1) # This block is important to ensure the thread sleeps until the start time is reached. Else the thread wont start if the script is started before the start time
while not (stop_event.is_set()) and (time.strftime("%H:%M:%S") >= config.urlMonitorStartTime and time.strftime("%H:%M:%S") <= config.urlMonitorEndTime): while not (stop_event.is_set()) and (time.strftime("%H:%M:%S") >= config.urlMonitorStartTime and time.strftime("%H:%M:%S") <= config.urlMonitorEndTime):
@ -134,6 +136,7 @@ def monitorUrls(stop_event):
urlFail = False urlFail = False
startTime = time.time() startTime = time.time()
request = loadUrl(url) request = loadUrl(url)
if request.status_code == 200: if request.status_code == 200:
@ -149,6 +152,7 @@ def monitorUrls(stop_event):
for response in responses: for response in responses:
if not response.status_code == 200: if not response.status_code == 200:
urlFail = True urlFail = True
endTime = time.time() endTime = time.time()
@ -156,13 +160,17 @@ def monitorUrls(stop_event):
print(baseUrl + " response time: " + str(timeDiff)) print(baseUrl + " response time: " + str(timeDiff))
print() # new line print() # new line
logURLLog(socket.gethostname(), datetime.now(), baseUrl, timeDiff) logURLLog(socket.gethostname(), datetime.now(), baseUrl, timeDiff)
else: else:
urlFail = True urlFail = True
time.sleep(config.urlMonitoringPeriod) time.sleep(config.urlMonitoringPeriod)
time.sleep(1)
def logHostLog(hostname, logTime, cpu, memory): def logHostLog(hostname, logTime, cpu, memory):
if not config.loggingMode == 'none': if not config.loggingMode == 'none':