Check system performance.

#!/bin/sh

# script name: checkperf
# description: Check system performance.
# Used for automating checks of CPU, memory, I/O, and network TCP performance.
#
# This script is described and explained in the BigAdmin Tech Tip:
# Automating a System Performance Check Using the checkperf Utility
# http://www.sun.com/bigadmin/content/submitted/perf_check.jsp
#

# directory where checkperf and runqueue.d are located
DIR=/home//bin

# generated warning message
LOG=/home//bin/perf_msg

# who receives the warning messages if there are any
RECEIVER=

# sar -uqrd $T $N
T=5
N=60

# CPU, memory, IO warning threshold
CPU_UTIL_WARN=80
MEM_FREEPHY_WARN_PERCENT=20
MEM_FREESWAP_WARN_PERCENT=20
IO_UTIL_WARN=80

Process_CPU_MEM()
{
CPU_IDLE=$1
CPU_RUNQUEUE=$2
# MB
MEM_FREEPHY=`expr $3 \* 8 / 1024`
MEM_FREESWAP=`expr $4 / 2 / 1024`

CPU_AVER_UTIL=`expr 100 - $CPU_IDLE`
if [ $CPU_AVER_UTIL -gt $CPU_UTIL_WARN ]; then
echo "CPU average utilization: $CPU_AVER_UTIL%(>$CPU_UTIL_WARN%)" >> $LOG
CPU_OFF_NUM=`psrinfo | grep off-line | wc -l`

if [ $CPU_OFF_NUM -gt 0 ]; then
echo "There are $CPU_OFF_NUM CPUs offline and use psradm to bring them online" >> $LOG
fi

if [ $CPU_RUNQUEUE -gt 0 ]; then
echo "Threads (per second) waiting for CPU to run: $CPU_RUNQUEUE." >> $LOG
echo "Recommend to add $CPU_RUNQUEUE CPUs to your system. Use prstat -L to see" >> $LOG
echo "if running processes have multiple threads so that you may switch to" >> $LOG
echo "thread-based-processor machine, such as Sun Fire T2000 server." >> $LOG
fi

if [ -f /usr/sbin/dtrace ]; then
RUNQ=`runqueue.d`
RUNQ=`echo "scale=1; $RUNQ / 10" | bc`
echo "The accurate threads waiting for CPU: $RUNQ" >> $LOG
fi
echo "\n" >> $LOG
fi

if [ $MEM_FREEPHY -lt $MEM_FREEPHY_WARN ]; then
echo "Available physical memory: $MEM_FREEPHY MB(> $LOG
fi

if [ $MEM_FREESWAP -lt $MEM_FREESWAP_WARN ]; then
echo "Available swap space: $MEM_FREESWAP MB(> $LOG

SWAP_PHY_SIZE=0
for block in `swap -l | grep -v blocks | nawk '{print $4}'`
do
SWAP_PHY_SIZE=`expr $SWAP_PHY_SIZE + $block / 2 / 1024`
done
NEEDED_SWAP_PHY=`expr $MEM_TOTALPHY \* 3 / 2 - $SWAP_PHY_SIZE`
if [ $NEEDED_SWAP_PHY -gt 5 ]; then
echo "Recommend to add $NEEDED_SWAP_PHY MB swap device. The total size" >> $LOG
echo " of physical swap devices should be 1.5 times physical memory.\n">>$LOG
fi
fi

echo "CPU average utilization: $CPU_AVER_UTIL %"
echo "Threads waiting for CPU: $CPU_RUNQUEUE"
echo "Memory physical free: $MEM_FREEPHY MB"
echo "Memory swap free: $MEM_FREESWAP MB"
}

