设为首页 收藏本站
查看: 1207|回复: 0

[经验分享] 基于redis ae实现 Linux中的文件系统监控机制(inotify)

[复制链接]

尚未签到

发表于 2015-11-12 11:47:03 | 显示全部楼层 |阅读模式

(英文部分为转的。代码是个人代码)


1 What’s inotify

The inotify API provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is
monitored, inotify will return events for the directory itself, and for files inside the directory.


2 How to use inotify


2.1 system calls used with this API

The following system calls are used with this API: inotify_init(orinotify_init1),inotify_add_watch, inotify_rm_watch, read,
and close.



2.1.1 inotify_init

It creates an inotify instance and returns a file descriptor referring to the inotify instance. The more recent inotify_init1
is like inotify_init,
but provides some extra functionality.


2.1.2 inotify_add_watch
It manipulates the "watch list" associated with an inotify instance. Each item ("watch") in the watch list specifies the pathname of a file or directory, along with some set of events that the kernel should
monitor for the file referred to by that pathname.

inotify_add_watch either creates a new watch item, or modifies an existing watch. Each watch has a unique "watch descriptor", an integer returned by inotify_add_watch when
the watch is created.

2.1.3 inotify_rm_watch
It removes an item from an inotify watch list.
When all file descriptors referring to an inotify instance have been closed, the underlying object and its resources are freed for reuse by the kernel; all associated watches are automatically freed.
2.1.4 read
To determine what events have occurred, an application reads from the inotify file descriptor.
If no events have so far occurred, then, assuming a blocking file descriptor,
read will block until at least one event occurs (unless interrupted
by a signal, in which case the call fails with the error EINTR; see
signal(7)).
Each successful read returns a buffer containing one or more of the following structures:
struct inotify_event {
    int      wd;       /* Watch descriptor */
    uint32_t mask;     /* Mask of events */
    uint32_t cookie;   /* Unique cookie associating related events (for rename ) */
    uint32_t len;      /* Size of name field */
    char     name[];   /* Optional null-terminated name */
};
2.2 Inotify events
The inotify_add_watch mask argument and the mask field of the inotify_event structure returned
when
reading an inotify file descriptor are both bit masks identifying inotify events. The following bits can be specified in mask when calling inotify_add_watch and
may be returned in the mask field returned by
read:
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 (*).
……
2.3 The flow for the systme calls used with inotify
DSC0000.jpg


3 代码示例
#include <sys/inotify.h>

static aeEventLoop *loop;
/* 全局的notify watch item */
static const char *wds[10];
void sync_file_thread(void*args)
{
int inotify_fd, wd;
int poll_num;
const char *dir, *file;
dir = &quot;/data/wcl/redis_proxy&quot;;
file = &quot;binlog_1&quot;;
loop =aeCreateEventLoop(1024);
/* 创建inotify instance */
inotify_fd = inotify_init1(IN_NONBLOCK);
if (inotify_fd == -1)
{
perror(&quot;Unable to create inotify instance\n&quot;);
exit(-1);
}
printf(&quot;inotify_fd [%d]\n&quot;,inotify_fd);
/* 监控目录下面的新建文件事件 */
wd = inotify_add_watch(inotify_fd, dir, IN_CREATE);
wds[wd] = dir;
/* 监控目录下面文件的修改事件 */
wd = inotify_add_watch(inotify_fd, file, IN_MODIFY);
wds[wd] = file;
/* 监听inotify的可读事件, 有可读事件, 则表示监控的文件系统有事件产生 */
if (aeCreateFileEvent(loop,inotify_fd,AE_READABLE,handle_inotify_events,NULL))
{
exit(0);
}
aeMain(loop);
aeDeleteEventLoop(loop);
}

void handle_inotify_events(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask)
{
char buf[4096], *ptr;
ssize_t len;
struct inotify_event *event;
len = read(fd, buf, sizeof(buf));
if (len == -1 && errno != EAGAIN)
{
perror(&quot;read error&quot;);
exit(-1);
}
if (len <= 0)
{
return;
}
for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len)
{
event = (struct inotify_event *)ptr;
if(event->mask & IN_CREATE)
{
/* new binlog files created */
if(event->len)
{
printf(&quot;New File created: %s\n&quot;, event->name);
}
}
if(event->mask & IN_MODIFY)
{
/* existing files are modified */
printf(&quot;File is modified: %s\n&quot;, wds[event->wd]);
}
}
}

这里我只监控了创建和修改的两个事件,然后根据文件名字和一些其他的对应机制进行对文件判断修改等。




版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-138291-1-1.html 上篇帖子: redis服务器的运维(配置、启动) 下篇帖子: NoSql学习之路一redis做消息队列
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表