将 lsof 输出格式化为可解析结构

THX*_*THX 2 formatting lsof

我正在尝试以更可解析的方式格式化 lsof 输出。

背景:由于并非所有打开句柄的进程都有线程 ID,因此不一定确定由空格(空白 AFAIS)分隔的字段数量。

作为输出字段,我需要 PID、UID/用户名和路径(如果它是一个文件 - 我正在查找路径,因为 +D 非常慢)。

作为字段分隔符,我从 NL 切换到 NUL(并用“|”替换 null 以提高可读性)

所以我尝试了

> /usr/sbin/lsof -F pnuf0 | sed 's/\x0/|/g' | grep "cvmfs" | tail -n 2
 ftxt|n/usr/bin/cvmfs2|
 fmem|n/usr/lib64/libcvmfs_fuse.so.2.3.5|
Run Code Online (Sandbox Code Playgroud)

它只产生文件描述符和名称(不是按照给定的顺序?)而不是 PID 或 UID?

作为旁注,单独选择 PID 和 UID 字段时,它们显然已经是“空”

> /usr/sbin/lsof -F u0 | sed 's/\x0/|/g' | grep "cvmfs" | tail -n 2
> /usr/sbin/lsof -F p0 | sed 's/\x0/|/g' | grep "cvmfs" | tail -n 2
> /usr/sbin/lsof -F n0 | sed 's/\x0/|/g' | grep "cvmfs" | tail -n 2
  n/usr/bin/cvmfs2|
  n/usr/lib64/libcvmfs_fuse.so.2.3.5|
Run Code Online (Sandbox Code Playgroud)

将 lsof 的输出解析为“PD,NAME,UID,FILEDESC”的正确方法是什么?

小智 7

由于我从未在网上找到对此问题的良好答案,因此我花了很多时间来解决这个问题。我希望我能让别人免受这种痛苦。lsof 本身会打印出带有缺失值的水平输出,导致无法正确解析

要格式化,lsof您需要使用以下命令:

lsof -F pcuftDsin
Run Code Online (Sandbox Code Playgroud)

添加 -F 将垂直打印结果,让我解释一下每个部分。

  • lsof:按进程获取所有打开文件的列表
  • -F:格式化输出垂直而不是水平
  • p:将为 PID 或(进程 ID)列添加前缀
  • c:将为命令或(进程名称)列添加前缀
  • u:将为进程正在运行的用户列添加前缀
  • f: 将作为文件描述符列的前缀
  • t: 将为类型列添加前缀
  • D: 将为设备列添加前缀
  • s: 将为 SizeOff 列添加前缀
  • i: 将为 Node 列添加前缀
  • n: 将作为名称或(文件路径)的前缀

输出:

p3026
ccom.apple.appkit.xpc.openAndSavePanelService
u501
fcwd
tDIR
D0x1000004
s704
i2
n/
ftxt
tREG
D0x1000004
s94592
i1152921500312434319
n/System/Library/Frameworks/AppKit.framework/Versions/C/XPCServices/com.apple.appkit.xpc.openAndSavePanelService.xpc/Contents/MacOS/com.apple.appkit.xpc.openAndSavePanelService
ftxt
tREG
D0x1000004
s27876
i45156619
n/Library/Preferences/Logging/.plist-cache.usI0gbvW
ftxt
tREG
D0x1000004
s28515184
i1152921500312399135
n/usr/share/icu/icudt64l.dat
ftxt
tREG
D0x1000004
s239648
i31225967
n/private/var/db/timezone/tz/2019c.1.0/icutz/icutz44l.dat
ftxt
tREG
D0x1000004
s3695464
i1152921500312406201
n/System/Library/CoreServices/SystemAppearance.bundle/Contents/Resources/SystemAppearance.car
ftxt
tREG
D0x1000004
s136100
i38828241
n/System/Library/Caches/com.apple.IntlDataCache.le.kbdx
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,每行都以上面指定的正确字母为前缀。另一件需要注意的重要事情是,“进程 ID”、“进程名称”和用户每组打开的文件只会打印一次,对于数据库存储,我需要为打印的每一行使用这些字段。我正在执行一个java项目,所以我用来解析它的代码如下所示:

    public static void main(String[] args) {

        String command = "lsof -F pcuftDsin";
        String captureBody = "";
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = "";

        String ProcessID = "";
        String ProcessName = "";
        String User = "";
        String FD = "null";
        String Type = "null";
        String Device = "null";
        String SizeOff = "null";
        String Node = "null";
        String File = "null";

        while(true) {
            try {
                line = reader.readLine();
                if (line == null) {
                    break;
                } else {
                    if (line.startsWith("p")) {
                        ProcessID = line;
                    }  else if (line.startsWith("c")) {
                        ProcessName = line;
                    } else if (line.startsWith("u")) {
                        User = line;
                    } else if (line.startsWith("f")) {
                        FD = line;
                    } else if (line.startsWith("t")) {
                        Type = line;
                    } else if (line.startsWith("D")) {
                        Device = line;
                    } else if (line.startsWith("s")) {
                        SizeOff = line;
                    } else if (line.startsWith("i")) {
                        Node = line;
                    } else if (line.startsWith("n")){
                        File = line;

                        System.out.println(ProcessID  + "," + ProcessName + "," + User + "," + FD + "," + Type  + "," + Device  + "," + SizeOff  + "," + Node  + "," + File);

                        FD = "null";
                        Type = "null";
                        Device = "null";
                        SizeOff = "null";
                        Node = "null";
                        File = "null";
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            proc.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
Run Code Online (Sandbox Code Playgroud)

输出

p94484,ccom.apple.CoreSimulator.CoreSim,u501,ftxt,tREG,D0x1000004,s239648,i31225967,n/private/var/db/timezone/tz/2019c.1.0/icutz/icutz44l.dat
Run Code Online (Sandbox Code Playgroud)

因为我正在存储输出,所以我需要空字段来显示某些内容,我使用了 null,您可以使用任何内容作为默认文本,甚至只对缺失的字段使用空字符串,并非所有字段都会被填充。如果有人对我如何提高代码性能有任何建议,我会洗耳恭听。