mutt+msmtp+inotify 监控文件发送邮件
2013-07-19 13:08
435 查看
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
安装完成后生成/usr/local/inotify/bin/inotifywait及/usr/local/inotify/bin/inotifywatch命令,inotifywait用来监控文件系统的更改,inotifywatch用来统计更改文件系统事件。
Inotifywait参数
详细参数请查看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下创建一个文件,看看是否有变化
如果测试失败,请查看系统的内核版本( uname –a)是否符合要求2.安装mutt+msmtp
3.配置mutt+msmtp+inotify编写监控脚本
编写发送邮件脚本
( 在执行脚本./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修改格式重新执行即可)
测试:
环境需求: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 |
Inotifywait参数
-m--monitor##始终监控 -r--recursive##递归 -q--quiet##打印监控事件 -e--event##指出要监控的事件,有:modify,delete,create,attrib等 --timefmt##时间格式--format##变化文件的详细信息 |
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 |
[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 |
邮件发送测试
发现发送报错 可能是选择的发送邮箱的邮件服务器有问题,换成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的系统测试发送成功了,所以要注意系统版本问题 |
Vimonitor.sh#!/bin/bashclearsrc=/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/bashclearpath_f=/tmp/monitor.logemail=458162532@qq.com function mutt_send(){/usr/local/bin/mutt -s "WARN" -c $email < $path_f} if [ -s $path_f ]; thenecho "mail send.......";sleep 1/usr/local/bin/mutt -s "WARN" -c $email < $path_fficat /dev/null > $path_f//发完邮件有对文件进行清空 |
后台运行监控脚本nohup /bin/bash /root/monitor.sh & 把发送邮件的脚本加入计划Crontab –e*/5****/bin/bash/root/sendmail.sh保存退出并重启服务/etc/init.d/crond restart |
相关文章推荐
- inotify + msmtp + mutt 监控文件变化并发送邮件的配置实例-msmtp的QQ邮箱配置
- shell脚本远程监控服务器,并用mutt+msmtp发送报警邮件
- Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
- ubuntu下使用mutt和msmtp发送邮件的简单配置
- Linux 服务器上面 使用 mutt+msmtp 进行定时邮件发送。
- mutt + msmtp发送邮件 配置
- Centos7安装Logwatch配合msmtp邮件客户端发送服务器监控分析日志
- Nagios+mutt+msmtp 无法发送邮件的问题!
- 在树莓派上采用命令行发送邮件:mutt和msmtp
- Linux下使用mutt和msmtp命令行发送邮件
- python监控文件并且发送告警邮件
- python监控文件,并且发送告警邮件
- centos配置mutt跟msmtp发送邮件
- ubuntu下使用mutt+msmtp发送邮件
- 采用Mutt及msmtp配置进行邮件发送
- Centos 安装邮件发送报警软件 MSMTP+MUTT
- 烂泥:ubuntu下配置msmtp+mutt发送邮件
- Linux部署msmtp+mutt发送邮件功能
- Linux下使用mutt和msmtp命令行发送邮件
- mutt加msmtp发送邮件