从 shell_exe('df -h') 获取特定列数据作为关联数组

发布于 2024-09-05 10:46:47 字数 1172 浏览 4 评论 0原文

我需要构建一个脚本来查询具有多个驱动器的远程存储服务器的可用空间。

我只需使用 SSH 登录,运行 df 并将输出放入变量中。看起来像这样:

    Array
(
    [0] => Filesystem           1K-blocks      Used Available Use% Mounted on
    [1] => /dev/sda1             59392320  14949240  41426064  27% /
    [2] => /dev/sdd1            140592240 114503840  18946708  86% /home/overflow
    [3] => /dev/sdf1            287826944 273844324         0 100% /home/node2
    [4] => /dev/sde1            287826944 253278964  19927228  93% /home/node3
    [5] => /dev/sdg1            287826944   4771768 268434424   2% /home/node4
    [6] => /dev/sdh1            287826944   4329780 268876412   2% /home/node5
    [7] => /dev/sdi1            488302976 439077756  24420864  95% /home/node1
    [8] => /dev/sdh1            287826944   4329780 268876412   2% /home/mnode6
    [9] => /dev/sdh1            287826944   4329780 268876412   2% /home/mnode7
    [10] => tmpfs                  3145728         0   3145728   0% /tmp
)

我需要提取每个“节点”或“mnode”有多少可用空间。所以也许一个很好的数组有这样的对:

mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000

I need to build a script that will query a remote storage server with multiple drives for their free space.

I simply login with SSH, run df and throw the output into a variable. Which looks like this:

    Array
(
    [0] => Filesystem           1K-blocks      Used Available Use% Mounted on
    [1] => /dev/sda1             59392320  14949240  41426064  27% /
    [2] => /dev/sdd1            140592240 114503840  18946708  86% /home/overflow
    [3] => /dev/sdf1            287826944 273844324         0 100% /home/node2
    [4] => /dev/sde1            287826944 253278964  19927228  93% /home/node3
    [5] => /dev/sdg1            287826944   4771768 268434424   2% /home/node4
    [6] => /dev/sdh1            287826944   4329780 268876412   2% /home/node5
    [7] => /dev/sdi1            488302976 439077756  24420864  95% /home/node1
    [8] => /dev/sdh1            287826944   4329780 268876412   2% /home/mnode6
    [9] => /dev/sdh1            287826944   4329780 268876412   2% /home/mnode7
    [10] => tmpfs                  3145728         0   3145728   0% /tmp
)

I need to extract how much free space each "node" or "mnode" has. So perhaps a nice array that has pairs such as this:

mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-09-12 10:46:47

使用 explode(' ', preg_replace('/ +/', ' ', $line)) 获取所有值均已拆分的数组。

Use explode(' ', preg_replace('/ +/', ' ', $line)) to get an array with all values split up.

沙与沫 2024-09-12 10:46:47

要生成一个关联数组,其中键来自 Mounted On 值,值来自 Available 值,您可以循环并解析 shell_exec( 'df') 输出字符串(忽略第一行中的标头)并使用 sscanf() 将数据解析为所需的关联数组。 %*s 将匹配非空白字符,但不会捕获返回的匹配。我将使用数组解构(在第一个元素之前访问第二个元素,以最大程度地减少声明的变量总数。

代码:

$result = [];
foreach (explode(PHP_EOL, rtrim(shell_exec('df'))) as $i => $line) {
    if ($i) {
        [1 => $mountedOn, 0 => $result[$mountedOn]] = sscanf($line, '%*s %*d %*d %d %*s %s');
    }
}
var_export($result);

输出:

[
    '/' => 41426064,
    '/home/overflow' => 18946708,
    '/home/node2' => 0,
    '/home/node3' => 19927228,
    '/home/node4' => 268434424,
    '/home/node5' => 268876412,
    '/home/node1' => 24420864,
    '/home/mnode6' => 268876412,
    '/home/mnode7' => 268876412,
    '/tmp' => 3145728,
]

我还发布了一个非常类似问题的答案:仅显示 shell_exec('df') 中磁盘使用数据的特定列

To generate an associative array where the keys are from Mounted On values and the values are from Available values, you can loop over and parse the lines of the shell_exec('df') output string (ignoring the headers in the first line) and use sscanf() to parse the data into the desired associative array. %*s will match non-whitespace characters, but not capture the match for returning. I'll use array destructuring (accessing the 2nd element before the 1st element to minimize the total number of variables declared.

Code:

$result = [];
foreach (explode(PHP_EOL, rtrim(shell_exec('df'))) as $i => $line) {
    if ($i) {
        [1 => $mountedOn, 0 => $result[$mountedOn]] = sscanf($line, '%*s %*d %*d %d %*s %s');
    }
}
var_export($result);

Output:

[
    '/' => 41426064,
    '/home/overflow' => 18946708,
    '/home/node2' => 0,
    '/home/node3' => 19927228,
    '/home/node4' => 268434424,
    '/home/node5' => 268876412,
    '/home/node1' => 24420864,
    '/home/mnode6' => 268876412,
    '/home/mnode7' => 268876412,
    '/tmp' => 3145728,
]

I have also posted an answer to a very similar question: Display only specific columns of disk usage data from shell_exec('df')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文