#!/bin/bash
# Copyright (C) Mellanox Technologies Ltd. 2001-2012.  ALL RIGHTS RESERVED.
#
# This software product is a proprietary product of Mellanox Technologies Ltd.
# (the "Company") and all right, title, and interest in and to the software product,
# including all associated intellectual property rights, are and shall
# remain exclusively with the Company.
#
# This software product is governed by the End User License Agreement
# provided with the software product.

### BEGIN INIT INFO
# Provides:       fbcollectord
# Required-Start: openibd
# Required-Stop:
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Start the fabric collector
### END INIT INFO


common_fb_collector=/opt/mellanox/fabric_collector/sbin/common_fb_collector

if [ -f $common_fb_collector ]; then
    . $common_fb_collector
else
    log_msg "ERROR: $common_fb_collector doesn't exist"
    exit 1
fi

if [ ! -f $fb_collector_conf_file ]; then
    log_msg "ERROR: $fb_collector_conf_file doesn't exist"
    exit 1
fi

fb_collector_dir=$fb_collector_base_dir/fb_collector
fb_collector=$fb_collector_dir/fb_collector.pyo
fb_collector_log=/var/log/fb_collector.log
fb_collector_run_dir=/var/run/fabric_collector
ibpm_exe=$fb_collector_base_dir/sbin/ibpm
ibpm_conf_file=$fb_collector_run_dir/records.conf
python_exe=`get_python_exe`


export_ibpath() {
    local ibpath

    ibutils_dir=`get_ibutils_dir`
    if [ -n "$ibutils_dir" ]; then
        ibpath=$ibutils_dir
    else
        ibpath=${IBPATH:=/usr/sbin}
    fi
    export IBPATH=$ibpath
}

check_fabric_health_on_startup() {
    ibstat_out=`$IBPATH/ibstat`
    if [ -z "$ibstat_out" ]; then
        log_msg "No InfiniBand connectivity - Existing"
        return 1
    fi

    n=1
    while [ $n -le 6 ]; do
        ib0guid=`ip a s ib0 | awk '/infiniband/{gsub(":","",$2); print "0x" substr($2,25)}'`
        if $IBPATH/ibstat | grep -B 6 ${ib0guid} | grep LinkUp > /dev/null; then
            [ $n -gt 1 ] && echo "InfiniBand Port 1 is up"
            break
        fi
        echo "Waiting for InfiniBand Port 1 to be up..."
        sleep 10
        n=$(( n+1 ))
    done
    if [ $n -eq 7 ]; then
        log_msg "InfiniBand Port 1 is down - Exiting"
        return 1
    fi

    return 0
}

is_site_name_valid() {
    site_name=`get_site_name`
    if [ -z "$site_name" ]; then
        log_msg "Site name is not specified. Please insert a site name in configuration file."
        return 1
    elif [ "$site_name" == "IBSite" ]; then
        log_msg "Default site name 'IBSite' is no longer allowed. Please insert another site name in configuration file."
        return 1
    fi
    return 0
}

create_runtime_dir() {
    mkdir -p $fb_collector_run_dir &>/dev/null
    if [ $? -ne 0 ]; then
        log_msg "Failed to create the runtime directory $fb_collector_run_dir"
        return 1
    fi
    return 0
}

start() {
    pid=`ps -ef | grep $fb_collector | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]; then
        echo "Fabric collector is already running."
        return 0
    fi

    nohup env PYTHONPATH=$fb_collector_dir/common $python_exe -O $fb_collector >> $fb_collector_log 2>&1 &
    status=$?
    if [ $status -eq 0 ]; then
        echo "Fabric collector has started."

        echo "=======================================================================================" >> $fb_collector_log
        echo " Fabric collector has started on `hostname` at `date` " >> $fb_collector_log
        echo "=======================================================================================" >> $fb_collector_log
    fi
    return $status
}

stop() {
    fb_collector_pid=`ps -ef | grep $fb_collector | grep -v grep | awk '{print $2}'`
    if [ -z "$fb_collector_pid" ]; then
        echo "Fabric collector is not running."
        return 0
    fi

    ibpm_pid=`pgrep -f $ibpm_exe.*$ibpm_conf_file`
    kill -9 $fb_collector_pid $ibpm_pid &> /dev/null
    status=$?
    if [ $status -eq 0 ]; then
        echo "Fabric collector has stopped."

        echo "=======================================================================================" >> $fb_collector_log
        echo " Fabric collector has stopped on `hostname` at `date` " >> $fb_collector_log
        echo "=======================================================================================" >> $fb_collector_log
    fi
    return $status
}

status() {
    pid=`ps -ef | grep $fb_collector | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]; then
        echo "Fabric collector is running (pid=$pid)."
    else
        echo "Fabric collector is not running."
    fi

    return 0
}

log_msg "fbcollectord $1"

case "$1" in
    start)
        create_runtime_dir || exit 1
        is_site_name_valid || exit 1
        export_ibpath
        check_fabric_health_on_startup || exit 1
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
        *)
        echo $"Usage: $0 {start|stop|status}"
        exit 1
        ;;
esac

exit $?
