Linux 相当于 Mac OS X 的 fs_usage

Linux 相当于 Mac OS X 的 fs_usage

Linux 中是否有与 Mac OS X 命令 fs_usage 等效的命令?根据手册页它执行以下操作:“实时报告与文件系统活动相关的系统调用和页面错误”。

我认为一个选择是使用 strace 并过滤数据,但是 fs_usage 命令允许同时“跟踪”系统上运行的所有进程。

以下是示例输出:

03:44:25  stat64            erences/ByHost/org.mozilla.firefox.69AC0B48-F675-5045-B873-A28B119C33E7.plist     0.000029   firefox     
03:44:25  stat64            /Users/**********/Library/Preferences                                             0.000011   firefox     
03:44:25  statfs64          /Users/**********/Library/Preferences                                             0.000004   firefox     
03:44:25  open              /Users/**********/Library/Preferences/org.mozilla.firefox.plist.lockfile          0.000090   firefox     
03:44:25  stat64            /Users/**********/Library/Preferences/org.mozilla.firefox.plist                   0.000004   firefox     
03:44:25  stat64            /Users/**********/Library/Preferences                                             0.000002   firefox     
03:44:25  open              /Users/**********/Library/Preferences/org.mozilla.firefox.plist.vjLkANe           0.000100   firefox   

有任何想法吗?

答案1

您可以使用审计控制监视与文件系统活动相关的系统调用,如 openstatlstat。不幸的是,监视readwrite似乎存在问题。

auditctl是一个帮助控制内核审计系统的实用程序。您需要 root 权限才能使用它。它支持各种过滤器,例如:

devmajor    Device Major Number

devminor    Device Minor Number

dir         Full Path of Directory to watch. This will place a recursive
            watch on the directory and its whole subtree. It can only be
            used on exit list. See "-w".

egid        Effective Group ID. May be numeric or the groups name.

euid        Effective User ID. May be numeric or the user account name.

filetype    The target file's type. Can be either file, dir, socket, symlink,
            char, block, or fifo.

path        Full Path of File to watch. It can only be used  on exit list.

pid         Process ID

ppid        Parent's Process ID

示例(在 Fedora 16 x86_64 上测试)

要添加审计规则,请以 root 身份运行:

for syscall in open stat lstat read write; do
    auditctl -a exit,always -F arch=b64 -S $syscall \
             -F euid=1000 \
             -F dir=/tmp/superuser.com/questions/370070
done

稍后若要删除它们,请将其替换-a-d

for syscall in open stat lstat read write; do
    auditctl -d exit,always -F arch=b64 -S $syscall \
             -F euid=1000 \
             -F dir=/tmp/superuser.com/questions/370070
done

添加规则后,以 UID 1000 的用户身份在该目录下执行以下操作:

cd  /tmp/superuser.com/questions/370070
echo foo > bar
cat bar
stat bar

ausearch --start 00:00:00 --uid-effective 1000将返回以下内容(日志为/var/log/audit/audit.log):

time->Thu Jun 14 00:02:32 2012
type=PATH msg=audit(1339621352.871:18529): item=0 name="/tmp/superuser.com/questions/370070" inode=178 dev=fd:03 mode=040775 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=CWD msg=audit(1339621352.871:18529):  cwd="/home/ciupicri"
type=SYSCALL msg=audit(1339621352.871:18529): arch=c000003e syscall=4 success=yes exit=0 a0=139bbf0 a1=7fff32d832d0 a2=7fff32d832d0 a3=24 items=1 ppid=2249 pid=3446 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts6 ses=1 comm="bash" exe="/bin/bash" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
[root@hermes ~]# ausearch --start 00:00:00 --uid-effective 1000
----
time->Thu Jun 14 00:02:32 2012
type=PATH msg=audit(1339621352.871:18529): item=0 name="/tmp/superuser.com/questions/370070" inode=178 dev=fd:03 mode=040775 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=CWD msg=audit(1339621352.871:18529):  cwd="/home/ciupicri"
type=SYSCALL msg=audit(1339621352.871:18529): arch=c000003e syscall=4 success=yes exit=0 a0=139bbf0 a1=7fff32d832d0 a2=7fff32d832d0 a3=24 items=1 ppid=2249 pid=3446 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts6 ses=1 comm="bash" exe="/bin/bash" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
----
time->Thu Jun 14 00:02:47 2012
type=PATH msg=audit(1339621367.175:18531): item=0 name="bar" inode=218 dev=fd:03 mode=0100664 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=CWD msg=audit(1339621367.175:18531):  cwd="/tmp/superuser.com/questions/370070"
type=SYSCALL msg=audit(1339621367.175:18531): arch=c000003e syscall=2 success=yes exit=3 a0=7fff5ed6b37f a1=0 a2=0 a3=7fff5ed69460 items=1 ppid=3446 pid=4735 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts6 ses=1 comm="cat" exe="/bin/cat" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
----
time->Thu Jun 14 00:02:47 2012
type=PATH msg=audit(1339621367.172:18530): item=1 name="bar" inode=218 dev=fd:03 mode=0100664 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=PATH msg=audit(1339621367.172:18530): item=0 name="/tmp/superuser.com/questions/370070" inode=178 dev=fd:03 mode=040775 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=CWD msg=audit(1339621367.172:18530):  cwd="/tmp/superuser.com/questions/370070"
type=SYSCALL msg=audit(1339621367.172:18530): arch=c000003e syscall=2 success=yes exit=3 a0=1665500 a1=241 a2=1b6 a3=4 items=2 ppid=2249 pid=3446 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts6 ses=1 comm="bash" exe="/bin/bash" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
----
time->Thu Jun 14 00:02:47 2012
type=PATH msg=audit(1339621367.971:18532): item=0 name="bar" inode=218 dev=fd:03 mode=0100664 ouid=1000 ogid=1000 rdev=00:00 obj=unconfined_u:object_r:user_tmp_t:s0
type=CWD msg=audit(1339621367.971:18532):  cwd="/tmp/superuser.com/questions/370070"
type=SYSCALL msg=audit(1339621367.971:18532): arch=c000003e syscall=6 success=yes exit=0 a0=7fffdc713375 a1=7fffdc711580 a2=7fffdc711580 a3=7fffdc7112f0 items=1 ppid=3446 pid=4736 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts6 ses=1 comm="stat" exe="/usr/bin/stat" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)

