mutt+msmtp+inotify 監控文件發送郵件

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 [email protected]

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 [email protected]

set envelope_from=yes

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

defaults

account zbill

host mail.yylog.org

from [email protected]

auth login

port 25

tls off

user [email protected]

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="[email protected]"' >>/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的系統測試發送成功了,所以要注意系統版本問題

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 protected]


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 ~]#




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章