From b9905a052fd72aa3742f43cdc7356c252940e138 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20M=C3=A1gr?= <mmagr@redhat.com>
Date: Mon, 14 May 2018 13:27:10 +0200
Subject: [PATCH] Add health check for OVN containers

This patch adds script for ovn_controller and ovn-dbs-bundle containers.
Health is check using verification of connection to OVN DB.

Change-Id: I9c8d0445b7f010838fe94dae2ae6fb86952fdeab
---
 healthcheck/common.sh      | 17 +++++++++++++++--
 healthcheck/ovn-controller | 14 ++++++++++++++
 healthcheck/ovn-dbs        | 13 +++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 healthcheck/ovn-controller
 create mode 100644 healthcheck/ovn-dbs

diff --git a/healthcheck/common.sh b/healthcheck/common.sh
index 85eb9a186..dc83fd5ae 100644
--- a/healthcheck/common.sh
+++ b/healthcheck/common.sh
@@ -14,7 +14,7 @@ healthcheck_port () {
   process=$1
 
   # ss truncate command name to 15 characters and this behaviour
-  # cannot be diabled
+  # cannot be disabled
   if [ ${#process} -gt 15 ] ; then
     process=${process:0:15}
   fi
@@ -29,7 +29,7 @@ healthcheck_listen () {
   process=$1
 
   # ss truncate command name to 15 characters and this behaviour
-  # cannot be diabled
+  # cannot be disabled
   if [ ${#process} -gt 15 ] ; then
     process=${process:0:15}
   fi
@@ -40,6 +40,19 @@ healthcheck_listen () {
   ss -lnp | awk '{print $5,"-",$7}' | egrep ":($ports)" | grep "$process"
 }
 
+healthcheck_socket () {
+  process=$1
+  socket=$2
+
+  # lsof truncate command name to 15 characters and this behaviour
+  # cannot be disabled
+  if [ ${#process} -gt 15 ] ; then
+    process=${process:0:15}
+  fi
+  lsof -Fc -Ua $socket | grep "c$process"
+}
+
+
 get_config_val () {
   crudini --get "$1" "$2" "$3" 2> /dev/null || echo "$4"
 }
diff --git a/healthcheck/ovn-controller b/healthcheck/ovn-controller
new file mode 100644
index 000000000..aa2a005ec
--- /dev/null
+++ b/healthcheck/ovn-controller
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+. ${HEALTHCHECK_SCRIPTS:-/usr/share/openstack-tripleo-common/healthcheck}/common.sh
+
+process='ovn-controller'
+args="${@:-6642}"
+
+if healthcheck_port $process $args; then
+  exit 0
+else
+  ports=${args// /,}
+  echo "There is no $process process connected to ovsdb ports $ports running in the container"
+  exit 1
+fi
diff --git a/healthcheck/ovn-dbs b/healthcheck/ovn-dbs
new file mode 100644
index 000000000..46b78d84d
--- /dev/null
+++ b/healthcheck/ovn-dbs
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+. ${HEALTHCHECK_SCRIPTS:-/usr/share/openstack-tripleo-common/healthcheck}/common.sh
+
+process='ovn-northd'
+sockets="${@:-/run/openvswitch/ovnnb_db.sock /run/openvswitch/ovnsb_db.sock}"
+
+for sock in sockets; do
+    if ! healthcheck_socket $process $sock; then
+        echo "There is no $process process connected to socket $sock running in the container"
+        exit 1
+    fi
+done