#!/bin/ksh
#
# $Id: start_nmon_now,v 1.1 2023/03/27 19:59:17 root Exp $
# 
# The following code is Confidential and is covered by the installation license
# (c) Copyright Fortra, LLC. and its group of companies.
# 
#&& starts nmon to run till the end of the day every 5 mins.
# 
#&@ pndcutils.sh is also required
# 
#& An attempt is made to start nmon every 15 minutes if it is not aleady running.
#&% This occurs via crontab at 1, 16, 31, 46 minutes after the hour
#&% In crontab, the -q (quite) option is used.
# 
## start_nmon_now Version 2.1.3
#
# Changes
##### add -c, -s, "-24" options
##### test "is_nmon_running" more
##### notify when creating .01 etc. files
##### option to not start when a current nmon is running (use with -f)
#	verify and expand is_nmon_running  tnm code
#
## Jack Miller 4/18/2009
#       Updated shell to ksh, not sh compliant.
#
## Jack Miller 8/18/2009
#       Added nmon vi topas     
#       Added external data collection
#       
## *** NOTE *** 
# Additional nmon options can be added by modifying the nmonopts variable in 
# the pn.config file (i.e. -t top data on each snap )
#       

invocdir=`dirname $0`  # invocation dir (default for temp/data files, utilities)

# Next line necessary to ensure application run correctly from cron.
cd $invocdir

# Added a time reference file for 0000 today called mpg_today 
# for external data collection process of current data
jdate=`date +%Y%m%d`
touch -t ${jdate}0000 ${invocdir}/mpg_today
if [ ! -s  ${invocdir}/last_cend ]; then
   echo "start_nmon_now `date`" > ${invocdir}/last_cend
   touch -t ${jdate}0000 ${invocdir}/last_cend
fi

# Load the utilities variables
if [ -s $invocdir/pndcutils.sh ] ; then
   . $invocdir/pndcutils.sh
fi

# Load the Performance Navigator configuration options 
if [ -s $invocdir/pn.config ] ; then
   . $invocdir/pn.config
fi


# # Return to the data directory, if specified (may not be needed)
# cd $nmondatadir 

if [ "X${auto_start_nmon}" = "XF" ] ; then
   # Returning 0 even though not starting, because it is intentionally 
   # not started, by having the auto_start_nmon variable set to F
   if [ ! -z "$debug" ] ; then 
      echo "auto_start_nmon = (${auto_start_nmon})" 
   fi   
   exit 0  
fi

export datafile invocdir nmondatadir installdir mpgdatadir

# Next line can be use to check variables
# set > $invocdir/set_start_nmon_now.mpginc

let samplefreqmins=5
# ext_data_collect value is set in pn.config
# External data collection process options are set in pnedcp.config 

function usage {
   echo "USAGE: $0 <options>"
   echo "  where options may be any of:"
   echo "  -f		force start (don't ask, just do it)"
   echo "    	  will not restart an nmon running in the current dir, use kill first"
   echo "  -m #		sample frequency in whole minutes (default: $samplefreqmins)"
   echo "  -d nmondatadir	nmon data directory"
}

function get_start_nmon_now_command {
   phh=`date +%H`
   pmm=`date +%M`
   hh=${phh##0}
   mm=${pmm##0}
   let samplefreqsecs=$samplefreqmins*60
   let ssf=$hh*3600+$mm*60
   let spd=24*3600
   let rsecs=$spd-$ssf
   let nsamples=$rsecs/$samplefreqsecs
   # Only start nmon from mpg directory.
   # Version of nmon that are too old cause problems.
   export nmf
   nmon_start_command="$invocdir/nmon -m $nmondatadir -F $nmf -s$samplefreqsecs -c$nsamples ${nmonopts}"
   echo $nmon_start_command
}

# if the default output file exists, try <file>.01 etc...
function get_available_nmon_outfile {
   typeset nmf=`date +mpg_%Y%m%d.nmon`
   export nmf=${nmondatadir}/$nmf
   if [ -e $nmf ] ; then
      nmft=$nmf
      nmfnum=0
      while [ -e $nmft ] ; do
         let nmfnum=$nmfnum+1
         let nmfext=$nmfnum
         if [ $nmfext -lt 10 ] && [ ${#nmfext} -lt 2 ] ; then nmfext="0$nmfext"; fi
         nmft=$nmf.$nmfext
      done
      export nmf=$nmft
   fi
   echo $nmf
}

function set_echo_opt {
   # set the bash xpg_echo shopt if unset (so echo \c works)
   # ksh works by default
   shell=`basename $SHELL`
   if [ "$shell" = "bash" ] ; then
      shopt xpg_echo > /dev/null 2>&1
      xpge=$?
      if [ "$xpge" = "1" ] ; then # bash, but unset
         shopt -s xpg_echo > /dev/null 2>&1
      fi
   fi
}

function is_nmon_running {
   basename_pwd=`basename $PWD`
   basename_nmondata=`basename $nmondatadir`
   cnm=`ps -efl | grep "mpg_20" | grep "\.nmon" | grep $basename_nmondata`
   running=$?
   tnm=`ps -efl | egrep "\/usr\/bin\/nmon|\/usr\/bin\/topas_nmon|$PWD\/nmon" | grep -v grep | grep $basename_nmondata`
   thisrunning=$?
   if [ "$running" != "0" ] ; then
      cnm="none:"
   else
      if [ "$thisrunning" = "0" ] ; then
         cnm="THIS:$tnm"
      else
         cnm="OTHER:$cnm"
      fi
   fi

   echo $cnm
}




while getopts "efvqm:d:" opt ; do
   case $opt in
	f)	force_start="T" ;;
	v)	verbose="T" ;;
	q)	quiet="T" ;;
	m)	opt_samplefreqminutes=$OPTARG ;;
	d)	opt_nmondatadir=$OPTARG ;;
	*)	usage;  exit ;;
   esac
