Home Site Map Search Contact Us About Us About Us
Copyright © 2000
UNIX Script


#H @(#)  v1.0.1  G2 System Startup                     Aug 30, 1994
#H NAME
#H      xg2fire
#H
#H DESCRIPTION
#H      Script to start, stop or check a G2 application/instance.
#H
#H SYNOPSIS
#H      xg2fire <Application> <Run_Mode> Mail_Mode> [Display] [X Server]
#H           <Application> -  a section name in g2config.ini
#H           <Run_Mode>    -  operation mode (start | stop | check)
#H           <Mail_Mode>   -  mail mode (mail | nomail)
#H           [Display]     - if run from X-Vision, will be -display
#H           [X Server]    - is an X-Windows server in /etc/hosts
#H
#H      This script can also be used to list the applications in
#H      g2config.ini by executing it as follows:
#H
#H      xg2fire list
#H
#H RETURNS
#H      The following error codes are returned
#H           1 - Problem with the G2 configuration file
#H           2 - Bad Run_Mode
#H           3 - Bad Mail_Mode
#H           4 - User is not root
#H           5 - Path not set up correctly
#H           6 - Critical parameter bad or not found in G2 configuration file
#H           7 - Bad or missing service spec in /etc/services
#H           8 - G2 process already running
#H           9 - Could not start G2 process
#H          10 - Wrong host for running this application
#H          11 - X-Server requested is not in /etc/hosts
#H          12 - Operating system version not supported
#H
#H SEE ALSO
#H      g2 doc, cciconfv doc
#H
#H AUTHOR
#H      James Bischoff
#R
#R REVISION
#R      v1.0.0  James Bischoff                 Aug 18, 1994
#R              Initial Release
#R      v1.0.1  James Bischoff                 Aug 30, 1994
#R              Add quotes to echo in GetParam function
#H -----------------------------------------------------------------------

#
#A      find this script with which
#A      If the number of command line args is < 1 or the first one is -?
#A          echo this scripts header to stdout
#
ScriptSpec=`which $0`
if [ $# -lt 1 -o "$1" = "-?" ]; then
  ( echo; grep "^#H" ${ScriptSpec}; echo ) | more
  exit 0
fi

#
#A      Set up global variables
#
CfgPath=/home/udata/g2config.ini         #Path to config file
PrgName=`basename $0`                    #This scripts name
OSVer=`uname -r | cut -f1,2 -d "."`      #Operating system version
RetVal=                                  #Used to check error levels
AppName=$1                               #Application - see parameter 1
RunMode=$2                               #Run_Mode - see parameter 2
MailMode=$3                              #Mail_Mode - see parameter 3
DisplaySpec=$4                           #Display - see parameter 4
DisplayHost=$5                           #X Server host - see parameter 5
HostName=                                #Host to run g2 application
G2Path=                                  #Path containing g2 executable
G2okPath=                                #Path containing g2.ok
G2LockDir=                               #Path for lock and log files
G2ServiceName=                           #Service name in /etc/services
G2Rgn1Lmt=                               #Region 1 memory allocation
G2Rgn2Lmt=                               #Region 2 memory allocation
G2AppDir=                                #Directory with G2 kb's
G2Appkb=                                 #Top level kb to load
G2Opt=                                   #Other G2 options

#
#A      Uncomment the set -x line to turn on debug mode
#
# set -x

# -----------------------------------------------------------------------

#
#F      Function to issue an error message to stdout and the mail file
#F
#F      ErrMesg
#F                error message
#
ErrMesg ()
{
#
#A      Output message to stdout and mail file (/dev/null for nomail)
#
   echo "${PrgName} - ERROR : $*" | tee -a ${MailFile}
}

#
#F      Function to obtain a parameter from the G2 configuration file
#F      The entry's value is echoed to stdout
#F
#F      GetParam
#F                      The entry whose value is to be obtained
#
GetParam ()
{
#
#A      Get entry's value from the application's section
#A      If the entry is not in the application's section
#A           then try the default section
#A      Echo the entry's value to stdout
#
   Entry=$1
   EntryValue=`cciconfv ${AppName} ${Entry} -f ${CfgPath}`
   if [ $? -eq 4 ]; then
     EntryValue=`cciconfv default ${Entry} -f ${CfgPath}`
   fi

   echo "${EntryValue}"
}

#
#F      Function to test if a g2 process is running with specified socket
#F      If the process is running, it's PID is echoed to stdout
#F
#F      GetG2Pid
#F               service socket ID
#
GetG2Pid ()
{
#
#A       Make sure OS supported
#
   case ${OSVer} in
      5.3) PROCpid=`ps -ef | grep -v "grep" | grep "tcpport $1" | awk '{print $2}'`
           ;;
      4.1) PROCpid=`ps -ax | grep -v "grep" | grep "tcpport $1" | awk '{print $1}'`
           ;;
      *)   return 12
           ;;
   esac  

