使用shell脚本进行服务器系统监控——系统负载监控(2)

开发者在线 Builder.com.cn 更新时间:2008-01-10作者:徐建明 来源:CSDN

本文关键词: 监控 服务器 SHELL

#!/bin/ksh


SECS=30  # Defines the number of seconds for each sample
INTERVAL=10 # Defines the total number of sampling intervals
OS=$(uname) # Defines the UNIX flavor

###################################################
##### SETUP THE ENVIRONMENT FOR EACH OS HERE ######
###################################################

# These "F-numbers" point to the correct field in the
# command output for each UNIX flavor.

case $OS in
AIX|HP-UX|SunOS)  
       F1=2
       F2=3
       F3=4
       F4=5
       echo "nThe Operating System is $OSn"
       ;;
Linux)
       F1=3
       F2=4
       F3=5
       F4=6
       echo "nThe Operating System is $OSn"
       ;;
*) echo "nERROR: $OS is not a supported operating systemn"
   echo "nt...EXITING...n"
   exit 1
   ;;
esac

###################################################
######## BEGIN GATHERING STATISTICS HERE ##########
###################################################

echo "Gathering CPU Statistics using sar...n"
echo "There are $INTERVAL sampling periods with"
echo "each interval lasting $SECS seconds"
echo "n...Please wait while gathering statistics...n"

# This "sar" command take $INTERVAL samples, each lasting
# $SECS seconds. The average of this output is captured.

sar $SECS $INTERVAL | grep Average
          | awk '{print $'$F1', $'$F2', $'$F3', $'$F4'}'
          | while read FIRST SECOND THIRD FOURTH
do
      # Based on the UNIX Flavor, tell the user the
      # result of the statistics gathered.

      case $OS in
      AIX|HP-UX|SunOS)
            echo "nUser part is $%"
            echo "System part is $%"
            echo "I/O wait state is $%"
            echo "Idle time is $%n"
            ;;
      Linux)
            echo "nUser part is $%"
            echo "Nice part is $%"
            echo "System part is $%"
            echo "Idle time is $%n"
            ;;
      esac
done

 


查看本文来源

用户评论

  • 用户名
  • 评论内容