#!/bin/sh
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2009, 2010, 2012, 2014
# All rights reserved
#
# $Copyright$
# Copyright (C) Fujitsu Siemens Computers GmbH 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
# All rights reserved
#############################################################################
#
# srvmagt_scs:  ServerView Agent Linux Remote Connector Daemon
#
# chkconfig: 2345 58 22
# processname: SVRemoteConnector
# description: Starts and stops the SVRemoteConnector daemon
#
### BEGIN INIT INFO
# Provides: srvmagt_scs
# Required-Start: $local_fs $network $syslog +eecd
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Starts and stops the SVRemoteConnector daemon
### END INIT INFO
#

#
# Exit status codes (see LSB 3 spec, ch. 20) are :
# 0     OK
# 1     generic or unspecified error
# 2     invalid or excess argument(s)
# 3     unimplemented feature (e.g. "reload")
# 4     user had insufficient privileges
# 5     program is not installed
# 6     program is not configured
# 7     program is not running
# 8+    reserved (see LSB Core Spec 3.1, ch. 20)

# Set up a decent path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

# Use LSB function library for convenience.

[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

# Source configuration files and function library
# export SV_SCS_PROVIDERS=<path to subdirectory for providers>
# export SV_SCS_IDDEF_CONFIG=<path to subdirectory for provider XML configuration files>
# export SV_SCS_LOGFILES=<path to provider and daemon log files>

TraceFileLimit=1
[ -f /etc/srvmagt/config ] && . /etc/srvmagt/config
. /etc/fujitsu/ServerViewSuite/SCS/scs.conf
. /opt/fujitsu/ServerViewSuite/SCS/functions

export TraceFileLimit

case "$1" in
start|trace|traceall )
    case "$1" in
    trace|traceall) flags=" -#$2" ;;
    *)              flags= ;;
    esac

    umask 0066

    # Do not restart an already running SVRemoteConnector daemon

    pid=$(mypidof "/opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector") && {
        mylog_success_msg "Starting ServerView SVRemoteConnector (already running $pid)"
        exit 0
    }

    SetSvEnv

    # Start daemon when the needed TCP port #3172 is free
#
#    echo -e "Waiting for TCP port #3172 ..\c"
#    count=99    # If >99 adjust \b's below
#    while [ $count -gt 0 ]; do
#        #LC_ALL=C netstat -ant | egrep -q '[^0-9]3172[^0-9].*LISTEN' || break
#        LC_ALL=C netstat -ant | egrep -q '[^0-9]3172[^0-9]' || break
#        sleep 1
#        count=$(($count - 1))
#        if [ $count -ge 10 ]; then
#            echo -e "\b\b$count\c"
#        else
#            echo -e "\b\b$count \c"
#        fi
#    done
#
#    [ $count -eq 0 ] && {
#        echo " giving up after 99 seconds"
#        mylog_failure_msg "Starting ServerView SVRemoteConnector ${flags}"
#        exit 1
#    }
#    echo -e "\b\bdone ($count)"

    # Enable core dumps if so configured

    [ "$SV_DAEMON_COREFILE_LIMIT" = "" -o "$SV_DAEMON_COREFILE_LIMIT" = 0 ] ||
        ulimit -c $SV_DAEMON_COREFILE_LIMIT

    # On ESX we continue to use IPv4 sockets.

    #[ "$SV_SYSTEM" = "vmware" ] && ipv4flags="-u0.0.0.0"
    # ... no forced restriction to IPv4 for VMware V4 or higher

    # Save logfiles from previous session before clearing them.

    logFiles=$(cd $SV_SCS_LOGFILES; rm -f log.RemoteConnectorLogs.tgz; ls -1 log.* 2> /dev/null) && \
        (cd $SV_SCS_LOGFILES; tar -czf $SV_SCS_LOGFILES/log.RemoteConnectorLogs.tgz $logFiles; rm -f $logFiles)
    > $SV_SCS_LOGFILES/log.SVRemoteConnector
    chmod 600 $SV_SCS_LOGFILES/log.SVRemoteConnector

    export SV_SCS_PROVIDER_TRACE=all

    # This path is needed to catch additional provider libs for SVRemoteConnector
    if [ "$SV_SCS_ARCH" = "64" ]; then
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/fujitsu/ServerViewSuite/SCS/lib64:$SV_SCS_PROVIDERS64:$SV_SCS_PROVIDERS
    elif [ "$SV_ARCH" = "x86_64" -a -z "$SV_SCS_ARCH" ]; then
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/fujitsu/ServerViewSuite/SCS/lib64:$SV_SCS_PROVIDERS
	# SCS V1.30
    else
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/fujitsu/ServerViewSuite/SCS/lib:$SV_SCS_PROVIDERS
    fi 

    export LD_LIBRARY_PATH

    # Start the daemon. This SHOULD use the LSB start_daemon routine.

    /opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector $flags $ipv4flags \
        -ci$SV_SCS_IDDEF_CONFIG \
        -ssl_servcert=/opt/fujitsu/ServerViewSuite/SCS/ssl/server_key.crt \
        -ssl_capath=/opt/fujitsu/ServerViewSuite/SCS/ssl \
        2>$SV_SCS_LOGFILES/log.SVRemoteConnector >&2 </dev/null

    # Show formatted completion status and set subsystem ready

    if [ $? -eq 0 ]; then
        mylog_success_msg "Starting ServerView SVRemoteConnector $flags"
        [ -d /var/lock/subsys ] &&  touch /var/lock/subsys/srvmagt_scs
        exit 0
    else
        mylog_failure_msg "Starting ServerView SVRemoteConnector $flags"
        [ -e /var/lock/subsys/srvmagt_scs ] &&  rm -f /var/lock/subsys/srvmagt_scs
        exit 1
    fi ;;

