| title | API Reference |
|---|---|
| description | Complete API documentation for all public methods and classes in Cbox System Metrics |
| weight | 80 |
Complete API documentation for all public methods.
Main entry point for system metrics.
Get environment detection (OS, kernel, architecture, virtualization, containers, cgroups).
Returns: Result<EnvironmentSnapshot>
Caching: Results are automatically cached after first call
Example:
$env = SystemMetrics::environment()->getValue();
echo "OS: {$env->os->name}\n";Get CPU time counters (raw ticks, not percentages).
Returns: Result<CpuSnapshot>
Example:
$cpu = SystemMetrics::cpu()->getValue();
echo "Cores: {$cpu->coreCount()}\n";Calculate CPU usage percentage over the specified interval (blocking).
Parameters:
$interval- Time to wait in seconds (e.g., 1.0 for 1 second)
Returns: Result<CpuDelta>
Example:
$delta = SystemMetrics::cpuUsage(1.0)->getValue();
echo "CPU: " . round($delta->usagePercentage(), 1) . "%\n";Get memory metrics (physical RAM and swap).
Returns: Result<MemorySnapshot>
Example:
$mem = SystemMetrics::memory()->getValue();
echo "Usage: " . round($mem->usedPercentage(), 1) . "%\n";Get system load average (1, 5, 15 minutes).
Returns: Result<LoadAverageSnapshot>
Example:
$load = SystemMetrics::loadAverage()->getValue();
echo "Load (1 min): {$load->oneMinute}\n";Get system uptime since last boot.
Returns: Result<UptimeSnapshot>
Example:
$uptime = SystemMetrics::uptime()->getValue();
echo "Uptime: {$uptime->humanReadable()}\n";Get filesystem usage and disk I/O statistics.
Returns: Result<StorageSnapshot>
Example:
$storage = SystemMetrics::storage()->getValue();
foreach ($storage->mountPoints as $mount) {
echo "{$mount->mountPoint}: {$mount->usedPercentage()}%\n";
}Get network interface statistics and connections.
Returns: Result<NetworkSnapshot>
Example:
$network = SystemMetrics::network()->getValue();
foreach ($network->interfaces as $iface) {
echo "{$iface->name}: {$iface->stats->totalBytes()} bytes\n";
}Get container resource limits and usage (cgroups).
Returns: Result<ContainerLimits>
Example:
$container = SystemMetrics::container()->getValue();
if ($container->hasCpuLimit()) {
echo "CPU quota: {$container->cpuQuota} cores\n";
}Get actual resource limits (environment-aware: host vs container).
Returns: Result<SystemLimits>
Example:
$limits = SystemMetrics::limits()->getValue();
echo "CPU: {$limits->cpuCores} cores\n";
echo "Memory: " . round($limits->memoryBytes / 1024**3, 2) . " GB\n";Get complete snapshot of all available metrics in a single call.
Core metrics (environment, CPU, memory) are required — if any fail, the result is a failure.
Optional metrics (storage, network, load average, uptime, limits, container) gracefully degrade to null.
Returns: Result<SystemOverview>
Properties:
| Property | Type | Required |
|---|---|---|
environment |
EnvironmentSnapshot |
Yes |
cpu |
CpuSnapshot |
Yes |
memory |
MemorySnapshot |
Yes |
storage |
?StorageSnapshot |
No |
network |
?NetworkSnapshot |
No |
loadAverage |
?LoadAverageSnapshot |
No |
uptime |
?UptimeSnapshot |
No |
limits |
?SystemLimits |
No |
container |
?ContainerLimits |
No |
Example:
$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";
echo "Load: {$overview->loadAverage?->oneMinute}\n";
echo "Uptime: {$overview->uptime?->humanReadable()}\n";
if ($overview->limits?->isContainerized()) {
echo "Container memory limit: " . round($overview->limits->memoryBytes / 1024**2) . " MB\n";
}Clear cached environment detection results.
Returns: void
Example:
SystemMetrics::clearEnvironmentCache();
$env = SystemMetrics::environment()->getValue(); // Fresh readProcess-level monitoring.
Get one-time snapshot of a process.
Parameters:
$pid- Process ID
Returns: Result<ProcessSnapshot>
Example:
$process = ProcessMetrics::snapshot(getmypid())->getValue();
echo "Memory: {$process->resources->memoryRssBytes} bytes\n";Get snapshot of process group (parent + all children).
Parameters:
$pid- Parent process ID
Returns: Result<ProcessGroupSnapshot>
Example:
$group = ProcessMetrics::group($pid)->getValue();
echo "Total processes: {$group->totalProcessCount()}\n";Start tracking a process or process group.
Parameters:
$pid- Process ID$includeChildren- Track children too
Returns: Result<string> - Tracker ID
Example:
$trackerId = ProcessMetrics::start($pid)->getValue();Take manual sample of tracked process (optional).
Parameters:
$trackerId- Tracker ID fromstart()
Returns: Result<void>
Example:
ProcessMetrics::sample($trackerId);Stop tracking and get statistics.
Parameters:
$trackerId- Tracker ID fromstart()
Returns: Result<ProcessStats>
Example:
$stats = ProcessMetrics::stop($trackerId)->getValue();
echo "Peak memory: {$stats->peak->memoryRssBytes} bytes\n";All metrics methods return Result<T> objects with these methods:
Check if operation succeeded.
Check if operation failed.
Get the value (throws if failure).
Get value or default if failure.
Get error (null if success).
Transform success value.
Execute callback on success.
Execute callback on failure.
Set custom CPU metrics source.
Set custom memory metrics source.
Set custom environment detector.
See Custom Implementations for details.
- Quick Start - Get started quickly
- Basic Usage - Common usage patterns
- Error Handling - Result pattern
- Custom Implementations - Extend the library