PHP code example of makinacorpus / monitoring-bundle
1. Go to this page and download the library: Download makinacorpus/monitoring-bundle 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/ */
declare(strict_types=1);
namespace App\Monitoring;
use MakinaCorpus\Monitoring\Probe;
use MakinaCorpus\Monitoring\ProbeStatus;
/**
* Collects number of items to process in queue, raise error when it is too much.
*/
final class QueueSizeProbe implements Probe
{
/** @var ?int */
private $warningThreshold;
/** @var ?int */
private $criticalThreshold;
/** @var \My\Favourite\Database\Client */
private $database;
public function __construct(
\My\Favourite\Database\Client $database,
?int $warningThreshold = null,
?int $criticalThreshold = null
) {
$this->database = $database;
$this->warningThreshold = $warningThreshold;
$this->criticalThreshold = $criticalThreshold;
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
// Internal name.
return 'queue_size';
}
/**
* {@inheritdoc}
*/
public function getTitle(): string
{
// Human readable name, for reports.
return "Queue size";
}
/**
* {@inheritdoc}
*/
public function getStatus(): ProbeStatus
{
$queueSize = $this->database->query('SELECT COUNT(*) FROM "my_queue_table" WHERE "is_consumed" is false')->fetch();
// For those who know Nagios or the like, just set a very short status
// message intended for display purpose in larger reports.
$message = \sprintf("my queue size: %d items", $queueSize);
if ($queueSize >= $this->criticalThreshold) {
return ProbeStatus::critical([$message, "queue is failing !"]);
}
if ($queueSize >= $this->warningThreshold) {
return ProbeStatus::warning($message);
}
return ProbeStatus::ok($message);
}
}
declare(strict_types=1);
namespace App\Monitoring;
use MakinaCorpus\Monitoring\InfoCollector;
use MakinaCorpus\Monitoring\Output\CollectionBuilder;
/**
* Collects information about all database tables.
*/
final class DataInfoCollector implements InfoCollector
{
/** @var \My\Favourite\Database\Client */
private $database;
public function __construct(\My\Favourite\Database\Client $database)
{
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
// Internal name.
return 'data';
}
/**
* {@inheritdoc}
*/
public function getTags(): iterable
{
// Tags will help build specific reports.
return ['database', 'data', 'volume'];
}
/**
* {@inheritdoc}
*/
public function getTitle(): string
{
// Human readable name, for reports.
return "Data information";
}
/**
* {@inheritdoc}
*/
public function info(CollectionBuilder $builder): void
{
// Yes, this should work with pgsql.
$rows = $this->database->query(<<<SQL
SELECT
pg_size_pretty(pg_total_relation_size(relid))
AS "total size",
pg_size_pretty(pg_table_size(relid))
AS "table size",
pg_size_pretty(pg_indexes_size(relid))
AS "index size",
concat(schemaname, '.', relname),
concat('seq_scan: ',seq_scan, E'\\nseq_tup_read: ', seq_tup_read, E'\\nidx_scan: ', idx_scan, E'\\nidx_tup_fetch: ', idx_tup_fetch)
AS "reads",
concat('insert: ',n_tup_ins, E'\\nupdate: ', n_tup_upd, E'\\nhot_update: ', n_tup_hot_upd, E'\\ndelete: ', n_tup_del)
AS "writes",
concat('live: ',n_live_tup, E'\\ndead: ', n_dead_tup, E'\\nmod_since_analyze: ', n_mod_since_analyze)
AS "state",
concat('last_vacuum: ',last_vacuum, 'auto: ', last_autovacuum, E'\\nlast_analyze: ', last_analyze, ' auto: ', last_autoanalyze, E'\\ncpt_vacuum: ', vacuum_count, ' auto: ', autovacuum_count, E'\\ncpt_analyze: ', analyze_count, ' auto: ', autoanalyze_count)
AS "vacuum"
FROM pg_stat_user_tables
ORDER BY pg_total_relation_size(relid) DESC
SQL
);
$table = $builder->addTable()->setHeaders([
'table', 'total_size', // ...
]);
foreach ($rows as $row) {
$table->addRow([
$row['relname'],
$row['total size'],
// ...
]);
}
}
}
declare(strict_types=1);
namespace App\EventSubscriber;
use MakinaCorpus\Monitoring\Event\ProbeResultEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @codeCoverageIgnore
*/
final class MonitoringEventSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdo}
*/
public static function getSubscribedEvents()
{
return [
ProbeResultEvent::class => 'onProbeResult',
];
}
public function onProbeResult(ProbeResultEvent $event)
{
if ($event->isCritical()) {
// Do something.
} else if ($event->isMalfunctioning()) {
// Do something else.
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.