zetacomponents/system-information
Provides access to system and environment information via the eZ Components/Zeta Components library. Query OS details, hardware and memory stats, CPU and uptime, network interfaces, load, and related runtime metrics for monitoring or diagnostics in PHP apps.
Installation
composer require zetacomponents/system-information
Add to composer.json under require if not using Composer globally.
First Use Case: Fetching Basic System Info
use Zeta\Components\SystemInformation\SystemInformation;
$systemInfo = new SystemInformation();
$cpuInfo = $systemInfo->getCpuInfo();
$memoryInfo = $systemInfo->getMemoryInfo();
// Output CPU details
echo "CPU: " . $cpuInfo['type'] . " @ " . $cpuInfo['speed'] . " MHz";
// Output memory details
echo "Memory: " . round($memoryInfo['total'] / (1024 * 1024 * 1024), 2) . " GB";
Where to Look First
Zeta\Components\SystemInformation\SystemInformationtests/ for real-world usage examples.Zeta\Components\SystemInformation\SystemInformation::* for predefined keys (e.g., CPU_TYPE, MEMORY_TOTAL).// In a Laravel controller or service
public function getSystemStats()
{
$systemInfo = new SystemInformation();
return [
'cpu' => [
'type' => $systemInfo->getCpuInfo()['type'],
'cores' => $systemInfo->getCpuInfo()['cores'],
'speed' => $systemInfo->getCpuInfo()['speed'],
],
'memory' => [
'total' => $this->formatBytes($systemInfo->getMemoryInfo()['total']),
'free' => $this->formatBytes($systemInfo->getMemoryInfo()['free']),
],
'disk' => $this->getDiskUsage(),
];
}
private function formatBytes($bytes)
{
return round($bytes / (1024 ** 2), 2) . ' MB';
}
return Cache::remember('system_stats', now()->addMinutes(1), function () {
return $this->getSystemStats();
});
public function validateHardwareRequirements()
{
$systemInfo = new SystemInformation();
$memory = $systemInfo->getMemoryInfo()['total'];
$cpuCores = $systemInfo->getCpuInfo()['cores'];
if ($memory < 4 * 1024 * 1024 * 1024) { // <4GB
throw new \RuntimeException("Minimum 4GB RAM required.");
}
if ($cpuCores < 2) {
throw new \RuntimeException("Minimum 2 CPU cores required.");
}
return true;
}
use Illuminate\Support\Facades\Log;
public function logSystemMetrics()
{
$systemInfo = new SystemInformation();
Log::info('System Metrics', [
'cpu' => $systemInfo->getCpuInfo(),
'memory' => $systemInfo->getMemoryInfo(),
'os' => $systemInfo->getOsInfo(),
]);
}
$schedule->call('App\Services\SystemMetricsLogger@logSystemMetrics')->daily();
public function getOptimizedConfig()
{
$systemInfo = new SystemInformation();
$cpuCores = $systemInfo->getCpuInfo()['cores'];
$memoryGB = round($systemInfo->getMemoryInfo()['total'] / (1024 ** 3));
return [
'queue' => [
'connections' => min($cpuCores * 2, 8), // Max 8 workers
'max_jobs' => $memoryGB * 100, // Arbitrary scaling
],
];
}
Register the package as a singleton in AppServiceProvider:
public function register()
{
$this->app->singleton(SystemInformation::class, function () {
return new SystemInformation();
});
}
Now inject SystemInformation anywhere:
public function __construct(private SystemInformation $systemInfo) {}
Share system info across all views:
public function boot()
{
View::composer('*', function ($view) {
$view->with('systemInfo', app(SystemInformation::class));
});
}
Access in Blade:
<div class="system-info">
CPU: {{ $systemInfo->getCpuInfo()['type'] }} |
Memory: {{ $systemInfo->getMemoryInfo()['free'] / (1024 ** 2) }} MB
</div>
public function handle($request, Closure $next)
{
$systemInfo = new SystemInformation();
if ($systemInfo->getMemoryInfo()['total'] < 8 * 1024 * 1024 * 1024) { // <8GB
abort(403, 'Insufficient hardware resources.');
}
return $next($request);
}
Platform-Specific Data
php_uname(), sys_getloadavg(), and OS-specific commands (e.g., lscpu, free).null or less accurate data.
Fix: Add fallback logic:
$cpuCores = $systemInfo->getCpuInfo()['cores'] ?? 1; // Default to 1 core
Permission Issues
lscpu or free may fail if the PHP process lacks permissions.getCpuInfo() returns empty array).
Fix: Wrap calls in try-catch:
try {
$cpuInfo = $systemInfo->getCpuInfo();
} catch (\RuntimeException $e) {
$cpuInfo = ['type' => 'unknown', 'cores' => 1];
}
Memory Units Inconsistency
getMemoryInfo() returns bytes, but some methods (e.g., getOsInfo()) may return values in KB/MB.$memoryGB = $systemInfo->getMemoryInfo()['total'] / (1024 ** 3);
Caching Stale Data
Cache::remember('is_memory_low', now()->addSeconds(10), function () {
return app(SystemInformation::class)->getMemoryInfo()['free'] < 1 * 1024 ** 3;
});
Verify OS Support
Check php_uname('s') to confirm the OS:
$os = php_uname('s');
if (strpos($os, 'Linux') === false && strpos($os, 'Darwin') === false) {
// Windows or other OS; handle gracefully
}
Log Raw Outputs Debug command outputs by enabling verbose mode:
$systemInfo = new SystemInformation();
$systemInfo->setVerbose(true); // If supported (check package docs)
Fallback Values Provide defaults for critical paths:
$cpuInfo = $systemInfo->getCpuInfo() ?: [
'type' => 'unknown',
'speed' => 0,
'cores' => 1,
];
class ExtendedSystemInformation extends SystemInformation
{
public function
How can I help you explore Laravel packages today?