分类: LINUX
2010-04-12 22:01:45
有用脚本 2009-07-21 17:49:02 阅读72 评论0 字号:大中小
一,系统运行情况监控
二,系统日志监控
三,发送邮件
一,系统运行情况监控
#!/bin/bash
# /usr/monitor
########################################################################
#监控系统状态
#cpu, memory, disk, load average, oracle datafile
########################################################################
#make the around command path
ECHO=/bin/echo
SED=/bin/sed
AWK=/bin/awk
UPTIME=/bin/uptime
VMSTAT=/usr/bin/vmstat
FREE=/usr/bin/free
IPTABLES=/sbin/iptables
GREP=/bin/grep
TOP=/usr/bin/top
HEAD=/usr/bin/head
DF=/bin/df
CAT=/bin/cat
HOSTNAME=/bin/hostname
#check the iostat file is exits
DATE_FILE=`date +%Y%m%d`
IOSTAT_FILE=`pwd`/cctv_$DATE_FILE.txt
ORADATA=/opt/oracle/oradata/uniqo
if [ -e $IOSTAT_FILE ]
then
echo "The iostat file is exits!"
else
touch $IOSTAT_FILE
fi
#setup the time of the check
DATE=`/bin/date +%c`
$ECHO " " >> $IOSTAT_FILE
$ECHO " " >> $IOSTAT_FILE
$ECHO "The detection time is:$DATE" >> $IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check hostname
$ECHO "HOST NAME: `$HOSTNAME`" >>$IOSTAT_FILE
$ECHO "IP: 192.168.1.123 WAN: 58.39.145.88" >>$IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check system load average:
$ECHO "system load average:" >>$IOSTAT_FILE
$ECHO "----------------------------------------------------------------------------------------------" >>$IOSTAT_FILE
$ECHO " `top -b -n 1 |grep -n load |awk '{print $10,$11,$12,$13,$14}'` " >>$IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check the cpu stat
$ECHO "The current status of CPU usage is as following:" >> $IOSTAT_FILE
$ECHO "----------------------------------------------------------------------------------------------" >>$IOSTAT_FILE
$ECHO "`$TOP -b -n 1 |$GREP Cpu`" >> $IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check the memory stat
$ECHO "The current status of Memory is as following:" >> $IOSTAT_FILE
$ECHO "----------------------------------------------------------------------------------------------" >>$IOSTAT_FILE
$ECHO "`$FREE |$GREP -1 Mem |$HEAD -n 2 `" >> $IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check the disk stat
$ECHO "The current status of disk space is as following:" >> $IOSTAT_FILE
$ECHO "----------------------------------------------------------------------------------------------" >>$IOSTAT_FILE
$ECHO "`$DF -h `" >> $IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
#check oracle datafile
$ECHO "Check oracle da
$ECHO "----------------------------------------------------------------------------------------------" >>$IOSTAT_FILE
cd $ORADATA
$ECHO "`du -sh *`" >>$IOSTAT_FILE
$ECHO "" >> $IOSTAT_FILE
scp $IOSTAT_FILE
二,系统日志监控
RESULT=/usr/monitor/cron_monitor/$HOSTNAME
if ! test -e ${RESULT}
then
touch ${RESULT}
fi
cron()
{
CRON=/var/log/cron
echo "About $HOSTNAME cron log at date: `date -d"1 day ago"` " >> $RESULT
echo "$ABOUTME" >> $RESULT
echo "" >> $RESULT
grep "$YESTERDAY" $CRON | grep -v "run-parts" >> $RESULT
echo "=============================================== " >> $RESULT
echo "" >> $RESULT
echo "About $HOSTNAME cron log at date: `date ` " >> $RESULT
echo "" >> $RESULT
grep "$TODAY" $CRON | grep -v "run-parts" >> $RESULT
echo "" >>$RESULT
}
sec()
{
echo "" >> $RESULT
echo "***** This is system Login History *****" >> $RESULT
echo "------------------------------------------" >> $RESULT
grep "Accepted" /var/log/secure >> $RESULT
echo "" >> $RESULT
echo "***** Runing Program *****" >> $RESULT
echo "------------------------------------------" >> $RESULT
netstat -natp |grep LISTEN >> $RESULT
echo "" >> $RESULT
}
cron
sec
#将结果scp到远程计算机
scp $RESULT
rm -rf $RESULT
三,发送邮件
#!/bin/bash
# /usr/monitor
#########################################
#将监控结果 以邮件形式发送给郑彬
#########################################
#30 17 * * * /usr/monitor/send.mail
#00 17 * * * /usr/monitor/cctv/cctv_monitor.sh
DATE_FILE=`date +%Y%m%d`
MMLOYAL_TXT=/usr/monitor/mmloyal/mmloyal_$DATE_FILE.txt
SC_TXT=/usr/monitor/sc/sc_$DATE_FILE.txt
CCTV_TXT=/usr/monitor/cctv/cctv_$DATE_FILE.txt
UK66_TXT=/usr/monitor/uk66/monitor_$DATE_FILE.txt
META37_TXT=/usr/monitor/37/37_$DATE_FILE.txt
ERROR_TXT=/usr/monitor/error.txt
#MAIL ADD
ZBB_META=""
#file exist and size more than zero
uk66()
{
if [ -s $UK66_TXT ]
then
mail -s "SYSTEM MONITOR FOR 10.1.10.66" $ZBB_META < $UK66_TXT
else
mail -s "!! uk66 system monitor ERROR !!" $ZBB_META < $ERROR_TXT
fi
}
uniqlo()
{
if [ -s $UNIQLO_TXT ]
then
mail -s " UNIQLO" $ZBB_META < $UNIQLO_TXT
else
mail -s "!! uniqlo system monitor ERROR !!" $ZBB_META < $ERROR_TXT
fi
}
mmloyal()
{
if [ -s $MMLOYAL_TXT ]
then
mail -s " mmloyal" $ZBB_META < $MMLOYAL_TXT
else
mail -s "!! MMLOYAL system monitor ERROR !!" $ZBB_META < $ERROR_TXT
fi
}
sc()
{
if [ -s $SC_TXT ]
then
mail -s "SC" $ZBB_META < $SC_TXT
else
mail -s "!! SC system monitor ERROR !!" $ZBB_META < $ERROR_TXT
fi
}
meta37()
{
if [ -s $META37_TXT ]
then
mail -s "192.168.254.37" $ZBB_META < $META37_TXT
else
mail -s "!! 37 system monitor ERROR !!" $ZBB_META < $ERROR_TXT
fi
}
uk66
sc
mmloyal
cctv
meta37
说明:
1,发送给多个接收者
# mail -s "hello"
2,查看发送情况
[root@monitor monitor]# sendmail -bp
/var/spool/mqueue (1 request)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
nB9ELx2M005177 191 Wed Dec 9 09:21 <>
(Deferred: Connection refused by metasphere.cn.)
<>
Total requests: 1
2009年12月10日更新
[root@monitor monitor]# vi 51_send.mail
#!/bin/bash
# /usr/monitor
#
#为了解决时间差的问题,让它每一小时执行一次。同时加入一个标签,不让他重复执行!
#00 */1 * * * /usr/monitor/send.mail
DATE_FILE=`date +%Y%m%d`
UK66_TXT=/usr/monitor/uk66/monitor_$DATE_FILE.txt
#引入的标签
UK66_PID=/usr/monitor/uk66/$DATE_FILE.pid
ERROR_TXT=/usr/monitor/error.txt
#MAIL ADD
ZBB_META=""
IT_UK=
#file exist and size more than zero
uk66()
{
if [ -s $UK66_TXT ]
then
if [ ! -e $UK66_PID ]
then
mail -s "SYSTEM MONITOR FOR 10.1.10.66" $ZBB_META $IT_UK < $UK66_TXT
touch ${UK66_PID}
fi
fi
}
uk66
# END