#!/bin/bash
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# $Header: /var/log/sar/RCS/ctxt,v 1.4 1997/07/20 21:38:14 dgavin Exp $
#
# ctxt - display context switching activity
#
# input - /var/log/sar/data/ctxt.Mondd.yy
# output- chart showing context switches per second
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
trap '/bin/rm /tmp/gnuplot.ctxt* 2>/dev/null;exit' SIGHUP SIGINT SIGQUIT SIGTERM
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
# Function definitions
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
function checkForEmpty {
if [ ! -f ${1} ] ; then echo "0 0" >${1} ; fi
return
}
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
# Mainline code
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
# Variable initialization
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
HARDCOPY=""
GHOSTVIEW=""
CMDNAME=`basename $0`
#
HELP="${CMDNAME} generates X11 or hardcopy graph of context switching."
USAGE="${CMDNAME} [-DHLVhPp] [MonDD[.YY]]
  -D turns on execution tracing
  -H displays help info and exits
  -L displays RCS log and exits
  -V display Revision info and exits
  -h displays help info and exits
  -P Print without display
  -p display and offer print option

  MonDD.YY - day to work with, defaults to today
   i.e. Jun12.97 -the year will default to this year

"
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
# Parse options
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
while getopts DHLVhPp c ; do
  case ${c} in
    h|H) echo -e "\n${HELP}\n\n${USAGE}\n"
       exit 1
       ;;
    p) HARDCOPY="YES"
       GHOSTVIEW="YES"
       ;;
    P) HARDCOPY="YES"
       GHOSTVIEW=""
       ;;
    D) echo "DEBUG ON"
       set -x
       DEBUG="yes"
       trap '' SIGHUP SIGINT SIGQUIT SIGTERM
       ;;
    L) echo '
$Log: ctxt,v $
Revision 1.4  1997/07/20 21:38:14  dgavin
removed psnup - single chart
dgavin

Revision 1.3  1997/06/19 05:26:36  dgavin
cleanup
dgavin

Revision 1.2  1997/06/12 03:43:54  dgavin
removed extra log area

Revision 1.1  1997/06/12 03:36:35  dgavin
Initial revision

'
       exit 1
       ;;
    V) echo '$Revision: 1.4 $'
       exit 1
       ;;
    ?) echo "\n${USAGE}\n"
       exit 3
       ;;
  esac
done
# re-align the positional parameters as $1, $2,...
shift `expr ${OPTIND} - 1`
#
cd /var/log/sar/data
if [ $# -eq 0 ] ; then
 FN="ctxt.`date +%b%d.%y`"
else
 l=`echo ${1} | wc -c`
 l=`expr ${l} + 0`
 case ${l} in
  6) FN="ctxt.${1}.`date +%y`";;
  9) FN="ctxt.${1}";;
  *) echo -e "\n${CMDNAME}: Invalid date  - ${1}\n"
     exit
     ;;
 esac
fi
if [ ! -f ${FN} ] ; then
  echo "No such file: ${FN}"
  exit
fi
if [ ! -r ${FN} ] ; then
  echo "File not readable: ${FN}"
  exit
fi
if [ ! -s ${FN} ] ; then
  echo "File empty: ${FN}"
  exit
fi
#
#0940 8065221
#0945 8068341
#0950 8071218
#0955 8073893
#1000 8076925

awk '
BEGIN{}
{
 if (NR == 1) {
   mm=substr($1,3,2) * .016;
   tim=substr($1,1,2)+mm;
   old_time = (substr($1,1,2) * 3600) + (substr($1,3,2) * 60);
   old_cs = $2;
 } else {
   mm=substr($1,3,2) * .016;
   tim=substr($1,1,2)+mm;
   now = (substr($1,1,2) * 3600) + (substr($1,3,2) * 60);
   delta_tim = now - old_time;
   if(delta_tim < 1 ) {
     delta_tim = 1;
   } 
   if ( old_cs > $2 ) {
     old_cs = 0;
   }
   cs = ($2 - old_cs) / delta_tim;
   printf("%5.2f %7.2f\n",tim, cs ) >"/tmp/gnuplot.ctxt";
   old_cs = $2;
 }
}
END{}' ${FN}
#
# Get system name and date of report into variables...
system=`hostname -s | /usr/bin/tr "[A-Z]" "[a-z]"`
#sysdate=`/bin/date`
sysdate=`echo ${FN} | cut -c 6-`
echo "Generating plot for $system $sysdate"
#
#
# Start building the gnuplot.ctxt.script
#
if [ -z ${HARDCOPY} ]; then
  TERMINAL="set terminal X11"
  PAUSE="pause 10"
  YLABEL=""
else
  TERMINAL='set terminal postscript landscape monochrome dashed "Times-Roman" 18
set output "/tmp/gnuplot.ctxt.ps"'
  PAUSE=""
  YLABEL='set ylabel "Context Switches per second"'
fi
#
# Generate the plots
#
gnuplot <<EOF
$TERMINAL
set data style lines
set nogrid
set key
set title "Context Switches Per Second - $system On $sysdate"
set xlabel "Time of Day"
set xrange [00:24]
set xtics 0,1,24
${YLABEL}
set autoscale y
plot \
"/tmp/gnuplot.ctxt" title "csw/second" with line 1
${PAUSE}
quit
EOF
#
if [ .${HARDCOPY} = ".YES" ]; then
  if [ -z ${GHOSTVIEW} ] ; then
    lpr /tmp/gnuplot.ctxt.ps
  else
    ghostview -upsidedown -magstep 3 /tmp/gnuplot.ctxt.ps
    echo "Print ?"
    read ans
    if [ ${ans} = "y" ] ; then
      lpr /tmp/gnuplot.ctxt.ps
    fi
  fi
fi
# clean up the junk
if [ ${DEBUG}. = "." ]; then
  rm /tmp/gnuplot.ctxt* 2>/dev/null
fi
exit
