nova_bfd_monitor.py 2.14 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
import time
import logging
import argparse
from daemonize import Daemonize
6
from nova_router import setupLogfile, bfd
7 8 9 10 11 12 13 14

APP = "nova_bfd_monitor"
DESCRIPTION = "Check that everything is ok, and signal link down via bfd otherwise"


def main(logfile, interface):
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(name)s %(levelname)s %(message)s')
15
    logging.getLogger("requests").setLevel(logging.WARNING)
16
    if logfile:
17
        setupLogfile(logfile)
Sigmund Augdal's avatar
Sigmund Augdal committed
18

19
    logging.info("Process starting")
Sigmund Augdal's avatar
Sigmund Augdal committed
20

21 22
    state = bfd.check_state(interface)
    bfd.set_state(state)
23
    logging.info("bfd-check starting, initial state: %s", state)
24
    upstream_state = bfd.get_upstream_state()
25
    bfd.first_upstream_state(interface, upstream_state)
26 27
    while True:
        time.sleep(1)
28
        new_state = bfd.check_state(interface)
29
        if new_state != state:
30
            bfd.set_state(new_state)
31 32
            logging.info("state changed from %s to %s", state, new_state)
            state = new_state
33
        new_upstream_state = bfd.get_upstream_state()
34
        bfd.new_upstream_state(interface, new_upstream_state, upstream_state)
35
        upstream_state = new_upstream_state
36 37


38 39 40
def daemon_main(args):
    try:
        main(args.logfile, args.interface)
41
    except Exception:
42 43 44
        logging.exception("Unhandled exception")


45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
def parse_args():
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    parser.add_argument('-d', '--daemonize', default=False, action='store_true',
                        help="Run as daemon")
    parser.add_argument('--pidfile', type=str, default="/var/run/{}.pid".format(APP),
                        help="pidfile when run as daemon")
    parser.add_argument('--logfile', default='/var/log/{}.log'.format(APP),
                        help="logfile to use")
    parser.add_argument('--interface', help="Downstream interface to monitor status of")

    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    if args.daemonize:
60
        daemon = Daemonize(app=APP, pid=args.pidfile, action=lambda: daemon_main(args))
61 62 63
        daemon.start()
    else:
        main(None, args.interface)