Commit f425d662 authored by Sigmund Augdal's avatar Sigmund Augdal

Move most code to new file for easier future unit testing

parent 265fa850
#!/usr/bin/env python
import os.path
import requests
import time
import subprocess
import logging
import argparse
from daemonize import Daemonize
from nova_router import setupLogfile
from nova_router import setupLogfile, bfd
APP = "nova_bfd_monitor"
DESCRIPTION = "Check that everything is ok, and signal link down via bfd otherwise"
ADMIN_DOWN_MARKER = "/etc/admin_down"
HAPROXY_CHECK_URL = "http://localhost:1936/haproxy_up"
BFDD_CONTROL = "/usr/local/bin/bfdd-control"
def check_state(interface):
"""
Checks downstream status to determine which state we should announce upstream
"""
try:
response = requests.get(HAPROXY_CHECK_URL, timeout=1)
response.raise_for_status()
except:
return "down"
ifstate_filename = "/sys/class/net/{}/operstate".format(interface)
if not os.path.exists(ifstate_filename) or open(ifstate_filename).read().strip() != "up":
return "down"
if os.path.exists(ADMIN_DOWN_MARKER):
return "admin"
return "up"
def set_state(new_state):
"""
Sets announced state
"""
if new_state not in ("up", "down", "admin"):
raise ValueError("Invalid new state: {}".format(new_state))
subprocess.call([BFDD_CONTROL, "session", "all", "state", new_state])
def main(logfile, interface):
......@@ -50,14 +18,14 @@ def main(logfile, interface):
logging.info("Process starting")
state = check_state(interface)
set_state(state)
state = bfd.check_state(interface)
bfd.set_state(state)
logging.info("bfd-check starting, initial state: %s", state)
while True:
time.sleep(1)
new_state = check_state(interface)
new_state = bfd.check_state(interface)
if new_state != state:
set_state(new_state)
bfd.set_state(new_state)
logging.info("state changed from %s to %s", state, new_state)
state = new_state
......
import requests
import os.path
import subprocess
ADMIN_DOWN_MARKER = "/etc/admin_down"
HAPROXY_CHECK_URL = "http://localhost:1936/haproxy_up"
BFDD_CONTROL = "/usr/local/bin/bfdd-control"
def check_state(interface):
"""
Checks downstream status to determine which state we should announce upstream
"""
try:
response = requests.get(HAPROXY_CHECK_URL, timeout=1)
response.raise_for_status()
except:
return "down"
ifstate_filename = "/sys/class/net/{}/operstate".format(interface)
if not os.path.exists(ifstate_filename) or open(ifstate_filename).read().strip() != "up":
return "down"
if os.path.exists(ADMIN_DOWN_MARKER):
return "admin"
return "up"
def set_state(new_state):
"""
Sets announced state
"""
if new_state not in ("up", "down", "admin"):
raise ValueError("Invalid new state: {}".format(new_state))
subprocess.call([BFDD_CONTROL, "session", "all", "state", new_state])
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