系统调用号可以在 中找到/usr/include/asm/unistd_64.h

答案2

iowatch有可能。


要观察/etc 中的活动,你可以运行

$ iwatch/etc

特征

  • 命令行和守护进程模式
  • xml 配置文件
  • 能够递归地监视目录并监视新创建的目录
  • 可以指定例外列表
  • 可以使用正则表达式来比较文件/目录名称
  • 如果发生事件则可以执行命令
  • 发电子邮件
  • 系统日志
  • 打印时间戳

可用活动

    access  : file was modified
    modify  : file was modified
    attrib  : file attributes changed
    close_write : file closed, after being opened in writeable mode
    close_nowrite   : file closed, after being opened in read-only mode
    close   : file closed, regardless of read/write mode
    open    : file was opened
    moved_from  : File was moved away from.
    moved_to    : File was moved to.
    move    : a file/dir within watched directory was moved
    create  : a file was created within watched director
    delete  : a file was deleted within watched directory
    delete_self : the watched file was deleted
    unmount : file system on which watched file exists was unmounted
    q_overflow  : Event queued overflowed
    ignored : File was ignored
    isdir   : event occurred against dir
    oneshot : only send event once

答案3

是的,是的,我知道...旧帖子...

但是……Apple 的 fs_usage 源代码可以在线获取,您完全可以自己编译。当然,也有一些注意事项……

它基于 Apple OS X 的 netbsd 系统调用,因此需要进行一些(哈!)编辑...

甚至附带一个免费的 Makefile(可惜是 netbsd 风格的)。

但如果您在接下来的 10 分钟内打电话,他们甚至会提供一个可用的手册页...全部免费!

我看了太多深夜电视购物节目。

Apple 的 fs_usage 源代码

(不退款、不含电池、不可退回商店、需要组装)

(在查看了代码并看到了您的示例之后,我确实相信可以创建一些东西来快速执行 fs_usage 功能的“子集”......让我修改一下......)

我有一个可以运行的程序,开始看起来与你所寻找的类似,需要做更多的工作才能更接近,但欢迎大家尝试一下。

Bitbucket Hg 存储库 - fs_usage

答案4

fatrace(文件活动跟踪)

脂肪痕迹报告文件访问事件(打开、读取、写入、关闭)全部正在运行的进程。其主要目的是查找那些不必要地不断唤醒磁盘并因此阻碍省电的进程。

运行时它会以以下格式实时输出每个文件事件一行:

<timestamp> <process-name(pid)>: <file-event> </path/to/file>

例如:

23:10:21.375341 Plex Media Serv(2290): W /srv/dev-disk-by-uuid-UID/Plex/Library/Application Support/Plex Media Server/Logs/Plex Media Server.log

你可以从中轻松获得所有必要的信息

  • 什么时候:时间戳(如果使用该--timestamp选项)

  • WHO:进程名称(谁在访问)

  • 什么:文件操作事件(O-pen、R-read、W-rite、C-lose)

  • 在哪里:文件路径(写入的位置)。

  • 您可以限制搜索范围,--current-mount仅记录当前目录的分区/挂载上的事件。

    • 因此,只需cd先进入与您的旋转硬盘相对应的卷,然后ftrace使用该--current-mount选项运行。
  • 若没有此选项,所有(真实)分区/挂载点都会受到监视。

超级实用

  • 作为 macOS Unix/Terminal 用户已有 20 年,现在也在使用 Linux,这是我发现的最接近 macOS 的fs_usage
  • 使用此工具您可以很快发现哪些过程您至今还不知道导致您目前不知道的文件活动有哪些立刻。
  • 所有其他工具对我的目的来说都没什么用,因为它们要么让你监视特定进程,要么监视特定目录/文件。但不能让你以快速实时的方式“调查未知事物”。

相关内容