You are here

monitoring.install in Monitoring 8

Same filename and directory in other branches
  1. 7 monitoring.install

Monitoring base install file.

File

monitoring.install
View source
<?php

/**
 * @file
 * Monitoring base install file.
 */
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Serialization\Yaml;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\node\Entity\NodeType;
use Drupal\search_api\Entity\Index;

/**
 * Implements hook_install().
 *
 * Declares initial set of sensors.
 */
function monitoring_install() {
  if (\Drupal::service('config.installer')
    ->isSyncing()) {

    // Don't create any config when syncing.
    // @todo change to $is_syncing param after 8.9 is minimum per
    // https://www.drupal.org/node/3098920
    return;
  }

  // Declares initial set of search api sensors (if module exists).
  if (\Drupal::moduleHandler()
    ->moduleExists('search_api')) {
    foreach (Index::loadMultiple() as $index) {
      $sensor = SensorConfig::create(array(
        'id' => 'search_api_' . $index
          ->id(),
        'label' => new FormattableMarkup('Search index queue size of @index', array(
          '@index' => $index
            ->label(),
        )),
        'plugin_id' => 'search_api_unindexed',
        'value_type' => 'number',
        'value_label' => 'Unindexed items',
        'category' => 'Search API',
        // Cache for 15 minutes.
        'caching_time' => 900,
        'status' => TRUE,
        'settings' => array(
          'index_id' => $index
            ->id(),
        ),
        'dependencies' => array(
          'module' => 'search_api',
        ),
      ));
      $sensor
        ->save();
    }
  }

  // Declares core requirements sensors.
  // Load .install files
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
  drupal_load_updates();
  foreach (\Drupal::moduleHandler()
    ->getImplementations('requirements') as $module) {
    initialize_requirements_sensors($module);
  }
}

/**
 * Maps severities to their machine names.
 *
 * @return array
 *   Severity names array keyed by RfcLogLevel constants.
 */
function monitoring_event_severities() {
  return array(
    RfcLogLevel::DEBUG => 'debug',
    RfcLogLevel::INFO => 'info',
    RfcLogLevel::NOTICE => 'notice',
    RfcLogLevel::WARNING => 'warning',
    RfcLogLevel::ERROR => 'error',
    RfcLogLevel::EMERGENCY => 'emergency',
    RfcLogLevel::ALERT => 'alert',
    RfcLogLevel::CRITICAL => 'critical',
  );
}

/**
 * Creates the used temporary files sensor config.
 */
function monitoring_update_8101() {

  // Only create if the sensor config does not exist yet.
  if (!SensorConfig::load('temporary_files_usages')) {
    $config_path = drupal_get_path('module', 'monitoring') . '/config/install/monitoring.sensor_config.temporary_files_usages.yml';
    $data = Yaml::decode(file_get_contents($config_path));
    \Drupal::service('monitoring.sensor_manager')
      ->clearCachedDefinitions();
    SensorConfig::create($data)
      ->trustData()
      ->save();
    return 'Created new sensor to monitor used temporary files';
  }
}

/**
 * Disable user sensors, if dblog is not installed.
 */
function monitoring_update_8102() {
  if (!\Drupal::moduleHandler()
    ->moduleExists('dblog')) {
    $sensor_configs = SensorConfig::loadMultiple([
      'user_failed_logins',
      'user_session_logouts',
      'user_successful_logins',
      'user_void_failed_logins',
    ]);
    foreach ($sensor_configs as $sensor) {
      $sensor
        ->delete();
    }
  }
}

/**
 * Change count field to daily_count in redirect 404 sensor.
 */
function monitoring_update_8103() {
  $redirect_404_sensor = SensorConfig::load('redirect_404');
  if ($redirect_404_sensor) {
    $verbose_fields = $redirect_404_sensor
      ->getSetting('verbose_fields', []);
    if (($key = array_search('count', $verbose_fields)) !== FALSE) {
      array_splice($verbose_fields, $key + 1, 0, 'daily_count');
      $settings = $redirect_404_sensor
        ->getSettings();
      $settings['verbose_fields'] = $verbose_fields;
      $redirect_404_sensor
        ->set('settings', $settings)
        ->save();
    }
  }
}

Functions

Namesort descending Description
monitoring_event_severities Maps severities to their machine names.
monitoring_install Implements hook_install().
monitoring_update_8101 Creates the used temporary files sensor config.
monitoring_update_8102 Disable user sensors, if dblog is not installed.
monitoring_update_8103 Change count field to daily_count in redirect 404 sensor.