Skip to content

Commit

Permalink
Merge pull request #5 from w-vision/feat/database-table-size-check
Browse files Browse the repository at this point in the history
Feat/database table size check
  • Loading branch information
alexloetscher95 committed Apr 12, 2023
2 parents 3d03c55 + 0be929b commit ad9864e
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ requirements, like availability of PHP extensions.
- **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.
- **DatabaseTableSize:** Checks how much space each Database Table 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 All @@ -27,6 +28,7 @@ requirements, like availability of PHP extensions.
* [Installation & Bundle Configuration](docs/00-installation-configuration.md)
* [Adding and Running Checks](docs/01-adding-custom-checks.md)
* [Commands](docs/02-commands.md)
* [Defaults](docs/03-defaults.md)

## License
**w-vision AG**, Sandgruebestrasse 4, 6210 Sursee, Switzerland
Expand Down
68 changes: 68 additions & 0 deletions docs/03-defaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Defaults

```yaml
pimcore_monitor:
checks:
app_environment:
enabled: true
skip: false
environment: '%kernel.environment%'
disk_usage:
enabled: true
skip: false
warning_threshold: 90 # percentage
critical_threshold: 95 # percentage
path: '%kernel.project_dir%'
doctrine_migrations:
enabled: true
skip: false
hosting_size:
enabled: true
skip: false
warning_threshold: 48318382080 # 45 GB
critical_threshold: 53687091200 # 50 GB
path: '%kernel.project_dir%'
database_size:
enabled: true
skip: false
warning_threshold: 964689920 # 920 MB
critical_threshold: 1073741824 # 1 GB
database_table_size:
enabled: true
skip: false
warning_threshold: 94371840 # 90 MB
critical_threshold: 104857600 # 100 MB
https_connection:
enabled: true
skip: false
mysql_version:
enabled: true
skip: false
version: '10.5'
operator: '>='
php_version:
enabled: true
skip: false
version: '8.0'
operator: '>='
pimcore_areabricks:
enabled: true
skip: false
pimcore_bundles:
enabled: true
skip: false
pimcore_element_count:
enabled: true
skip: false
warning_threshold: 100000
critical_threshold: 150000
pimcore_maintenance:
enabled: true
skip: false
pimcore_users:
enabled: true
skip: false
pimcore_version:
enabled: true
skip: false
```
2 changes: 1 addition & 1 deletion src/PimcoreMonitorBundle/Check/DatabaseSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 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)
* @copyright Copyright (c) 2023 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)
*/

Expand Down
115 changes: 115 additions & 0 deletions src/PimcoreMonitorBundle/Check/DatabaseTableSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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) 2023 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 DatabaseTableSize extends AbstractCheck
{
protected const IDENTIFIER = 'device:database_table_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');
}

$sizes = $this->getDatabaseTableSizes();

if (!\is_array($sizes)) {
return new Failure('Database table sizes could not be retrieved');
}

$data = [
'ok' => 0,
'warning' => [],
'critical' => [],
];

foreach ($sizes as $size) {
if ($size['size'] >= $this->criticalThreshold) {
$data['critical'][$size['table']] = \formatBytes($size['size']);
continue;
}

if ($size['size'] >= $this->warningThreshold) {
$data['warning'][$size['table']] = \formatBytes($size['size']);
continue;
}

++$data['ok'];
}

if (\count($data['critical']) > 0) {
return new Failure(
\sprintf(
'Following database table sizes are too high: %s',
\implode(',', \array_keys($data['critical']))),
$data
);
}

if (\count($data['warning']) > 0) {
return new Warning(
\sprintf(
'Following database table sizes are high: %s',
\implode(',', \array_keys($data['warning']))),
$data
);
}

return new Success('All database table sizes are ok.', $data);
}

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

/**
* Returns the sizes of the connected database tables
*/
private function getDatabaseTableSizes(): ?array
{
$query = "SELECT TABLE_NAME AS `table`,
(DATA_LENGTH + INDEX_LENGTH) AS `size`
FROM information_schema.TABLES
ORDER BY
(DATA_LENGTH + INDEX_LENGTH)
DESC;";
return $this->connection->fetchAll($query);
}
}
25 changes: 25 additions & 0 deletions src/PimcoreMonitorBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ private function buildChecksNode(): NodeDefinition
->end()
->end()
->end()
->arrayNode('database_table_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 all database tables size in bytes.')
->defaultValue(94371840)
->isRequired()
->min(0)
->end()
->integerNode('critical_threshold')
->info('The critical threshold for all database tables size in bytes.')
->defaultValue(104857600)
->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 @@ -46,6 +46,16 @@ services:
tags:
- { name: pimcore_monitor.check, alias: database_size }

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

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

0 comments on commit ad9864e

Please sign in to comment.