- 论坛徽章:
- 0
|
[求助]如何用shell编写系统性能的监控的脚本!!!
以前在网上找到的一个现成的脚步。但是我对netstat 的用法一直没有看懂,如果明白请告知。
===========
#!/bin/sh
# sccsid: @(#)watch.system 1.18 08/23/99 13:20:10
#
# watch.system: Watches the system. Alternative to the "watcher" program.
# Add a line to cron like the below to run it, perferably not as root:
#
## watch the system, to see if it's been good or bad
##
# 15,45 * * * * /etc/watch.system
##
# See comments regarding Solaris 2 throughout the file
#
# User settables
#
FT="-t 4.2" # File system type, should be 4.2
FZ=98 # Maxium percent file system used
IN=70 # Maxium percent inodes used per file system
NET=le0 # Network interface
PSLIMIT=99 # Maxium number of processes to be running
RATE=100 # Collision rate, lower means more tolerant
WARNSWAP=15000 # Remaining swap space warning level
DANGERSWAP=8000 # Remaining swap space danger level
MEMWARNING=15 # Real Memory warning level
LOADWARNING=10 # Load average warning level
LOADDANGER=15 # Load average danger level
MAILTO=root # Who to tell
EXCLUDES='Filesystem' # df Don't check these, egrep fs1|fs2|etc syntax
#
# End of normal user settables
HOST=`hostname`
PATH=/usr/ucb:/bin:/usr/bin:/usr/etc:/usr/local/bin:/etc; export PATH
#
# Collison rate routine, $9 + 1 is a hack to avoid devision by zero
# in the awk statement. The -gt 0 check is a catch for the roll over
# to negative numbers on packet counters.
#
NS=`netstat -i | grep $NET`
COLL=`echo $NS | awk '{ N = ( $9 + 1 ); C = ( $5 + $7 ) / N } { print C }'`
if [ $COLL -gt 0 -a $RATE -gt $COLL ]; then
SUB="WARNING $HOST collision rate 1 in $COLL"
netstat -i | /usr/ucb/mail -s "$SUB" $MAILTO
fi
#
#
# File system size
#
DF=`df $FT | egrep -v $EXCLUDES | awk '{print $5}' | sed 's/%//g'`
for I in $DF;do
if [ $I -gt $FZ ]; then
SUB="WARNING $HOST file system over $FZ percent full"
df $FT | egrep -v $EXCLUDES | /usr/ucb/mail -s "$SUB" $MAILTO
break
fi
done
#
# Inode usage
#
# Uncomment first df line for SunOS, second line for Solaris
#
#DFI=`df $FT -i | grep % | awk '{print $4}' | sed 's/%//g'`
DFI=`df -i | grep % | awk '{print $4}' | sed 's/%//g'`
for I in $DFI; do
if [ $I -gt $IN ]; then
SUB="WARNING `hostname` inode usage over $IN percent"
df -i $FT | /usr/ucb/mail -s "$SUB" $MAILTO
break
fi
done
#
# Load averages. Generally 10 is bad, 20 is really bad. Use top if you
# have it, otherwise use uptime.
#
UP=`uptime | awk -Fg '{print $2}' | awk -F: '{print $2}' | awk -F, '{print $1}'`
if [ $UP -ge $LOADDANGER ]; then
SUB="DANGER `hostname` load average at $UP"
# uptime | /usr/ucb/mail -s "$SUB" $MAILTO
top -d1 -n1| /usr/ucb/mail -s "$SUB" $MAILTO
elif [ $UP -ge $LOADWARNING ]; then
SUB="WARNING `hostname` load average at $UP"
# uptime | /usr/ucb/mail -s "$SUB" $MAILTO
top -d1 -n1| /usr/ucb/mail -s "$SUB" $MAILTO
fi
#
# Real memory usage.
#
MEM=`top -d1 -n1| grep Memory | sed 's/M/ /g'| awk '{ print $4 }'`
if [ $MEM -le $MEMWARNING ]; then
SUB="WARNING `hostname` real memory free down to $MEM"
top -ores -d1 | /usr/ucb/mail -s "$SUB" $MAILTO
fi
#
# Mbufs in use, comment this section out for use with Solaris 2
#
#MB=`netstat -m | grep "mbufs in use" | sed 's:/: :g'|awk '{ print $2 }'`
#
#if [ $MB -gt 3000 ]; then
# SUB="DANGER `hostname` $MB mbufs allcated"
# echo "`netstat -m`" | /usr/ucb/mail -s "$SUB" $MAILTO
#
#elif [ $MB -gt 2000 ]; then
# SUB="WARNING `hostname` $MB mbufs allcated"
# echo "`netstat -m`" | /usr/ucb/mail -s "$SUB" $MAILTO
#fi
#
# Number of processes running
#
PS=`ps ax | wc -l`
if [ $PS -gt $PSLIMIT ]; then
SUB="WARNING `hostname` $PS running processes"
ps ax | /usr/ucb/mail -s "$SUB" $MAILTO
fi
#
# check free swap space
#
# uncomment the pstat line for SunOS or the /etc/swap line for Solaris 2
#
#SWAP=`pstat -T | grep swap | awk -F/ '{print $1 " " $2}' | awk '{ C = $2 - $1} { print C }'`
SWAP=`/etc/swap -s | awk -F, '{print $2}' | awk -Fk '{print $1}'`
if [ $SWAP -lt $DANGERSWAP ]; then
SUB="DANGER $HOST only $SWAP bytes of swap space left"
swap -s | /usr/ucb/mail -s "$SUB" $MAILTO
elif [ $SWAP -lt $WARNSWAP ]; then
SUB="WARNING $HOST only $SWAP bytes of swap space left"
swap -s | /usr/ucb/mail -s "$SUB" $MAILTO
fi
========== |
|