CPU/RAM now works on non POSIX systems closing issue #11.
This commit is contained in:
parent
839c36bbf7
commit
8192b5efe2
2 changed files with 55 additions and 6 deletions
|
@ -1,7 +1,8 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
hostMonitoringPeriod = 5
|
hostMonitoringPeriod = 1 # on posix systems, it is suggested this is a minimum of 5 unless forceNonPOSIXCPU is true
|
||||||
urlMonitoringPeriod = 30
|
urlMonitoringPeriod = 10
|
||||||
urls = ["https://www.bootlesshacker.com"]
|
urls = ["https://www.bootlesshacker.com"]
|
||||||
urlTimeout = 10
|
urlTimeout = 10
|
||||||
maxWorkers = 4
|
maxWorkers = 4
|
||||||
|
forceNonPOSIXCPU = False # this will force the custom cpu monintor to run instead if set to True
|
||||||
|
|
52
main.py
52
main.py
|
@ -35,6 +35,8 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
stop_event = threading.Event()
|
stop_event = threading.Event()
|
||||||
|
nonPOSIXCPULoads = []
|
||||||
|
lock = threading.Lock()
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
print('SIGINT/SIGTERM aknowledged. Stopping script gracefully, please wait...')
|
print('SIGINT/SIGTERM aknowledged. Stopping script gracefully, please wait...')
|
||||||
|
@ -53,10 +55,54 @@ def prepareUrl(src, baseUrl):
|
||||||
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
|
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
|
||||||
return src
|
return src
|
||||||
|
|
||||||
def monitorHost(stop_event):
|
def nonPOSIXCPULoad(stop_event):
|
||||||
|
|
||||||
|
global nonPOSIXCPULoads
|
||||||
|
nonPOSIXCPULoad = 0
|
||||||
|
|
||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
|
|
||||||
|
nonPOSIXCPULoad = psutil.cpu_percent(interval=1)
|
||||||
|
|
||||||
|
with lock:
|
||||||
|
|
||||||
|
nonPOSIXCPULoads.append(nonPOSIXCPULoad)
|
||||||
|
|
||||||
|
if len(nonPOSIXCPULoads) > 60:
|
||||||
|
nonPOSIXCPULoads.pop(0)
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def getNonPOSIXCPUAverage():
|
||||||
|
|
||||||
|
global nonPOSIXCPULoads
|
||||||
|
|
||||||
|
with lock:
|
||||||
|
if sum(nonPOSIXCPULoads) > 0:
|
||||||
|
avgLoad = sum(nonPOSIXCPULoads) / len(nonPOSIXCPULoads)
|
||||||
|
else:
|
||||||
|
avgLoad = 0
|
||||||
|
|
||||||
|
return avgLoad
|
||||||
|
|
||||||
|
def monitorHost(stop_event):
|
||||||
|
|
||||||
|
nonPOSIXCPUStarted = False
|
||||||
|
|
||||||
|
while not stop_event.is_set():
|
||||||
|
|
||||||
|
if os.name != 'posix' or config.forceNonPOSIXCPU:
|
||||||
|
|
||||||
|
if not nonPOSIXCPUStarted:
|
||||||
|
|
||||||
|
nonPOSIXCPUMonitor = threading.Thread(target=nonPOSIXCPULoad, args=(stop_event,))
|
||||||
|
nonPOSIXCPUMonitor.start()
|
||||||
|
nonPOSIXCPUStarted = True
|
||||||
|
|
||||||
|
loadavg = round(getNonPOSIXCPUAverage(), 2)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
load1, load5, load15 = psutil.getloadavg() # this takes time to warm up if not running script on *nix
|
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)
|
loadavg = round((load1/os.cpu_count()) * 100, 2)
|
||||||
|
|
||||||
|
@ -64,6 +110,7 @@ def monitorHost(stop_event):
|
||||||
|
|
||||||
print("CPU %: " + str(loadavg))
|
print("CPU %: " + str(loadavg))
|
||||||
print("Memory %: " + str(memory))
|
print("Memory %: " + str(memory))
|
||||||
|
print() # new line
|
||||||
|
|
||||||
time.sleep(config.hostMonitoringPeriod)
|
time.sleep(config.hostMonitoringPeriod)
|
||||||
|
|
||||||
|
@ -97,7 +144,8 @@ def monitorUrls(stop_event):
|
||||||
endTime = time.time()
|
endTime = time.time()
|
||||||
timeDiff = endTime - startTime
|
timeDiff = endTime - startTime
|
||||||
|
|
||||||
print(timeDiff)
|
print(baseUrl + " response time: " + str(timeDiff))
|
||||||
|
print() # new line
|
||||||
|
|
||||||
else:
|
else:
|
||||||
urlFail = True
|
urlFail = True
|
||||||
|
|
Loading…
Add table
Reference in a new issue