#
#A      grep on the program and the socket number and retrieve PID
#A      if PID defined, echo to stdout
#
   if [ -z "${PROCpid}" ]; then
     return 2
   else
     echo ${PROCpid}
   fi
}

#
#F      Function to lookup socket ID from service name
#F      Socket ID is echoed to stdout
#F
#F      LookupSocket
#F                logical service name
#F                                must be defined in /etc/services !
#
LookupSocket ()
{
#
#A      Get and check services spec from /etc/services
#A      Get and check the socket number
#A      Echo the socket number to stdout
#
   SVCname=$1
   SVCspec=`grep "^${SVCname}" /etc/services`
   if [ -z "${SVCspec}" ]; then
     ErrMesg "Missing Service Name [${SVCname}] in /etc/services"
     return 7
   fi

   set ${SVCspec}
   SVCsocket=`echo $2 | cut -f1 -d "/"`
   if [ -z "${SVCsocket}" ]; then
     ErrMesg "Invalid Service Definition for [${SVCname}] in /etc/services"
     return 7
   fi

   echo ${SVCsocket}
   return 0
}

#
#F      Function to start a G2 instance
#F
#F      StartG2
#
StartG2 ()
{
#
#A      Lookup and check socket from service name
#A      Verify that G2 process is not running
#
   SVCsocket=`LookupSocket ${G2ServiceName}`
   RetVal=$?
   if [ ${RetVal} -ne 0 ]; then
     return ${RetVal}
   fi

   GetG2Pid ${SVCsocket} >/dev/null
   RetVal=$?
   if [ ${RetVal} -eq 12 ]; then
     return ${RetVal}
   fi
   if [ ${RetVal} -eq 0 ]; then
     ErrMesg "Process g2 is already running for ${AppName} at ${SVCsocket}"
     return 8
   fi

#
#A      Remove any old lock file and log file for this service
#A      Change to the application directory
#A      Start G2 process
#A      Sleep 30 seconds
#A      Verify that G2 process is running now
#A      Save process PID in lock file
#

   G2base=${G2LockDir}/${AppName}
   rm ${G2base}.lo? >/dev/null 2>&1

   cd ${G2AppDir}
   ${G2Path}/g2 -kb ${G2Appkb} -tcpport ${SVCsocket}\
        ${DisplaySpec} ${DisplayHost}\
        -rgn1lmt ${G2Rgn1Lmt} -rgn2lmt ${G2Rgn2Lmt}\
        ${G2Opt} -tcpipexact -ok ${G2okPath}/g2.ok > ${G2base}.log 2>&1 &
   sleep 30

   G2pid=`GetG2Pid ${SVCsocket}`
   RetVal=$?
   if [ ${RetVal} -eq 12 ]; then
     return ${RetVal}
   fi
   if [ ${RetVal} -ne 0 ]; then
     ErrMesg "Starting process g2 for ${AppName} at ${SVCsocket} failed"
     return 9
   fi

   echo ${G2pid} > ${G2base}.lok
   echo "G2 instance started for [${AppName}]"
   return 0
}

