Implemented SIGINT/SIGTERM recept
This commit is contained in:
parent
fd99599ba8
commit
ca69ebaffb
2 changed files with 17 additions and 7 deletions
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
hostMonitoringPeriod = 5
|
hostMonitoringPeriod = 5
|
||||||
urlMonitoringPeriod = 10
|
urlMonitoringPeriod = 30
|
||||||
urls = ["https://www.bootlesshacker.com"]
|
urls = ["https://www.bootlesshacker.com"]
|
||||||
urlTimeout = 10
|
urlTimeout = 10
|
||||||
maxWorkers = 4
|
maxWorkers = 4
|
||||||
|
|
22
main.py
22
main.py
|
@ -29,10 +29,17 @@ import time
|
||||||
import log
|
import log
|
||||||
import requests
|
import requests
|
||||||
import threading
|
import threading
|
||||||
|
import signal
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
stop_event = threading.Event()
|
||||||
|
|
||||||
|
def signal_handler(sig, frame):
|
||||||
|
print('SIGINT/SIGTERM aknowledged. Stopping script gracefully, please wait...')
|
||||||
|
stop_event.set()
|
||||||
|
|
||||||
def loadUrl(url):
|
def loadUrl(url):
|
||||||
|
|
||||||
headers = { 'User-Agent': 'Monutil monitor' }
|
headers = { 'User-Agent': 'Monutil monitor' }
|
||||||
|
@ -46,9 +53,9 @@ def prepareUrl(src, baseUrl):
|
||||||
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
|
return baseUrl.rstrip("/") + "/" + src.lstrip("/")
|
||||||
return src
|
return src
|
||||||
|
|
||||||
def monitorHost():
|
def monitorHost(stop_event):
|
||||||
|
|
||||||
while True:
|
while not stop_event.is_set():
|
||||||
|
|
||||||
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)
|
||||||
|
@ -62,9 +69,9 @@ def monitorHost():
|
||||||
|
|
||||||
time.sleep(config.hostMonitoringPeriod)
|
time.sleep(config.hostMonitoringPeriod)
|
||||||
|
|
||||||
def monitorUrls():
|
def monitorUrls(stop_event):
|
||||||
|
|
||||||
while True:
|
while not stop_event.is_set():
|
||||||
|
|
||||||
for url in config.urls:
|
for url in config.urls:
|
||||||
|
|
||||||
|
@ -100,9 +107,12 @@ def monitorUrls():
|
||||||
time.sleep(config.urlMonitoringPeriod)
|
time.sleep(config.urlMonitoringPeriod)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|
||||||
hostMonitorThread = threading.Thread(target=monitorHost)
|
hostMonitorThread = threading.Thread(target=monitorHost, args=(stop_event,))
|
||||||
urlMonitorThread = threading.Thread(target=monitorUrls)
|
urlMonitorThread = threading.Thread(target=monitorUrls, args=(stop_event,))
|
||||||
|
|
||||||
hostMonitorThread.start()
|
hostMonitorThread.start()
|
||||||
urlMonitorThread.start()
|
urlMonitorThread.start()
|
||||||
|
|
Loading…
Reference in a new issue