Commit 6e28fcbe authored by Sigmund Augdal's avatar Sigmund Augdal

Added a script to monitor state of system and pull down bfd if we are not able...

Added a script to monitor state of system and pull down bfd if we are not able to route as we should
parent f3870c38
#!/usr/bin/env python
import os.path
import requests
import time
import subprocess
import logging
import argparse
from daemonize import Daemonize
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"
def check_state(interface):
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):
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):
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.getLogger("requests").setLevel(logging.WARNING)
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=10*1024**3, backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
state = check_state(interface)
set_state(state)
logging.info("bfd-check starting, initial state: %s", state)
while True:
time.sleep(1)
new_state = check_state(interface)
if new_state != state:
set_state(new_state)
logging.info("state changed from %s to %s", state, new_state)
state = new_state
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:
daemon_main = lambda: main(args.logfile, args.interface)
daemon = Daemonize(app=APP, pid=args.pidfile, action=daemon_main)
daemon.start()
else:
main(None, args.interface)
if __name__ == '__main__':
main()
......@@ -6,7 +6,7 @@ rm -rf pkg/opt
target=pkg/opt/nova_router_services
mkdir -p $target/bin
cp clean_stale.py dhcp_configurator.py iptables_configurator.py neighbors.py $target/bin
cp clean_stale.py dhcp_configurator.py iptables_configurator.py neighbors.py bfd_monitor.py $target/bin
virtualenv $target
source $target/bin/activate
......
#!/bin/sh
### BEGIN INIT INFO
# Provides: nova_bfd_monitor
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: bfd monitor for nova routers
# Description: This file should be used to start and stop bfd monitor on nova routers
### END INIT INFO
NAME=bfd_monitor
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PIDFILE=/var/run/$NAME.pid
EXE=/opt/nova_router_services/bin/$NAME.py
test -x $ETCD || exit 0
if [ -e /etc/default/$NAME ]; then
. /etc/default/$NAME
fi
[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions
die()
{
log_failure_msg $1
exit 1
}
args="-d --pidfile $PIDFILE --interface $INTERFACE"
start()
{
. /opt/nova_router_services/bin/activate
start-stop-daemon --start --pidfile "$PIDFILE" \
--exec $EXE -- $args || return 2
return 0
}
stop()
{
if [ ! -f $PIDFILE ] ; then
# This is a success according to LSB
return 0
fi
start-stop-daemon --stop --pidfile "$PIDFILE"
rm -f $PIDFILE
return 0
}
status()
{
if [ ! -f $PIDFILE ] ; then
# program not running
return 3
fi
for pid in $(cat $PIDFILE) ; do
if ! ps --no-headers p "$pid" | grep $EXE > /dev/null ; then
# program running, bogus pidfile
return 1
fi
done
return 0
}
case "$1" in
start)
log_daemon_msg "Starting $NAME" "$NAME"
start
ret=$?
case "$ret" in
0)
log_end_msg 0
;;
1)
log_end_msg 1
echo "pid file '$PIDFILE' found, $NAME not started."
;;
2)
log_end_msg 1
;;
esac
exit $ret
;;
stop)
log_daemon_msg "Stopping $NAME" "$NAME"
stop
ret=$?
case "$ret" in
0|1)
log_end_msg 0
;;
2)
log_end_msg 1
;;
esac
exit $ret
;;
restart)
log_daemon_msg "Restarting $NAME" "$NAME"
stop
start
ret=$?
case "$ret" in
0)
log_end_msg 0
;;
1)
log_end_msg 1
;;
2)
log_end_msg 1
;;
esac
exit $ret
;;
status)
status
ret=$?
case "$ret" in
0)
echo "$NAME is running."
;;
1)
echo "$NAME dead, but $PIDFILE exists."
;;
*)
echo "$NAME not running."
;;
esac
exit $ret
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|reload|restart|status}"
exit 2
;;
esac
:
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