inotify enterprise work scenarios:
inotify is a powerful, fine-grained, asynchronous file system event monitoring mechanism, the software used has inotify, sersync
do:
1. Prepare the environment, configured rsync, may be determined push pull files, then the client with inotify
[root @ localhost ~] # uname -r # check kernel version, to be In the 2. 6 job .13 or more [root @ localhost ~] # LS the -l / proc / SYS / FS / inotify / # display three files the proof support -rw-r - r-- 1 root root 0 Sep 23 05 : 49 number of max_queued_events # monitor here -rw-r - r-- 1 root root 0 Sep 23 05 : 49 max_user_instances # -rw -r - r-- 1 root root 0 Sep 23 05 : 49 max_user_watches
2. Download the source code package inotify
[root@localhost tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Downloading remember to look at ah:
[root@localhost tools]# ls inotify-tools-3.14.tar.gz
3. Extract and compile source packages installed
[root@localhost tools]# tar -xf inotify-tools-3.14.tar.gz [root@localhost tools]# cd inotify-tools-3.14 [root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inontify-tools-3.14 [root@localhost inotify-tools-3.14]# make&&make install
4. Create a soft link
[root @ localhost Tools] # LN -s / usr / local / inontify-Tools- 3.14 / usr / local / inontify [root @ localhost inontify] # LL Total 16 drwxr -XR-the X- 2 root root 4096 Sep 23 06 : 07 bin # inoyify execute commands (binary) drwxr -XR-the X- 3 root root 4096 Sep 23 06 : 07 required to include # inotify header files drwxr -XR-the X- 2 root root 4096 Sep 23 06 : 07 lib dynamic link # the library drwxr -XR-the X-4 root root 4096 Sep 23 06 : 07 report this content share # Help Files
-------------------------------------------------- --------------------------------------------------
inotifywait commands commonly used parameters details
-r|--recursive Watch directories recursively.#递归查询目录 --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events).#打印监控事件的信息 -qq Print nothing (not even events). --format <fmt> Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt> strftime-compatible format string for use with %T in --format string. -c|--csv Print events in CSV format. -t|--timeout <seconds>#指定时间输出的格式 When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received.#始终保持时间监听状态 -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern>#排除文件或目录时,不区分大小写 Like --exclude but case insensitive. -e|--event<EVENT1> [-e | - Event <Event2> ...] # events need to be monitored can be specified by this parameter, as follows: the Listen for specific Event (S) the If Omitted, All Events are. Listened for #. Note that this argument is in line with the following parameters of Events: Access file or directory contents were the read # file or directory is read modify file or directory contents were written # contents of the file or directory is modified attrib file or directory attributes changed # file or directory property is changed close_write closed file or Directory, the After being the Opened in event of writeable mode # to write the file is closed close_nowrite closed file or Directory, the After being the Opened in the Read - only the MODE Closed File Directory or use Close, regardless of the Read / # files or directories the Write the MODE closed, regardless of read / write mode open file or directory opened # files in the directory is opened moved_to file or directory moved to watched directory file or directory is moved to another directory moved_from file or directory moved from Watched directory the move file or directory moved to or from Watched directory # file or directory is moved to another directory or move to another directory from the current directory created create file or directory within watched directory # file or directory re-create the current directory delete file or directory deleted within watched directory # file or directory is removed delete_self file directory wAS deleted or unmount file system or directory file containing unmounted # filesystem is unmounted
-------------------------------------------------- -------------------------------------------------- ------------
5. manual testing event monitoring
tests create:
[root@localhost inontify]# /usr/local/inontify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /backup
To open a connection and then create files in the directory to monitor, so that you can see the original content will change the CRT window
[root@localhost backup]# touch 6.txt 24/09/19 00:46 /backup/6.txt
Test delete and create:
[root@localhost inontify]# /usr/local/inontify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup
6. Develop real-time monitoring script
server / down / scriots: script to be placed in a fixed position
[root@localhost scriots]# vim inotify.sh #!/bin/sh cmd="/usr/local/inontify/bin/inotifywait" $cmd -mrq --format '%T %w%f' -e create,close_write,delete /backup|\ while read line do rsync -az --deldet $line rsync_backup@192.168.157.132::oldboy --password-file=/etc/rsync.password done
7. Event-related parameters Size:
[root@localhost scriots]# cat /proc/sys/fs/inotify/max_queued_events 16384#改成327679 [root@localhost scriots]# cat /proc/sys/fs/inotify/max_user_instances 128 [root@localhost scriots]# cat /proc/sys/fs/inotify/max_user_watches 8192#改成50000000
inotify disadvantages of:
1. not be greater than 200 concurrent file
2. General script, each time pushing all at once