#!/bin/bash

. /usr/lib/eole/diagnose.sh

#
# Parse the Cluster status
# using crm_mon
#
function _crm_mon_info_get()
{

  crm_mon -1 -n 2>&1 | \
    awk -v len_pf=${len_pf} \
        'BEGIN {
           dateformat = "%s|%s/%s/%s|%s "
           nodeformat = "%s|%s|%s "
           resformat = "%s|%s|%s|%s "
         }
         {
           if (/^Last updated:/) {
             update_print = update_print sprintf(dateformat, "Update", $5, $4, $7, $6)
           }

           if (/^Last change:/) {
             update_print = update_print sprintf(dateformat, "Change", $5, $4, $7, $6)
           }

           if (/^Node/) {
               gsub(":$", "", $2)
               currnode = $2
               if ($3 == "online") {
                 node_print = node_print sprintf(nodeformat, "Noeud", currnode, "OK")
               }
               else {
                 node_print = node_print sprintf(nodeformat, "Noeud", currnode, $3)
               }
           }

           if (/^\t/){
              if ($3 == "Started") {
                 status = "OK"
              }
              else {
                 status = "$3"
              }
              rec_print = rec_print sprintf(resformat, "Ressource",  $1, status, currnode)
           }
         }
         END {
           print node_print update_print rec_print
         }'
}

#
# Show the Nodes status with diagnose format
#
function _show_node_status()
{
  local node=$1
  local state=$2

  printf ".  %${len_pf}s %s => " "Noeud" "${node}"
  if [[ "${state}" == "OK" ]]
  then
    EchoVert "OK"
  else
    EchoRouge "${state}"
  fi
}

#
# Show the update and change dates with diagnose format
#
function _show_dates()
{
  local subject=$1
  local date=$2
  local hour=$3

  printf ".  %${len_pf}s => %s %s\n" "${subject}" "${date}" "${hour}"
}

#
# Show the resources with diagnose format
#
function _show_resource()
{
  local name=${1}
  local state=${2}
  local node=${3}

  printf ".  %${len_pf}s %s => " "Ressource" "${name}"

  if [[ ${state} == "OK" ]]
  then
    EchoVert "${state} (${node})"
  else
    EchoRouge "${state} (${node})"
  fi

}

#
# Print the Cluster status with diagnose formating
#
function _crm_mon_show()
{
  info=$(_crm_mon_info_get)
  for elm in ${info}
  do
    IFS="|" read -a array <<< "${elm}"
    if [[ ${array[0]} == "Noeud" ]]
    then
      _show_node_status ${array[1]} ${array[2]}
    elif [[ ${array[0]} == "Update" ]] || [[ ${array[0]} == "Change" ]]
    then
      _show_dates ${array[0]} ${array[1]} ${array[2]}
    elif [[ ${array[0]} == "Ressource" ]]
    then
      _show_resource ${array[1]} ${array[2]} ${array[3]}
    fi
  done
}

function _check_corosync()
{

  printf ".  %${len_pf}s => " "Service Corosync"

  output=$(crm_mon -1 -n 2>&1)
  if [[ ${?} -ne 0 ]]
  then
    EchoRouge "KO"
    return 10
  else
    EchoVert "OK"
    return 0
  fi
}

activer_haute_dispo=$(CreoleGet activer_haute_dispo 2>/dev/null)

if [ "$activer_haute_dispo" = "maitre" ] || [ "$activer_haute_dispo" == "esclave" ]; then
    echo ""
    EchoGras "*** Haute disponibilité"
    _check_corosync
    [[ ${?} -ne 0 ]] && exit 0
    _crm_mon_show
fi
exit 0
