#!/bin/sh
echo '<<<glusterfs>>>'

inpath() {
    command -v "${1:?No command to test}" >/dev/null 2>&1
}

systemd_service() {
  systemctl is-active --quiet "${1?No service to check for}"
}

_command() {
  echo "# $*"
  "$@"
}

if ! inpath gluster; then
  echo 1 "gluster not installed"
  exit 0
fi

if ! inpath systemctl; then
  echo 1 "systemctl not installed"
  exit 0
fi

if ! systemd_service glusterd; then
  echo 1 "glusterd is not active"
  exit 0
fi

echo 0 ok

_command gluster pool list
_command gluster peer status

echo "# gluster volume list"
if ! VOL_LIST=$(gluster volume list); then
  echo "gluster volume list failed"
  exit 0
fi

printf '%s\n' "$VOL_LIST" | while IFS= read -r name; do
  [ -n "$name" ] || continue
  [ "$name" != "No volumes present" ] || continue
  case "$name" in
    *[![:alnum:]_.-]*)
      echo "# Ignoring invalid volume name: $name"
      continue
      ;;
  esac
  _command gluster volume info "$name"
  _command gluster volume heal "$name" info
  _command gluster volume heal "$name" info split-brain
  _command gluster volume rebalance "$name" status
done
