常用的主机监控的 Shell 脚本

来源:互联网 发布:arcgis js 双击事件 编辑:程序博客网 时间:2024/04/30 01:28

作为系统运维人员,就要实时的监控系统的一些重要参数,不如僵尸进程,CPU的利用率,内存的使用情况,磁盘空间的使用情况,系统的均衡负载,根据得到的最新信息,我们就能判断系统运行的状态是否良好。本人接触Linux系统也有一点时间,在网上看看了,经过整理,将一些常用的系统监控的命令shell脚本拿出来晒晒,希望给新手提供学习的机会,同时也希望经验丰富者提够些技术知道,多多探讨交流!

获得某个用户的某个进行的信息:

  1. function GetPID #user #name 
  2. PsUser=$1 
  3. #echo $PsUser 
  4. PsName=$2 
  5. #echo $PsName 
  6. pid=`ps -u $PsUser | grep $PsName | grep -v grep | grep -v vi | grep -v dbx\n | grep -v tail | grep -v start | grep -v stop | sed -n 1p | awk '{print $1}'` 
  7. echo $pid 
  8. PID=`GetPID lgy cat` 
  9. echo $PID 
  10. #The process does not exist. 
  11. if [ "-$PID" == "-" ] 
  12. then 
  13. echo "The process does not exist." 
  14. fi 

这里面涉及一些最基本的进程监控命令,在linux中你只要man ps就会得到很多使用说明,grep按照一定的匹配规则进行字符串分割匹配,还有一些简单的控制语句,这些基本的shell语法应该是我们的基本技能,在晚上会有很多的学习资料。

获得CPU的使用率:

  1. function GetCPU 
  2. CPUValue=`ps -p $1 -o pcpu | grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'` 
  3. echo $CPUValue 
  4. function CheckCPU 
  5. PID=$1 
  6. cpu=`GetCPU $PID` 
  7. if [ $cpu -gt 80 ] 
  8. then 
  9. echo "The usage of cpu is larger than 80%" 
  10. else 
  11. echo "The usage of cpu is normal" 
  12. fi 
  13. CheckCPU $PID 

这个程序和上一个代码片段是一脉相承,本段代码的运行结果一会会有相应的展示!

这段脚本的主要目的是用来进行对某个进程的内存使用情况的检测:

  1. function GetMem 
  2. MemUsage=`ps -o vsz -p $1 | grep -v VSZ` 
  3. ((MemUsage /= 1024)) 
  4. echo $MemUsage 
  5. mem=`GetMem $PID` 
  6. if [ $mem -gt 1600 ] 
  7. then 
  8. echo "The usage of memory is larger than 1.6G" 
  9. else 
  10. echo "The usage of memory is normal" 
  11. fi 

这是用来进行获得此进程的句柄使用量:

  1. function GetDes 
  2. DES=`ls /proc/$1/fd | wc -l` 
  3. echo $DES 
  4. des=`GetDes $PID` 
  5. if [ $des -gt 900 ] 
  6. then 
  7. echo "The number of des is larger than 900" 
  8. else 
  9. echo "The number of des is normal" 
  10. fi 

进行对某个端口的绑定的查询:

  1. function Listening 
  2. TCPListeningNum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l` 
  3. UDPListenNum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l` 
  4. ((ListeningNum = TCPListeningNum + UDPListenNum )) 
  5. if [ $ListeningNum == 0 ] 
  6. then 
  7. echo "0" 
  8. else 
  9. echo "1" 
  10. fi 
  11. isListen=`Listening 8080` 
  12. if [ $isListen -eq 1 ] 
  13. then 
  14. echo "The port is listening" 
  15. else 
  16. echo "The port is not listening" 
  17. fi 

系统CPU的使用情况:

  1. function GetSysCPU 
  2. CPUIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{xx = x + $15} END {print x/5}' | awk -F. '{print $1}'` 
  3. CPUNum=`echo "100-$CPUIdle" | bc` 
  4. echo $CPUNum 
  5. cpu=`GetSysCPU` 
  6. echo "The System CPU is $cpu" 
  7. if [ $cpu -gt 90 ] 
  8. then 
  9. echo "The usage of system cpu is larger than 90%" 
  10. else 
  11. echo "The usage of system cpu is normal" 
  12. fi 

