cboxdk/system-metrics
Get real-time system metrics from Linux and macOS in pure PHP. No extensions or dependencies—just type-safe, readonly classes covering CPU, memory, storage, network, load, uptime, and container metrics with explicit Result-based error handling.
Get real-time system metrics from Linux and macOS in pure PHP. No extensions, no dependencies, just clean type-safe access to CPU, memory, storage, network, and container metrics.
use Cbox\SystemMetrics\SystemMetrics;
$overview = SystemMetrics::overview()->getValue();
echo "OS: {$overview->environment->os->name}\n";
echo "CPU Cores: {$overview->cpu->coreCount()}\n";
echo "Memory: " . round($overview->memory->usedPercentage(), 1) . "%\n";
✨ Pure PHP Implementation
🔒 Type-Safe with Modern PHP
declare(strict_types=1))🎯 Explicit Error Handling
📊 Comprehensive Metrics
🏗️ Production-Ready
/proc, /sys filesystemssysctl, vm_stat, sw_vers commandsNote: No special permissions or root access required.
composer require cboxdk/system-metrics
use Cbox\SystemMetrics\SystemMetrics;
$result = SystemMetrics::overview();
if ($result->isSuccess()) {
$overview = $result->getValue();
// Environment
echo "OS: {$overview->environment->os->name} {$overview->environment->os->version}\n";
echo "Architecture: {$overview->environment->architecture->kind->value}\n";
// CPU
echo "CPU Cores: {$overview->cpu->coreCount()}\n";
// Memory (cgroup-aware — shows container limit when containerized)
if ($overview->limits !== null) {
echo "Memory: " . round($overview->limits->memoryUtilization(), 1) . "%\n";
echo "Containerized: " . ($overview->limits->isContainerized() ? 'yes' : 'no') . "\n";
}
// Load Average
echo "Load Average (1 min): {$overview->loadAverage?->oneMinute}\n";
// Uptime
echo "Uptime: {$overview->uptime?->humanReadable()}\n";
}
// Environment detection
$env = SystemMetrics::environment()->getValue();
echo "OS: {$env->os->family->value}\n";
// CPU metrics
$cpu = SystemMetrics::cpu()->getValue();
echo "CPU Cores: {$cpu->coreCount()}\n";
// Memory metrics
$mem = SystemMetrics::memory()->getValue();
echo "Memory: " . round($mem->usedPercentage(), 1) . "%\n";
// Load average
$load = SystemMetrics::loadAverage()->getValue();
echo "Load (1 min): {$load->oneMinute}\n";
// Storage metrics
$storage = SystemMetrics::storage()->getValue();
echo "Storage: " . round($storage->usedPercentage(), 1) . "%\n";
// Network metrics
$network = SystemMetrics::network()->getValue();
echo "Interfaces: " . count($network->interfaces) . "\n";
// Container metrics (cgroups)
$container = SystemMetrics::container()->getValue();
if ($container->hasCpuLimit()) {
echo "Container CPU limit: {$container->cpuQuota} cores\n";
}
// Unified limits (environment-aware)
$limits = SystemMetrics::limits()->getValue();
echo "Available CPU: {$limits->availableCpuCores()} cores\n";
echo "Available Memory: " . round($limits->availableMemoryBytes() / 1024**3, 2) . " GB\n";
// Process monitoring
$process = ProcessMetrics::snapshot(getmypid())->getValue();
echo "Process Memory: " . round($process->resources->memoryRssBytes / 1024**2, 2) . " MB\n";
CPU metrics return raw time counters. To calculate usage percentage:
// Convenience method (blocks for 1 second)
$delta = SystemMetrics::cpuUsage(1.0)->getValue();
echo "CPU Usage: " . round($delta->usagePercentage(), 1) . "%\n";
// Or manually with two snapshots
$snap1 = SystemMetrics::cpu()->getValue();
sleep(2);
$snap2 = SystemMetrics::cpu()->getValue();
$delta = CpuSnapshot::calculateDelta($snap1, $snap2);
echo "CPU Usage: " . round($delta->usagePercentage(), 1) . "%\n";
All methods return Result<T> for explicit error handling:
$result = SystemMetrics::memory();
if ($result->isSuccess()) {
$memory = $result->getValue();
echo "Memory: " . round($memory->usedPercentage(), 1) . "%\n";
} else {
echo "Error: " . $result->getError()->getMessage() . "\n";
}
// Or use functional style
SystemMetrics::cpu()
->onSuccess(fn($cpu) => echo "CPU: {$cpu->coreCount()} cores\n")
->onFailure(fn($err) => error_log($err->getMessage()));
Comprehensive documentation is available in the docs/ directory:
# Run tests
composer test
# With coverage
composer test-coverage
# Static analysis
composer analyse
# Code style
composer format
See Testing Guide for details.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?