溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使Linux shell腳本實現(xiàn)CPU預警

發(fā)布時間:2021-09-28 13:49:44 來源:億速云 閱讀:150 作者:iii 欄目:開發(fā)技術

本篇內(nèi)容介紹了“如何使Linux shell腳本實現(xiàn)CPU預警”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

#!/bin/bash
#cpu idle percent
cpu_idle=`top -b -d 1 -n 2 | grep Cpu | awk 'NR>1{print $5}' | cut -f 1 -d "."`
 
if [ -d "/opt/scripts/log" ]
then
  cd /opt/scripts
else
  mkdir /opt/scripts/log
fi
 
count=0
condition=true
while condition
do
    if [ $cpu_idle -le 20 ]
    then
      pids=`top -b -d 1 -n 1 | grep java | awk '{if($9 > 80) print $1}'`
      for ((a = 1; a < 10; a++))
      do
       for pid in $pids
       doi
         #involve jdk existence check later
         date=`date +%Y%m%d%H%M`
         sh showstack.sh -c 30 -p $pid > log/stack_$pid_$date.log
         jstat -gcutil $pid > log/gc_$pid_$date.log
       done
      done
      for pid in $pids
      do
       date=`date +%Y%m%d%H%M`
       jmap -heap $pid > log/heap_$pid_$date.log
      done
      count++
      cpu_idle=`top -b -d 1 -n 2 | grep Cpu | awk 'NR>1{print $5}' | cut -f 1 -d "."`
      if [ $count -ge 3]
      then
       #send emaili
       #server ip address
       ip=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
       condition=false
       count=0
      fi
    else
      condition=false
      count=0
    fi
done

“如何使Linux shell腳本實現(xiàn)CPU預警”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI