Linux Inotify详解和使用

本文部分摘自  https://blog.csdn.net/longwang155069/article/details/54016789

适用场景:

Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多

日志采集系统中,日志文件的改动。

文件系统中文件或者目录是否变动。

监控ftp服务器收到的文件,(这种情况可以监控mask的写关闭事件IN_CLOSE_WRITE,这样可以防止文件没有写完就处理数据)

接口函数

inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过Inotify可以监控文件系统添加、删除、移动、修改等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况。Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象。可以使用select、poll、epoll这些接口来监听,当有事件发生是,inotify文件描述符会可读。

此种机制的出现的目的是当内核空间发生某种事件之后,可以立即通知到用户空间。方便用户做出具体的操作。

Inotify API 接口函数

int fd = inotify_init(void)

int inotify_init1(int flags);

每一个 inotify 实例对应一个独立的排序的队列。

用于创建一个inotify的实例,然后返回inotify事件队列的文件描述符。 同样内核也提供了inotify_init1()中多了一个参数flags,用来在初始化时设置inotify文件描述符的属性。flags中可以设置的标志有两个:IN_NONBLOCK和IN_CLOEXEC。这两个标志不难理解,前一个是用来将inotify文件描述设置为非阻塞状态,后一个是设置close-on-exec(FD_CLOEXEC)标志。通过使用这两个标志就避免在创建inotify文件描述后再调用fcntl()的消耗了,代码看起来也会简洁一些。inotify_init1(int flags)接口函数,当flag等于0的时候,该函数等价于inotify_init(void)函数。

int wd = inotify_add_watch(int fd, const char* pathname, uint32_t  mask)

该函数用于添加“watch list”,也就是检测列表。 可以是一个新的watch,也可以是一个已经存在的watch。其中fd就是inotify_init的返回值,pathname是要检测目录或者文件的路径,mask就是要检测的事件类型。该函数成功返回的是一个unique的watch描述符。返回值是一个inotify标识,注意不要和inotify_init()的返回值搞混淆了。inotify_init()的返回值是在读取事件、注册监听文件时使用,而inotify_add_watch()的返回值用来判断返回的事件属于哪个监听的文件(后面介绍inotify_event结构时会看到),以及移除监听文件时使用。

int inotify_rm_watch(int fd, int wd)

 

用于从watch list种移除检测的对象。

数据结构

内核使用struct inotify_event代表一个文件事件。当检测的文件对象发生变化时,使用read系统调用就会返回一个或者多个inotify_event的文件事件对象。

struct inotify_event {
   int      wd;       /* Watch descriptor */
   uint32_t mask;     /* Mask of events */
   uint32_t cookie;   /* Unique cookie associating related
                         events (for rename(2)) */
   uint32_t len;      /* Size of name field */
   char     name[];   /* Optional null-terminated name */
};

.wd:        就是检测的对象的watch descriptor

.mask:    检测事件的mask

.cookie:  和rename事件相关。

.len:        name字段的长度。

.name:    检测对象的name。

可以看到name字段的长度是0,也就是变长的。因为检测的对象的name不定,使用变长可以方便记录检测对象的name。

 

mask有关检测的事件类型分为好几种,如下:

IN_ACCESS         File was accessed (read) (*).
IN_ATTRIB         Metadata  changed,  e.g.,  permissions, timestamps, extended
                 attributes, link count (since Linux 2.6.25), UID, GID,  etc.(*).
IN_CLOSE_WRITE    File opened for writing was closed (*).
IN_CLOSE_NOWRITE  File not opened for writing was closed (*).
IN_CREATE         File/directory created in watched directory (*).
IN_DELETE         File/directory deleted from watched directory (*).
IN_DELETE_SELF    Watched file/directory was itself deleted.
IN_MODIFY         File was modified (*).
IN_MOVE_SELF      Watched file/directory was itself moved.
IN_MOVED_FROM     File moved out of watched directory (*).
IN_MOVED_TO       File moved into watched directory (*).
IN_OPEN           File was opened (*).

             IN_ACCESS: 文件被访问

             IN_ATTRIB:元数据被改变,例如权限、时间戳、扩展属性、链接数、UID、GID等

             IN_CLOSE_WRITE:关闭打开写的文件

             IN_CLOSE_NOWRITE: 和IN_CLOSE_WRITE刚好相反,关闭不是打开写的文件

             IN_CREATE:这个是用于目录,在监控的目录中创建目录或文件时发生

             IN_DELETE:这个也是用于目录,在监控的目录中删除目录或文件时发生

             IN_DELETE_SELF:监控的目录或文件本身被删除

             IN_MODIFY:文件被修改,这种事件会用到inotify_event中的cookie。

             IN_MOVE_SELF:监控的文件或目录本身被移动

             IN_MOVED_FROM: 从监控的目录中移出文件

             IN_MOVED_TO:向监控的目录中移入文件

             IN_OPEN: 文件被打开

