Skip to content

[1.x] Customise server stats #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 87 additions & 23 deletions src/Recorders/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ class Servers
{
use Concerns\Throttling;

/**
* Callback to detect CPU usage.
*
* @var null|(callable(): int)
*/
protected static $detectCpuUsing;

/**
* Callback to detect memory.
*
* @var null|(callable(): array{total: int, used: int})
*/
protected static $detectMemoryUsing;

/**
* The events to listen for.
*
Expand All @@ -32,6 +46,26 @@ public function __construct(
//
}

/**
* Detect CPU via the given callback.
*
* @param null|(callable(): int) $callback
*/
public static function detectCpuUsing(?callable $callback): void
{
self::$detectCpuUsing = $callback;
}

/**
* Detect memory via the given callback.
*
* @param null|(callable(): array{total: int, used: int}) $callback
*/
public static function detectMemoryUsing(?callable $callback): void
{
self::$detectMemoryUsing = $callback;
}

/**
* Record the system stats.
*/
Expand All @@ -41,29 +75,8 @@ public function record(SharedBeat $event): void
$server = $this->config->get('pulse.recorders.'.self::class.'.server_name');
$slug = Str::slug($server);

$memoryTotal = match (PHP_OS_FAMILY) {
'Darwin' => intval(`sysctl hw.memsize | grep -Eo '[0-9]+'` / 1024 / 1024),
'Linux' => intval(`cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+'` / 1024),
'Windows' => intval(((int) trim(`wmic ComputerSystem get TotalPhysicalMemory | more +1`)) / 1024 / 1024),
'BSD' => intval(`sysctl hw.physmem | grep -Eo '[0-9]+'` / 1024 / 1024),
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};

$memoryUsed = match (PHP_OS_FAMILY) {
'Darwin' => $memoryTotal - intval(intval(`vm_stat | grep 'Pages free' | grep -Eo '[0-9]+'`) * intval(`pagesize`) / 1024 / 1024), // MB
'Linux' => $memoryTotal - intval(`cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+'` / 1024), // MB
'Windows' => $memoryTotal - intval(((int) trim(`wmic OS get FreePhysicalMemory | more +1`)) / 1024), // MB
'BSD' => intval(intval(`( sysctl vm.stats.vm.v_cache_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_inactive_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_active_count | grep -Eo '[0-9]+' ) | awk '{s+=$1} END {print s}'`) * intval(`pagesize`) / 1024 / 1024), // MB
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};

$cpu = match (PHP_OS_FAMILY) {
'Darwin' => (int) `top -l 1 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5 }'`,
'Linux' => (int) `top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'`,
'Windows' => (int) trim(`wmic cpu get loadpercentage | more +1`),
'BSD' => (int) `top -b -d 2| grep 'CPU: ' | tail -1 | awk '{print$10}' | grep -Eo '[0-9]+\.[0-9]+' | awk '{ print 100 - $1 }'`,
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};
['total' => $memoryTotal, 'used' => $memoryUsed] = $this->memory();
$cpu = $this->cpu();

$this->pulse->record('cpu', $slug, $cpu, $event->time)->avg()->onlyBuckets();
$this->pulse->record('memory', $slug, $memoryUsed, $event->time)->avg()->onlyBuckets();
Expand All @@ -82,4 +95,55 @@ public function record(SharedBeat $event): void
], flags: JSON_THROW_ON_ERROR), $event->time);
});
}

/**
* CPU usage.
*/
protected function cpu(): int
{
if (self::$detectCpuUsing) {
return (self::$detectCpuUsing)();
}

return match (PHP_OS_FAMILY) {
'Darwin' => (int) `top -l 1 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5 }'`,
'Linux' => (int) `top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }'`,
'Windows' => (int) trim(`wmic cpu get loadpercentage | more +1`),
'BSD' => (int) `top -b -d 2| grep 'CPU: ' | tail -1 | awk '{print$10}' | grep -Eo '[0-9]+\.[0-9]+' | awk '{ print 100 - $1 }'`,
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};
}

/**
* Memory usage.
*
* @return array{total: int, used: int}
*/
protected function memory(): array
{
if (self::$detectMemoryUsing) {
return (self::$detectMemoryUsing)();
}

$memoryTotal = match (PHP_OS_FAMILY) {
'Darwin' => intval(`sysctl hw.memsize | grep -Eo '[0-9]+'` / 1024 / 1024),
'Linux' => intval(`cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+'` / 1024),
'Windows' => intval(((int) trim(`wmic ComputerSystem get TotalPhysicalMemory | more +1`)) / 1024 / 1024),
'BSD' => intval(`sysctl hw.physmem | grep -Eo '[0-9]+'` / 1024 / 1024),
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};

$memoryUsed = match (PHP_OS_FAMILY) {
'Darwin' => $memoryTotal - intval(intval(`vm_stat | grep 'Pages free' | grep -Eo '[0-9]+'`) * intval(`pagesize`) / 1024 / 1024), // MB
'Linux' => $memoryTotal - intval(`cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+'` / 1024), // MB
'Windows' => $memoryTotal - intval(((int) trim(`wmic OS get FreePhysicalMemory | more +1`)) / 1024), // MB
'BSD' => intval(intval(`( sysctl vm.stats.vm.v_cache_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_inactive_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_active_count | grep -Eo '[0-9]+' ) | awk '{s+=$1} END {print s}'`) * intval(`pagesize`) / 1024 / 1024), // MB
default => throw new RuntimeException('The pulse:check command does not currently support '.PHP_OS_FAMILY),
};

return [
'total' => $memoryTotal,
'used' => $memoryUsed,
];
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Recorders/ServersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,31 @@
expect($aggregates->pluck('key')->unique()->values()->all())->toBe(['foo']);
expect($aggregates->pluck('aggregate')->unique()->values()->all())->toBe(['avg']);
});

it('can customise CPU and memory resolution', function () {
Config::set('pulse.recorders.'.Servers::class.'.server_name', 'Foo');
Date::setTestNow(Date::now()->startOfMinute());

Servers::detectCpuUsing(fn () => 987654321);
Servers::detectMemoryUsing(fn () => [
'total' => 123456789,
'used' => 1234,
]);
event(new SharedBeat(CarbonImmutable::now(), 'instance-id'));
Pulse::ingest();

$value = Pulse::ignore(fn () => DB::table('pulse_values')->sole());

$payload = json_decode($value->value);
expect($payload->cpu)->toBe(987654321);
expect($payload->memory_used)->toBe(1234);
expect($payload->memory_total)->toBe(123456789);

$aggregates = Pulse::ignore(fn () => DB::table('pulse_aggregates')->get());
expect($aggregates->count())->toBe(8);
expect($aggregates->pluck('type')->unique()->values()->all())->toBe(['cpu', 'memory']);
expect($aggregates->pluck('value')->unique()->values()->all())->toEqual(['987654321.00', '1234.00']);

Servers::detectCpuUsing(null);
Servers::detectMemoryUsing(null);
});