Linux 負載監控指令碼,linux負載指令碼

來源:互聯網
上載者:User

Linux 負載監控指令碼,linux負載指令碼

#!/bin/bash

Date=`echo $(date +%Y\-%m\-%d\ %H:%M:%S)`
HostName=`hostname`
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
ServerInfo=`echo -e "--------$Date--------\n伺服器:$HostName IP:$IP"`

#刪除5天前的監控日誌
LOGPWD=/var/log/monitor
LOGDATE=`echo $(date -d -5day +%Y%m%d)`
LOGGREP=`ls $LOGPWD | grep -c "$LOGDATE"`
if [ $LOGGREP -gt 0 ]
then
rm -fr $LOGPWD/*$LOGDATE.log
fi

echo -e "$ServerInfo\nuptime\n`uptime`\nfree -m\n`free -m`" > $LOGPWD/monitor.log
#監控cpu負載
PhysicalCpu=`grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
CoreCpu=`grep 'core id' /proc/cpuinfo | sort -u | wc -l`
count_cores=`echo "$PhysicalCpu*$CoreCpu" | bc`
count_uptime=`uptime |wc -w`
AverageLoad=`uptime | awk '{print $'$count_uptime'}'`
AverageInt=`echo $AverageLoad | cut -f 1 -d "."`
if [ $AverageInt -gt $count_cores ]
then
echo -e "$ServerInfo\n15分鐘的負載為$AverageLoad,核心數$count_cores,超過cpu核心數,請處理!" >> $LOGPWD/cpu_status_$(date +%Y%m%d).log
echo -e "15分鐘的負載為$AverageLoad,核心數$count_cores,超過cpu核心數,請處理!" >> $LOGPWD/monitor.log
else
echo -e "$ServerInfo\n15分鐘的負載為$AverageLoad,核心數$count_cores,負載正常。" >> $LOGPWD/cpu_status_$(date +%Y%m%d).log
echo -e "15分鐘的負載為$AverageLoad,核心數$count_cores,負載正常。" >> $LOGPWD/monitor.log
fi
#監控記憶體使用量率
MemTotal=`free -m | grep Mem | awk '{print $2}'`
MemFree=`free -m | grep Mem | awk '{print $4}'`
MemRate=`echo "100-$MemFree*100/$MemTotal" | bc`
if [ $MemRate -gt 80 ]
then
echo -e "$ServerInfo\n記憶體使用量率$MemRate%,大於80%,請處理。" >> $LOGPWD/mem_status_$(date +%Y%m%d).log
echo -e "記憶體使用量率$MemRate%,大於80%,請處理。" >> $LOGPWD/monitor.log
else
echo -e "$ServerInfo\n記憶體使用量率$MemRate%,小於等於80%,記憶體負載正常。" >> $LOGPWD/mem_status_$(date +%Y%m%d).log
echo -e "記憶體使用量率$MemRate%,小於等於80%,記憶體負載正常。" >> $LOGPWD/monitor.log
fi

if [ $AverageInt -gt $count_cores ]||[ $MemRate -gt 80 ]
then
cat $LOGPWD/monitor.log | mail -s "$HostName伺服器負載監控警示"xxx@xxx.xxx
fi

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.