Skip to content

Commit

Permalink
optomize df command
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasvielmetter committed Feb 20, 2024
1 parent 16898a2 commit 055412d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
49 changes: 22 additions & 27 deletions src/Recorders/RemoteServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function record(SharedBeat $event): void
[1] 5534304
top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'
[2] 18
df --output=used,avail /
[3] Used Avail
[4] 34218600 473695292
df / | awk 'NR==2 {print $3 "\n" $4 }'
[3] 34218600
[4] 473695292
*/

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

if (count($storageDirectories) == 1 && $storageDirectories[0] == "/") {
$storage = preg_replace('/\s+/', ' ', $remoteServerStats[4]); // replace multi space with single space
$storage = explode(" ", $storage); // break into segments based on sigle space

$storageTotal = $storage[0] + $storage[1]; // used and availble
$storageUsed = $storage[0]; // used

$storage = [collect([
'directory' => "/",
'total' => intval(round($storageTotal / 1024)), // MB
'used' => intval(round($storageUsed / 1024)), // MB
'total' => (round((intval($remoteServerStats[3]) + intval($remoteServerStats[4])) / 1024)), // MB
'used' => (round(intval($remoteServerStats[3]) / 1024)), // MB
])];
} else {
$storage = collect($storageDirectories)
->map(function (string $directory) use ($remote_ssh) {
$storage = match (PHP_OS_FAMILY) {
'Darwin' => (`$remote_ssh 'df $directory'`),
'Linux' => (`$remote_ssh 'df $directory'`),
default => throw new RuntimeException('The pulse:check command does not currently support ' . PHP_OS_FAMILY),
};

$storage = explode("\n", $storage); // break in lines
$storage = preg_replace('/\s+/', ' ', $storage[1]); // replace multi space with single space
$storage = explode(" ", $storage); // break into segments based on sigle space

$storageTotal = $storage[2] + $storage[3]; // used and availble
$storageUsed = $storage[2]; // used

->map(function (string $directory) use ($remote_ssh, $remoteServerStats) {
if($directory=="/") {
$storageTotal = intval($remoteServerStats[3]) + intval($remoteServerStats[4]); // used and availble
$storageUsed = intval($remoteServerStats[3]); // used
} else {
$storage = match (PHP_OS_FAMILY) {
'Darwin' => (`$remote_ssh 'df $directory' | awk 'NR==2 {print $3 "\n" $4 }'`),
'Linux' => (`$remote_ssh 'df $directory' | awk 'NR==2 {print $3 "\n" $4 }'`),
default => throw new RuntimeException('The pulse:check command does not currently support ' . PHP_OS_FAMILY),
};
$storage = explode("\n", $storage); // break in lines
$storageTotal = intval($storage[0]) + intval($storage[1]); // used and availble
$storageUsed = intval($storage[0]); // used
}

return [
'directory' => $directory,
'total' => intval(round($storageTotal / 1024)), // MB
'used' => intval(round($storageUsed / 1024)), // MB
'total' => (round($storageTotal / 1024)), // MB
'used' => (round($storageUsed / 1024)), // MB
];
})
->all();
Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/server-stats-linux.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+'
cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+'
top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'
df --output=used,avail /
df / | awk 'NR==2 {print $3 "\n" $4 }'

0 comments on commit 055412d

Please sign in to comment.