done
shift `expr $OPTIND - 1`

if [ "X$nmondatadir" = "X" ] ; then
   nmondatadir=$invocdir
fi
if [ "X$opt_nmondatadir" != "X" ] ; then
   nmondatadir=$opt_nmondatadir ;
fi
if [ "X$opt_samplefreqminutes" != "X" ] ; then
   samplefreqmins=$opt_samplefreqminutes ;
fi
set_echo_opt
export nmf=`get_available_nmon_outfile`
nmc=`get_start_nmon_now_command`
running_nmons=`is_nmon_running`
which_nmons=${running_nmons%%:*}
running_nmons_str=${running_nmons#*:}

# Changed behaviour of start_nmon_now to always start the external data collector process 
# script to keep the mpgdata2_current.hostname file up to date
# Will export variable ext_data_collect to have collection scripts record or not record data.
# The snap script will always keep the current data updated.
#
# Performance Navigator External Data Collection Process
# NMON variables to start the collection process.
 # Type of timestamp TIMESTAMP 0 is Tnumber, 1 is timedate
export TIMESTAMP=0
export pnedcp_out="${mpgdatadir}/mpgdata3.$sysname"
export pnedcp_day="${mpgdatadir}/mpgdata3_day.$sysname"
export pnedcp_day_last="${mpgdatadir}/mpgdata3_day_last.$sysname"
export pnedcp_ps="${mpgdatadir}/mpgdataps_${jdate}.$sysname"
export pnedcp_ps_current="${mpgdatadir}/mpgdata5_current.$sysname"
 # Added to allow copying mpg_*.nmon & mpg_*.nmon.## to 
 # mpgdata2_current.hostname
export pnedcp_current="${mpgdatadir}/mpgdata2_current.$sysname"
 # This script is ran to put headers in data.
export NMON_START="${installdir}/pnedcp_start.sh"
 # This script is ran gather snap data.
export NMON_SNAP="${installdir}/pnedcp_snap.sh"
 # This script is ran at the end of gathering data.
export NMON_END="${installdir}/pnedcp_end.sh"
 # Indicates how often external data is collected. 1 every time, 2 every other
unset NMON_ONE_IN 
# export NMON_ONE_IN=1 
export TIMESTAMP NMON_START NMON_SNAP NMON_END NMON_ONE_IN pnedcp_out pnedcp_day pnedcp_current 
PATH=$PATH:/usr/local/mpg

if [ "$ext_data_collect" = "T" ] ; then
   export ext_data_collect="T"
else  # Any answer other than "on" is off 
   export ext_data_collect="F"
fi

if [ "$ext_ps_collect" = "T" ] ; then
   export ext_ps_collect="T"
else  # Any answer other than "on" is off 
   export ext_ps_collect="F"
fi

export=

if [ "$force_start" = "T" ] ; then
   runit="Y"
   fsmsg="starting nmon now with command:\n$nmc"
   if [ "$which_nmons" = "THIS" ] ; then
      if [ "$verbose" = "T" ] ; then
         echo "FOUND CURRENTLY RUNNING nmon PROCESS HERE, not starting another:"
         echo $running_nmons_str
      fi
      fsmsg=""
      runit="N"
   elif  [ "$which_nmons" = "OTHER" ] ; then 
      if [ "$verbose" = "T" ] ; then
         echo "found currently running nmon processes in other locations:"
         echo $running_nmons_str
      fi
   fi
   if [ "$quiet" != "T" ] && [ ! -z "$fsmsg" ] ; then echo $fsmsg ; fi
else
   if [ "$which_nmons" != "none" ] ; then
      echo "FOUND CURRENTLY RUNNING nmon PROCESSES:"
      echo $running_nmons_str
      snprompt="Are you sure you want to start nmon now? \c"
   else
      snprompt="do you want to start nmon now? \c"
   fi
   runit="N"
   echo "command to start nmon now is:"
   echo $nmc
   echo $snprompt
   read answer
   yn=${answer:-"n"}
   if [ "$yn" = "Y" ] ; then runit="Y" ; fi
   if [ "$yn" = "y" ] ; then runit="Y" ; fi
fi
# echo "answer=$answer, yn=$yn, runit=$runit"
# echo '$TIMESTAMP  $NMON_START  $NMON_SNAP  $NMON_END  $NMON_ONE_IN'
# echo "$TIMESTAMP  $NMON_START  $NMON_SNAP  $NMON_END  $NMON_ONE_IN"

if [ "$runit" = "Y" ] ; then
   # echo "Running:   $nmc"
   $nmc
fi
