linux下监控某个目录是否被更改
需求:对一个目录(比如/data/test)进行监控,当这个目录下文件或子目录出现变动(如修改、创建、删除、更名等操作)时,就发送邮件!
针对上面的需求,编写shell脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
[[email protected] opt] # vim file_monit.sh
#!/bin/bash #此脚本用于检测linux系统重要文件是否被改动,如果改动则用邮件报警 #建议用定时任务执行此脚本,如每5分钟执行一次,也可修改此脚本用于死循环检测 #Ver:1.0 #http://www.cnblogs.com/kevingrace #定义验证文件所在目录 FileDir= '/var/CheckFile'
#获取主机名或自己定义 HostName=$( hostname )
#定义邮件参数:xmtp地址,发送邮件帐号,发送邮件密码,接收邮件地址,邮件主题,邮件内容 Mail_Smtp= "smtp.wangshibo.com"
Mail_User= "[email protected]"
Mail_Pass= "[email protected]"
Mail_From= "[email protected]"
Mail_To= "[email protected]"
Mail_Subject= "${HostName}:There are changes to system files"
Mail_Conntent= "${HostName}:There are changes to system files"
#定义需要验证的文件目录。这里我监控的是/data/test目录 CheckDir=( /data/test ) #生成所定义需验证的文件样本日志函数 OldFile () { for i in ${CheckDir[@]}
do /bin/find ${i} - type f | xargs md5sum >> ${FileDir} /old .log
done } NewFile () { for i in ${CheckDir[@]}
do /bin/find ${i} - type f | xargs md5sum >> ${FileDir} /new .log
done } #生成所定义文件新日志函数 SendEMail () { /usr/local/bin/sendEmail -f $Mail_From -t $Mail_To -s $Mail_Smtp -u $Mail_Subject -xu $Mail_User -xp $Mail_Pass -m $Mail_Conntent
} if [ ! -d ${FileDir} ]
then mkdir ${FileDir}
fi #假如验证文件目录不存在则创建 if [ ! -f ${FileDir} /old .log ]
then OldFile fi #假如没有安装sendEmail则安装 if [ ! -f /usr/local/bin/sendEmail ]
then cd /usr/local/src/
wget http: //caspian .dotconf.net /menu/Software/SendEmail/sendEmail-v1 .56. tar .gz
tar -xf sendEmail-v1.56. tar .gz
cd sendEmail-v1.56
cp sendEmail /usr/local/bin
chmod 0755 /usr/local/bin/sendEmail
fi #生成新验证日志 NewFile #新验证日志与样本日志进行比较 /usr/bin/diff ${FileDir} /new .log ${FileDir} /old .log >${FileDir} /diff .log
Status=$? #假如比较结果有变化,则发送邮件报警 if [ ${Status} - ne 0 ]
then Mail_Conntent= "$(grep '<' ${FileDir}/diff.log |awk '{print $3}')"
SendEMail fi #清除新旧日志,把比较结果进行备份 /bin/mv -f ${FileDir} /diff .log ${FileDir} /diff $( date +%F__%T).log
cat /dev/null > ${FileDir} /old .log
cat /dev/null > ${FileDir} /new .log
#重新生成样本日志 OldFile #删除目录内30天以前的比较结果备份文件 /bin/find ${FileDir} - type f -mtime +30 | xargs rm -f
|
确保本机能连上shell脚本中指定的smtp服务器的25好端口
1
2
3
4
5
|
[[email protected] opt] # telnet smtp.wangshibo.com 25
Trying 223.252.214.65... Connected to smtp.wangshibo.com. Escape character is '^]' .
220 icoremail.net Anti-spam GT for Coremail System (icoremail-gateway-smtp[20170531])
|
下面开始测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
[[email protected] opt] # cd /data/test/
total 0 total 4 drwxr-xr-x. 2 root root 4096 Jan 10 01:42 haha total 8 drwxr-xr-x. 2 root root 4096 Jan 10 01:42 haha -rw-r--r--. 1 root root 7 Jan 10 01:42 test .txt
执行监控脚本 注意:当首次执行脚本的时候,由于所监控的目录下的文件没有变动,所以不会发送邮件! 查看对比后的日志 drwxr-xr-x. 2 root root 4096 Jan 10 01:44 /var/CheckFile/
total 4 -rw-r--r--. 1 root root 0 Jan 10 01:44 diff2018-01-10__01:44:30.log -rw-r--r--. 1 root root 0 Jan 10 01:44 new.log -rw-r--r--. 1 root root 166 Jan 10 01:44 old.log 237267ea7fefa88360c22ab6fd582d7e /data/test/ .hhhh.swp
5ab557c937e38f15291c04b7e99544ad /data/test/test .txt
f447b20a7fcbf53a5d5be013ea0b15af /data/test/haha/heihei
============================================================================================================================== 现在开始对 /data/test 目录下的文件做下变动
total 12 drwxr-xr-x. 2 root root 4096 Jan 10 01:47 haha drwxr-xr-x. 2 root root 4096 Jan 10 01:47 heihei -rw-r--r--. 1 root root 14 Jan 10 01:47 test .txt
执行监控脚本 查看对比后的日志 total 8 -rw-r--r--. 1 root root 0 Jan 10 01:44 diff2018-01-10__01:44:30.log -rw-r--r--. 1 root root 179 Jan 10 01:47 diff2018-01-10__01:47:41.log -rw-r--r--. 1 root root 0 Jan 10 01:47 new.log -rw-r--r--. 1 root root 221 Jan 10 01:47 old.log 2,3c2 < 4533551682ca49b2f9b1f2829bf3b29d /data/test/test .txt
< d41d8cd98f00b204e9800998ecf8427e /data/test/haha/bobo
--- > 5ab557c937e38f15291c04b7e99544ad /data/test/test .txt
237267ea7fefa88360c22ab6fd582d7e /data/test/ .hhhh.swp
4533551682ca49b2f9b1f2829bf3b29d /data/test/test .txt
d41d8cd98f00b204e9800998ecf8427e /data/test/haha/bobo
f447b20a7fcbf53a5d5be013ea0b15af /data/test/haha/heihei
通过上面的 diff 日志,可以看到新变动的文件或子目录已经记录到日志里了。
|
查看邮件,就能看到/data/test目录下变动的文件或子目录信息了
通过crontab定时任务,每5分钟执行一次检查:
1
2
|
* /5 * * * * /bin/bash -x /opt/file_monit .sh > /dev/null 2>&1
|
以上脚本也可以用于检测linux系统重要文件是否被更改,只需将检查的目录由脚本中的/data/test改为/etc即可!
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************