stop)
    # Do not stop an already stopped SVRemoteConnector daemon

    pid=$(mypidof "/opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector") || {
        mylog_success_msg "Shutting down ServerView SVRemoteConnector (not running)"
        exit 0
    }

    # Stop the daemon. This SHOULD use LSB killproc.
    # Try SIGTERM first and have patience ...; then use SIGKILL
    # and clear susystem ready

    killsig=TERM
    kill -TERM $pid

    echo -e "Shutting down ServerView SVRemoteConnector ..\c"
    count=90
    while [ $count -gt 0 ]; do
        sleep 1
        pid=$(mypidof "/opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector") || break
        count=$(($count - 1))
        if [ $count -ge 10 ]; then
            echo -e "\b\b$count\c"
        else
            echo -e "\b\b$count \c"
        fi
    done
    echo -e "\r\c"

    if [ "$pid" ]; then
        killsig=KILL
        kill -KILL $pid
    fi

    if [ "$killsig" = "TERM" ]; then
        mylog_success_msg "Shutting down ServerView SVRemoteConnector: $killsig"
        [ -e /var/lock/subsys/srvmagt_scs ] &&  rm -f /var/lock/subsys/srvmagt_scs
        exit 0
    else
        mylog_failure_msg "Shutting down ServerView SVRemoteConnector: $killsig"
        [ -e /var/lock/subsys/srvmagt_scs ] &&  rm -f /var/lock/subsys/srvmagt_scs
        exit 1
    fi ;;

force-reload)
    # Reload configuration, if the service supports this,
    # otherwise start the service if it is not running

    pid=$(mypidof /opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector) || $0 start
    exit $? ;;

restart)
    $0 stop
    $0 start
    exit $? ;;

status)
    # Status codes in this case are:
    # 0     service is running [OK]
    # 1     service is dead /var/run/<pid file> exists
    # 2     service is dead and /var/lock/<lock file> ecists
    # 3     service is not running
    # 4     service status is unknown
    # 5+    reserved (see LSB Core Spec 3.1, ch. 20)

    pid=$(mypidof "/opt/fujitsu/ServerViewSuite/SCS/SVRemoteConnector")
    if [ $? -eq 0 ]; then
        statMsg=$(printf "Checking ServerView SVRemoteConnector: pid %s" $pid)
        rc=0
    else
        statMsg=$(printf "Checking ServerView SVRemoteConnector: stopped")
        rc=3
    fi
    echo $statMsg
    logger -t $MYNAME $statMsg
    exit $rc ;;

try-restart|reload|*)
    echo "Usage: $0 {start|trace|stop|restart|force-reload|status}"
    exit 3 ;;
esac

exit 1
# EOF
