mutt+msmtp+inotify监控文件发送邮件


环境需求:inotify要求系统内核版本为2.6.13以上

要求redhat系统必须为5.0以上


软件需求:inotify-tools-3.14.tar.gz

mutt-1.5.21.tar.gz

 msmtp-1.4.30.tar.bz2


实现结果:通用监控指定目录中文件的增减改等操作,并把操作的动作发到指定邮箱通知


步骤:

1.安装inotify

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar -zxvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure --prefix=/usr/local/inotify

make&&  make install

安装完成后生成/usr/local/inotify/bin/inotifywait/usr/local/inotify/bin/inotifywatch命令,inotifywait用来监控文件系统的更改,inotifywatch用来统计更改文件系统事件。


Inotifywait参数

-m--monitor##始终监控

-r--recursive##递归

-q--quiet##打印监控事件

-e--event##指出要监控的事件,有:modify,delete,create,attrib

--timefmt##时间格式

--format##变化文件的详细信息

详细参数请查看http://muxu303.blog.163.com/blog/static/512801920121204449935/


inotify测试

使用inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w%e' -e modify,delete,create,attrib 监控/data目录,然后在/data下创建一个文件,看看是否有变化

/usr/local/inotify/bin/inotifywait -mrq  --timefmt '%d/%m/%y %H:%M' --format '%T %w%e' -e modify,delete,create,attrib  /data

16/02/13 11:01 /data/CREATE

16/02/13 11:01 /data/MODIFY

echo "notify data  test...">/data/test.bat

如果测试失败,请查看系统的内核版本( uname –a)是否符合要求

2.安装mutt+msmtp

[root@mymail ~]# tar -jxvf msmtp-1.4.30.tar.bz2

[root@mymail ~]# cd  msmtp-1.4.30

[root@mymail msmtp-1.4.30]#  ./configure --prefix=/usr/local/msmtp

[root@mymail msmtp-1.4.30]#  make

[root@mymail msmtp-1.4.30]# make  install

[root@mymail ~]# tar -zxvf mutt-1.5.21.tar.gz

[root@mymail ~]# cd  mutt-1.5.21

[root@mymail mutt-1.5.21]# ./configure  --prefix=/usr/local/mutt

[root@mymail mutt-1.5.21]#make

[root@mymail mutt-1.5.21]#make  install

Msmtp+mutt配置

[root@mymail mutt-1.5.21]# mkdir  -p /usr/local/msmtp/etc

[root@mymail mutt-1.5.21]# vi  /root/.msmtprc

host mail.yylog.org #smtp地址

tls off

auth plain

from zbill@yylog.org

user zbill

password 123456789

[root@mymail mutt-1.5.21]# vi  /root/.muttrc

set  sendmail="/usr/local/msmtp/bin/msmtp"#指定msmtp安装位置

set use_from=yes

set from=zbill@yylog.org

set envelope_from=yes

[root@mymail mutt-1.5.21]# vi  /usr/local/msmtp/etc/msmtprc

defaults

account zbill

host mail.yylog.org

from zbill@yylog.org

auth login

port 25

tls off

user zbill@yylog.org

password123456789

account default : zbill

logfile /usr/local/msmtp/log/msmtp.log

[root@mymail mutt-1.5.21]#  mkdir -p /usr/local/msmtp/log

[root@mymail mutt-1.5.21]# echo  'set sendmail="/usr/local/msmtp/bin/msmtp"' >>/etc/Muttrc

[root@mymail mutt-1.5.21]#  echo "set use_from=yes" >>/etc/Muttrc

[root@mymail mutt-1.5.21]# echo  'set realname="zbill@yylog.org"' >>/etc/Muttrc

[root@mymail mutt-1.5.21]# echo  'set editor="vim"' >>/etc/Muttrc

[root@mymail mutt-1.5.21]# ln  -s /usr/local/msmtp/bin/msmtp /usr/bin

邮件发送测试

发现发送报错

mutt+msmtp+inotify 监控文件发送邮件_mutt+msmtp+inotify 监

可能是选择的发送邮箱的邮件服务器有问题,换成smtp.163.com后测试发送正常

[root@mymail  mutt-1.5.21]# /usr/local/mutt/bin/mutt -s "test" -c 458162532@qq.com</.sh/1.sh

发现未收到邮件,查看系统版本 cat /etc/issue redhat4.6,于是换了台redhat5.2的系统测试发送成功了,所以要注意系统版本问题

3.配置mutt+msmtp+inotify

编写监控脚本

Vimonitor.sh

#!/bin/bash

clear

src=/root/a//监控的目录

/usr/local/inotify/bin/inotifywait -m -r -d -o  /tmp/monitor.log --timefmt '%F%T' --format '%T%w%f%e' -e  modify,attrib,move,close_write,create,delete,delete_self $src



编写发送邮件脚本

Vi sendmail.sh

#!/bin/bash

clear

path_f=/tmp/monitor.log

email=458162532@qq.com


function mutt_send()

{

/usr/local/bin/mutt -s "WARN" -c $email < $path_f

}


if [ -s $path_f ]; then

echo "mail  send.......";sleep 1

/usr/local/bin/mutt -s "WARN" -c $email < $path_f

fi

cat /dev/null > $path_f//发完邮件有对文件进行清空




( 在执行脚本./sendmail.sh的时候会有报错:-bash: ./file.sh: /bin/bash^M: bad interpreter: No such file or directory

错误原因很有可能是你的脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, ASCII码分别是0x0D, 0x0A.

查看脚本格式:setff?会显示fileformat=dos使用setff=unix修改格式重新执行即可)



后台运行监控脚本

nohup /bin/bash /root/monitor.sh &


把发送邮件的脚本加入计划

Crontab –e

*/5****/bin/bash/root/sendmail.sh

保存退出并重启服务

/etc/init.d/crond restart



测试:

[root@localhost ~]#  cd /root/a

[root@localhost a]#  ls

123abcrf

[root@localhost a]#  rm -f *

[root@localhost a]#  cat /tmp/monitor.log

2013-07-1904:40:04/root/a/123DELETE

2013-07-1904:40:04/root/a/abcDELETE

2013-07-1904:40:04/root/a/rfDELETE

[root@localhost a]#  cd

[root@localhost ~]#  ./sendmail.sh

mail send.......

[root@localhost ~]#  cat /tmp/monitor.log

[root@localhost ~]#

mutt+msmtp+inotify 监控文件发送邮件_mutt+msmtp+inotify 监_02