获得某制定的磁盘空间:

  1. function GetDiskSpc 
  2. if [ $# -ne 1 ] 
  3. then 
  4. return 1 
  5. fi 
  6. Folder="$1$" 
  7. DiskSpace=`df -k | grep $Folder | awk '{print $5}' | awk -F% '{print $1}'` 
  8. echo $DiskSpace 
  9. Folder="/dev" 
  10. DiskSpace=`GetDiskSpc $Folder` 
  11. echo "The system $Folder disk space is $DiskSpace%" 
  12. if [ $DiskSpace -gt 90 ] 
  13. then 
  14. echo "The usage of system disk($Folder) is larger than 90%" 
  15. else 
  16. echo "The usage of system disk($Folder) is normal" 
  17. fi 

在本机上代码检测的结果如下:

  1. lgy@lgy-HP:~$ ./monitorCPU.sh 
  2. 2388 
  3. The usage of cpu is normal 
  4. The usage of memory is normal 
  5. The number of des is normal 
  6. The port is not listening 
  7. The System CPU is 1 
  8. The usage of system cpu is normal 
  9. The system /dev disk space is 1% 
  10. The usage of system disk(/dev) is normal
  11. 1、查看主机网卡流量

    1. #!/bin/bash 
    2. #!/bin/bash 
    3. #network 
    4. #Mike.Xu 
    5. while : ; do 
    6. time='date +%m"-"%d" "%k":"%M' 
    7. day='date +%m"-"%d' 
    8. rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
    9. tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
    10. sleep 2 
    11. rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
    12. tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
    13. rx_result=$[(rx_after-rx_before)/256] 
    14. tx_result=$[(tx_after-tx_before)/256] 
    15. echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps" 
    16. sleep 2 
    17. done 
    18. done 

    2、系统状况监控

    1. #!/bin/sh 
    2. #systemstat.sh 
    3. #Mike.Xu 
    4. IP=192.168.1.227 
    5. top -n 2| grep "Cpu" >>./temp/cpu.txt 
    6. free -m | grep "Mem" >> ./temp/mem.txt 
    7. df -k | grep "sda1" >> ./temp/drive_sda1.txt 
    8. #df -k | grep sda2 >> ./temp/drive_sda2.txt 
    9. df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt 
    10. df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt 
    11. time=`date +%m"."%d" "%k":"%M` 
    12. connect=`netstat -na | grep "219.238.148.30:80" | wc -l` 
    13. echo "$time  $connect" >> ./temp/connect_count.txt 

    3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告

    1. #!/bin/bash 
    2. #monitor available disk space 
    3. SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed  's/%//' 
    4. if [ $SPACE -ge 90 ] 
    5. then 
    6. fty89@163.com 
    7. fi 

    4、 监控CPU和内存的使用情况

    1. #!/bin/bash 
    2. #script  to capture system statistics 
    3. OUTFILE=/home/xu/capstats.csv 
    4. DATE='date +%m/%d/%Y' 
    5. TIME='date +%k:%m:%s' 
    6. TIMEOUT='uptime' 
    7. VMOUT='vmstat 1 2' 
    8. USERS='echo $TIMEOUT | gawk '{print $4}' ' 
    9. LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' ' 
    10. FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' ' 
    11. IDLE='echo  $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' ' 
    12. echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE 

    5、全方位监控主机

    1. #!/bin/bash 
    2. # check_xu.sh 
    3. # 0 * * * * /home/check_xu.sh 
    4. DAT="`date +%Y%m%d`" 
    5. HOUR="`date +%H`" 
    6. DIR="/home/oslog/host_${DAT}/${HOUR}" 
    7. DELAY=60 
    8. COUNT=60 
    9. # whether the responsible directory exist 
    10. if ! test -d ${DIR} 
    11. then 
    12. /bin/mkdir -p ${DIR} 
    13. fi 
    14. # general check 
    15. export TERM=linux 
    16. /usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 & 
    17. # cpu check 
    18. /usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 & 
    19. #/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 & 
    20. #/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 & 
    21. # memory check 
    22. /usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 & 
    23. # I/O check 
    24. /usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 & 
    25. # network check 
    26. /usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 & 
    27. #/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 & 

    放在crontab里每小时自动执行:

    1. 0 * * * * /home/check_xu.sh 

    这样会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据。

    如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。

    原文链接:http://www.dbasky.net/archives/2009/10/shell.html

     
原创粉丝点击