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
- class \Drupal\monitoring\SensorPlugin\SensorPluginBase implements SensorPluginInterface uses MessengerTrait, StringTranslationTrait
- class \Drupal\monitoring\Plugin\monitoring\SensorPlugin\SystemLoadSensorPlugin
Expanded class hierarchy of SystemLoadSensorPlugin
File
- src/
Plugin/ monitoring/ SensorPlugin/ SystemLoadSensorPlugin.php, line 22
Namespace
Drupal\monitoring\Plugin\monitoring\SensorPluginView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
SensorPluginBase:: |
protected | property | Allows plugins to control if the value type can be configured. | 6 |
SensorPluginBase:: |
protected | property | The plugin implementation definition. | |
SensorPluginBase:: |
protected | property | The plugin_id. | |
SensorPluginBase:: |
protected | property | Current sensor config object. | |
SensorPluginBase:: |
protected | property | ||
SensorPluginBase:: |
public | function |
Service setter. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides SensorPluginInterface:: |
4 |
SensorPluginBase:: |
public | function |
Configurable value type. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets sensor name (not the label). Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
@todo: Replace with injection Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Determines if sensor is enabled. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
3 |
SensorPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SystemLoadSensorPlugin:: |
protected | property | Holds the state system instance. | |
SystemLoadSensorPlugin:: |
protected | property | The load average of the system provided by sys_getloadavg(). | |
SystemLoadSensorPlugin:: |
public | function |
Form constructor. Overrides SensorPluginBase:: |
|
SystemLoadSensorPlugin:: |
public static | function |
Creates an instance of the sensor with config. Overrides SensorPluginBase:: |
|
SystemLoadSensorPlugin:: |
public | function |
Default configuration for a sensor. Overrides SensorPluginBase:: |
|
SystemLoadSensorPlugin:: |
protected | function | Gets the load average for the selected setting. | |
SystemLoadSensorPlugin:: |
public | function |
Runs the sensor, updating $sensor_result. Overrides SensorPluginInterface:: |
|
SystemLoadSensorPlugin:: |
public | function |
Instantiates a sensor object. Overrides SensorPluginBase:: |