You are here

class SystemLoadSensorPlugin in Monitoring 8

Monitors system load time average.

Plugin annotation


@SensorPlugin(
  id = "system_load",
  label = @Translation("System load"),
  description = @Translation("Monitors system load average."),
  addable = TRUE
)

Hierarchy

Expanded class hierarchy of SystemLoadSensorPlugin

File

src/Plugin/monitoring/SensorPlugin/SystemLoadSensorPlugin.php, line 22

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin
View source
class SystemLoadSensorPlugin extends SensorPluginBase {

  /**
   * Holds the state system instance.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The load average of the system provided by sys_getloadavg().
   *
   * @var array
   */
  protected $systemLoadAverage;

  /**
   * {@inheritdoc}
   */
  public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition, StateInterface $state_system) {
    parent::__construct($sensor_config, $plugin_id, $plugin_definition);
    $this->state = $state_system;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['average_monitored'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Average monitored'),
      '#options' => [
        '0' => $this
          ->t('1 minute'),
        '1' => $this
          ->t('5 minutes'),
        '2' => $this
          ->t('15 minutes'),
      ],
      '#default_value' => $this->sensorConfig
        ->getSetting('average_monitored'),
      '#description' => $this
        ->t('You can select which average will be monitored. Its value will be multiplied by 100 where 100% means that a single CPU is used. For more information check <a href="https://en.wikipedia.org/wiki/Load_(computing)">this</a>.'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function runSensor(SensorResultInterface $result) {
    $load_average = $this
      ->getLoadAverage();
    if (!$load_average) {
      $result
        ->setStatus(SensorResultInterface::STATUS_CRITICAL);
      $result
        ->setMessage('Could not get the required information, sys_getloadavg() is not available.');
      return;
    }
    else {

      // If setting is set use it for selecting the average.
      $setting = $this->sensorConfig
        ->getSetting('average_monitored');
      $average = $load_average[$setting];

      // Convert average to int.
      $average = (int) ($average * 100);

      // Set values and status based on the system thresholds.
      $result
        ->setValue($average);
      $result
        ->addStatusMessage(implode(', ', $load_average));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultConfiguration() {
    return [
      'value_label' => '% Average',
      'caching_time' => 0,
      'value_type' => 'number',
      'thresholds' => [
        'type' => 'exceeds',
        'warning' => 80,
        'critical' => 100,
      ],
      'settings' => [
        'average_monitored' => '1',
      ],
    ];
  }

  /**
   * Gets the load average for the selected setting.
   *
   * @return array|NULL
   *   The load averages or NULL if the method does not exist.
   */
  protected function getLoadAverage() {
    if ($data = $this->state
      ->get('monitoring.test_load_average')) {
      return $data;
    }
    if (!function_exists('sys_getloadavg')) {
      return NULL;
    }
    else {
      return sys_getloadavg();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
SensorPluginBase::$configurableValueType protected property Allows plugins to control if the value type can be configured. 6
SensorPluginBase::$pluginDefinition protected property The plugin implementation definition.
SensorPluginBase::$pluginId protected property The plugin_id.
SensorPluginBase::$sensorConfig protected property Current sensor config object.
SensorPluginBase::$services protected property
SensorPluginBase::addService public function Service setter. Overrides SensorPluginInterface::addService
SensorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides SensorPluginInterface::calculateDependencies 4
SensorPluginBase::getConfigurableValueType public function Configurable value type. Overrides SensorPluginInterface::getConfigurableValueType
SensorPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
SensorPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
SensorPluginBase::getSensorId public function Gets sensor name (not the label). Overrides SensorPluginInterface::getSensorId
SensorPluginBase::getService public function @todo: Replace with injection Overrides SensorPluginInterface::getService
SensorPluginBase::isEnabled public function Determines if sensor is enabled. Overrides SensorPluginInterface::isEnabled
SensorPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 3
SensorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SystemLoadSensorPlugin::$state protected property Holds the state system instance.
SystemLoadSensorPlugin::$systemLoadAverage protected property The load average of the system provided by sys_getloadavg().
SystemLoadSensorPlugin::buildConfigurationForm public function Form constructor. Overrides SensorPluginBase::buildConfigurationForm
SystemLoadSensorPlugin::create public static function Creates an instance of the sensor with config. Overrides SensorPluginBase::create
SystemLoadSensorPlugin::getDefaultConfiguration public function Default configuration for a sensor. Overrides SensorPluginBase::getDefaultConfiguration
SystemLoadSensorPlugin::getLoadAverage protected function Gets the load average for the selected setting.
SystemLoadSensorPlugin::runSensor public function Runs the sensor, updating $sensor_result. Overrides SensorPluginInterface::runSensor
SystemLoadSensorPlugin::__construct public function Instantiates a sensor object. Overrides SensorPluginBase::__construct