监控软件inotify介绍

​ inotify可以监控同步数据服务器目录中信息的变化,采用异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过inotify可以监控文件系统中添加、删除,修改、移动等各种事件。

可以通过以下命令查看在内核文件中该模块:

1
2
# grep -i inotify /boot/config-3.10.0-1062.9.1.el7.x86_64 
CONFIG_INOTIFY_USER=y

列出下面的文件,说明服务器内核支持inotify

1
2
3
4
5
# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jun 11 09:26 max_queued_events
-rw-r--r-- 1 root root 0 Jun 11 09:26 max_user_instances
-rw-r--r-- 1 root root 0 May 12 10:53 max_user_watches

安装设置inotify

安装:

1
#yum install inotify-tools        #基于epel源

inotifywait命令

inotifywait命令常见选项

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
-m, --monitor 始终保持事件监听

-d, --daemon 以守护进程方式执行,和-m相似,配合-o使用

-r, --recursive 递归监控目录数据信息变化

-q, --quiet 输出少量事件信息

--exclude <pattern> 指定排除文件或目录,使用扩展的正则表达式匹配的模

式实现

--excludei <pattern> 和exclude相似,不区分大小写

-o, --outfile <file>打印事件到文件中,相当于标准正确输出

-s, --syslogOutput 发送错误到syslog相当于标准错误输出

--timefmt <fmt> 指定时间输出格式

--timefmt <fmt>时间格式,参考 man 3 strftime

%Y 年份信息,包含世纪信息

%y 年份信息,不包括世纪信息

%m 显示月份,范围 01-12

%d 每月的第几天,范围是 01-31

%H 小时信息,使用 24小时制,范围 00-23

%M 分钟,范围 00-59

示例:

--timefmt "%Y-%m-%d %H:%M"

--format <fmt> 指定的输出格式;即实际监控输出内容

-e 指定监听指定的事件,如果省略,表示所有事件都进行监听

示例: -e create,delete,moved_to,close_write, attrib
Events: 事件
access file or directory contents were read 文件或目录被(访问)读取r
modify file or directory contents were written 文件或目录被写入w
attrib file or directory attributes changed 文件或目录属性变更【理由:chmod更改属性】
close_write file or directory closed, after being opened in writeable mode 文件或目录被写关闭【理由:文件内容被更改】
close_nowrite file or directory closed, after being opened in read-only mode 文件或目录以只读方式打开后关闭
close file or directory closed, regardless of read/write mode 文件或目录被用编辑器(不管是读或写)关闭
open file or directory opened 文件或目录被用编辑器打开
moved_to file or directory moved to watched directory 文件或目录被移动进来【理由:mv目录内重命名】
moved_from file or directory moved from watched directory 文件或目录被移动出去
move file or directory moved to or from watched directory 文件或目录不管是移出或移进
create file or directory created within watched directory 文件或目录被创建【理由:mkdir创建目录】
delete file or directory deleted within watched directory 文件或目录被删除【理由:rm删除】
delete_self file or directory was deleted 文件或目录自删除
unmount file system containing file or directory unmounted 文件系统取消挂载

常用组合

持续后台监控,并记录日志

inotifywait -mrq /data/www -o /root/inotify.log --timefmt “%Y-%m-%d %H:%M” --format “%T %w%f event: %e”

例:

持续后台监控特定事件

1
inotifywait -mrq /data -o /root/inotify.log --timefmt "%F %H:%M" --format "%T %w%f event: %;e" -e create,delete,moved_to,close_write,attrib

inotifywatch命令:

收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计

实现同步步骤:

192.168.12.27 inotify服务器

192.168.12.57 rsync服务器

监控inotify服务器数据变化自动同步到 rsync服务器上

192.168.12.57 :

安装软件包

yum install rsync

服务器端修改rsync配置文件

vi /etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.12.0/24
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass

生成服务器验证文件

1
2
#echo "rsyncuser:123456" > /etc/rsync.pass
#chmod 600 /etc/rsync.pass

创建文件用于存放备份文件

1
2
#mkdir /backup
#chmod 600 /backup/

启动rsyncd服务

1
2
#rsync --daemon     可加入/etc/rc.d/rc.local实现开机启动
#systemctl start rsyncd

安装软件包,创建需要监控备份的文件夹

1
2
3
#yum install inotify-tools
#mkdir /data/www
#ls /data/www/

生成密码文件,如果没有会变成交互式命令,创建后rsync会使用该密码自动同步

1
2
shell#echo "123456" > /etc/rsync.pass
#chmod 600 /etc/rsync.pass

测试能否同步到rsync服务器

1
2
3
4
5
6
7
8
9
#rsync -avz --password-file=/etc/rsync.pass /data/www/ rsyncuser@192.168.12.57::backup
57::backup
sending lncremental file list
./
f1.txt
f2.txt

sent 165 bytes received 57 bytes 444.00 bytes/sec
total size is 0 speedup is 0.00

创建实时监控脚本

vim inotify_rsync.sh

1
2
3
4
5
6
7
#!/bin/bash
SRC='/data/www/'
DEST='rsyncuser@192.168.12.57::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done

最后把脚本添加到/etc/rc.local使开机启动就完成了。