#!/bin/sh # # Quick and simple script to monitor memory use over time. # # # Shmuel Y. Protter NDS Israel # 15-08-2007 # # Revision History # YYYY-MM-DD Description # 2007-08-16 Changed to getops, added feature to only look at top proccesses # Version 1.1 released # 2007-08-16 Added a user changeable log file. # Version 1.1.2 released # 2007-09-01 Corrected HP-UX filter flaws..... # Version 1.2.0 released # 2007-09-02 Changed format to put process name on the far right # Version 1.2.1 released # # numarg=$# export UNIX95=1 out_help(){ echo "Error in parameters; USAGE: $PRGN [ -d delay -r runs -n number -f filter -l logfile ] delay is the number of seconds between data collection runs. runs is the number of times the program should loop before terminating. number is the number of processes to be checked. by default the program checks the top 20 proccesses in terms of memory use." } if [ $numarg -lt 0 ] then out_help exit 1 else echo "utility is running.... $1 $2 $3" fi # set defaults for program if options are not given # Program will run once an hour for 12 hours. # Program will use top 20 processes in terms of memory use NUM=20 RUNS=12 DELAY=3600 dflg=0;nflg=0;fflg=0 while getopts d:n:f:r:l: opt do case "$opt" in d) dflg=1 DELAY=$OPTARG;; r) rflg=1 RUNS=$OPTARG;; n) nflg=1 NUM=$OPTARG;; l) lflg=1 LF=$OPTARG;; f) nflg=1 FILTER=$OPTARG;; ?) out_help exit ;; esac done 2>/dev/null #shift $(($OPTIND -1)) # if [ $(( $uflg + $nflg )) -ne 2 ]; then # NEW=TRUE # while true; do # echo "Please print name of the account \c" # read USERNAME # if grep ^${USERNAME}: /etc/passwd ; then # echo "User $USERNAME exists" # else # break # fi # done # Get the operating system so you can build the command line properly # OS=$(uname -a | awk '{ print $1 }') export UNIX95=1 export UNIX95=1 # # # case "$OS" in HP-UX) echo "Building command line $OS" if [ -z "$FILTER" ] then CL="ps -ef -o pid,sz,vsz,args | sort -nr -k 2,3| head -$NUM" else CL="ps -a -o pid,sz,vsz,args -C $FILTER | sort -nr -k 2,3| head -$NUM" fi ;; Linux) echo "Building command line $OS" if [ -z "$FILTER" ] then CL="ps -e -o pid,rss,vsz,args --sort -rss,-vsz | head -$NUM" else CL="ps -C $FILTER -o pid,rss,vsz,args --sort -rss,-vsz | head -$NUM" fi ;; SunOS) echo "Building command line $OS" if [ -z "$FILTER" ] then CL="ps -e -o pid,rss,vsz,args | sort -nr -k 2,3| head -$NUM" else CL="ps -e -o pid,rss,vsz,args | grep $FILTER | sort -nr -k 2,3| head -$NUM" fi ;; esac # export UNIX95=1;ps -e -o pid,args,vsz,rss -C $FILTER | sort -nr -k 3,4| head -20 # sun export UNIX95=1;ps -e -o pid,args,vsz,rss | grep $FILTER | sort -nr -k 3,4| head -20 # Note for HP-UX you need SZ not RSS # and for HP-UX you don't need the -e # export UNIX95=1;ps -o pid,args,vsz,sz -C $FILTER | sort -nr -k 3,4| head -20 # initialize data files if [ -n $FILTER ] then LOGFILE="/tmp/memuse.mon.${FILTER}-`date +%d-%b-%Y`.log" else LOGFILE=/tmp/memuse.mon-`date +%d-%b-%Y-%m-%d`.log fi if [ -z $LF ] then echo "Using the default log..." else echo "Using custom log: $LF" LOGFILE=$LF fi > $LOGFILE # echo "Command line is: ${CL} UNIX95:${UNIX95} ${LF} ${LOGFILE}" # echo "Sleeping 2 seconds in case shmuel is not happy..." # sleep 2 COUNT=0 # Main loop until (( COUNT > RUNS )) do # echo "$LOGFILE $CL count: $COUNT" (( COUNT = $COUNT + 1 )) date >> $LOGFILE echo "PROC# -Vmem- Resident Memory --------- command --------- " >> $LOGFILE eval $CL >> $LOGFILE echo "--------------------------------------------------------" >> $LOGFILE sleep $DELAY done