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

#!/bin/ksh


SECS=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|HP-UX)   SWITCH='-t'
             F1=3
             F2=4
             F3=5
             F4=6
             echo "/nThe Operating System is $OS/n"
             ;;
Linux|SunOS) SWITCH='-c'
             F1=1
             F2=2
             F3=3
             F4=4
             echo "/nThe Operating System is $OS/n"
             ;;

*) echo "/nERROR: $OS is not a supported operating system/n"
   echo "/n/t...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 $SECS seconds"
echo "/n...Please wait while gathering statistics.../n"

# Use "iostat" 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=4,to point directly
# to the 4th position, for this example. The syntax
# for this techniques is ==>  $'$F1'.

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值