配置文件变更的邮件通知

系统中有各种配置文件,程序员经常需要变更这些重要的文件,但不一定会通知SA。希望在程序员变更之后,能够有邮件发送到SA那里。

由于需要监控多台服务器的相关文件,为了统一化管理,将一台机器上安装ftp服务,并安装mailx,作为邮件通知的服务器。其他机器将变更结果通过ftp传递给该服务器。变更的检查就通过diff命令即可。

步骤如下:

1,在邮件通知的服务器上,安装ftp服务,并安装mailx。mailx的版本要看好。在/etc/nail.rc末尾下增加

View Code
set from=a@163.com
set smtp=smtp.163.com
set smtp-auth-user=a
set smtp-auth-password=123456
set smtp-auth=login

2,然后就是检查ftp中是否有文件变化的脚本,其实就是diff命令,最后一行即是发送邮件的命令:

View Code
file_list="/var/ftp/pub/85/changes.log"
shell_root='/script'
is_empty='1'
is_all_empty='1'
echo "" > $shell_root/mail_text.txt
#[ -x "$shell_root/mail_text.log" ] && rm $shell_root/mail_text.log
my_email_addr='aa@163.com'
for i in $file_list
do
        is_empty=$(echo '1')
        root_name=$(echo $i | sed 's/\/.*//g')
        file_name=$(echo $i | sed 's/.*\///g')
        cp $i $i.new
        diff_result_file=$(diff $i.new $i.old)
        if [ "$diff_result_file"x != ""x ] ; then
                is_empty=$(echo '0')
                is_all_empty=$(echo '0')
                echo $file_name >> $i.log
        fi
        [ "$is_empty"x == "0"x ] && echo $i | sed 's/^M//g' >> $shell_root/mail_text.txt
        diff $i.new $i.old | sed 's/^M//g' >> $shell_root/mail_text.txt
        cp $i.new $i.old
        rm $i.new
done
[ "$is_all_empty"x == "0"x ] && mailx -s "Detect Changes" $my_email_addr < $shell_root/mail_text.txt

3,然后在监控的机器上复制下面的脚本,其实也就是diff,然后是ftp

View Code
file_list=""
cmd_list='crontab;-l mount'
shell_list=""
shell_root='/script/detect'
ftp_name='ftp'
ftp_pwd=''
ftp_ip='172.16.5.2'

is_empty='1'

cd $shell_root
/usr/bin/ftp -n $ftp_ip <<!
user ftp ^M
cd /pub/89
ascii
prompt
mput *.old
close
bye
!
for i in $file_list
do
        file_name=$(echo $i | sed 's/.*\///g')
        cp $i $shell_root/$file_name.new
        diff_result_file=$(diff $shell_root/$file_name.new $shell_root/$file_name.old)
        if [ "$diff_result_file"x != ""x ] ; then
                is_empty=$(echo '0')
                echo $file_name >> $shell_root/changes.log
        fi
        diff $shell_root/$file_name.new $shell_root/$file_name.old >> $shell_root/changes.log
        cp $shell_root/$file_name.new $shell_root/$file_name.old
        rm $shell_root/$file_name.new
done
for j in $cmd_list
do
        done_j=$(echo $j | sed 's/;/ /g')
        $done_j 1>$shell_root/$j.new
        diff_result=$(diff $shell_root/$j.new $shell_root/$j.old)
        if [ "$diff_result"x != ""x ] ; then
                is_empty=$(echo '0')
                echo $j >> $shell_root/changes.log
        fi
        diff $shell_root/$j.new $shell_root/$j.old >> $shell_root/changes.log
        cp $shell_root/$j.new $shell_root/$j.old
        rm $shell_root/$j.new
done
for k in $shell_list
do
        sh $shell_root/$k > $shell_root/$k.new
        diff_result_shell=$(diff $shell_root/$k.new $shell_root/$k.old)
        if [ "$diff_result_shell"x != ""x ] ; then
                is_empty=$(echo '0')
                echo &k >> $shell_root/changes.log
        fi
        diff $shell_root/$k.new $shell_root/$k.old >> $shell_root/changes.log
        cp $shell_root/$k.new $shell_root/$k.old
        rm $shell_root/$k.new
done
[ "$is_empty"x == "0"x ] && date >> $shell_root/changes.log
cd $shell_root
/usr/bin/ftp -n $ftp_ip <<!
user ftp ^M
cd /pub/89
ascii
prompt
put changes.log
close
bye

!

4,在服务器上用crontab创建的排程,每天晚上2点的时候运行检查一下。

参考:

http://www.cnblogs.com/xiaoshi1991/archive/2012/09/19/2694465.html

原文地址:https://www.cnblogs.com/zhaofei2013/p/2969307.html