Resolves issue #4

This commit is contained in:
Thomas Williams 2024-06-27 18:25:59 +01:00
parent c4edd9badd
commit 39059f342e
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
2 changed files with 61 additions and 40 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
monitoringPeriod = 30
hostMonitoringPeriod = 5
urlMonitoringPeriod = 10
urls = ["https://www.bootlesshacker.com"]
urlTimeout = 10
maxWorkers = 4

36
main.py
View file

@ -28,12 +28,15 @@ import os
import time
import log
import requests
import threading
from functools import partial
from concurrent.futures import ThreadPoolExecutor, as_completed
from bs4 import BeautifulSoup
def loadUrl(url):
headers = { 'User-Agent': 'Monutil monitor' }
response = requests.get(url, timeout=config.urlTimeout, headers=headers)
return response
@ -43,7 +46,9 @@ def prepareUrl(src, baseUrl):
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
return src
while True:
def monitorHost():
while True:
load1, load5, load15 = psutil.getloadavg() # this takes time to warm up if not running script on *nix
loadavg = round((load1/os.cpu_count()) * 100, 2)
@ -55,11 +60,11 @@ while True:
print("CPU %: " + str(loadavg))
print("Memory %: " + str(memory))
# Log CPU/Memory
time.sleep(config.hostMonitoringPeriod)
headers = {
'User-Agent': 'Monutil monitor'
}
def monitorUrls():
while True:
for url in config.urls:
@ -75,20 +80,35 @@ while True:
imageUrls = [img['src'] for img in html.find_all('img')]
with ThreadPoolExecutor(max_workers=config.maxWorkers) as executor:
responses = [executor.submit(loadUrl, prepareUrl(url, baseUrl)) for url in imageUrls]
responses = [executor.submit(loadUrl, prepareUrl(url, baseUrl)) for url in imageUrls]
responses = [future.result() for future in as_completed(responses)]
for response in responses:
if not response.status_code == 200:
urlFail = True
endTime = time.time()
timeDiff = endTime - startTime
print(timeDiff)
else:
urlFail = True
time.sleep(config.monitoringPeriod)
time.sleep(config.urlMonitoringPeriod)
def main():
hostMonitorThread = threading.Thread(target=monitorHost)
urlMonitorThread = threading.Thread(target=monitorUrls)
hostMonitorThread.start()
urlMonitorThread.start()
hostMonitorThread.join()
urlMonitorThread.join()
if __name__ == "__main__":
main()