一、安装inotify-tools工具
[root@localhost ~]# yum install -y inotify-tools
二、验证inotifywait命令是否可用
# 可能会出现inotifywait无法使用或者找不到这个命令的现象,可以使用以下命令查看inotify-tools安装文件的路径
[root@aliyunserver ~]# rpm -ql inotify-tools
/usr/bin/inotifywait # inotifywait命令安装路径
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz
# 系统中已安装inotify-tools,命令无法使用的原因是/usr/bin/inotifywait没有在环境变量中,需手动添加
[root@localhost ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
export PATH="$PATH:/usr/bin" # 添加/usr/bin路径下的命令到环境变量中
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@localhost ~]# source .bashrc
三、配置文件实时监控脚本
# 实验场景:监控/tmp/vip.txt文件,文件内容如下,要求监控vip.txt文件,当该文件的内容发生改变或者被删除时,记录告警日志并将告警内容输出至/mnt/11.log日志文件中
[root@localhost ~]# cat /tmp/vip.txt
user:password --> root:1234
user:password --> root:12345
user:password --> root:123qaz
[root@localhost ~]# vim /bin/file_monitoring
#!/bin/bash
ip=`ifconfig | grep inet | awk 'NR==1{print $2}'`
hostname=`hostname`
# mkdir -p /mnt/11.log # 11.log为监控异常结果的日志输出文件
exec 2>> /mnt/111.log # 使用exec 2>> /mnt/111.log将标准错误输出重定向到日志文件
log_file="/mnt/111.log"
file_to_watch="/tmp/vip.txt" # 监控的文件
if [ -n "$file_to_watch" ]; then
/usr/bin/inotifywait -m -q -e create,move,delete,modify --format '%e %w %f' "$file_to_watch" | while read event file _ ;do
echo "$(date '+%Y-%m-%d %H:%M:%S') Hostname: $hostname IP: $ip - Event: $event, Filename: $file" >> "$log_file"
done
fi
# while read event file _ ;其中_表示引用空值,
#在这个命令中,event、file、operation三个变量名分别表示监视事件的类型、文件名和操作类型。具体来说:
# --format 参数来指定输出的格式,%e 表示事件类型,%w 表示监视的文件路径,%f 表示文件名
#- event:表示监视事件的类型,比如 "MODIFY" 表示文件被修改。
#- file:表示文件名,即发生事件的文件名。
#- operation:表示操作类型,比如 "ISDIR" 表示文件是一个目录。
# ——————————————————————————————————————————————————————————————————————————————————————————————————————
# $operation 变量没有值的原因是因为在 while 循环中,只有事件和文件名会被输出,而操作不会被输出。因此,可以将 operation 变量注释掉,或者将其注释为 `#operation`。
# ——————————————————————————————————————————————————————————————————————————————————————————————————————
# 监视事件的类型和操作类型的分类和区别:
# 监视事件的类型包括文件系统中发生的各种事件,如创建、删除、修改、移动等。
# 操作类型则是针对具体的文件或目录的操作,比如文件被修改、文件被删除等。
#区别在于监视事件类型是针对整个文件系统的事件,而操作类型是针对具体文件或目录的操作。
# ——————————————————————————————————————————————————————————————————————————————————————————————————————
#因此,当监视到 /tmp/vip.txt 文件的修改事件时,event 将获取 "MODIFY",file 将获取 "/tmp/vip.txt",operation 将获取相应的操>作类型
# 脚本添加执行权限
[root@localhost ~]# chmod 755 /bin/file_monitoring
[root@localhost ~]# ll /bin/file_monitoring
-rwxr-xr-x 1 root root 1100 Dec 26 17:25 /bin/file_monitoring
# 脚本放在系统后台运行实时监控文件状态变化
[root@localhost ~]# echo "/bin/zipmail &" >> /etc/rc.local
四、监控日志输出内容示例
[root@localhost mnt]# cat /mnt/111.log
Couldn't watch /tmp/vip.txt: No such file or directory
2023-12-26 18:39:30 Hostname: localhost IP: 192.168.0.187 - Event: MODIFY, Filename: /tmp/vip.txt