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
|
||||
|
||||
hostMonitoringPeriod = 5
|
||||
urlMonitoringPeriod = 30
|
||||
hostMonitoringPeriod = 1 # on posix systems, it is suggested this is a minimum of 5 unless forceNonPOSIXCPU is true
|
||||
urlMonitoringPeriod = 10
|
||||
urls = ["https://www.bootlesshacker.com"]
|
||||
urlTimeout = 10
|
||||
maxWorkers = 4
|
||||
forceNonPOSIXCPU = False # this will force the custom cpu monintor to run instead if set to True
|
||||
|
|
56
main.py
56
main.py
|
@ -35,6 +35,8 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
|||
from bs4 import BeautifulSoup
|
||||
|
||||
stop_event = threading.Event()
|
||||
nonPOSIXCPULoads = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('SIGINT/SIGTERM aknowledged. Stopping script gracefully, please wait...')
|
||||
|
@ -53,17 +55,62 @@ def prepareUrl(src, baseUrl):
|
|||
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
|
||||
return src
|
||||
|
||||
def monitorHost(stop_event):
|
||||
def nonPOSIXCPULoad(stop_event):
|
||||
|
||||
global nonPOSIXCPULoads
|
||||
nonPOSIXCPULoad = 0
|
||||
|
||||
while not stop_event.is_set():
|
||||
|
||||
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)
|
||||
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
|
||||
loadavg = round((load1/os.cpu_count()) * 100, 2)
|
||||
|
||||
memory = psutil.virtual_memory().percent
|
||||
|
||||
print("CPU %: " + str(loadavg))
|
||||
print("Memory %: " + str(memory))
|
||||
print() # new line
|
||||
|
||||
time.sleep(config.hostMonitoringPeriod)
|
||||
|
||||
|
@ -97,7 +144,8 @@ def monitorUrls(stop_event):
|
|||
endTime = time.time()
|
||||
timeDiff = endTime - startTime
|
||||
|
||||
print(timeDiff)
|
||||
print(baseUrl + " response time: " + str(timeDiff))
|
||||
print() # new line
|
||||
|
||||
else:
|
||||
urlFail = True
|
||||
|
|
Loading…
Reference in a new issue