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

v1.1.0(1mo ago)0560↓33.3%1MITPHPPHP ^7.4|^8.0CI 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 master Synced 1mo ago

READMEChangelog (2)Dependencies (14)Versions (4)Used By (1)

Jmonitor
========

[](#jmonitor)

Jmonitor.io is a SaaS monitoring service that provides insights, alerting and premade dashboards from multiple sources commonly found in PHP web project stack (MySQL, Redis, Apache, Nginx, etc.).

This package provides collectors that gather metrics and send them to Jmonitor.io.

- Website:
- Symfony integration:

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

[](#requirements)

- PHP 7.4
- 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)
- ### 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:

    ```
