View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
class DisappearedSensorsSensorPlugin extends SensorPluginBase {
public function runSensor(SensorResultInterface $result) {
$available_sensors = \Drupal::state()
->get('monitoring.available_sensors', array());
$sensor_config = monitoring_sensor_manager()
->getAllSensorConfig();
$available_sensors = $this
->updateAvailableSensorsList($available_sensors, $sensor_config);
$this
->checkForMissingSensors($result, $available_sensors, $sensor_config);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
try {
$result = monitoring_sensor_run($this->sensorConfig
->id());
} catch (\Exception $e) {
$this
->messenger()
->addError($e
->getMessage());
return array();
}
$form = parent::buildConfigurationForm($form, $form_state);
if ($result
->isCritical()) {
$form['clear_missing_sensors_wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Missing sensors'),
'#description' => t('This action will clear the missing sensors and the critical sensor status will go away.'),
'#weight' => -10,
);
$form['clear_missing_sensors_wrapper']['info'] = array(
'#type' => 'item',
'#title' => t('Sensor message'),
'#markup' => $result
->getMessage(),
);
$form['clear_missing_sensors_wrapper']['clear_missing_sensor'] = array(
'#type' => 'submit',
'#submit' => array(
'monitoring_clear_missing_sensor_submit',
),
'#value' => t('Clear missing sensors'),
);
}
return $form;
}
protected function updateAvailableSensorsList($available_sensors, $sensor_config) {
$new_sensors = array();
foreach ($sensor_config as $key => $config) {
if (!isset($available_sensors[$key])) {
$new_sensors[$key] = array(
'name' => $key,
'enabled' => $config
->isEnabled(),
);
}
elseif ($available_sensors[$key]['enabled'] != $config
->isEnabled()) {
$available_sensors[$key]['enabled'] = $config
->isEnabled();
}
}
if (!empty($new_sensors)) {
\Drupal::state()
->set('monitoring.available_sensors', $available_sensors + $new_sensors);
\Drupal::logger('monitoring')
->notice('@count new sensor/s added: @names', array(
'@count' => count($new_sensors),
'@names' => implode(', ', array_keys($new_sensors)),
));
}
return $available_sensors;
}
protected function checkForMissingSensors(SensorResultInterface $result, $available_sensors, $sensor_config) {
$result
->setStatus(SensorResultInterface::STATUS_OK);
$sensors_to_remove = array();
foreach ($available_sensors as $available_sensor) {
if (!in_array($available_sensor['name'], array_keys($sensor_config))) {
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 (!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),
));
}
}
}