#
#F      Function to stop a G2 instance
#F
#F      StopG2
#
StopG2 ()
{
#
#A      Lookup and check socket from service name
#A      Verify that G2 process is running
#
   SVCsocket=`LookupSocket ${G2ServiceName}`
   RetVal=$?
   if [ ${RetVal} -ne 0 ]; then
     return ${RetVal}
   fi

   G2pid=`GetG2Pid ${SVCsocket}`
   RetVal=$?
   if [ ${RetVal} -eq 12 ]; then
     return ${RetVal}
   fi
   if [ ${RetVal} -eq 0 ]; then
     kill -9 ${G2pid}
   fi

   G2base=${G2LockDir}/${AppName}
   rm ${G2base}.lok >/dev/null 2>&1
   echo "G2 instance stopped for [${AppName}]"
   return 0
}

#
#F      Function to check a G2 instance
#F      Returns 9999 if process is not running but should be
#F
#F      CheckG2
#
CheckG2 ()
{
#
#A      Lookup and check socket from service name
#A      Verify that G2 process is running
#
   SVCsocket=`LookupSocket ${G2ServiceName}`
   RetVal=$?
   if [ ${RetVal} -ne 0 ]; then
     return ${RetVal}
   fi

   GetG2Pid ${SVCsocket} >/dev/null
   RetVal=$?
   if [ ${RetVal} -eq 12 ]; then
     return ${RetVal}
   fi
   if [ ${RetVal} -eq 0 ]; then
     echo "The g2 instance for [${AppName}] is up."
     return 0
   fi

#
#A      Process is not running; check if it should be
#A      It should be running, if the lock file exists
#
   G2base=${G2LockDir}/${AppName}
   if [ -s ${G2base}.lok ]; then
     ErrMesg "Missing process g2 for ${AppName} at ${SVCsocket}"
     return 9999
   fi

   echo "The g2 instance for [${AppName}] is down."
   return 0
}

#
#F      Function to obtain G2 parameters from the G2 configuration file
#F
#F      SetVars
#
SetVars ()
{
#
#A      Get host name
#
   HostName=`GetParam HostName`
   if [ -z ${HostName} ]; then
     ErrMesg "A value for [HostName] could not be found in [${CfgPath}]"
     return 1
   fi

#
#A      Get path to g2
#A      Check to see if path contains g2 executable
#
   G2Path=`GetParam G2Path`
   if [ ! -x ${G2Path}/g2 ]; then
     ErrMesg "The directory [${G2Path}] does not contain the g2 executable."
     return 1
   fi

#
#A      Get path to g2.ok
#A      Check to see if path contains G2.OK
#
   G2okPath=`GetParam G2okPath`
   if [ ! -s ${G2okPath}/g2.ok ]; then
     ErrMesg "The directory [${G2okPath}] does not contain g2.ok."
     return 1
   fi

#
#A      Get path for lock dir
#A      Check to see if it is a directory
#
   G2LockDir=`GetParam G2LockDir`
   if [ ! -d ${G2LockDir} ]; then
     ErrMesg "The lock/log file directory [${G2LockDir}] does not exist."
     return 1
   fi

#
#A      Get service name for this application
#A      Check to see if value is non-blank
#
   G2ServiceName=`GetParam G2ServiceName`
   if [ -z "${G2ServiceName}" ]; then
     ErrMesg "A value for [G2ServiceName] could not be found in [${CfgPath}]"
     return 1
   fi

#
#A      Get memory allocation parameters
#A      Check to see that values are non-blank
#
   G2Rgn1Lmt=`GetParam G2Rgn1Lmt`
   if [ -z "${G2Rgn1Lmt}" ]; then
     ErrMesg "A value for [G2Rgn1Lmt] could not be found in [${CfgPath}]"
     return 1
   fi
   G2Rgn2Lmt=`GetParam G2Rgn2Lmt`
   if [ -z "${G2Rgn2Lmt}" ]; then
     ErrMesg "A value for [G2Rgn2Lmt] could not be found in [${CfgPath}]"
     return 1
   fi

#
#A      Get application directiry
#A      Check to see if directory exists
#
   G2AppDir=`GetParam G2AppDir`
   if [ ! -d ${G2AppDir} ]; then
     ErrMesg "The application directory [${G2AppDir}] does not exist."
     return 1
   fi

#
#A      Get value for application KB
#A      If a value was obtained then
#A          Check to see if the KB exists in G2AppDir
#
   G2Appkb=`GetParam G2Appkb`
   if [ -n "${G2Appkb}" ]; then
     if [ ! -s ${G2AppDir}/${G2Appkb} ]; then
       return 1
     fi
   fi

#
#A      Get additional G2 options
#
   G2Opt=`GetParam G2Opt`

   return 0
}


