: /bin/sh script
#ident	"@(#)cpq.cmds:nic/nicinit	1.1"
#ident	"$Header: $"
########################################################
# Copyright 1992, COMPAQ Computer Corporation
########################################################
#
# Title   : $RCSfile: nic_agent,v $
#
# Version : $Revision: 1.16 $
#
# Date    : $Date: 1993/08/19 21:50:29 $
#
# Author  : $Author: chadw $
#
########################################################
#
# Change Log :
#
#           $Log: nic_agent,v $
# Revision 1.16  1993/08/19  21:50:29  chadw
# Reference PET # 35942. Cleans up any data files left laying around from
# previous boots. This could happen if a user invoked reboot while the agent
# was running. Under certain conditions (removal of hardware for instance),
# this could result in erroneous data being reported to CIM (e.g. data for a
# NIC that had actually been removed from the system. If a data file is
# present, the SMUX peer will assume it's valid and report it to the SNMP
# daemon. This eliminates that possibility.
#
# Revision 1.15  1993/08/04  16:54:22  chadw
# Silenced the start and stop modes when SMUX is not present.
#
# Revision 1.14  1993/08/02  22:57:23  chadw
# Fixed some cosmetic message details.
#
# Revision 1.13  1993/08/02  16:12:15  chadw
# Changed start/stop messages to agree with group consensus.
#
# Revision 1.12  1993/07/30  22:31:04  chadw
# Added nicer title to start and stop messages.
#
# Revision 1.11  1993/07/29  16:47:32  chadw
# Escaped back-quotes around basename call to make expression work right.
#
# Revision 1.10  1993/07/29  16:10:07  chadw
# Fixed script name expression so it works with shutdown scripts (K) as well
# as startup (S) and etc versions.
#
# Revision 1.9  1993/07/29  15:50:01  chadw
# Suppressed "stopped" message from kill_agent at multiuser startup.
#
# Revision 1.8  1993/07/28  19:05:17  chadw
# Added -i option to suppress SMUX stop message and removed message about
# SMUX absence.
#
# Revision 1.7  1993/07/21  14:32:39  chadw
# Added -p in front of poll time argument to agent. Needed for compatibility
# with standard command line argument processing.
#
# Revision 1.6  1993/07/19  19:26:28  chadw
# Changed agenterrs.log file location to /usr/bin/compaq.
#
# Revision 1.5  1993/07/19  15:55:41  chadw
# Ensures registry directory is there before startup.
#
# Revision 1.4  1993/07/16  22:36:00  chadw
# Suppressed some messages from "ps" and ensured the startup script name
# would never include its "rc" directory prefix (S99).
#
# Revision 1.3  1993/07/16  19:41:32  chadw
# Plugged some holes.
# .
#
# Revision 1.2  1993/06/30  16:40:55  chadw
# Changed check for registry directory to check for smux directory, as it
# more accurately represents what we need to run.
#
# Revision 1.1  1993/06/29  21:42:40  chadw
# Initial revision
#
#           $EndLog$
#
########################################################

# is_running

# Detects if a process id is active at a particular moment.
# Usefull for checking if a startup or shutdown actually succeeded.

is_running()
   {
   [ "`/bin/su root -c \"/bin/ps -e 2>/dev/null | sed -n \"/$1/p\"\"`" ] && \
      return 0 || return 1
   }

# alert

# Encapsulates output so we can easily implement "silent" mode where needed.

alert()
   {
   [ "$silent" ] || echo "$1"
   }

# kill_agent

# Attempts to kill the agent using one of three possible methods:

# 1) Gets the pid via a ps on the current kernel . . . or
# 2) Gets the pid via a ps on the previous kernel . . . or
# 3) Gets the pid from a file created by this script . . . or
# 4) Does nothing

kill_agent()
   {
   pid=`/bin/su root -c "/bin/ps -e 2>/dev/null | sed -n \"s/[ ]*\([^ ]*\).* $agent/\1/p\""`
   [ "$pid" ] ||
      {
      pid=`/bin/su root -c "/bin/ps -en /unix.old 2>/dev/null | sed -n \"s/[ ]*\([^ ]*\).* $agent/\1/p\""`
      [ "$pid" ] ||
	 {
	 [ -f /tmp/.${agent}Pid ] &&
	    {
	    pid=`cat /tmp/.${agent}Pid`
	    rm -f /tmp/.${agent}Pid 2>/dev/null
	    } || :
	 }
      }

   # If there's something to kill, does it.

   [ "$pid" ] &&
      {
      # Outputs the "stopping" message only if we're not being invoked at
      # multiuser startup time. This prevents a "stopped"/"started" sequence
      # in the case where a "pid" file was left laying around from a previous
      # agent start (agent terminated with kill -9 for instance).

      [ "`expr \"$script\" : \"\(S99\)$agent\"`" ] || \
	 alert "Stopping the $title process . . . \c"

      kill -15 $pid 2>/dev/null
      sleep 1

      # Outputs the "stopped" message only if we're not being invoked at
      # multiuser startup time. This prevents a "stopped"/"started" sequence
      # in the case where a "pid" file was left laying around from a previous
      # agent start (agent terminated with kill -9 for instance).

      [ "`expr \"$script\" : \"\(S99\)$agent\"`" ] ||
	 {
	 is_running $pid && alert "\07stop failed." || alert "stop succeeded."
	 }
      return 0
      } || :
   return 1
   }

# main

title="Compaq Network Interface Monitor"
script=`basename $0`
agent=nic_agent
cpq_base="/usr/bin/compaq"
log_dir="${cpq_base}"
log="${log_dir}/agenterrs.log"
default_poll=15
min_poll=5
silent=		# We may set this later if we need to be silent.

# Forces at least the start/stop argument.

[ "$1" ] ||
   {
   echo "$agent: usage: $0 {start,stop} [-p poll period (seconds)] [-i]"
   exit 0
   }

# Poll time in seconds is optional.

[ "$2" ] &&
   {
   # Forces the minimum poll time if the one specified is less than
   # the minimum.

   [ $2 -gt $min_poll ] && poll_time=$2 || poll_time=$min_poll
   } || poll_time=$default_poll

# If the Compaq SMUX Peer is not installed, we won't display messages.
# This is handled in the "alert" function.

[ -d ${cpq_base}/smux ] || silent="yes"

case $1 in

   start)
 
      # This assumes that when someone issues agent start, they mean it.
      # It kills the agent if it's running and starts a new one.
      # This is the equivalent of agent stop and agent start, issued
      # back-to-back.

      kill_agent

      # Removes any data monitoring files left laying around.

      rm -rf ${cpq_base}/registry/transmission/dot*
      rm -rf ${cpq_base}/registry/unixware/ifmap*

      # Requires that the Compaq SMUX peer package be installed on the
      # system, since this agent provides no functionality without it
      # and will simply eat up CPU cycles.

      [ -d ${cpq_base}/smux ] &&
	 {
	 # Ensures the full path for the $log file is present.

	 [ -d $log_dir ] || mkdir $log_dir

	 alert "Starting the $title process . . . \c"
	 ${cpq_base}/nic/$agent -p $poll_time -i 1>>$log 2>&1 &
	 pid=$!
	 sleep 1
	 is_running $pid &&
	    {
	    echo $pid >/tmp/.${agent}Pid
	    alert "startup succeeded."
	    } || alert "\07startup failed."
	 } || :
      ;;

   stop)
      kill_agent || alert "$title is not running."
      rm -f /tmp/.${agent}Pid 2>/dev/null
      ;;

   *)
      echo "Usage: $0 [ start | stop ]"
      exit 1
      ;;
esac

