Linux系統效能監測指令碼(LPMT).__Linux

來源:互聯網
上載者:User

轉載於:http://www.lslnet.com/linux/dosc1/34/linux-258596.htm

 

剛剛寫好了我的系統監測指令碼,可以用來監測,記憶體,CPU,硬碟空間,SWAP,event log的情況,只要這些值超過預先設定的值就會發郵件通知管理員. 當然事先你的伺服器是能發出郵件的.
這是我寫的第二個script,水平有限,出錯再所難免.
如果有什麼建議以及需要修改的地方,請直接告訴我. Lorenlan@gmail.com
lpmt.sh
[code]#!/bin/bash
######################################################## #
# Linux Performance Monitor Tool V 0.1 #
# To monitor the disk usage,mem usage,CPU utilization #
# and system event Log error, Once the performance is- #
# sue happend on server, This script will auto sent #
# mail to inform the administrator. #
# Author: Lan, Loren #
# 8/19/2005 #
#########################################################
. /root/.bash_profile


hostname=`hostname`
recipients=lorenlan@gmail.com
subject="Warning Mail from $hostname"
logdir=/var/log/lpmt
logfile=$logdir/lpmt.log
warnmail=$logdir/lpmt.war
cpuper=80 #Max CPU usage 80%
maxswap=100 #Max swap usage 100M
memminusage=20 #Min free memory 20%
minmem=100 #Min free memory 100M
null=/tmp/null
echo "Temp file for LPMT" > $null
dffile=/tmp/df.log
#Before run this script, you must confirm this server can sent
# out the mail via mutt.

#check the log folder.
if [ -d "$logdir" ]
then
echo "Folder $logdir exists" >> $null
else
mkdir $logdir
fi
rm -f $logfile
rm -f $warnmail
touch $logfile
date >> $logfile
echo "$hostname" >> $logfile

########Check the CPU############

cpuusr=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $3}'`
cpusys=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $5}'`
echo "Usercpu = $cpuusr%" >> $logfile
echo "Syscpu = $cpusys%" >> $logfile
cpuuser=`echo $cpuusr% |awk -F . '{print $1}'`
cpusystem=`echo $cpusys% |awk -F . '{print $1}'`

cpuusage=$(bc<<EOF
scale=4
($cpuuser+$cpusystem)
EOF)

echo "cpu usage = $cpuusage%" >> $logfile

if [ $cpuusage -lt $cpuper ]
then
echo "$cpuusr" >>$null
else
echo "CPU usage = $cpuusage% >$cpuper% " >> $warnmail
fi



##############Check swap############
swap=`/usr/bin/free -m |grep Swap |awk '{print $3}'`
echo "Swap useage= $swap M." >> $logfile
if [ $swap -lt $maxswap ]
then
echo "$swap" >> $null
else
echo "Swap usage=$swap M." >> $warnmail
fi
/usr/bin/free -m >> $logfile

#############Check MEMORY#######################
totalmem=`/usr/bin/free -m |grep Mem |awk '{print $2}'`
freemem=`/usr/bin/free -m |grep cache: |awk '{print $4}'`
echo "Total memory = $totalmem M" >> $logfile
echo "Free memory = $freemem M" >> $logfile
if [ $freemem -lt $minmem ]
then
echo "Free memory = $freemem M" >>$warnmail
else
echo "Free memory = $freemem M" >>$null
fi
#memory usage
free=$(bc<<EOF
scale=4
($freemem/$totalmem)*100
EOF)
freeusage=`echo $free |awk -F . '{print $1}'`
echo "Free Memory = $freeusage%" >> $logfile
if [ $freeusage -lt $memminusage ]
then
echo "Free memory = $freeusage%" >>$warnmail
else
echo "Free memory= $freeusage%" >>$null
fi

#####Event Log###
/bin/cat /var/log/messages |grep error >> $warnmail
/bin/cat /var/log/dmesg |grep error >> $warnmail

#######Disk#####
df -h >> $logfile
df -m > $dffile
#Once disk usge out 90%, will send to $warnmail.
cat $dffile |sed 's/%//g'|grep /dev|egrep -v none |awk '{if ( $5 > 90 ) {print $1 " " $5"%" " " $6}}' >>$warnmail

###Send mail to admin.
if [ -s "$warnmail" ]
then
echo "Have some problem happed on the server $hostname, please check it." >>$warnmail
mutt -a "$logfile" -s "$subject" $recipients < "$warnmail"
else
echo " System is no problem." >> $logfile
fi
[/code]

聯繫我們

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