# if retransmit rate/duplicate rate is higher 15%, or listen queue drop is more than 100,
# then send warning message.
CheckTCP()
{
i=0
netstat -s -P tcp | egrep 'tcpOutSegs|tcpRetransSegs|tcpInSegs|tcpInDupSegs|tcpListenDrop' \
| while read line
do
i=`expr $i + 1`
if [ $i -eq 1 ]; then
tcpOutSegs=`echo "$line" | nawk -F'[ = ]+' '{print $4}'`
elif [ $i -eq 2 ]; then
tcpRetransSegs=`echo "$line" | nawk -F'[ = ]+' '{print $2}'`
tcpRetrans=`expr $tcpOutSegs \* 15 / 100 - $tcpRetransSegs`
echo The total TCP retransmit rate: $tcpRetransSegs/$tcpOutSegs
if [ $tcpRetrans -lt 0 ]; then
echo "TCP retransmit rate is too high(>15%)" >> $LOG
fi
elif [ $i -eq 3 ]; then
tcpInSegs=`echo "$line" | nawk -F'[ = ]+' '{print $2}'`
elif [ $i -eq 4 ]; then
tcpInDupSegs=`echo "$line" | nawk -F'[ = ]+' '{print $2}'`
tcpInDup=`expr $tcpInSegs \* 15 / 100 - $tcpInDupSegs`
if [ $tcpInDup -lt 0 ]; then
echo "TCP duplicate rate is too high(>15%)" >> $LOG
fi
elif [ $i -eq 5 ]; then
tcpListenDrop=`echo "$line" | nawk -F'[ = ]+' '{print $2}'`
if [ $tcpListenDrop -gt 100 ]; then
echo "TCP listen queue drops is too high(>100)" >> $LOG
fi
fi
done
}

# main()

PATH=/usr/bin:/usr/sbin:$DIR; export PATH

MEM_TOTALPHY=`prtconf -vp | grep "Memory size" | awk '{print $3}'`
tmp=`swap -s | nawk -F= '{print $2}' | nawk '{print $1, $3}'`
MEM_TOTALSWAP=0
for num in `echo $tmp | nawk -Fk '{ for( i=1 ; i $LOG
i=1
sar -uqrd $T $N | sed -n '/^Average/,/*/p' | while read line
do
if [ $i -eq 1 ]; then
CPU_IDLE=`echo "$line" | awk '{print $5}'`
i=`expr $i + 1`
elif [ $i -eq 2 ]; then
CPU_RUNQUEUE=`echo "$line" | awk '{print $2}'`
i=`expr $i + 1`
elif [ $i -eq 3 ]; then
MEM_FREEPHY=`echo "$line" | awk '{print $2}'`
MEM_FREESWAP=`echo "$line" | awk '{print $3}'`
i=`expr $i + 1`
Process_CPU_MEM $CPU_IDLE $CPU_RUNQUEUE $MEM_FREEPHY $MEM_FREESWAP
elif [ $i -eq 4 ]; then
IO_UTIL=`echo "$line" | awk '{print $3}'`
if [ $IO_UTIL -gt $IO_UTIL_WARN ]; then
IO_DEVICENAME=`echo "$line" | awk '{print $2}'`
echo "IO utilization on $IO_DEVICENAME: $IO_UTIL% (>$IO_UTIL_WARN%)" >> $LOG
fi
i=`expr $i + 1`
else
IO_UTIL=`echo "$line" | awk '{print $2}'`
if [ $IO_UTIL -gt $IO_UTIL_WARN ]; then
IO_DEVICENAME=`echo "$line" | awk '{print $1}'`
if echo "$IO_DEVICENAME" | grep "," > /dev/null; then
:
else
echo "IO utilization on $IO_DEVICENAME: $IO_UTIL%(>$IO_UTIL_WARN%)" >> $LOG
fi
fi
i=`expr $i + 1`
fi
done

CheckTCP

if [ `cat $LOG | wc -c` -gt 1 ]; then
mailx -s "`hostname` Performance Warning" $RECEIVER < $LOG
fi

##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.jsp
##############################################################################

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s