/etc/mail.rc"/>
溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

腳本監(jiān)控當(dāng)前服務(wù)器負(fù)載情況

發(fā)布時(shí)間:2020-07-21 09:38:52 來源:網(wǎng)絡(luò) 閱讀:724 作者:一招拜師 欄目:建站服務(wù)器

#!/bin/bash

#觸發(fā)閾值發(fā)郵件還沒有寫.

export PATH

export LANG=zh_CN.UTF-8

#先配置好mailx郵箱賬號(hào)密碼:

#cat>/etc/mail.rc<<"EOF"

#set sendcharsets=iso-8859-1,utf-8

#set from=xxx@163.com

#set smtp=smtp.163.com

#set smtp-auth-user=xx@163.com

#set smtp-auth-password=xxx

#set smtp-auth=login

#EOF


readonly times=$(date +%Y"/"%m"/"%d" "%k":"%M)

readonly machine="192.168.1.10"


function top_check(){

   runup=$(top -n 1|grep user|awk -F '[, ]+' '{print $5}')

   echo "$times   $machine   運(yùn)行多久: $runup 分鐘"

 

   userd=$(top -n 1|grep user|awk -F '[, ]+' '{print $7}')

   echo "$times   $machine   當(dāng)前在線用戶數(shù): $userd"

   

   load1=$(top -n 1|grep user|awk -F '[, ]+' '{print $11}')

   echo "$times   $machine   1分鐘CPU負(fù)載: $load1"

   

   load5=$(top -n 1|grep user|awk -F '[, ]+' '{print $12}')

   echo "$times   $machine   5分鐘CPU負(fù)載: $load5"

   

   load15=$(top -b -n 1|grep user|awk '{print $NF}')

   echo "$times   $machine   15分鐘CPU負(fù)載: $load15"

   

   cpuus=$(top -n 1|grep Cpu|awk -F "," '{print $1}'|awk -F " " '{print $2}')

   echo "$times   $machine   用戶空間占用CPU百分比: $cpuus"


   cpusy=$(top -n 1|grep Cpu|awk -F "," '{print $2}'|awk -F " " '{print $2}')

   echo "$times   $machine    內(nèi)核空間占用CPU百分比: $cpusy"


   cpuni=$(top -n 1|grep Cpu|awk -F "," '{print $3}'|awk -F " " '{print $2}')

   echo "$times   $machine    改變過優(yōu)先級(jí)的進(jìn)程占用CPU的百分比: $cpuni"


   cpuid=$(top -n 1|grep Cpu|awk -F "," '{print $4}'|awk -F " " '{print $2}')

   echo "$times   $machine    空閑CPU百分比: $cpuid"


   cpuwa=$(top -n 1|grep Cpu|awk -F "," '{print $5}'|awk -F " " '{print $2}')

   echo "$times   $machine    I/O等待占用CPU的百分比: $cpuwa"


   cpuhi=$(top -n 1|grep Cpu|awk -F "," '{print $6}'|awk -F " " '{print $2}')

   echo "$times   $machine    硬中斷占用CPU的百分比: $cpuhi"


   cpusi=$(top -n 1|grep Cpu|awk -F "," '{print $7}'|awk -F " " '{print $2}')

   echo "$times   $machine    軟中斷占用CPU的百分比: $cpusi"


   cpust=$(top -n 1|grep Cpu|awk -F "," '{print $8}'|awk -F " " '{print $2}')

   echo "$times   $machine    虛擬 CPU 等待實(shí)際 CPU 的時(shí)間的百分比: $cpust"

   

   #內(nèi)存部分M為單位

   memtotal=$(($(top -n 1|grep buff/cache|awk -F '[, ]+' '{print $4}') / 1024))

   echo "$times   $machine   內(nèi)存總共: $memtotal M"

   memfree=$(($(top -n 1|grep buff/cache|awk -F '[, ]+' '{print $7}') / 1024))

   echo "$times   $machine   空閑內(nèi)存: $memfree M"

   memused=$(($(top -n 1|grep buff/cache|awk -F '[, ]+' '{print $10}') / 1024))

   echo "$times   $machine   已使用內(nèi)存: $memused M"

   membuff=$(($(top -n 1|grep buff/cache|awk -F '[, ]+' '{print $13}') / 1024))

   echo "$times   $machine   內(nèi)存buff/cache: $membuff M"

   #swap部分M

   swaptotal=$(($(top -n 1|grep avail|awk -F '[, ]+' '{print $3}') / 1024))

   echo "$times   $machine   swap總共: $swaptotal M"

   swapfree=$(($(top -n 1|grep avail|awk -F '[, ]+' '{print $6}') / 1024))

   echo "$times   $machine   swap空閑: $swaptotal M"

   swapused=$(($(top -n 1|grep avail|awk -F '[, ]+' '{print $9}') / 1024))

   echo "$times   $machine   swap已使用: $swapused M"

   swapavail=$(($(top -n 1|grep avail|awk -F '[, ]+' '{print $11}') / 1024))

   echo "$times   $machine   swap內(nèi)存avail Mem: $swapavail M"

   

}

files="/tmp/top.logs"

top_check >$files

dos2unix -k $files

mailx -s "$machine" xxx@vip.qq.com < $files

向AI問一下細(xì)節(jié)

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

AI