#! /bin/sh
#
# Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston MA 02111-1307, USA.
# 
# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
# Mountain View, CA  94043, or:
# 
# http://www.sgi.com 
# 
# For further information regarding this notice, see: 
# 
# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
# Start or Stop the Performance Co-Pilot Inference Engine
#
# $Id: etc_init.d_pmie,v 1.4 2003/06/20 20:37:37 kenmcd Exp $
#
# chkconfig: 2345 95 05
# description: pmie is a performance monitoring inference engine

# If a TRIX MAC label is defined, run pcp stuff at that MAC label
#
if [ -x /usr/sbin/sysconf ] && [ `/usr/sbin/sysconf MAC` = "1" ]
then
    if [ ! -n "$PCP_MAC_LABEL" ]
    then
        if [ -f /etc/pcp.maclabel ]
        then
            . /etc/pcp.maclabel
        else
            PCP_MAC_LABEL=dbadmin
        fi
    fi

    _label=` id -M | cut -d' ' -f3 | sed -e 's/.*(//' -e 's/)$//'`


    if [ "$PCP_MAC_LABEL" != "$_label" ]
    then
        if [ "$2" == "trixbox" ]
        then
            echo "$0 : Failed to launch with correct label"
            exit 1
        fi

        /sbin/suattr -M $PCP_MAC_LABEL -c "$0 $1 trixbox > /var/pcp/etc/init.d_pmie.out 2>&1"
        /sbin/suattr -C CAP_MAC_READ+ep -c "cat /var/pcp/etc/init.d_pmie.out"
        exit 0
    fi
fi

# source the PCP configuration environment variables
. /etc/pcp.env

# Get the portable PCP rc script functions
if [ -f $PCP_SHARE_DIR/lib/rc-proc.sh ] ; then
    . $PCP_SHARE_DIR/lib/rc-proc.sh
fi

PMIE=pmie
PMIECTRL=$PCP_VAR_DIR/config/pmie/control
prog=$PCP_RC_DIR/`basename $0`
MAIL=Mail

tmp=/var/tmp/$$
status=1
trap "rm -f $tmp.* ; exit \$status" 0 1 2 3 15

if [ "$PCP_PLATFORM" = "linux" ]
then
    ECHO=echo			# Linux does not have verbose flags
    IS_ON=is_chkconfig_on
    RUNDIR=$PCP_LOG_DIR/pmie
else
    IS_ON=/sbin/chkconfig
    RUNDIR=${PCP_LOGDIR:-/var/adm/pmielog}
    if $IS_ON verbose
    then			# For a verbose startup and shutdown
	ECHO=echo
    else			# For a quiet startup and shutdown
	ECHO=:
    fi
fi

# determine real name for localhost
#
LOCALHOSTNAME=`hostname`
if [ -z "$LOCALHOSTNAME" ] ; then
    LOCALHOSTNAME="localhost"
    [ -x /usr/bsd/hostname ] && LOCALHOSTNAME=`/usr/bsd/hostname`
fi
LOGDIR=$RUNDIR/$LOCALHOSTNAME

id=`id | sed -e "s/(.*//" -e "s/.*=//"`
if [ "$id" != 0 ]
then
    echo "$prog:"'
Error: You must be root (uid 0) to start or stop pmie.'
    exit
fi

_reboot_setup()
{
    # base directories and house-keeping for pmie instances
    #
    if [ ! -d $PCP_TMP_DIR/pmie ]
    then
	mkdir -p $PCP_TMP_DIR/pmie
    else
	rm -rf $tmp.ent $tmp.pid
	here=`pwd`
	cd $PCP_TMP_DIR/pmie
	ps -ef | grep pmie | grep -v grep | $PCP_AWK_PROG '{ print $2 }' \
	    | sed -e 's/[ 	]*//g' | sort >$tmp.pid
	ls [0-9]* 2>&1 | sed -e '/\[0-9]\*/d' \
	    | sed -e 's/[ 	][ 	]*//g' | sort >$tmp.ent
	# remove entries without a pmie process
	rm -f `comm -23 $tmp.ent $tmp.pid`
	rm -f $tmp.ent $tmp.pid
	cd $here
    fi
    chmod 1777 $PCP_TMP_DIR/pmie

    if [ -x /sbin/suattr ]
    then
    # Obtain capabilities since /var/adm is labeled dblow and owned by adm under TRIX
        [ ! -d $LOGDIR ] && /sbin/suattr -C CAP_MAC_WRITE,CAP_DAC_WRITE+ep -c "mkdir -p $LOGDIR"
    else
        [ ! -d $LOGDIR ] && mkdir -p $LOGDIR
    fi

}

