#!/bin/bash

set -e
set -o pipefail

HACRM=$(ngcp-ha-crm)
if [[ "${HACRM}" != 'pacemaker' ]]; then
  echo "warning: crmsh not available, requires pacemaker based clustering" >&2
  exit 0
fi

if [[ -r /etc/default/ngcp-roles ]]; then
  # shellcheck disable=SC1091
  . /etc/default/ngcp-roles
fi

if [[ -z "${NGCP_HOSTNAME}" ]]; then
  echo "error: NGCP_HOSTNAME is not set" >&2
  exit 1
fi
if [[ -z "${NGCP_PEERNAME}" ]]; then
  echo "error: NGCP_PEERNAME is not set" >&2
  exit 1
fi

for r in $(crm resource status |
           sed -e 's/^[[:space:]]*\*[[:space:]]*//' |
           awk '$1 ~ /^p_/ { print $1 }'); do
  echo "${r}:"
  for h in ${NGCP_HOSTNAME} ${NGCP_PEERNAME}; do
    count=$(crm resource failcount "${r}" show "${h}" | sed 's,.*value=,,')
    echo "  ${h}: ${count}"
  done
done
