From 12a27195a84c5891ddd6afb072dc4c25fe57d326 Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Mon, 7 Sep 2020 08:01:02 -0400 Subject: [PATCH] Add mis-configured netns detection Change-Id: I904288dddbc0fd607475b0d6d0e4e68c96953a4f --- .../cmd/prometheus/routers_l3_ha.py | 53 +++++++++++++++---- requirements.txt | 3 +- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/openstack_tools/cmd/prometheus/routers_l3_ha.py b/openstack_tools/cmd/prometheus/routers_l3_ha.py index f46fccf..b53f874 100644 --- a/openstack_tools/cmd/prometheus/routers_l3_ha.py +++ b/openstack_tools/cmd/prometheus/routers_l3_ha.py @@ -27,14 +27,46 @@ import time import os import psutil +from pyroute2 import NetNS + + +def get_pid_files(state_path): + """Get all PID files for Keepalived.""" + + ha_conf_dir = os.path.join(state_path, 'ha_confs') + pid_glob = os.path.join(ha_conf_dir, '*.pid.keepalived-vrrp') + return glob.glob(pid_glob) + + +def verify_router_configured(router_id, master): + """Verify is the router is properly configured on the system.""" + + namespace = NetNS('qrouter-%s' % router_id) + + for link in namespace.get_links(): + name = link.get_attr('IFLA_IFNAME') + if name[:3] not in ('qg-', 'qr-'): + continue + + addr = namespace.get_addr(label=name) + + # Router is not master but addresses are configured. + if master == 0 and len(addr) != 0: + return 0 + + # Router is master but addresses are not configured. + if master == 1 and len(addr) == 0: + return 0 + + return 1 def main(): """Entry-point for script.""" parser = argparse.ArgumentParser() - parser.add_argument("--metric", default="node_openstack_l3_router_master", - help="Name of metric") + parser.add_argument("--prefix", default="node_openstack_l3_router", + help="Prefix of metric") parser.add_argument("--state", default="/var/lib/neutron", help="Neutron state path") parser.add_argument("--loop", type=int, help="Loop every N seconds") @@ -42,12 +74,8 @@ def main(): args = parser.parse_args() while True: - ha_conf_dir = os.path.join(args.state, 'ha_confs') - pid_glob = os.path.join(ha_conf_dir, '*.pid.keepalived-vrrp') - pid_files = glob.glob(pid_glob) - output = "" - for pid_file in pid_files: + for pid_file in get_pid_files(args.state): with open(pid_file) as pid_fd: pid = int(pid_fd.read()) @@ -62,8 +90,15 @@ def main(): with open(state_file) as state_fd: master = 1 if 'master' in state_fd.read() else 0 - output += '%s{router_id="%s"} %d\n' % ( - args.metric, + configured = verify_router_configured(router_id, master) + + output += '%s_configured{router_id="%s"} %d\n' % ( + args.prefix, + router_id, + configured + ) + output += '%s_master{router_id="%s"} %d\n' % ( + args.prefix, router_id, master ) diff --git a/requirements.txt b/requirements.txt index 7a760b5..3e027bc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ ovs -psutil \ No newline at end of file +psutil +pyroute2 \ No newline at end of file