Commit d61758c9 authored by Jonathan Michalon's avatar Jonathan Michalon

Implement continuous polling

Continuous polling means that the process runs forever, checking
everything from time to time.
To get results, a listing of checks' state is printed when SIGUSR1
is caught. Single run (for config or debug) is accessible with the
-1 or --one command line switch.
parent fe95099c
from lib.checks import *
# This is the base granularity (in seconds) for polling
# Each check may then individually be configured to run every N * tick
base_tick = 60
mail = Host(ipv4='127.0.0.1', ipv6='::1')
web = Host(ipv4='127.0.0.0', ipv6='::42')
alsace = Host(ipv4='127.0.0.1', ipv6='::1')
......
import concurrent.futures
from config import *
import signal
import argparse
from time import sleep
def usr1_handler(signum, frame):
print ("""Signal SIGUSR1 caught, printing state of checks.
Checks in error:""")
for check in checks:
if not check.ok:
print ('-' * 10)
print ("Check %s is in error state:\n\t%s" % (check,
check.errmsg.strip()))
print ('-' * 10, """
Other checks (usually OK):""")
for check in checks:
if check.ok:
print ('-' * 10)
print ("Check %s is OK" % check)
print ('-' * 10)
if __name__ == '__main__':
# register signal handling
signal.signal(signal.SIGUSR1, usr1_handler)
# Parse command line
parser = argparse.ArgumentParser()
parser.add_argument("-1", "--one",
help="single run with immediate output of check results (test/debug)",
action="store_true")
args = parser.parse_args()
# do the actual polling
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
def runner(check):
return check.run(), check
futures = []
for check in checks:
futures.append(executor.submit(runner, check))
for future in concurrent.futures.as_completed(futures):
success, check = future.result()
if success:
print("Check %s successful!" % (str(check)))
else:
print("Check %s failed:\n%s" %
(str(check), check.errmsg))
if args.one:
futures = []
for check in checks:
futures.append(executor.submit(runner, check))
for future in concurrent.futures.as_completed(futures):
success, check = future.result()
if success:
print("Check %s successful!" % (str(check)))
else:
print("Check %s failed:\n%s" %
(str(check), check.errmsg))
else:
# This will drift slowly as it takes (base_tick + espilon) seconds
while True:
for check in checks:
executor.submit(check.run())
sleep(base_tick)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment