#!/bin/sh
# Event script to monitor GlusterFS NFS in a cluster environment
# Source: https://xrsa.net
# Author: Ben Draper
# Email: ben@xrsa.net
# Install Location: /etc/ctdb/events.d/60.glusternfs
#

[ -n "$CTDB_BASE" ] || \
    export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")

. $CTDB_BASE/functions

service_ports="111 2049 38465 38466"

# Placeholder
service_name="glusterfs_nfs"

# Verify GlusterFS Services and running
verify_supporting_services ()
{
    l_ret=0
    /usr/bin/systemctl -q is-active glusterd || {
        echo "Service - glusterd is not running"
        l_ret=1
    }

    /usr/bin/systemctl -q is-active glusterfsd || {
        echo "Service - glusterfsd is not running"
        l_ret=1
    }

    /usr/bin/systemctl -q is-active rpcbind || {
        echo "Service - rpcbind is not running"
        l_ret=1
    }

    if [ $l_ret -eq 1 ]; then
        exit $l_ret
    fi
}

# This will verify all required ports are listening
verify_ports ()
{
    l_ret=0
    for check_port in $service_ports; do
        ctdb_check_tcp_ports $check_port || {
            l_ret=1
        }
    done

    if [ $l_ret -eq 1 ]; then
        exit $l_ret
    fi
}

loadconfig
case "$1" in
    monitor)
        verify_supporting_services
        verify_ports
        update_tickles 2049    
    ;;

    *)
	ctdb_standard_event_handler "$@"
    ;;
esac

exit 0
