Skip to content

Commit 055412d

Browse files
optomize df command
1 parent 16898a2 commit 055412d

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

src/Recorders/RemoteServers.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function record(SharedBeat $event): void
6363
[1] 5534304
6464
top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'
6565
[2] 18
66-
df --output=used,avail /
67-
[3] Used Avail
68-
[4] 34218600 473695292
66+
df / | awk 'NR==2 {print $3 "\n" $4 }'
67+
[3] 34218600
68+
[4] 473695292
6969
*/
7070

7171
$memoryTotal = intval($remoteServerStats[0] / 1024);
@@ -75,37 +75,32 @@ public function record(SharedBeat $event): void
7575
$storageDirectories = $this->config->get('pulse.recorders.' . self::class . '.directories');
7676

7777
if (count($storageDirectories) == 1 && $storageDirectories[0] == "/") {
78-
$storage = preg_replace('/\s+/', ' ', $remoteServerStats[4]); // replace multi space with single space
79-
$storage = explode(" ", $storage); // break into segments based on sigle space
80-
81-
$storageTotal = $storage[0] + $storage[1]; // used and availble
82-
$storageUsed = $storage[0]; // used
83-
8478
$storage = [collect([
8579
'directory' => "/",
86-
'total' => intval(round($storageTotal / 1024)), // MB
87-
'used' => intval(round($storageUsed / 1024)), // MB
80+
'total' => (round((intval($remoteServerStats[3]) + intval($remoteServerStats[4])) / 1024)), // MB
81+
'used' => (round(intval($remoteServerStats[3]) / 1024)), // MB
8882
])];
8983
} else {
9084
$storage = collect($storageDirectories)
91-
->map(function (string $directory) use ($remote_ssh) {
92-
$storage = match (PHP_OS_FAMILY) {
93-
'Darwin' => (`$remote_ssh 'df $directory'`),
94-
'Linux' => (`$remote_ssh 'df $directory'`),
95-
default => throw new RuntimeException('The pulse:check command does not currently support ' . PHP_OS_FAMILY),
96-
};
97-
98-
$storage = explode("\n", $storage); // break in lines
99-
$storage = preg_replace('/\s+/', ' ', $storage[1]); // replace multi space with single space
100-
$storage = explode(" ", $storage); // break into segments based on sigle space
101-
102-
$storageTotal = $storage[2] + $storage[3]; // used and availble
103-
$storageUsed = $storage[2]; // used
104-
85+
->map(function (string $directory) use ($remote_ssh, $remoteServerStats) {
86+
if($directory=="/") {
87+
$storageTotal = intval($remoteServerStats[3]) + intval($remoteServerStats[4]); // used and availble
88+
$storageUsed = intval($remoteServerStats[3]); // used
89+
} else {
90+
$storage = match (PHP_OS_FAMILY) {
91+
'Darwin' => (`$remote_ssh 'df $directory' | awk 'NR==2 {print $3 "\n" $4 }'`),
92+
'Linux' => (`$remote_ssh 'df $directory' | awk 'NR==2 {print $3 "\n" $4 }'`),
93+
default => throw new RuntimeException('The pulse:check command does not currently support ' . PHP_OS_FAMILY),
94+
};
95+
$storage = explode("\n", $storage); // break in lines
96+
$storageTotal = intval($storage[0]) + intval($storage[1]); // used and availble
97+
$storageUsed = intval($storage[0]); // used
98+
}
99+
105100
return [
106101
'directory' => $directory,
107-
'total' => intval(round($storageTotal / 1024)), // MB
108-
'used' => intval(round($storageUsed / 1024)), // MB
102+
'total' => (round($storageTotal / 1024)), // MB
103+
'used' => (round($storageUsed / 1024)), // MB
109104
];
110105
})
111106
->all();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+'
22
cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+'
33
top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'
4-
df --output=used,avail /
4+
df / | awk 'NR==2 {print $3 "\n" $4 }'

0 commit comments

Comments
 (0)