Introduction and Usage

zendframework/zenddiagnostics provides diagnostic tests for real-world PHP applications.

It currently ships with the following diagnostic checks:

  • ApcFragmentation - check if APC memory fragmentation is below given threshold,
  • ApcMemory - check available APC memory,
  • Callback - call a user-defined diagnostic function,
  • ClassExists - make sure class exists in current environment,
  • CouchDBCheck - check if connection is possible,
  • CpuPerformance - check server CPU performance is above baseline,
  • DirReadable - make sure given path is readable,
  • DirWritable - make sure given path is writable,
  • DiskFree - check there's enough free space on given path,
  • DiskUsage - check if the disk usage is below warning/critical percent thresholds,
  • DoctrineMigration - make sure all migrations are applied.
  • ExtensionLoaded - make sure extension is loaded,
  • GuzzleHttpService - check if given http host is responding using Guzzle,
  • HttpService - check if given http host is responding,
  • Memcache - check if memcache extension is loaded and given server is reachable,
  • Mongo - check if connection to MongoDb is possible,
  • OpCacheMemory - check if the OpCache memory usage is below warning/critical thresholds,
  • PDOCheck - check if connection is possible,
  • PhpVersion - make sure that PHP version matches constraint,
  • PhpFlag - make sure that given PHP flag (feature) is turned on or off.
  • ProcessRunning - check if a process with given name or ID is currently running,
  • RabbitMQ - Validate that a RabbitMQ service is running,
  • Redis - Validate that a Redis service is running,
  • SecurityAdvisory - check installed composer dependencies against SensioLabs SA database,
  • StreamWrapperExists - make sure given stream wrapper is available.

It also provides the following file validation checks:

  • IniFile - check if given INI file is available and valid,
  • JsonFile - check if given JSON file is available and valid,
  • XmlFile - check if given XML file is available and valid,
  • YamlFile - check if given YAML file is available and valid

Performing diagnostics with Zend Framework 2

  1. Install the ZFTool module.
  2. Enable diagnostic tests in your application config.php.
  3. In your console type php public/index.php diag to run diagnostics.

Note: this does not work with Zend Framework 3; use the plain PHP diagnostics instructions below when using that framework version.

Using diagnostics with Symfony 2

  1. Install the LiipMonitorBundle.
  2. Enable diagnostic tests in your application configuration.
  3. In your console type ./app/console monitor:health to run diagnostics.

Using diagnostics with PSR-7 middleware

Install the rstgroup/diagnostics-middleware.

Using diagnostics in plain PHP

  1. Create an instance of ZendDiagnostics\Runner.
  2. Add tests using Runner::addTest().
  3. Optionally add a reporter to display progress using Runner::addReporter().
  4. Run diagnostics Runner::run().

For example:

<?php
// run_diagnostics.php

use ZendDiagnostics\Check;
use ZendDiagnostics\Runner\Runner;
use ZendDiagnostics\Runner\Reporter\BasicConsole;

include 'vendor/autoload.php';

// Create Runner instance
$runner = new Runner();

// Add checks
$runner->addCheck(new Check\DirWritable('/tmp'));
$runner->addCheck(new Check\DiskFree(100000000, '/tmp'));

// Add console reporter
$runner->addReporter(new BasicConsole(80, true));

// Run all checks
$results = $runner->run();

// Emit an appropriate exit code
$status = ($results->getFailureCount() + $results->getWarningCount()) > 0 ? 1 : 0;
exit($status);

You can now run the file in your console (command line):

$ php run_diagnostics.php
Starting diagnostics:

..

OK (2 diagnostic tests)

Using a result collection

The diagnostics runner will always return a ZendDiagnostics\Result\Collection, even when no reporter is attached. This collection contains results for all tests and failure counters.

As an example:

<?php
use ZendDiagnostics\Check;
use ZendDiagnostics\Result;
use ZendDiagnostics\Runner\Runner;

$runner = new Runner();
$checkSpace = new Check\DiskFree(100000000, '/tmp');
$checkTemp  = new Check\DirWritable('/tmp');
$runner->addCheck($checkSpace);
$runner->addCheck($checkTemp);

// Run all checks
$results = $runner->run();

echo "Number of successful tests: " . $results->getSuccessCount() . "\n";
echo "Number of failed tests:     " . $results->getFailureCount() . "\n";

if ($results[$checkSpace] instanceof Result\FailureInterface) {
    echo "Oooops! We're running out of space on temp.\n";
}

if ($results[$checkTemp] instanceof Result\FailureInterface) {
    echo "It seems that /tmp is not writable - this is a serious problem!\n";
}

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!