PHPackages                             jmonitor/collector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jmonitor/collector

ActiveLibrary[Database &amp; ORM](/categories/database)

jmonitor/collector
==================

PHP library for collecting server metrics (PHP, MySQL, Nginx, Apache, Redis, System…) and sending them to Jmonitor.io

v2.0.0(1mo ago)02.8k↑129.6%1MITPHPPHP ^8.1CI passing

Since Mar 20Pushed 1mo agoCompare

[ Source](https://github.com/jmonitor/collector)[ Packagist](https://packagist.org/packages/jmonitor/collector)[ Docs](https://jmonitor.io/)[ RSS](/packages/jmonitor-collector/feed)WikiDiscussions 2.x Synced 2w ago

READMEChangelog (9)Dependencies (70)Versions (19)Used By (1)

Jmonitor
========

[](#jmonitor)

### Simple monitoring for PHP &amp; Symfony stacks

[](#simple-monitoring-for-php--symfony-stacks)

Plug the collectors, get clear dashboards in minutes — without building and maintaining a Grafana/Prometheus stack.

[![Packagist Version](https://camo.githubusercontent.com/d49f4458562ced84fabeb717e41f9129e85ab1aaf99b0b5f0d7683566c7e8aab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6d6f6e69746f722f636f6c6c6563746f723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jmonitor/collector)[![PHP Version](https://camo.githubusercontent.com/4a005ab785319c31a0bb9197f9abd2395a43a6f7642fee3adf66ab13de337464/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a6d6f6e69746f722f636f6c6c6563746f723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jmonitor/collector)[![Tests](https://camo.githubusercontent.com/23f7f737b66ad83d16547cc502ce7b8f2058b0afb434ee06a8c29110f1ff0158/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a6d6f6e69746f722f636f6c6c6563746f722f74657374732e796d6c3f6272616e63683d322e78266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/jmonitor/collector/actions)[![License](https://camo.githubusercontent.com/b9572e3eacaf081a8a18a6ce4fc749fcac3cd71d3e005daf3031f6e246694f81/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a6d6f6e69746f722f636f6c6c6563746f723f7374796c653d666c61742d737175617265)](LICENSE)[![Last Commit](https://camo.githubusercontent.com/139f73eaea299ffe559f365b48728887ce7b84d436b4f84c9289b79dd4309597/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f6a6d6f6e69746f722f636f6c6c6563746f723f7374796c653d666c61742d737175617265)](https://github.com/jmonitor/collector/commits)

[**Website**](https://jmonitor.io) · [**Symfony bundle** (optional)](https://github.com/jmonitor/jmonitor-bundle)

[![Jmonitor dashboard](.github/assets/hero-dashboard.png)](.github/assets/hero-dashboard.png)

Why Jmonitor?
-------------

[](#why-jmonitor)

Grafana, Prometheus and Datadog are powerful, but they take time to configure and expertise to run. Jmonitor focuses on getting you readable dashboards fast, made for the PHP world.

- **Built for the PHP/Symfony ecosystem** — dedicated collectors for MySQL, Redis, Apache, Nginx, Caddy, PHP, FrankenPHP &amp; PostgreSQL, plus a Symfony bundle for drop-in integration.
- **Readable out of the box** — premade dashboards (gauges + graphs) anyone on the team can understand, not just observability experts.
- **Lightweight to install** — a small PHP collector library running as a worker. No agent, no heavy infra to maintain.
- **Multi-project &amp; team-ready** — manage several projects with role-based access (Owner / Admin / Member).

> Jmonitor monitors itself with Jmonitor.

---

**This package** provides the PHP collectors that gather metrics and send them to [Jmonitor.io](https://jmonitor.io). For Symfony projects, use the [Jmonitor bundle](https://github.com/jmonitor/jmonitor-bundle) for drop-in integration.

Requirements
------------

[](#requirements)

- PHP 8.1+ (for PHP 7.4/8.0, use the [1.x branch](https://github.com/jmonitor/collector/tree/1.x))
- A project using [Composer](https://getcomposer.org/)

Installation
------------

[](#installation)

```
composer require jmonitor/collector
```

Quick Start
-----------

[](#quick-start)

Create a project in [jmonitor.io](https://jmonitor.io) and get your API key.

Then, create a separate script and start collecting metrics:

```
use Jmonitor\Jmonitor;
use Jmonitor\Collector\Apache\ApacheCollector;

$jmonitor = new Jmonitor('apiKey');

// Add some collectors... see the documentation below for more collectors
$jmonitor->addCollector(new ApacheCollector('https://example.com/server-status'));
$jmonitor->addCollector(new SystemCollector());
// ...

// send metrics to Jmonitor (see "Running the collector" section)
$jmonitor->collect();
```

### HTTP Client (PSR-18)

[](#http-client-psr-18)

You can inject any PSR-18 HTTP client (e.g., Symfony HttpClient via Psr18Client, Guzzle via an adapter, etc.). Example :

```
composer require symfony/http-client nyholm/psr7
```

```
use Symfony\Component\HttpClient\Psr18Client;

$httpClient = ... // create or retrieve your Symfony HttpClient instance
$client = (new Psr18Client())->withOptions(...);

$jmonitor = new Jmonitor('apiKey', $client);
```

Running the collector
---------------------

[](#running-the-collector)

The collector is designed to be run as a worker in a separate process.

This means you **must not** integrate it into your application and call `$jmonitor->collect()` on every web request.

One basic worker script is provided in the `examples` folder. Copy it into your project, update it to include the collectors you need, and run it from CLI.

In production, it is recommended to run the worker under a process manager (e.g. Supervisor or systemd) to ensure it is kept running and restarted periodically. For practical guidance, you can follow Symfony Messenger's recommendations:

You also can take a look at the CollectorCommand from the Symfony bundle for a more advanced example:

Some metrics are fairly static and remain cached for the lifetime of the collector, so among others reasons (memory, ...), it is **strongly recommended** to restart the collector regularly, at least once a day.

Debugging and Error Handling
----------------------------

[](#debugging-and-error-handling)

Each collector is isolated and executed within a try/catch block.
Use the CollectionResult returned by `collect()` method to inspect outcomes.

By default, collect() call send metrics to Jmonitor.
You can disable this by passing `send: false`

By default, collect() do not throws when the server response status code is &gt;= 400.
You can disable this by passing `throwOnFailure: false`

Finally, you can pass a PSR-3 logger to the constructor to get more detailed information about the collection process. You will receive messages ranging from debug to error level.

```
use Psr\Http\Message\ResponseInterface;
use Jmonitor\CollectionResult;
use Jmonitor\Jmonitor;

$jmonitor = new Jmonitor('apiKey', logger: new SomeLogger());

/**
 * Send metrics, you can :
 * - Disable throwing an exception on error
 * - Disable sending metrics to the server
 */
$result = $jmonitor->collect(throwOnFailure: false);
// Or disable completely the sending of metrics to the server
$result = $jmonitor->collect(send: false);

/**
 * Use $result to inspect
 */
// Human-readable summary (string)
$conclusion = $result->getConclusion();

// List of Exceptions if any (\Throwable[])
$errors = $result->getErrors();

// The raw response from jmonitor, if any (ResponseInterface|null)
$response = $result->getResponse();

// All metrics collected (mixed[])
$metrics = $result->getMetrics();
```

Collectors
----------

[](#collectors)

- [System](#system)
- [Apache](#apache)
- [Nginx](#nginx)
- [Mysql](#mysql)
- [Php](#php)
- [Redis](#redis)
- [Caddy](#caddy)
- [FrankenPHP](#frankenphp)
- [PostgreSQL](#postgresql)
- ### System

    [](#system-)

    Collects system metrics like CPU usage, memory usage, disk usage, etc. Linux only for now. Feel free to open an issue if you need other OS support.

    ```
    use Jmonitor\Collector\System\SystemCollector;

    $collector = new SystemCollector();

    // There is actually a "RandomAdapter" you can use on a Windows OS for testing purposes
    $collector = new SystemCollector(new RandomAdapter());
    ```
- ### Apache

    [](#apache-)

    Collects metrics from Apache "mod\_status" module. Enable it and expose a status URL. There are some resources to help you with that:

    - Apache docs (FR - EN): [https://httpd.apache.org/docs/current/mod/mod\_status.html](https://httpd.apache.org/docs/current/mod/mod_status.html).
    - Guide (EN):
    - Guide (FR): [https://www.blog.florian-bogey.fr/activer-et-configurer-le-server-status-apache-mod\_status.html](https://www.blog.florian-bogey.fr/activer-et-configurer-le-server-status-apache-mod_status.html)

    ```
    use Jmonitor\Collector\Apache\ApacheCollector;

    $collector = new ApacheCollector('http://localhost/server-status');
    ```
- ### Nginx

    [](#nginx-)

    Collects metrics from Nginx "stub\_status" module. Enable it and expose a status URL. There are some resources to help you with that:

    - Nginx docs (EN): [https://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html)
    - Stackoverflow (EN):
    - Guide (EN):

    ```
    use Jmonitor\Collector\Nginx\NginxCollector;

    $collector = new NginxCollector('http://localhost/nginx_status');
    ```
- ### Mysql

    [](#mysql-)

    Collects MySQL metrics from variables, status, and the `performance_schema` and `information_schema` tables if availables.
    Connect via PDO or Doctrine DBAL (open an issue if you need other drivers, e.g., mysqli).

    ```
    use Jmonitor\Collector\Mysql\MysqlCollector;
    use Jmonitor\Collector\Mysql\Adapter\PdoAdapter;
    use Jmonitor\Collector\Mysql\Adapter\DoctrineAdapter;
    use Jmonitor\Collector\Mysql\MysqlStatusCollector;
    use Jmonitor\Collector\Mysql\MysqlVariablesCollector;
    use Jmonitor\Collector\Mysql\MysqlInformationSchemaCollector
    use Jmonitor\Collector\Mysql\MysqlSlowQueriesCollector

    // Using PDO
    $adapter = new PdoAdapter($pdo); // your \PDO instance

    // or using Doctrine DBAL
    $adapter = new DoctrineAdapter($connection); // your Doctrine\DBAL\Connection instance

    // Mysql has multiple collectors, use the same adapter for all of them
    $collector = new MysqlInformationSchemaCollector($adapter, 'your_db_name');
    $collector = new MysqlSlowQueriesCollector($adapter, 'your_db_name');
    $collector = new MysqlStatusCollector($adapter);
    $collector = new MysqlVariablesCollector($adapter);

    // The slow queries collector can be configured to filter results:
    // - limit: maximum number of results to return (1–10, default: 5)
    // - minExecCount: minimum number of executions required to include a query (default: 1)
    // - minAvgTimeMs: minimum average execution time (in ms) for a query to be considered slow (default: 0)
    // - orderBy: column used for sorting; allowed values are "sum", "avg", "max"
    //   (see constants in MysqlSlowQueriesCollector, default: "avg")
    $collector = new MysqlSlowQueriesCollector($adapter, 'your_db_name', $limit, $minExecCount, $minAvgTimeMs, $orderBy);
    ```
- ### PHP

    [](#php-)

    Collects PHP metrics (loaded extensions, some ini keys, FPM, opcache, etc.).

Important

PHP configuration can differ significantly between CLI and web server.
To collect web‑server context metrics from a CLI script, which is probably what you want to do, expose an HTTP endpoint that returns these metrics as JSON (see below).

- Collect CLI-context metrics (current context)

    ```
    use Jmonitor\Collector\Php\PhpCollector;

    $collector = new PhpCollector();
    ```
- Collect web-server context metrics from CLI
    Expose a metrics endpoint (and **make sure it is properly secured**). You can reuse php-exposer.php from this repo or create your own:

    ```
