You are here

protected function DisappearedSensorsSensorPlugin::checkForMissingSensors in Monitoring 8

Checks for missing sensors.

Parameters

\Drupal\monitoring\Result\SensorResultInterface $result: The current sensor result object.

array $available_sensors: The available sensors list.

\Drupal\monitoring\Entity\SensorConfig[] $sensor_config: The current sensor config.

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

File

src/Plugin/monitoring/SensorPlugin/DisappearedSensorsSensorPlugin.php, line 126
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\DisappearedSensorsSensorPlugin.

Class

DisappearedSensorsSensorPlugin
Monitors if sensors disappeared without prior being disabled.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

protected function checkForMissingSensors(SensorResultInterface $result, $available_sensors, $sensor_config) {
  $result
    ->setStatus(SensorResultInterface::STATUS_OK);
  $sensors_to_remove = array();

  // Check for missing sensors.
  foreach ($available_sensors as $available_sensor) {
    if (!in_array($available_sensor['name'], array_keys($sensor_config))) {

      // If sensor is missing and was not disabled prior to go missing do
      // escalate to critical status.
      if (!empty($available_sensor['enabled'])) {
        $result
          ->setStatus(SensorResultInterface::STATUS_CRITICAL);
        $result
          ->addStatusMessage('Missing sensor @name', array(
          '@name' => $available_sensor['name'],
        ));
      }
      else {
        $sensors_to_remove[] = $available_sensor['name'];
      }
    }
  }

  // If having sensor to remove, reset the available sensors variable.
  if (!empty($sensors_to_remove)) {
    foreach ($sensors_to_remove as $sensor_to_remove) {
      unset($available_sensors[$sensor_to_remove]);
    }
    \Drupal::state()
      ->set('monitoring.available_sensors', $available_sensors);
    \Drupal::logger('monitoring')
      ->notice('@count new sensor/s removed: @names', array(
      '@count' => count($sensors_to_remove),
      '@names' => implode(', ', $sensors_to_remove),
    ));
  }
}