shell如何監控cpu,memory,load average

來源:互聯網
上載者:User

shell 監控cpu,memory,load average,記錄到log,當負載壓力時,發電郵通知管理員。

原理:

1.擷取cpu,memory,load average的數值

2.判斷數值是否超過自訂的範圍,例如(CPU>90%,Memory<10%,load average>2)

3.如數值超過範圍,發送電郵通知管理員。發送有時間間隔,每小時只會發送一次。

4.將數值寫入log。

5.設定crontab 每30秒運行一次。

返回欄目頁:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

ServerMonitor.sh

#!/bin/bash        # 系統監控,記錄cpu、memory、load average,當超過規定數值時發電郵通知管理員        # *** config start ***        # 目前的目錄路徑  ROOT=$(cd "$(dirname "$0")"; pwd)        # 當前伺服器名  HOST=$(hostname)        # log 檔案路徑  CPU_LOG="${ROOT}/logs/cpu.log"  MEM_LOG="${ROOT}/logs/mem.log"  LOAD_LOG="${ROOT}/logs/load.log"        # 通知電郵列表  NOTICE_EMAIL='admin@admin.com'        # cpu,memory,load average 記錄上一次發送通知電郵時間  CPU_REMARK='/tmp/servermonitor_cpu.remark'  MEM_REMARK='/tmp/servermonitor_mem.remark'  LOAD_REMARK='/tmp/servermonitor_loadaverage.remark'        # 發通知電郵間隔時間  REMARK_EXPIRE=3600  NOW=$(date +%s)        # *** config end ***              # *** function start ***        # 擷取CPU佔用  function GetCpu() {      cpufree=$(vmstat 1 5 |sed -n '3,$p' |awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}')      cpuused=$((100 - $cpufree))      echo $cpuused            local remark      remark=$(GetRemark ${CPU_REMARK})            # 檢查CPU佔用是否超過90%      if [ "$remark" = "" ] && [ "$cpuused" -gt 90 ]; then          echo "Subject: ${HOST} CPU uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}          echo "$(date +%s)" > "$CPU_REMARK"      fi  }        # 擷取記憶體使用量情況  function GetMem() {      mem=$(free -m | sed -n '3,3p')      used=$(echo $mem | awk -F ' ' '{print $3}')      free=$(echo $mem | awk -F ' ' '{print $4}')      total=$(($used + $free))      limit=$(($total/10))      echo "${total} ${used} ${free}"            local remark      remark=$(GetRemark ${MEM_REMARK})            # 檢查記憶體佔用是否超過90%      if [ "$remark" = "" ] && [ "$limit" -gt "$free" ]; then          echo "Subject: ${HOST} Memory uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}          echo "$(date +%s)" > "$MEM_REMARK"      fi  }        # 擷取load average  function GetLoad() {      load=$(uptime | awk -F 'load average: ' '{print $2}')      m1=$(echo $load | awk -F ', ' '{print $1}')      m5=$(echo $load | awk -F ', ' '{print $2}')      m15=$(echo $load | awk -F ', ' '{print $3}')      echo "${m1} ${m5} ${m15}"            m1u=$(echo $m1 | awk -F '.' '{print $1}')            local remark      remark=$(GetRemark ${LOAD_REMARK})            # 檢查是否負載是否有壓力      if [ "$remark" = "" ] && [ "$m1u" -gt "2" ]; then          echo "Subject: ${HOST} Load Average more than 2 $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}          echo "$(date +%s)" > "$LOAD_REMARK"      fi  }        # 擷取上一次發送電郵時間  function GetRemark() {      local remark            if [ -f "$1" ] && [ -s "$1" ]; then          remark=$(cat $1)                if [ $(( $NOW - $remark )) -gt "$REMARK_EXPIRE" ]; then              rm -f $1              remark=""          fi      else          remark=""      fi            echo $remark  }              # *** function end ***        cpuinfo=$(GetCpu)  meminfo=$(GetMem)  loadinfo=$(GetLoad)        echo "cpu: ${cpuinfo}" >> "${CPU_LOG}"  echo "mem: ${meminfo}" >> "${MEM_LOG}"  echo "load: ${loadinfo}" >> "${LOAD_LOG}"        exit 0

crontab

* * * * * /home/xxxx/ServerMonitor.sh  * * * * * sleep 30; /home/xxxx/ServerMonitor.sh

作者:csdn部落格 傲雪星楓

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.