You are here

protected function DiskUsageSensorPlugin::getDiskData in Monitoring 8

Get disk info and calculate percent of used space.

Return value

array Result info array with 3 items: used_space_percent, used_space, total_space. They represent used disk space in percents (float), used disk space (string) and total disk space (string). Used and total disk space are formatted in human readable size ('KB', 'MB', 'GB', 'TB', 'PB', 'EB','ZB', 'YB').

Throws

\RuntimeException Thrown when the directory is not valid.

1 call to DiskUsageSensorPlugin::getDiskData()
DiskUsageSensorPlugin::runSensor in src/Plugin/monitoring/SensorPlugin/DiskUsageSensorPlugin.php
Runs the sensor, updating $sensor_result.

File

src/Plugin/monitoring/SensorPlugin/DiskUsageSensorPlugin.php, line 122

Class

DiskUsageSensorPlugin
Monitors disk space usage.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

protected function getDiskData() {

  // Condition is used to simulate data for purpose of testing.
  if ($data = $this->state
    ->get('monitoring.test_disk_usage')) {
    return $data;
  }

  // Get partition info and calculate used space percent.
  $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),
    ];
  }
}