错误日志自动发送至个人邮箱脚本

#!/bin/bash
#
#systemlog.sh
#
maillogpath="/var/log/maillog"

syslogpath="/var/log/messages"

Y=`date '+%Y'`

M=`date |awk '{print $2}'`

D=`date '+%d'`

IP=`/sbin/ifconfig -a |grep inet |grep -v "127.0.0.1" |grep -v "inet6" |grep -v "192.168.*.*" |head -1 |awk '{split($2,a,":"); print a[2]}'`

Mailto="[email protected]"

awk 'BEGIN{print "=========================this is mail log=============================\n"}{if($1==mon && $2==day)print $0}' mon=$M day=$D $maillogpath |grep -v "2.0.0" >/root/log

awk 'BEGIN{print "=========================this is system log=============================\n"}{if($1==mon && $2==day)print $0}' mon=$M day=$D $syslogpath |grep -v "nagios" >>/root/log

if [ -s /root/log ];then

      less /root/log |mail -s "$IP's messages" $Mailto

else

       echo -e "system no error" |mail -s "$IP's messages" $Mailto

fi

    if [ `echo $?` -eq 0 ];then

          rm -rf /root/log

    fi

此脚本暂时只适用于每天定时发送系统日志与mail日志至个人邮箱,当然也可以发送如apache,tomcat之类的其它日志,可以排除一些无害的信息,只发送错误信息给个人邮箱,这样每天就不用去看繁琐的日志了。。当然也可以做日志服务器,这个脚本只是写来测试的,如果有需要使用的,最好修改下。

你可能感兴趣的:(log,shell,职场,休闲)