View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DiskUsageSensorPlugin extends SensorPluginBase {
protected $fileSystem;
protected $state;
public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition, FileSystemInterface $file_system, StateInterface $state_system) {
parent::__construct($sensor_config, $plugin_id, $plugin_definition);
$this->fileSystem = $file_system;
$this->state = $state_system;
}
public static function create(ContainerInterface $container, SensorConfig $sensor_config, $plugin_id, $plugin_definition) {
return new static($sensor_config, $plugin_id, $plugin_definition, $container
->get('file_system'), $container
->get('state'));
}
public function runSensor(SensorResultInterface $sensor_result) {
$disk_data = $this
->getDiskData();
$sensor_result
->setValue($disk_data['used_space_percent']);
$sensor_result
->addStatusMessage($this
->t('@used used of @total available.', [
'@total' => $disk_data['total_space'],
'@used' => $disk_data['used_space'],
]));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Directory'),
'#description' => $this
->t('A directory of the filesystem or disk partition.'),
'#default_value' => $this->sensorConfig
->getSetting('directory'),
];
return $form;
}
public function getDefaultConfiguration() {
return [
'caching_time' => 86400,
'value_type' => 'number',
'category' => 'System',
'value_label' => '%',
'thresholds' => [
'type' => 'exceeds',
'warning' => 80,
'critical' => 95,
],
'settings' => [
'directory' => 'public://',
],
];
}
protected function getDiskData() {
if ($data = $this->state
->get('monitoring.test_disk_usage')) {
return $data;
}
$real_path = $this->fileSystem
->realpath($this->sensorConfig
->getSetting('directory'));
if (!$real_path || !is_dir($real_path) || !($total_space = disk_total_space($real_path))) {
throw new \RuntimeException($this
->t('Invalid directory.'));
}
else {
$free_space = disk_free_space($real_path);
return [
'used_space_percent' => number_format((1 - $free_space / $total_space) * 100, 2),
'used_space' => format_size($total_space - $free_space),
'total_space' => format_size($total_space),
];
}
}
}