From 9e0b21fb2f55a63e805a1ac9e04a87012a98558b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Lo=CC=88tscher?= Date: Wed, 12 Apr 2023 16:20:16 +0200 Subject: [PATCH 1/7] feat: add new check: database table size --- .../Check/DatabaseSize.php | 2 +- .../Check/DatabaseTableSize.php | 115 ++++++++++++++++++ .../DependencyInjection/Configuration.php | 27 ++++ .../Resources/config/services/checks.yaml | 10 ++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/PimcoreMonitorBundle/Check/DatabaseTableSize.php diff --git a/src/PimcoreMonitorBundle/Check/DatabaseSize.php b/src/PimcoreMonitorBundle/Check/DatabaseSize.php index 7767d56..3126f57 100644 --- a/src/PimcoreMonitorBundle/Check/DatabaseSize.php +++ b/src/PimcoreMonitorBundle/Check/DatabaseSize.php @@ -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) */ diff --git a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php new file mode 100644 index 0000000..9ad8815 --- /dev/null +++ b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php @@ -0,0 +1,115 @@ +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); + } +} diff --git a/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php b/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php index a7e96a1..264db2d 100644 --- a/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php +++ b/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php @@ -181,6 +181,33 @@ 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) + ->defaultValue(20971520) + ->isRequired() + ->min(0) + ->end() + ->integerNode('critical_threshold') + ->info('The critical threshold for all database tables size in bytes.') +// ->defaultValue(104857600) + ->defaultValue(52428800) + ->isRequired() + ->min(0) + ->end() + ->end() + ->end() ->arrayNode('https_connection') ->addDefaultsIfNotSet() ->children() diff --git a/src/PimcoreMonitorBundle/Resources/config/services/checks.yaml b/src/PimcoreMonitorBundle/Resources/config/services/checks.yaml index f7a8e30..908ee17 100644 --- a/src/PimcoreMonitorBundle/Resources/config/services/checks.yaml +++ b/src/PimcoreMonitorBundle/Resources/config/services/checks.yaml @@ -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: From 907514f691c5c5229d760677e41f006628ffbfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Lo=CC=88tscher?= Date: Wed, 12 Apr 2023 16:35:06 +0200 Subject: [PATCH 2/7] docs: add DatabaseTableSize to README and add documentation for default values --- README.md | 2 + docs/03-defaults.md | 68 +++++++++++++++++++ .../DependencyInjection/Configuration.php | 6 +- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 docs/03-defaults.md diff --git a/README.md b/README.md index f62ab4c..5312ec0 100644 --- a/README.md +++ b/README.md @@ -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 the Database Tables 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. @@ -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 diff --git a/docs/03-defaults.md b/docs/03-defaults.md new file mode 100644 index 0000000..5d678f2 --- /dev/null +++ b/docs/03-defaults.md @@ -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 +``` diff --git a/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php b/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php index 264db2d..5cc3a08 100644 --- a/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php +++ b/src/PimcoreMonitorBundle/DependencyInjection/Configuration.php @@ -194,15 +194,13 @@ private function buildChecksNode(): NodeDefinition ->end() ->integerNode('warning_threshold') ->info('The warning threshold for all database tables size in bytes.') -// ->defaultValue(94371840) - ->defaultValue(20971520) + ->defaultValue(94371840) ->isRequired() ->min(0) ->end() ->integerNode('critical_threshold') ->info('The critical threshold for all database tables size in bytes.') -// ->defaultValue(104857600) - ->defaultValue(52428800) + ->defaultValue(104857600) ->isRequired() ->min(0) ->end() From d00990b39d7e4a51080ca4874c2658638d3ec6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20L=C3=B6tscher?= <60733818+alexloetscher95@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:30:31 +0200 Subject: [PATCH 3/7] Update README.md Co-authored-by: Aaron --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5312ec0..efd47f4 100644 --- a/README.md +++ b/README.md @@ -13,7 +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 the Database Tables 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. From 611d541c416f55e16f23de128e4aabce0cc15490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20L=C3=B6tscher?= <60733818+alexloetscher95@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:30:42 +0200 Subject: [PATCH 4/7] Update src/PimcoreMonitorBundle/Check/DatabaseTableSize.php Co-authored-by: Aaron --- src/PimcoreMonitorBundle/Check/DatabaseTableSize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php index 9ad8815..7fcb70d 100644 --- a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php +++ b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php @@ -46,7 +46,7 @@ public function check(): ResultInterface $sizes = $this->getDatabaseTableSizes(); - if (!is_array($sizes)) { + if (!\is_array($sizes)) { return new Failure('Database table sizes could not be retrieved'); } From 5c975ba6353b616e9cdcbc96c7ad3c1a2a80bfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20L=C3=B6tscher?= <60733818+alexloetscher95@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:30:53 +0200 Subject: [PATCH 5/7] Update src/PimcoreMonitorBundle/Check/DatabaseTableSize.php Co-authored-by: Aaron --- src/PimcoreMonitorBundle/Check/DatabaseTableSize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php index 7fcb70d..ae99cab 100644 --- a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php +++ b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php @@ -56,7 +56,7 @@ public function check(): ResultInterface 'critical' => [], ]; - foreach($sizes as $size) { + foreach ($sizes as $size) { if ($size['size'] >= $this->criticalThreshold) { $data['critical'][$size['table']] = \formatBytes($size['size']); continue; From 834effc1321f2cf82e8827aa3685ece758d50a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20L=C3=B6tscher?= <60733818+alexloetscher95@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:31:03 +0200 Subject: [PATCH 6/7] Update src/PimcoreMonitorBundle/Check/DatabaseTableSize.php Co-authored-by: Aaron --- src/PimcoreMonitorBundle/Check/DatabaseTableSize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php index ae99cab..c68dc92 100644 --- a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php +++ b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php @@ -74,7 +74,7 @@ public function check(): ResultInterface return new Failure( \sprintf( 'Following database table sizes are too high: %s', - \implode(',', array_keys($data['critical']))), + \implode(',', \array_keys($data['critical']))), $data ); } From 0be929b0472bc2783949d6655276ca2c92e2b0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20L=C3=B6tscher?= <60733818+alexloetscher95@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:31:12 +0200 Subject: [PATCH 7/7] Update src/PimcoreMonitorBundle/Check/DatabaseTableSize.php Co-authored-by: Aaron --- src/PimcoreMonitorBundle/Check/DatabaseTableSize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php index c68dc92..8b4c8a9 100644 --- a/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php +++ b/src/PimcoreMonitorBundle/Check/DatabaseTableSize.php @@ -83,7 +83,7 @@ public function check(): ResultInterface return new Warning( \sprintf( 'Following database table sizes are high: %s', - \implode(',', array_keys($data['warning']))), + \implode(',', \array_keys($data['warning']))), $data ); }