zoukankan      html  css  js  c++  java
  • inotify高效监控Linux文件系统事件

    inotify-tools3.14版本永久链接

    链接: inotify-tools-3.14
    提取码:15t5

     概要:为什么需要监控系统文件?

    • 跟踪某些关键的系统文件的变化
    • 通知配置文件的改变
    • 监控某个分区磁盘的整体情况

    Inotify到底是做什么的?

      Inotify是一种文件变化通知机制,linux内核从2.6.13 开始引入。

      在BSD和Mac OS 系统中比较有名的是kqueue,他可以高效的实时跟踪Linux文件系统的变化。近些年来,以fsnotify 作为后端,几乎所有的主流Linux发行版都支持Inotify机制。

    inotify的安装步骤:

    一、安装:

    1.1.1、查看服务器内核是否支持inotify:

      uname -r查看版本是否在2.6以上
      ll /proc/sys/inotify 查看是否有以下三个目录,有说明支持
    -rw-r--r--. 1 root root 0 7月   1 14:47 max_queued_events
    -rw-r--r--. 1 root root 0 7月   1 14:47 max_user_instances
    -rw-r--r--. 1 root root 0 7月   1 14:47 max_user_watches

    1.1.2、安装inotify-tools

    yum install make gcc gcc-c++  //安装编译工具
    上传 inotify工具包到/usr/local/src下
    cd /usr/local/src 
    tar zxf inotify-tools-3.14.tar.gz  //解压
    cd inotify-tools-3.14 
    ./configure --prefix=/usr/local/inotify  //配置
    make && make install

    1.1.3、设置系统环境变量,添加软链接

    echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
    source /etc/profile.d/inotify.sh #使设置立即生效
    echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
    ln -s /usr/local/inotify/include /usr/include/inotify 

    1.1.4、修改inotify默认参数 (inotify默认内核参数数值大小)

    查看系统默认参数值
    sysctl -a | grep max_queued_events
    结果是:fs.inotify.max_queued_events = 16384
    sysctl -a | grep max_user_watches
    结果是:fs.inotify.max_user_watches = 8192
    sysctl -a | grep max_user_instances
    结果是:fs.inotify.max_user_instances = 128

    修改参数为:

    sysctl -w fs.inotify.max_queued_events="99999999"
    
    sysctl -w fs.inotify.max_user_watches="99999999"
    
    sysctl -w fs.inotify.max_user_instances="65535"

    1.1.5、修改服务的配置文件:

    vi /etc/sysctl.conf //添加以下代码
    fs.inotify.max_queued_events=99999999
    fs.inotify.max_user_watches=99999999
    fs.inotify.max_user_instances=65535

    参数说明:

    max_queued_events:
    inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
    max_user_watches:
    要同步的文件包含多少目录,可以用:find /{filename} -type d | wc -l 统计,
        必须保证max_user_watches值大于统计结果(这里{filename}为同步文件目录) max_user_instances: 每个用户创建inotify实例最大值

    此时安装完成。

    二、实验是否可以使用此服务进行监控:

    2.1.1、测试1:

    inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e access,modify,close,move,open,delete,create,attrib /etc/firewalld/ 

    这时再开一个窗口,会输出文件的变动情况,具体参数如下方介绍。

    2.1.2、测试2:

    [root@localhost ~]# inotifywatch -v -e access -e modify -t 60 -r /etc/
    Establishing watches...
    Setting up watch(es) on /etc/
    OK, /etc/ is now being watched.
    Total of 598 watches.
    Finished establishing watches, now collecting statistics.
    Will listen for events for 60 seconds.
    total  access  modify  filename
    55     55      0       /etc/
    9      9       0       /etc/profile.d/
    7      2       5       /etc/keepalived/
    6      6       0       /etc/pam.d/

    三:具体参数介绍:

    语法:inotifywait

    inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ] [ ... ]
    –fromfile
    从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
    -m, –monitor
    接收到一个事情而不退出,无限期地执行。默认的行为是接收到一个事情后立即退出。
    -d, –daemon
    跟–monitor一样,除了是在后台运行,需要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。
    -o, –outfile
    输出事情到一个文件而不是标准输出。
    -s, –syslog
    输出错误信息到系统日志
    -r, –recursive
    监视一个目录下的所有子目录。
    -q, –quiet
    指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。
    –exclude
    正则匹配需要排除的文件,大小写敏感。
    –excludei
    正则匹配需要排除的文件,忽略大小写。
    -t , –timeout
    设置超时时间,如果为0,则无限期地执行下去。
    -e , –event
    指定监视的事件。
    -c, –csv
    输出csv格式。
    –timefmt
    指定时间格式,用于–format选项中的%T格式。
    –format
    指定输出格式。
    %w 表示发生事件的目录
    %f 表示发生事件的文件
    %e 表示发生的事件
    %Xe 事件以“X”分隔
    %T 使用由–timefmt定义的时间格式

    语法:inotifywatch

    inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]
    参数:
    -h, –help
    输出帮助信息
    -v, –verbose
    输出详细信息
    @
    排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
    –fromfile
    从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
    -z, –zero
    输出表格的行和列,即使元素为空
    –exclude
    正则匹配需要排除的文件,大小写敏感。
    –excludei
    正则匹配需要排除的文件,忽略大小写。
    -r, –recursive
    监视一个目录下的所有子目录。
    -t , –timeout
    设置超时时间
    -e , –event
    只监听指定的事件。
    -a , –ascending
    以指定事件升序排列。
    -d , –descending
    以指定事件降序排列。

    3.1.1、示例:

    inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib /data

    3.1.2、选项参数:

    -m:表示持续监视变化。 
    -r:表示使用递归形式监视目录。 
    -q:表示减少冗余信息,只打印出需要的信息。 
    -e:表示指定要监视的事件列表。 
    --timefmt是指定时间的输出格式。 
    --format指定文件变化的详细信息。
    其中 %w:表示监听的目录,%f表示触发事件的文件

    3.1.3、事件说明:

    access 访问,读取文件。 
    modify 修改,文件内容被修改。 
    attrib 属性,文件元数据被修改。 
    move 移动,对文件进行移动操作。 
    create 创建,生成新文件 
    open 打开,对文件进行打开操作。 
    close 关闭,对文件进行关闭操作。 
    delete 删除,文件被删除。
  • 相关阅读:
    JS 数组去重复值
    git 上传项目 到github
    IntelliJ IDEA 创建maven 项目 并且,将springMVC 与Mybatis 整合
    easyui datagrid 动态添加columns属性
    codeMirror的简单使用,js比较文本差异(标注出增删改)
    sql注入
    Web For Pentester靶场
    一些数据库
    nginx的使用场景
    dockerfile
  • 原文地址:https://www.cnblogs.com/security-guard/p/13224699.html
Copyright © 2011-2022 走看看