inotify-Linux下监控文件系统操作

作者: 倚楼 | 来源:发表于2017-01-17 13:08 被阅读546次

[TOC]

开发阶段当功能提交之后,测试人员需要频繁的修改系统时间进行功能的测试,系统事件常常被改的天昏地暗. 如果服务器宕机没有及时的反馈到开发人员,那么生成的core文件因无法知道版本[1]而无法查看宕机原因,失去它本身所承载的重要意义,所以需要在生成的文件名中增加Git log的SHA和版本号.

Linux的 core pattern 提供了一些自定义core文件的方法,但只能增加PID,UID,GID,hostname,filename,time,signal ID 这些附加信息,所以只能另寻出路.
inotify 提供了对 Linux 文件系统事件的监控,通过它可以监视目录下的各种事件. inotify 的详细信息参见

inotify-tools 工具包几乎包含了目录和文件的监控,所以本文侧重介绍在bash中使用这个工具.

查看系统是否支持 inotify

inotify 需要 Linux 内核2.6.13以上版本的支持.输入命令 uname -a 查看系统的内核版本号.

inotify-tools的下载及安装

下载地址: http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0

安装:

tar -zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure
make
make install    # 需要root权限

inotify-tools 使用

安装之后会有 inotifywaitinotifywatch 两个程序.

  • inotifywait 通过inotify提供了对文件改变的监视
  • inotifywatch 通过inotify提供了对文件系统访问的统计

inotifywait使用

参数说明

参数 说明
-m 事件发生后不退出,默认当事件发生后退出
-r 递归监控当前目录下的所有文件和目录.(默认的文件和目录数最大是 8192个;如果不满足可以修改/proc/sys/fs/inotify/max_user_watches .
-o <file> Print events to <file> rather than stdout.
-s Send errors to syslog rather than stderr.
-q Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> 输出指定内容格式.
--timefmt <fmt> 指定输出时间格式
-t <seconds> 超时时间.
-e <event1> <event2> ... ] 指定监视事件.

事件说明

事件 说明
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in writeable mode
close_nowrite file or directory closed, after being opened in read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted

详细的信息可以使用 man inotifywait 查看.

解决方案

当前目录下生成 core 文件之后,将 core 文件重命名为 core.filename.pid_version_sha 格式.
修改core pattern生成core.filename.pid格式的文件:

echo "core.%e.%p" > /proc/sys/kernel/core_pattern
echo "1" > /proc/sys/kernel/core_uses_pid

监视脚本:

#!/bin/bash

dir=$1
inotifywait -m -q -r -e close_write --format '%f' $dir | while read file
do
    if [[ $file == core.t3* ]];
    then
        log=`sed -n "/commit/ s/commit //p"` ../bin/bin.gitlog
        vsn=`cat ../bin/version.txt`
        mv $file ${file}_${vsn}_${log}
    fi
done

拓展:
Inotify: 高效、实时的Linux文件系统事件监控框架


  1. 为了避免发布人员将debug版本发到外网,我们内网使用的都是release版本的程序,编译完成之后分离出符号表

相关文章

  • inotify-Linux下监控文件系统操作

    [TOC] 开发阶段当功能提交之后,测试人员需要频繁的修改系统时间进行功能的测试,系统事件常常被改的天昏地暗. 如...

  • JAVA 文件监控 WatchService

    概述 java1.7中 提供了WatchService来监控系统中文件的变化。该监控是基于操作系统的文件系统监控器...

  • 监控系列讲座(十二)常见系统监控指标之存储

    4. 磁盘/存储监控指标 一般来说,我们监控存储设备的时候大多数都是在监控文件系统,也就是可以被操作系统直接使用的...

  • rsync+inotify实时数据同步

    一工具准备 操作系统:centos7 rsync:数据同步软件,linux系统自带 inotify:监控文件系统操...

  • Ceph Cookbook 中文版

    1、ceph介绍、ceph块存储、ceph对象存储、ceph文件系统、用Calamari监控Ceph、操作和管理c...

  • Linux inotify监听文件状态

    Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非...

  • mongodb运维(2) mongostat命令

    mongostat的功能类似于Unix / Linux文件系统使用vmstat,监控数据库上各项操作的统计,如增删...

  • 架构之美

    企业项目案例 共享存储实时备份的原理: inotify(实时同步工具) 异步文件系统事件监控机制,可以监控文件系统...

  • swoole+inotify实现异步实时文件监控

    inotify扩展介绍 inotify是Linux内核提供的一组系统调用,它可以监控文件系统操作,比如文件或者目录...

  • inotifywait文件监控

    inotifywait 异步文件系统监控机制 Inotify 一种强大的、细粒度的、异步文件系统监控机制,它满足各...

网友评论

    本文标题:inotify-Linux下监控文件系统操作

    本文链接:https://www.haomeiwen.com/subject/gxumbttx.html