PHP code example of innmind / server-status

1. Go to this page and download the library: Download innmind/server-status 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/ */

    

innmind / server-status example snippets


use Innmind\Server\Status\{
    ServerFactory,
    Server\Disk\Volume\MountPoint,
    Server\Process\Pid,
    EnvironmentPath,
};
use Innmind\Server\Control\ServerFactory as Control;
use Innmind\TimeContinuum\Earth\Clock;
use Innmind\TimeWarp\Halt\Usleep;
use Innmind\Stream\Streams;

$server = ServerFactory::build(
    $clock = new Clock,
    Control::build(
        $clock,
        Streams::fromAmbientAuthority(),
        new Usleep,
    ),
    EnvironmentPath::of(\getenv('PATH')),
);

$server->cpu()->match(
    function($cpu) {
        $cpu->user(); // percentage of the cpu used by the user
        $cpu->system(); // percentage of the cpu used by the system
        $cpu->idle(); // percentage of the cpu not used
        $cpu->cores(); // number of cores available
    },
    fn() => null, // unable to retrieve the cpu information
);

$server->memory()->match(
    function($memory) {
        $memory->total(); // total memory of the server
        $memory->active(); // memory that is used by processes
        $memory->free(); // memory that is not used
        $memory->swap(); // memory that is used and located on disk
        $memory->used(); // total - free
    },
    fn() => null, // unable to retrieve the memory information
);

$server->loadAverage()->lastMinute();
$server->loadAverage()->lastFiveMinutes();
$server->loadAverage()->lastFifteenMinutes();

$server->disk()->get(new MountPoint('/'))->match(
    function($disk) {
        $disk->size(); // total size of the volume
        $disk->available();
        $disk->used();
        $disk->usage(); // percentage of space being used
    },
    fn() => null, // the mount point doesn't exist
);

$server->processes()->get(new Pid(1))->match(
    function($process) {
        $process->user(); // root in this case
        $process->cpu(); // percentage
        $process->memory(); // percentage
        $process->start(); // point in time at which the process started
        $process->command();
    },
    fn() => null, // the process doesn't exist
);

$server->tmp(); // path to temp directory

use Innmind\Server\Status\Server\Logger;
use Psr\Log\LoggerInterface;

$server = new Logger($server, /** instance of LoggerInterface */);