inotify - 监控文件系统

简介: Inotify 原文链接#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/inotify.h>#define EVENT_SIZE ( sizeof (struct i

Inotify
原文链接

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.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 length, i = 0;
  int fd;
  int wd;
  char buffer[BUF_LEN];

  fd = inotify_init();

  if ( fd < 0 ) {
    perror( "inotify_init" );
  }

  wd = inotify_add_watch( fd, "/home/colin",
                         IN_MODIFY | IN_CREATE | IN_DELETE );
  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;
  }

  ( void ) inotify_rm_watch( fd, wd );
  ( void ) close( fd );

  exit( 0 );
}
目录
相关文章
|
9月前
|
Prometheus 运维 监控
linux磁盘I/O监控
【4月更文挑战第1天】在Linux中监控磁盘I/O性能至关重要,工具如iostat(-d显示磁盘统计)、iotop(进程级I/O查看)、vmstat、/proc/diskstats(详细统计信息)、Node Exporter(Prometheus集成)和Zabbix(动态监控与LLD)提供关键指标,如IOPS、吞吐量、利用率和服务时间,助力系统优化和故障排查。
268 4
linux磁盘I/O监控
|
2月前
|
Ubuntu Unix Linux
Linux网络文件系统NFS:配置与管理指南
NFS 是 Linux 系统中常用的网络文件系统协议,通过配置和管理 NFS,可以实现跨网络的文件共享。本文详细介绍了 NFS 的安装、配置、管理和常见问题的解决方法,希望对您的工作有所帮助。通过正确配置和优化 NFS,可以显著提高文件共享的效率和安全性。
241 7
|
Linux 网络安全
【Linux网络服务】Rsync+inotify+nfs实现数据实时备份
【Linux网络服务】Rsync+inotify+nfs实现数据实时备份
|
监控 Linux Python
Pyinotify – Linux中实时监控文件系统更改
Pyinotify 是一个简单而实用的 Python 模块,它用于通过 inotify 实时监控Linux文件系统的更改。用于在Linux中实时监控文件系统的变化。 作为系统管理员,您可以使用它来监视目标感兴趣的更改,如Web目录或应用程序数据存储目录及其他目录。
2245 0
|
监控 开发工具