Reproduced in: http://www.lslnet.com/linux/dosc1/34/linux-258596.htm
Just wrote my system monitoring script, can be used to monitor, memory, CPU, hard disk space, swap,event log case, as long as these values exceed the preset value will send mail to notify the administrator. Of course in advance your server is able to send mail.
This is the second script I write, the level is limited, mistakes are unavoidable.
If you have any suggestions and changes, please tell me directly. 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'll 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 to 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 $} '
Cpusys= '/usr/bin/sar-u 1 3 |grep Average |awk ' {print $} '
echo "USERCPU = $cpuusr%" >> $logfile
echo "SYSCPU = $cpusys%" >> $logfile
Cpuuser= ' echo $cpuusr% |awk-f. ' {print '} '
Cpusystem= ' echo $cpusys% |awk-f. ' {print '} '
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 $} '
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 $} '
Freemem= '/usr/bin/free-m |grep cache: |awk ' {print $} '
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 '} '
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%, 'll send to $warnmail.
Cat $dffile |sed ' s/%//g ' |grep/dev|egrep-v none |awk ' {if ($ > m) {print $ '% ' "' $}} ' >> $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]