picomon.py 2 KB
Newer Older
Jonathan Michalon's avatar
Jonathan Michalon committed
1
import concurrent.futures
2 3 4
import signal
import argparse
from time import sleep
5 6
import config as user_config
from lib import config
7 8 9 10 11 12


def usr1_handler(signum, frame):
    print ("""Signal SIGUSR1 caught, printing state of checks.

    Checks in error:""")
13
    for check in config.checks:
14
        if not check.ok:
15
            print ('-+' * 40)
16
            print ("Check %s is in error state:\n\t%s" % (check,
Colomban Wendling's avatar
Colomban Wendling committed
17
                   check.errmsg.strip()))
18
    print ('-+' * 40, """
19

20
    Other checks (usually OK but may be in retry mode):""")
21
    for check in config.checks:
22
        if check.ok:
23 24
            print ("Check %s is %s" % (check,
                   "OK" if check.retry_count == 0 else "retrying"))
Jonathan Michalon's avatar
Jonathan Michalon committed
25 26 27


if __name__ == '__main__':
28 29 30 31 32 33
    # register signal handling
    signal.signal(signal.SIGUSR1, usr1_handler)

    # Parse command line
    parser = argparse.ArgumentParser()
    parser.add_argument("-1", "--one",
Colomban Wendling's avatar
Colomban Wendling committed
34 35 36
                        help="single run with immediate output of " +
                             "check results (test/debug)",
                        action="store_true")
37 38 39
    args = parser.parse_args()

    # do the actual polling
Jonathan Michalon's avatar
Jonathan Michalon committed
40
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
41
        def runner(check):
42
            return check.run(immediate=True), check
43

44 45
        if args.one:
            futures = []
46
            for check in config.checks:
47 48 49 50 51 52 53 54
                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" %
55
                          (str(check), check.errmsg.strip()))
56 57 58
        else:
            # This will drift slowly as it takes (base_tick + espilon) seconds
            while True:
59
                for check in config.checks:
60
                    executor.submit(check.run())
61
                sleep(config.base_tick)