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

[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 使用

安装之后会有 inotifywait 和 inotifywatch 两个程序.

  • 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版本的程序,编译完成之后分离出符号表 ↩

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 13,802评论 2赞 33
  • 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多...
    jimlin阅读 1,316评论 0赞 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,377评论 19赞 139
  • 说明: 操作系统:CentOS 5.X 源服务器:192.168.21.129 目标服务器:192.168.21....
    好一朵阅读 1,576评论 0赞 0
  • 1.1 inotify介绍 inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2....
    minima1ist阅读 663评论 0赞 0

友情链接更多精彩内容