Linux系统性能监测脚本(LPMT).

转载于: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]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值