_start_pmie()
{
    wait_option=''
    [ ! -z "$PMCD_WAIT_TIMEOUT" ] && wait_option="-t $PMCD_WAIT_TIMEOUT" 

    if pmcd_wait $wait_option
    then
	pmie_check >$tmp.pmie 2>&1
	if [ -s $tmp.pmie ]
	then
	    pmpost "pmie_check start failed in $prog, mailing output to root"
	    $MAIL -s "pmie_check start failed in $prog" root <$tmp.pmie >/dev/null 2>&1
	fi
	rm -f $tmp.pmie
    else
	status=$?
	pmpost "pmcd_wait failed in $prog: exit status: $status" 
	echo "pmcd_wait: exit status: $status" | $MAIL -s "pmcd_wait failed in $prog" root
    fi
}

_stop_pmie()
{
    $ECHO "Waiting for pmie process(es) to terminate ..."

    pmie_check -s >$tmp.pmie 2>&1
    if [ -s $tmp.pmie ]
    then
	pmpost "pmie_check stop failed in $prog, mailing output to root"
	$MAIL -s "pmie_check stop failed in $prog" root <$tmp.pmie
    fi
    rm -fr $tmp.pmie $PCP_TMP_DIR/pmie
}


case "$1" in
  'start')
	ps -ef | grep "$PMIE" | grep -v grep >$tmp.pmies 2>&1
	[ -s $tmp.pmies ] && _stop_pmie

	# messages should go to stderr, not the GUI notifiers
	#
	unset PCP_STDERR

	_reboot_setup

	if which $PMIE >/dev/null 2>&1
	then
	    if $IS_ON pmie
	    then
		[ ! -d "$RUNDIR" ] && mkdir -p "$RUNDIR"
		cd $RUNDIR
		if which pmie_check >/dev/null 2>&1
		then
		    if [ ! -f $PMIECTRL ]
		    then
			echo "$prog:"'
Error: PCP inference engine control file '$PMIECTRL'
       is missing!  Cannot start any Performance Co-Pilot inference engine(s).'
		    else
			$ECHO "Performance Co-Pilot starting inference engine(s) ..."
			_start_pmie &
		    fi
		elif [ $PCP_PLATFORM = "irix" ]
		then
		    PMIECHECK=/usr/pcp/bin/pmie_check
		    echo "$prog:"'
Warning: Performance Co-Pilot installation is incomplete (at least the
	 script '$PMIECHECK' is missing) and the PCP Inference
	 Engine(s) cannot be started.
	 You should re-install the pcp_eoe.sw.eoe subsystem from the IRIX
	 distribution and consult pmie_check(1), else
		# /sbin/chkconfig pmie off'
		fi
	    else
		if [ "$0" = "$PCP_RC_DIR/pmie" ]
		then
		    if [ "$PCP_PLATFORM" = "irix" ]
		    then
			echo "$prog:"'
Warning: Performance Co-Pilot Inference Engine (pmie) is disabled.  To enable:
	 # /sbin/chkconfig pmie on'
		    else
			echo "$prog: Warning: Performance Co-Pilot Inference Engine (pmie) is disabled."
			chkconfig_on_msg pmie
		    fi
		fi
	    fi
	fi
	status=0
        ;;

  'stop')
	_stop_pmie
	status=0
        ;;

  *)
        echo "Usage: $PCP_RC_DIR/pmie {start|stop}"
        ;;
esac