# -----------------------------------------------------------------------
#
#F      Main function
#

#
#A      if the first arg is list then send all section names
#A      in g2config.ini to stdout and exit
#
if [ ${AppName} = list ]; then
  ( echo; grep "^\[" ${CfgPath} | grep -v "^\[default" ; echo ) | more
  exit 0
fi

#
#A      Verify the g2 config.ini file and the required section exist
#
cciconfv ${AppName} G2ServiceName -f ${CfgPath} > /dev/null
RetVal=$?
case ${RetVal} in
  4|3)
     ErrMesg "Entry [G2ServiceName] for section [${AppName}] does not exist in [${CfgPath}]."
     exit 1
     ;;
  2)
     ErrMesg "The G2 configuration file [${CfgPath}] was not found."
     exit 1
     ;;
  *)
     ;;
esac

#
#A      Validate the Run_Mode
#

case ${RunMode} in
  start|stop|check)
     ;;
  *) ErrMesg "Invalid run mode [${RunMode}].  Use start, stop or check."
     exit 2
     ;;
esac

#
#A      Set up the mail file.  Either /dev/null or a file
#
case ${MailMode} in
  mail)
     MailFile=/tmp/g2mon.$$
     ;;
  nomail)
     MailFile=/dev/null
     ;;
  *) ErrMesg "Invalid mail mode [${MailMode}].  Use mail or nomail."
     exit 3
     ;;
esac

#
#A      Verify user is root
#
id | cut -f 1 -d " " | grep "root" > /dev/null
if [ $? -ne 0 ]; then
  ErrMesg "User id is not [root]"
  exit 4
fi

#
#A      Verify that PATH has been set properly
#
echo ${PATH} | grep g2 > /dev/null
if [ $? -ne 0 ]; then
  ErrMesg "G2 is not in the path"
  exit 5
fi

#
#A      Get G2 parameters from G2 configuration file
#
SetVars
if [ $? -ne 0 ]; then
  exit 6
fi

#
#A      Make sure this machine is the right host
#
ScriptHost=`uname -a | cut -f2 -d " "`
if [ "${ScriptHost}" != "${HostName}" ]; then
  ErrMesg "[${AppName}] should be run on [${HostName}] not [${ScriptHost}]!"
  exit 10
fi

#
#A      If display was specified then
#A         check to see if X Server in /etc/hosts
#
if [ -n "${DisplaySpec}" ]; then
  DisplayHostName=`echo ${DisplayHost} | cut -f1 -d ":"`
  grep -w "${DisplayHostName}" /etc/hosts > /dev/null
  if [ $? -ne 0 ]; then
    ErrMesg "The requested X-Server [${DisplayHostName}] is not in /etc/hosts"
    exit 11
  fi
fi

#
#A      Perform the requested operation
#
case ${RunMode} in
  start) StartG2
         RetVal=$?
	 ;;
  stop)  StopG2
         RetVal=$?
	 ;;
  check) CheckG2
         RetVal=$?
	 if [ ${RetVal} -eq 9999 ]; then
	   StartG2
           RetVal=$?
	   if [ ${RetVal} -eq 0 ]; then
	     echo "${PrgName} - Restarted process G2 for ${AppName}" | tee -a ${MailFile}
	   fi
	 fi
	 ;;
esac

#
#A      If a mail file exists
#A        move it to the sysmail request area
#
if [ -s ${MailFile} ]; then
  mv ${MailFile} ${SN_BASEDIR}/sysmail/request
fi

#
#       Report unsupported OS problems
#
if [ ${RetVal} -eq 12 ]; then
  ErrMesg "The OS Version [${OSVer}] is not supported"
fi

exit ${RetVal}