PHP code example of cboxdk / system-metrics
1. Go to this page and download the library: Download cboxdk/system-metrics library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cboxdk / system-metrics example snippets
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";
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";
// 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";
$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()));