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

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

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

#!/bin/ksh


SECONDS=300  # Defines the number of seconds for each sample
INTERVAL=2   # Defines the total number of sampling intervals
STATCOUNT=0  # Initialize a loop counter to 0, zero
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)   # AIX has four relative columns in the output
       F1=14
       F2=15
       F3=16
       F4=17

       echo "nThe Operating System is $OSn"
       ;;
HP-UX) # HP-UX only has three relative columns in the output
       F1=16
       F2=17
       F3=18
       F4=1   # This "F4=1" is bogus and not used for HP-UX

       echo "nThe Operating System is $OSn"
       ;;
Linux) # Linux only has three relative columns in the output
       F1=14
       F2=15
       F3=16
       F4=1   # This "F4=1" is bogus and not used for Linux

       echo "nThe Operating System is $OSn"
       ;;
SunOS) # SunOS only has three relative columns in the output
       F1=20
       F2=21
       F3=22
       F4=1   # This "F4=1" is bogus and not used for SunOS

       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 vmstat...n"
echo "There are $INTERVAL sampling periods with"
echo "each interval lasting $SECONDS seconds"
echo "n...Please wait while gathering statistics...n"

# Use "vmstat" to monitor the CPU utilization and
# remove all lines that contain alphabetic characters
# and blank spaces. Then use the previously defined
# field numbers, for example F1=20,to point directly
# to the 20th position, for this example. The syntax
# for this techniques is ==>  $'$F1', and points directly
# to the positional parameter.

vmstat $SECONDS $INTERVAL | egrep -v '[a-zA-Z]|^$'
          | awk '{print $'$F1', $'$F2', $'$F3', $'$F4'}'
          | while read FIRST SECOND THIRD FORTH
do
  if ((STATCOUNT == 1)) # Loop counter to get the second set
  then                  # of data produces by "vmstat"

      case $OS in  # Show the results based on the UNIX flavor
      AIX)
            echo "nUser part is $%"
            echo "System part is $%"
            echo "Idle part is $%"
            echo "I/O wait state is $%n"
            ;;
      HP-UX|Linux|SunOS)
            echo "nUser part is $%"
            echo "System part is $%"
            echo "Idle time is $%n"
            ;;
      esac

  fi
  ((STATCOUNT = STATCOUNT + 1)) # Increment the loop counter
done
 


查看本文来源

用户评论

  • 用户名
  • 评论内容