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
[root@centos6-vm01 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= "notice@wangshibo.com"
Mail_Pass= "notice@123"
Mail_From= "notice@wangshibo.com"
Mail_To= "wangshibo@wangshibo.com"
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  in  ${CheckDir[@]}
do
/bin/find  ${i} - type  f | xargs  md5sum >> ${FileDir} /old .log
done
}
NewFile () {
for  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
[root@centos6-vm01 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
[root@centos6-vm01  test ] # cd /opt/
[root@centos6-vm01 opt] # cd /data/test/
[root@centos6-vm01  test ] # ll
total 0
[root@centos6-vm01  test ] # mkdir haha
[root@centos6-vm01  test ] # echo "123456" > haha/heihei
[root@centos6-vm01  test ] # ll
total 4
drwxr-xr-x. 2 root root 4096 Jan 10 01:42 haha
[root@centos6-vm01  test ] # echo "abcdef" > test.txt
[root@centos6-vm01  test ] # ll
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
 
执行监控脚本
[root@centos6-vm01  test ] # sh -x /opt/file_monit.sh
 
注意:当首次执行脚本的时候,由于所监控的目录下的文件没有变动,所以不会发送邮件!
 
查看对比后的日志
[root@centos6-vm01  test ] # ll -d /var/CheckFile/
drwxr-xr-x. 2 root root 4096 Jan 10 01:44  /var/CheckFile/
[root@centos6-vm01  test ] # ll /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
 
[root@centos6-vm01  test ] # cat /var/CheckFile/diff2018-01-10__01\:44\:30.log
[root@centos6-vm01  test ] # cat /var/CheckFile/new.log                       
[root@centos6-vm01  test ] # cat /var/CheckFile/old.log
237267ea7fefa88360c22ab6fd582d7e   /data/test/ .hhhh.swp
5ab557c937e38f15291c04b7e99544ad   /data/test/test .txt
f447b20a7fcbf53a5d5be013ea0b15af   /data/test/haha/heihei
 
 
==============================================================================================================================
现在开始对 /data/test 目录下的文件做下变动
[root@centos6-vm01  test ] # echo "aaaaaa" >> test.txt
[root@centos6-vm01  test ] # touch haha/bobo
[root@centos6-vm01  test ] # mkdir heihei
[root@centos6-vm01  test ] # ll
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
 
执行监控脚本
[root@centos6-vm01  test ] # sh -x /opt/file_monit.sh
 
 
查看对比后的日志
[root@centos6-vm01  test ] # ll /var/CheckFile/
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
[root@centos6-vm01  test ] # cat /var/CheckFile/diff2018-01-10__01\:47\:41.log
2,3c2
< 4533551682ca49b2f9b1f2829bf3b29d   /data/test/test .txt
< d41d8cd98f00b204e9800998ecf8427e   /data/test/haha/bobo
---
> 5ab557c937e38f15291c04b7e99544ad   /data/test/test .txt
 
[root@centos6-vm01  test ] # cat /var/CheckFile/old.log
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
[root@centos6-vm01  test ] # crontab -e
* /5  * * * *   /bin/bash  -x  /opt/file_monit .sh >  /dev/null  2>&1

以上脚本也可以用于检测linux系统重要文件是否被更改,只需将检查的目录由脚本中的/data/test改为/etc即可!

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
分类:  Shell, 监控系统
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/8260032.html,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1月前
|
Linux
关于Linux目录访问函数总结
关于Linux目录访问函数总结
13 1
|
1月前
|
Prometheus 运维 监控
linux磁盘I/O监控
【4月更文挑战第1天】在Linux中监控磁盘I/O性能至关重要,工具如iostat(-d显示磁盘统计)、iotop(进程级I/O查看)、vmstat、/proc/diskstats(详细统计信息)、Node Exporter(Prometheus集成)和Zabbix(动态监控与LLD)提供关键指标,如IOPS、吞吐量、利用率和服务时间,助力系统优化和故障排查。
43 4
linux磁盘I/O监控
|
2月前
|
算法 Linux C语言
【Linux系统编程】深入理解Linux目录操作:文件夹位置指针操作函数(telldir,seekdir,rewinddir)
【Linux系统编程】深入理解Linux目录操作:文件夹位置指针操作函数(telldir,seekdir,rewinddir)
23 0
|
6天前
|
安全 Linux
【亮剑】如何在Linux使用 chattr 命令更改文件或目录的扩展属性?
【4月更文挑战第30天】`chattr`是Linux中用于管理文件和目录扩展属性的命令,影响文件系统处理方式。常用属性包括:`a`(追加)、`i`(不可变)、`s`(安全删除)和`S`(同步更新)。通过`chattr [选项] &lt;模式&gt; &lt;文件或目录&gt;`设置属性,如`chattr +i &lt;文件名&gt;`使文件不可变,`-i`移除不可变属性。`lsattr`用于查看属性。注意,只有root用户有权更改属性,不是所有文件系统都支持所有属性,且更改关键文件属性前应备份。`chattr`有助于提升系统安全性和数据保护。
|
6天前
|
存储 监控 Linux
【亮剑】Linux中最低调、最易让人忽视的tmp目录,原来用处那么大!
【4月更文挑战第30天】`/tmp`目录在Linux系统中扮演着重要角色,用于存储临时文件,涉及程序运行、系统操作、用户文件及网络通信。它在系统维护、软件开发、数据处理和网络操作等场景中广泛应用。为保障系统稳定和数据安全,需进行权限控制、定期清理、设置磁盘配额、安全审计以及用户教育。理解和管理`/tmp`目录对于优化系统性能和防范风险至关重要。
|
6天前
|
人工智能 Unix Linux
轻松驾驭Linux命令:账户查看、目录文件操作详解
轻松驾驭Linux命令:账户查看、目录文件操作详解
11 1
|
6天前
|
Linux 数据安全/隐私保护
Linux常用命令大全:一杯水时间让你掌握!(附目录和快捷键)(下)
Linux常用命令大全:一杯水时间让你掌握!(附目录和快捷键)
21 2
|
6天前
|
Linux Shell Python
Linux常用命令大全:一杯水时间让你掌握!(附目录和快捷键)(上)
Linux常用命令大全:一杯水时间让你掌握!(附目录和快捷键)
14 2
|
7天前
|
Linux Python
【专栏】Linux 中某个目录中的文件数如何查看?这篇教程分分钟教会你!
【4月更文挑战第28天】在Linux中查看目录文件数的方法包括:使用`ls`结合`wc -l`,如`ls &lt;directory_path&gt; | wc -l`;使用`find`命令,如`find &lt;directory_path&gt; -type f | wc -l`;使用`tree`命令,如`tree &lt;directory_path&gt;`(可能需额外安装);以及通过编程方式,例如Python代码实现。注意权限、效率和选择适用方法以提升操作效率。本文提供了详细步骤和示例,助你轻松掌握!
|
7天前
|
存储 数据挖掘 Linux
【专栏】教你如何快速在 Linux 中找到某个目录中最大的文件
【4月更文挑战第28天】在 Linux 中查找目录中最大文件的方法包括:使用 `du` 结合 `sort`,`find` 结合 `xargs` 和 `sort`,以及编写 Python 脚本。这些技巧适用于服务器管理、数据分析和文件清理等场景,能帮助用户快速定位大文件进行分析、清理或优化。注意文件权限、目录深度和文件系统类型可能影响结果,可结合其他命令增强功能。