(注:linuxi下都可以抽象为文件,如果没有特别说明,文件可以指普通文件或目录)。

   inotify还为我们定义了一个IN_ALL_EVENTS宏,这个宏包含了上面提到的所有事件,这个宏在调用inotify_add_watch()中使用。

  下面的这些bit位只在调用inotify_add_watch()中会用到:

  IN_DONT_FOLLOW:如果监控的文件时一个符号链接,只监控符号链接本身,而不是链接指向的文件

  IN_MASK_ADD:对已监控的文件增加要监控的的事件(不是替换原先的掩码)

  IN_ONESHOT:只监控指定的文件一次,事件发生后从监控列表移除

  IN_ONLYDIR:如果监控的是一个目录,只监控目录本身(待定)

  下面的这些bit位可能在read()读取到的事件中设置:

  IN_IGNORED:监控被显式移除(调用inotify_rm_watch()),或者自动移除(文件被删除或者文件系统被卸载)

  IN_ISDIR:引发事件的是一个目录

  IN_Q_OVERFLOW:事件队列溢出(这种情况下inotify_event结构中的wd为-1)

  IN_UMOUNT:包含监控对象的文件系统被卸载

原文:https://blog.csdn.net/myiloveuuu/article/details/53296619
 

实例:1、

#include <sys/inotify.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
 
/*
struct inotify_event {
   int      wd;       // Watch descriptor
   uint32_t mask;     // Mask of events
   uint32_t cookie;   // Unique cookie associating related  events (for rename(2))
   uint32_t len;      // Size of name field
   char     name[];   // Optional null-terminated name
};
*/
 
int watch_inotify_events(int fd)
{
    char event_buf[512];
    int ret;
    int event_pos = 0;
    int event_size = 0;
    struct inotify_event *event;
    
    /*读事件是否发生,没有发生就会阻塞*/
    ret = read(fd, event_buf, sizeof(event_buf));
    
    /*如果read的返回值,小于inotify_event大小出现错误*/
    if(ret < (int)sizeof(struct inotify_event))
    {
        printf("counld not get event!\n");
        return -1;
    }
    
    /*因为read的返回值存在一个或者多个inotify_event对象,需要一个一个取出来处理*/
    while( ret >= (int)sizeof(struct inotify_event) )
    {
        event = (struct inotify_event*)(event_buf + event_pos);
        if(event->len)
        {
            if(event->mask & IN_CREATE)
            {
                printf("create file: %s\n",event->name);
            }
            else
            {
                printf("delete file: %s\n",event->name);
            }
        }
        
        /*event_size就是一个事件的真正大小*/
        event_size = sizeof(struct inotify_event) + event->len;
        ret -= event_size;
        event_pos += event_size;
    }
    
    return 0;
}
 
int main(int argc, char** argv)
{
    int InotifyFd;
    int ret;
    
    if (argc != 2)
    {
        printf("Usage: %s <dir>\n", argv[0]);
        return -1;
    }
    
    /*inotify初始化*/
    InotifyFd = inotify_init();
    if( InotifyFd == -1)
    {
        printf("inotify_init error!\n");
        return -1;
    }
    
    /*添加watch对象*/
    ret = inotify_add_watch(InotifyFd, argv[1], IN_CREATE |  IN_DELETE);
    
    /*处理事件*/
    watch_inotify_events(InotifyFd);
 
    /*删除inotify的watch对象*/
    if ( inotify_rm_watch(InotifyFd, ret) == -1)
    {
        printf("notify_rm_watch error!\n");
        return -1;
    }
    
    /*关闭inotify描述符*/
    close(InotifyFd);
    
    return 0;
}

实例2 使用epoll + inotify监控文件

