#!/bin/bash
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# $Header: /var/log/sar/RCS/mem,v 1.4 1997/06/19 05:28:32 dgavin Exp dgavin $
#
# mem - display memory utilization
#
# input - /var/log/sar/data/mem.Mondd.yy
# output- charts for total/free/used, buffer/cache usage and shared memory
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
trap '/bin/rm /tmp/gnuplot.mem* 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 memory utilization."
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: mem,v $
Revision 1.4  1997/06/19 05:28:32  dgavin
cleanup
dgavin

Revision 1.3  1997/06/13 14:26:57  dgavin
cleanup
dgavin

Revision 1.2  1997/06/12 03:45:39  dgavin
removed extra log area

Revision 1.1  1997/06/12 03:40:03  dgavin
Initial revision

'
       exit 1
       ;;
    V) echo '$Revision: 1.4 $'
       exit 1
       ;;
    ?) echo -e "\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="mem.`date +%b%d.%y`"
else
 l=`echo ${1} | wc -c`
 l=`expr ${l} + 0`
 case ${l} in
  6) FN="mem.${1}.`date +%y`";;
  9) FN="mem.${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
#
#HHMM page in page out
#HHMM total:   used:      free:  shared: buffers:  cached:
#0900 21897216 21577728   319488 13410304   245760  5988352
#
awk '
BEGIN{}
{
  if (NR == 1) {
    mm=substr($1,3,2) * .016;
    tim=substr($1,1,2)+mm;
  } else {
    mm=substr($1,3,2) * .016;
    tim=substr($1,1,2)+mm;
    printf("%5.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",tim,
          $2/1024,$3/1024,$4/1024 + $3/1024,$5/1024,
          $6/1024,$7/1024) >"/tmp/gnuplot.mem";
  }
}
END{}' ${FN}
#
# Get system name and date of report into variables...
system=`hostname -s | /usr/bin/tr "[A-Z]" "[a-z]"`
sysdate=`echo ${FN} | cut -c 5-`
echo "Generating plot for $system $sysdate"
#
# Start building the gnuplot.mem.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.mem.ps"'
  PAUSE=""
  YLABEL=""
fi
gnuplot <<EOF
${TERMINAL}
set data style lines
set nogrid
set key
set title "Memory Utilization - $system On $sysdate"
set xlabel "Time of Day"
set xrange [00:24]
set xtics 0,1,24
${YLABEL}
set autoscale y
plot \
"/tmp/gnuplot.mem" using 1:2 title "Total" with line 1, \
"/tmp/gnuplot.mem" using 1:3 title "Used" with line 2
${PAUSE}
plot \
"/tmp/gnuplot.mem" using 1:6 title "Buffers" with line 1, \
"/tmp/gnuplot.mem" using 1:7 title "Cached" with line 2
${PAUSE}
plot \
"/tmp/gnuplot.mem" using 1:5 title "Shared" with line 1
${PAUSE}
quit
EOF
#
if [ .${HARDCOPY} = ".YES" ]; then
  if [ -z ${GHOSTVIEW} ] ; then
    lpr /tmp/gnuplot.mem.ps
  else
    ghostview -upsidedown -landscape -magstep 0 /tmp/gnuplot.mem.ps
    echo "Print ?"
    read ans
    if [ ${ans} = "y" ] ; then
      lpr /tmp/gnuplot.mem.ps
    fi
  fi
fi
# clean up the junk
if [ ${DEBUG}. = "." ]; then
  rm /tmp/gnuplot.mem* 2>/dev/null
fi
exit
