#!/bin/ksh

. /opt/platform7/lbin/profile
#. /.kshrc

export TIME=`date "+%b.%d.%y.%H.%M"`

WORKDIR=/home/bmetzger/s/memory
RESULTS_DIR=${WORKDIR}/../results
export JUNK=${WORKDIR}/junk

OUTFILE=${RESULTS_DIR}/memory_all_results
PROBLEMS=${RESULTS_DIR}/memory_check_probs

#integer percent=0    #not used

if [ ! -d $RESULTS_DIR ]; then mkdir $RESULTS_DIR; fi;

if [ ! -f $OUTFILE ]
then 
       print  "Hostname Mb_avail Mb_used Mb_free PCT_used Top_Process Time" >  $OUTFILE
fi

if [ ! -f $PROBLEMS ]
then 
       print  "Hostname Mb_avail Mb_used Mb_free PCT_used Top_Process Time" >  ${PROBLEMS}
fi



#for f in s005
for f in `/opt/platform7/lbin/list_processors -ws`  a7server
do
   print -n  "$f " >> $OUTFILE
   remsh $f -n "/usr/sbin/swapinfo -mta |grep total" > $JUNK
   awk '{printf ("%d %d %d %d", $2, $3, $4, $5) }' < $JUNK >> $OUTFILE

      rcp ${WORKDIR}/kmeminfo $f:/tmp/wm
      remsh $f -n "/tmp/wm/kmeminfo -user > /tmp/wm/kmem.out"
      rcp $f:/tmp/wm/kmem.out ${WORKDIR}
      remsh $f -n "rm /tmp/wm/kmem*"
      big_process=`grep 0x  ${WORKDIR}/kmem.out | head -1 | awk '{print $NF }' `
   print -n " $big_process " >> $OUTFILE
   print  "$TIME" >> $OUTFILE

   sed -e 's/%//' $OUTFILE | tail -1 | read hostname avail used free percent top_process time
   if ((percent > 80))
   then
          print "$hostname $avail $used $free $percent $top_process $time" >> $PROBLEMS
   fi

   rm $JUNK ${WORKDIR}/kmem.out

done