#include <sys/epoll.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/inotify.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int
main (int argc, char *argv[])
{
  int i, epfd, nfds, fd;
  int wd;
  int length;

  char buffer[BUF_LEN];
  struct epoll_event ev, events[20];
  epfd = epoll_create (256);
  fd = inotify_init ();
  wd = inotify_add_watch (fd, "/home/cc/tmp",
                          IN_MODIFY | IN_CREATE | IN_DELETE);

  ev.data.fd = fd;
  ev.events = EPOLLIN | EPOLLET;

  epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &ev);

  for (;;)
    {
      nfds = epoll_wait (epfd, events, 20, 500);

      for (i = 0; i < nfds; ++i)
        {
          if (events[i].data.fd == fd)

            {
              length = read (fd, buffer, BUF_LEN);

              if (length < 0)
                {
                  perror ("read");
                }

              while (i < length)
                {
                  struct inotify_event *event =
                    (struct inotify_event *) &buffer[i];
                  if (event->len)
                    {
                      if (event->mask & IN_CREATE)
                        {
                          if (event->mask & IN_ISDIR)
                            {
                              printf ("The directory %s was created.\n",
                                      event->name);
                            }
                          else
                            {printf ("The file %s was created.\n",
                                      event->name);
                            }
                        }
                      else if (event->mask & IN_DELETE)
                        {
                          if (event->mask & IN_ISDIR)
                            {
                              printf ("The directory %s was deleted.\n",
                                      event->name);
                            }
                          else
                            {
                              printf ("The file %s was deleted.\n",
                                      event->name);
                            }
                        }
                      else if (event->mask & IN_MODIFY)
                        {
                          if (event->mask & IN_ISDIR)
                            {
                              printf ("The directory %s was modified.\n",
                                      event->name);
                            }
                          else
                            {
                              printf ("The file %s was modified.\n",
                                      event->name);
                            }
                        }
                    }
                  i += EVENT_SIZE + event->len;
                }
        
            }


        }

    }

 

  return 0;
}
脚本调用实现:

一.需求:

1.监控特定的服务配置文件和目录变更情况。

2.监控自定义文件和目录变更情况。

3.可以手动杀掉进程。

4.把所有变更信息弄到日志里。

二.Inotify介绍

Inotify 是一个内核用于通知用户空间程序文件系统变化的机制,是基于inode级别的文件系统监控技术,是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能,内核要求2.6.13以上。

2.1 Inotify监控事项
Events 含义
access 文件或目录被读取
modify 文件或目录内容被修改
attrib 文件或目录属性被改变
close 文件或目录封闭,无论读/写模式
open 文件或目录被打开
moved_to 文件或目录被移动至另外一个目录
moved_from 文件或目录被移动到另一个目录或从另一个目录移动至当前目录
create 文件或目录被创建在当前目录
delete 文件或目录被删除
unmount 文件系统被卸载

2.2 Inotify参数说明
参数 说明
-m 持续监听
-r 使用递归形式监视目录
-q 减少冗余信息,只打印出需要的信息
-e 指定要监视的事件,多个时间使用逗号隔开
--timefmt 时间格式
--format 监听到的文件变化的信息

ymd分别表示年月日,H表示小时,M表示分钟
--format 说明:
参数 说明
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

脚本如下:

#!/bin/bash
#监控文件和文件夹变更
A=y
B=`grep INOTIFY_USER /boot/config-$(uname -r)`
C=`echo ${B:0-1}`
system=`getconf LONG_BIT`
#定义安装inotify
function Install_inotify()
{
if [ "$A" = "$C" ]
    then
    echo -e "\033[31mstart install INOTIFY-3.14...... \033[0m"
    cd /opt
    tar zxvf inotify-tools-3.14.tar.gz &> /dev/null
    cd /opt/inotify-tools-3.14
    ./configure && make && make install &> /dev/null
    echo -e "\033[31mINOTIFY-3.14 install successful! \033[0m"
    if [ "$system" = 64 ]
    then
        echo -e "\033[31minstall 64-bit system patch......\033[0m"
        ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
    else
        echo -e "\033[31minstall 32-bit system patch......\033[0m"
        ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
    fi
    exit
else
     echo -e "\033[31mSystem not supported INOTIFY...... \033[0m"
     exit
fi
}
#定义tomcat监控目录
function Inotify_tomcat ()
{ 
#查找tomcat配置文件
 echo -e "\033[31m正在搜索tomcat配置文件目录,正在打印.....\033[0m"
 echo "`find / -name "server.xml"`"
 read -p "Please insert tomcat configuration file": File
 echo -e "\033[31mtomcat configuration file is $File \033[0m"
 read -p "Please insert tomcat Application web directory": Directory 
 echo -e "\033[31mtomcat Application web directory is $Directory \033[0m"
 touch /opt/Inotify_tomcat.log
 echo -n "" > /opt/Inotify_tomcat.log
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $File $Directory >> /opt/Inotify_tomcat.log &
 echo -e "\033[31mlook /opt/Inotify_tomcat.log  information \033[0m"
 exit
 }

