科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道使用shell脚本进行服务器系统监控——系统负载监控(1)

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

采用KSH脚本对I/O进行负载监控。增强iostat功能,该脚本可以用于AIX/HP-UX/SUN/LINUX等平台

作者:徐建明 来源:CSDN 2008年1月10日

关键字: 监控 服务器 shell

  • 评论
  • 分享微博
  • 分享邮件

#!/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


查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章