#! /usr/bin/env sh

#############################################################################
#
# Usage:
#
#############################################################################

#### Colors definition
BLACK='\033[30;40m'
RED='\033[0;31m'
REDB='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[34;40m'
MAGENTA='\033[0;35m'
CYAN='\033[36;40m'
WHITE='\033[0;37m'
WHITEB='\033[1;37m'
RESET='\033[0m'

## Return the state of processes passed in parameters
# process_info $PROCESS_LIST_TO_MONITOR $MESSAGE
process_info() {
  local PROCESS_LIST="${1}"
  local MSG="${2}"

  for PROCESS in ${PROCESS_LIST}; do
    MSG="${MSG}${MAGENTA}+ "
    if (ps ax | grep -v grep | grep ${PROCESS} > /dev/null); then
      MSG="${MSG}${WHITEB}${PROCESS}${RESET} [ ${GREEN}RUNNING${RESET} ] "
    else
      MSG="${MSG}${WHITEB}${PROCESS}${RESET} [ ${REDB}NOT RUNNING${RESET} ] "
    fi
  done

  printf '%b' "${MSG}"
}

## Return the listening socket
# service_info $PORT_LIST_TO_MONITOR $MESSAGE
service_info() {
  local PORT_LIST="${1}"
  local MSG="${2}"

  for PORT in ${PORT_LIST}; do
    MSG="${MSG}${MAGENTA}+ "
    # If a port listen
    if (ss -lutn|grep -m1 ":${PORT}" > /dev/null); then
      # Example: "tcp/127.0.0.1:25"
      #MSG="${MSG}${GREEN}$(ss -lutn|grep -m1 ":${PORT}"|awk '{print $1"/"$5}')${RESET} "
      MSG="${MSG}${GREEN}$(ss -lutn|grep ${PORT}|sort|head -n1|awk '{print $1"/"$5}')${RESET} "
    else
      # Example: "22: NOT LISTENING"
      MSG="${MSG}${REDB}${PORT}: NOT LISTENING${RESET} "
    fi
  done

  printf '%b' "${MSG}"
}


### IF CEPH
if [ $(command -v ceph) ]; then
  # If a keyring file exists
  if [ -f /etc/ceph/*.keyring ]; then
    printf '%b' "${MAGENTA}++++++++++++++++++++++++ ${WHITE}Ceph${RESET} ${MAGENTA}:++++++++++++++++++++++++${RESET}"
    for keyring in "$(find /etc/ceph -type f -name *.keyring)"; do
      CEPH_USERNAME=$(grep client ${keyring} | sed 's/^\[client.\(.*\)\]/\1/')

      # 1: test health
      CEPH_HEALTH=$(ceph health --id ${CEPH_USERNAME})
      CEPH_HEALTH_OK=$(ceph health --id ${CEPH_USERNAME} | grep -i -- 'health_ok')
      # Determine the color to use
      if [ ${CEPH_HEALTH_OK} ]; then
        CEPH_COLOR=${GREEN}
      else
        CEPH_COLOR=${RED}
      fi
      # Print
      printf '%b' "\n${MAGENTA}+ ${WHITEB}${keyring}: ${CEPH_COLOR}${CEPH_HEALTH}${RESET}"

      # 2: test MON
      # Ensure the host is define as a initial monitor
      if (grep "mon_initial_members.*$(hostname)" /etc/ceph/*.conf > /dev/null); then
        MSG=$(process_info "ceph-mon" '')
        MSG=$(service_info "6789" "${MSG}")
        printf '%b' "\n${MSG}"
      fi

      # 3: test OSD
      CEPH_OSD_OK=$(ceph osd tree --id ${CEPH_USERNAME} | grep -i -- "host $(hostname)" 2> /dev/null)
      if [ "${CEPH_OSD_OK}" ]; then
        MSG=$(process_info "ceph-osd" "${MSG}")
        MSG=$(service_info "6800 6801 6802" "${MSG}")
        printf '%b' "\n${MSG}"
      fi

      # Display end message for this keyring
      printf '%b' "\n${MAGENTA}++++++ ${WHITEB}END - ${keyring}${MAGENTA} ++++++${RESET}"

      # Reset color
      printf '%b' "${RESET}\n"
    done

  # Else use default
  elif [ -f /etc/ceph/ceph.conf ]; then
    printf '%b' "${MAGENTA}++++++++++++++++++++++++ ${WHITE}Ceph${RESET} ${MAGENTA}:++++++++++++++++++++++++${RESET}"

    # 1: test health
    CEPH_HEALTH=$(ceph health)
    CEPH_HEALTH_OK=$(ceph health | grep -i -- 'health_ok')
    # Determine the color to use
    if [ ${CEPH_HEALTH_OK} ]; then
      CEPH_COLOR=${GREEN}
    else
      CEPH_COLOR=${RED}
    fi
    # Print
    printf '%b' "\n${MAGENTA}+ ${CEPH_COLOR}${CEPH_HEALTH}$RESET"

    # 2: test MON
    # Ensure the host is define as a initial monitor
    if (grep "mon_initial_members.*$(hostname)" /etc/ceph/*.conf > /dev/null); then
      MSG=$(process_info "ceph-mon" '')
      MSG=$(service_info "6789" "${MSG}")
      printf '%b' "\n${MSG}"
    fi

    # 3: test OSD
    CEPH_OSD_OK=$(ceph osd tree | grep -i -- "host $(hostname)" 2> /dev/null)
    if [ "${CEPH_OSD_OK}" ]; then
      MSG=$(process_info "ceph-osd" '')
      MSG=$(service_info "6800 6801 6802" "${MSG}")
      printf '%b' "\n${MSG}"
    fi

    # Reset color
    printf '%b' "${RESET}\n"

  fi
fi
### FI CEPH