#自定义目录和文件监控
function Inotify_custom ()
{
echo -e "\033[31m编辑自定义文件和目录监控配置文件/opt/mon.txt: \033[0m"
touch /opt/Inotify_custom.log
echo -n "" > /opt/Inotify_custom.log
for line in $(cat /opt/mon.txt)
do
  echo $line
  /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $line >> /opt/Inotify_custom.log &
done
exit
}

#定义关闭监控进程
function Inotify_kill ()
{
echo -e "\033[31mMonitoring is turned off \033[0m"
pid=`ps -ef | grep inotifywait | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
    echo -e "\033[31mkill inotifywait pid : \033[0m"$pid
    kill -9 $pid
    echo -e "\033[31minotifywait turned off successful \033[0m"
    exit
else
    echo -e "\033[31mMonitoring does not exist \033[0m"
    exit
fi
}

#PS3一般为菜单提示信息# 
 PS3="Please Select Menu": 
#select为菜单选择命令,格式为select $var in ..command.. do .... done 
 select i in "Install_inotify" "Inotify_tomcat" "Inotify_custom" "Inotify_kill"
 do
#case 方式,一般用于多种条件下的判断 
case $i in
  Install_inotify ) 
  Install_inotify 
;;
  Inotify_tomcat ) 
  Inotify_tomcat 
;;
  Inotify_custom ) 
  Inotify_custom 
;;
  Inotify_kill ) 
  Inotify_kill 
;;
  *) 
  echo 
  echo "Please Insert $0: Install_inotify(1)|Inotify_tomcat(2) |Inotify_custom(3) |Inotify_kill(4)"
  break
;; 
esac 
done

四.使用说明:

一、功能介绍
1.文件变更删除,创建,移动,修改,卸载,权限改动实时监控
2.文件夹变更删除,创建,移动,修改,卸载,权限改动实时监控

二、使用选项说明
1.安装服务
2.tomcat监控 #配置文件和web应用目录需要提供
如:

找到tomcat配置文件并输入进去
Please insert tomcat configurationfile:
/opt/apache-tomcat-7.0.67/conf/server.xml
找到tomcat-web应用目录并输入进去

Please insert tomcat Application web directory:
/opt/apache-tomcat-7.0.67/webapps/vaiops
相应的监控日志在: /opt/Inotify_tomcat.log
3.自定义监控 #需要手动编辑/opt/mon.txt文件,把需要监控的目录和文件填入。
如:
vim /opt/mon.txt 
/opt/mon.txt
/etc
/opt
4.杀死监控进程

如:

Monitoring is turned off 
kill inotifywait pid : 4538 4539 4540
inotifywait turned off successful

三、脚本运行参数说明
例子:
运行sh intify.sh
1) Install_inotify
2) Inotify_tomcat
3) Inotify_custom
4) Inotify_kill

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Inotify是一种文件系统变化通知机制,可以即刻反映文件或文件夹的增删等事件在用户空间中。它是Linux内核从2.6.13版本开始引入的一种强大的、细粒度的、异步的文件系统事件监控机制。通过Inotify,第三方软件可以监控文件系统中文件的各种变化情况,包括添加、删除、移动和修改等。使用Inotify可以检测单个文件的变化,也可以监控整个目录。当监控目录时,目录本身和目录下的内容都会成为监控的对象。可以使用select、poll、epoll等接口监听Inotify文件描述符的可读事件,当有事件发生时,程序可以及时处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux inotify](https://download.csdn.net/download/chenggong526214/8942667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [linux - inotify](https://blog.csdn.net/iteye_12332/article/details/82511287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Linux Inotify详解使用](https://blog.csdn.net/zhanglei_admin/article/details/97636301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值