View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Database\Connection;
use Drupal\Core\State\StateInterface;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DatabaseDiskUsagePlugin extends SensorPluginBase implements ExtendedInfoSensorPluginInterface {
protected $state;
protected $database;
public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition, StateInterface $state_system, Connection $database) {
parent::__construct($sensor_config, $plugin_id, $plugin_definition);
$this->state = $state_system;
$this->database = $database;
}
public static function create(ContainerInterface $container, SensorConfig $sensor_config, $plugin_id, $plugin_definition) {
return new static($sensor_config, $plugin_id, $plugin_definition, $container
->get('state'), $container
->get('database'));
}
public function runSensor(SensorResultInterface $sensor_result) {
if (!($disk_usage = $this
->getDiskUsage())) {
throw new \RuntimeException($this
->t('The disk space usage is not available.'));
}
$sensor_result
->setValue(number_format($disk_usage, 2));
}
public function resultVerbose(SensorResultInterface $result) {
$output = [];
$disk_usage = $this
->getDiskUsage();
$database_name = $this->database
->getConnectionOptions()['database'];
$usage_by_table = $this
->getDiskUsageByTable($database_name);
if ($this->sensorConfig
->getThresholdValue('warning') && $this->sensorConfig
->getThresholdValue('critical') && $disk_usage) {
$output['database_usage'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Database usage'),
];
$output['database_usage']['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Usage'),
$this
->t('Warning level (@amountMB)', [
'@amount' => $this->sensorConfig
->getThresholdValue('warning'),
]),
$this
->t('Critical level (@amountMB)', [
'@amount' => $this->sensorConfig
->getThresholdValue('critical'),
]),
],
];
$output['database_usage']['table'][0]['usage'] = [
'#type' => 'item',
'#plain_text' => number_format($disk_usage, 2) . 'MB',
];
$output['database_usage']['table'][0]['warning'] = [
'#type' => 'item',
'#plain_text' => number_format($disk_usage * 100 / $this->sensorConfig
->getThresholdValue('warning'), 2) . '%',
];
$output['database_usage']['table'][0]['critical'] = [
'#type' => 'item',
'#plain_text' => number_format($disk_usage * 100 / $this->sensorConfig
->getThresholdValue('critical'), 2) . '%',
];
}
if ($usage_by_table) {
$output['tables_usage'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Biggest database tables by size.'),
];
$output['tables_usage']['table'] = [
'#type' => 'table',
'#header' => [
'table_name' => [
'data' => $this
->t('Table name'),
],
'table_rows' => [
'data' => $this
->t('Table rows'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'table_length' => [
'data' => $this
->t('Table length (MB)'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'index_length' => [
'data' => $this
->t('Index length (MB)'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'size' => [
'data' => $this
->t('Size (MB)'),
],
],
];
foreach ($usage_by_table as $key => $table_info) {
$output['tables_usage']['table'][$key]['table_name'] = [
'#type' => 'item',
'#plain_text' => $table_info->table_name,
];
$output['tables_usage']['table'][$key]['table_rows'] = [
'#type' => 'item',
'#plain_text' => $table_info->table_rows,
];
$output['tables_usage']['table'][$key]['table_length'] = [
'#type' => 'item',
'#plain_text' => $table_info->table_length,
];
$output['tables_usage']['table'][$key]['index_length'] = [
'#type' => 'item',
'#plain_text' => $table_info->index_length,
];
$output['tables_usage']['table'][$key]['size'] = [
'#type' => 'item',
'#plain_text' => $table_info->size,
];
}
}
return $output;
}
protected function getDiskUsage() {
$test_usage = $this->state
->get('monitoring.test_database_disk_usage');
if (isset($test_usage)) {
return $test_usage;
}
if ($this->database
->databaseType() !== 'mysql') {
throw new \RuntimeException($this
->t('The table information is only available for mysql databases.'));
}
$result = $this->database
->query("SELECT SUM(data_length + index_length + data_free) / 1048576 AS disk_used\n FROM information_schema.tables")
->fetch();
return $result->disk_used;
}
protected function getDiskUsageByTable($database_name, $from = 0, $count = 10) {
if ($this->database
->databaseType() !== 'mysql') {
throw new \RuntimeException($this
->t('The table information is only available for mysql databases.'));
}
if (!$database_name) {
throw new \RuntimeException($this
->t('The database name needs to be set.'));
}
$query = "\n SELECT table_schema, table_name,\n SUM(table_rows) AS 'table_rows',\n SUM(round(data_length / 1048576, 2)) AS 'table_length',\n SUM(round(index_length / 1048576, 2)) AS 'index_length',\n SUM(round(((data_length + index_length) / 1024 / 1024),2)) AS 'size'\n FROM information_schema.TABLES\n WHERE TABLE_TYPE = 'BASE TABLE'\n AND table_schema = :database_name\n GROUP BY table_schema, table_name\n ORDER BY size DESC";
return $this->database
->queryRange($query, $from, $count, [
':database_name' => $database_name,
])
->fetchAll();
}
public function getDefaultConfiguration() {
$default_config = [
'value_label' => 'mb',
'caching_time' => 86400,
'value_type' => 'number',
'thresholds' => [
'type' => 'exceeds',
],
];
return $default_config;
}
}