Skip to content

Commit

Permalink
Merge pull request #4 from w-vision/feat/db-check
Browse files Browse the repository at this point in the history
feat: add DatabaseSize check
  • Loading branch information
alexloetscher95 authored Apr 11, 2023
2 parents c036949 + 5c05cf5 commit 3d03c55
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ requirements, like availability of PHP extensions.
- **DiskUsage:** Checks how much space is being allocated on the disk.
- **DoctrineMigrations:** Checks whether all Doctrine Migrations have been migrated.
- **HostingSize:** Checks how much Disk space is used by this hosting.
- **DatabaseSize:** Checks how much space the Database uses on this hosting.
- **HttpsConnection:** Checks whether the HTTPS encryption is enabled.
- **MySqlVersion:** Checks what MySQL version is configured.
- **PhpVersion:** Checks what PHP version is configured.
Expand Down
92 changes: 92 additions & 0 deletions src/PimcoreMonitorBundle/Check/DatabaseSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

/**
* Pimcore Monitor
*
* LICENSE
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2022 w-vision AG (https://www.w-vision.ch)
* @license https://github.com/w-vision/PimcoreMonitorBundle/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
*/

namespace Wvision\Bundle\PimcoreMonitorBundle\Check;

use Doctrine\DBAL\Connection;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\ResultInterface;
use Laminas\Diagnostics\Result\Skip;
use Laminas\Diagnostics\Result\Success;
use Laminas\Diagnostics\Result\Warning;

class DatabaseSize extends AbstractCheck
{
protected const IDENTIFIER = 'device:database_size';

public function __construct(
protected bool $skip,
protected int $warningThreshold,
protected int $criticalThreshold,
protected Connection $connection
) {}

/**
* {@inheritDoc}
*/
public function check(): ResultInterface
{
if ($this->skip) {
return new Skip('Check was skipped');
}

$size = $this->getDatabaseSize();

if ($size === 0) {
return new Failure('Database size could not be retrieved');
}

$data = [
'size' => \formatBytes($size),
];

if ($size >= $this->criticalThreshold) {
return new Failure(\sprintf('Database size is too high: %s', \formatBytes($size)), $data);
}

if ($size >= $this->warningThreshold) {
return new Warning(\sprintf('Database size is high: %s', \formatBytes($size)), $data);
}

return new Success(\sprintf('Database size is %s', \formatBytes($size)), $data);
}

/**
* {@inheritDoc}
*/
public function getLabel(): string
{
return 'Database Size';
}

/**
* Returns the size of the connected database
*/
private function getDatabaseSize(): int
{
$query = "SELECT SUM(data_length + index_length) AS size
FROM information_schema.TABLES
GROUP BY table_schema";
$size = $this->connection->fetchAll($query);

if (\is_array($size) && isset($size[0]['size'])) {
return (int) $size[0]['size'];
}

return 0;
}
}
25 changes: 25 additions & 0 deletions src/PimcoreMonitorBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ private function buildChecksNode(): NodeDefinition
->end()
->end()
->end()
->arrayNode('database_size')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->info('Enables this check globally.')
->defaultValue(true)
->end()
->booleanNode('skip')
->info('Skips this check globally.')
->defaultValue(false)
->end()
->integerNode('warning_threshold')
->info('The warning threshold for the database size in bytes.')
->defaultValue(964689920)
->isRequired()
->min(0)
->end()
->integerNode('critical_threshold')
->info('The critical threshold for the database size in bytes.')
->defaultValue(1073741824)
->isRequired()
->min(0)
->end()
->end()
->end()
->arrayNode('https_connection')
->addDefaultsIfNotSet()
->children()
Expand Down
10 changes: 10 additions & 0 deletions src/PimcoreMonitorBundle/Resources/config/services/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ services:
tags:
- { name: pimcore_monitor.check, alias: hosting_size }

# Database Size Check
Wvision\Bundle\PimcoreMonitorBundle\Check\DatabaseSize:
arguments:
- '%pimcore_monitor.checks.database_size.skip%'
- '%pimcore_monitor.checks.database_size.warning_threshold%'
- '%pimcore_monitor.checks.database_size.critical_threshold%'
- '@doctrine.dbal.default_connection'
tags:
- { name: pimcore_monitor.check, alias: database_size }

# HTTPS Connection Check
Wvision\Bundle\PimcoreMonitorBundle\Check\HttpsConnection:
arguments:
Expand Down

0 comments on commit 3d03c55

Please sign in to comment.