You are here

monitoring_munin.module in Monitoring 7

Monitoring Munin module bootstrap file.

File

modules/monitoring_munin/monitoring_munin.module
View source
<?php

/**
 * @file
 * Monitoring Munin module bootstrap file.
 */
use Drupal\monitoring\SensorRunner;

/**
 * Implements hook_menu().
 */
function monitoring_munin_menu() {
  $items['admin/config/system/monitoring/munin'] = array(
    'title' => 'Munin',
    'description' => 'Configure Munin connector',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'monitoring_munin_graph_settings',
    ),
    'access arguments' => array(
      'administer monitoring',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'monitoring_munin.admin.inc',
  );
  $items['admin/config/system/monitoring/munin/graph-settings'] = array(
    'title' => 'Graph settings',
    'description' => 'Configure Munin graph output',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/system/monitoring/munin/config'] = array(
    'title' => 'Config script',
    'description' => 'Munin setup configuration script.',
    'page callback' => 'monitoring_munin_config_page',
    'access arguments' => array(
      'administer monitoring',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'monitoring_munin.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_munin_default_multigraphs()
 */
function monitoring_munin_munin_default_multigraphs() {
  return array(
    'Watchdog' => array(
      'vlabel' => 'Watchdog items',
      'categories' => array(
        'Watchdog',
      ),
    ),
    'Past' => array(
      'vlabel' => 'Past items',
      'categories' => array(
        'Past',
      ),
    ),
    'User activity' => array(
      'vlabel' => 'Users',
      'categories' => array(
        'User activity',
      ),
    ),
    'Elysia cron' => array(
      'vlabel' => 'Seconds',
      'categories' => array(
        'Elysia cron',
      ),
    ),
    'Content' => array(
      'vlabel' => 'Items',
      'categories' => array(
        'Content',
      ),
    ),
    'Drupal queue' => array(
      'vlabel' => 'Items',
      'categories' => array(
        'Drupal queue',
      ),
    ),
    'Search API' => array(
      'vlabel' => 'Items',
      'categories' => array(
        'Search API',
      ),
    ),
  );
}

/**
 * Gets munin multigraphs definitions.
 *
 * @return array
 *   Multigraphs definitions.
 */
function monitoring_munin_multigraphs() {
  return variable_get('monitoring_munin_multigraphs', array());
}

/**
 * Deletes multigraph and remove from sensor settings.
 *
 * @param string $title
 *   Multigraph title to remove.
 */
function monitoring_munin_multigraph_delete($title) {

  // Remove multigraph.
  $multigraphs = monitoring_munin_multigraphs();
  if (isset($multigraphs[$title])) {
    unset($multigraphs[$title]);
  }
  variable_set('monitoring_munin_multigraphs', $multigraphs);

  // Remove from settings.
  foreach (monitoring_sensor_info() as $info) {
    $munin_settings = $info
      ->getSetting('munin');

    // If the sensor has given multigraph remove it from the settings.
    if (isset($munin_settings['multigraphs']) && in_array($title, $munin_settings['multigraphs'])) {
      for ($i = 0; $i < count($munin_settings['multigraphs']); $i++) {
        if ($munin_settings['multigraphs'][$i] == $title) {
          unset($munin_settings['multigraphs'][$i]);
        }
      }
      $settings = monitoring_sensor_settings_get($info
        ->getName());
      $settings['munin'] = $munin_settings;
      monitoring_sensor_settings_save($info
        ->getName(), $settings);
    }
  }
}

/**
 * Saves new multigraph.
 *
 * @param string $title
 *   Multigraph title.
 * @param string $vlabel
 *   Multigraph vertical label.
 */
function monitoring_munin_multigraph_save($title, $vlabel) {
  $multigraphs = monitoring_munin_multigraphs();
  $multigraphs[$title] = array(
    'title' => $title,
    'vlabel' => $vlabel,
  );
  variable_set('monitoring_munin_multigraphs', $multigraphs);
}

/**
 * Returns the monitoring result.
 *
 * @return string
 *   Monitoring result string.
 */
function _monitoring_munin_result() {
  $sensor_info = monitoring_sensor_manager()
    ->getEnabledSensorInfo();
  $munin_enabled = array();

  // Select only those sensors that are enabled for munin.
  foreach (monitoring_munin_config() as $graph) {
    foreach ($graph as $key => $value) {

      // If the key does not represent label, continue.
      if (strpos($key, '.label') === FALSE) {
        continue;
      }

      // Retrieve sensor name from the label.
      $label = str_replace('.label', '', $value);
      $parts = explode('__', $label);

      // Check if such sensor info exists.
      if (!isset($sensor_info[$parts[1]])) {
        continue;
      }
      $munin_enabled[$value] = $sensor_info[$parts[1]];
    }
  }

  // Run enabled sensors.
  $runner = new SensorRunner($munin_enabled);
  $config = array();

  /** @var \Drupal\monitoring\Result\SensorResultInterface $result */
  foreach ($runner as $sensor_name => $result) {
    $munin_settings = $result
      ->getSensorInfo()
      ->getSetting('munin');
    $sensor_name = monitoring_munin_sensor_identifier($sensor_name);

    // Compose multigraph results.
    if (!empty($munin_settings['multigraphs'])) {
      foreach ($munin_settings['multigraphs'] as $multigraph) {
        $config[monitoring_munin_multigraph_identifier($multigraph)][$sensor_name] = $result
          ->toNumber();
      }
    }
    else {
      $config[$sensor_name][$sensor_name] = $result
        ->toNumber();
    }
  }

  // Build the munin result output.
  $output = '';
  foreach ($config as $key => $multigraph) {
    $output .= "multigraph {$key}\n";
    foreach ($multigraph as $sensor_name => $value) {
      $output .= $sensor_name . '.value ' . $value . "\n";
    }
    $output .= "\n";
  }
  return $output;
}

/**
 * Returns the munin graphs definitions.
 *
 * @return array
 *   Munin graphs definitions.
 */
function monitoring_munin_config() {
  $host = monitoring_host();
  $config = array();

  // Collect all multigraphs and create their definitions.
  foreach (monitoring_munin_multigraphs() as $multigraph) {
    $config[monitoring_munin_multigraph_identifier($multigraph['title'])] = array(
      'graph_title' => $multigraph['title'],
      'graph_vlabel' => $multigraph['vlabel'],
      'graph_category' => $host,
    );
  }
  foreach (monitoring_sensor_manager()
    ->getEnabledSensorInfo() as $sensor_name => $info) {
    $munin_settings = $info
      ->getSetting('munin');

    // Skip in case sensor is disabled by munin.
    if (empty($munin_settings['munin_enabled'])) {
      continue;
    }
    $graph = array();
    $munin_sensor_name = monitoring_munin_sensor_identifier($sensor_name);

    // If a sensor belongs to one or more multigraphs add its definition to all
    // of the multigraphs.
    if (!empty($munin_settings['multigraphs'])) {
      foreach ($munin_settings['multigraphs'] as $multigraph) {
        $multigraph = monitoring_munin_multigraph_identifier($multigraph);
        $config[$multigraph][$munin_sensor_name . '.label'] = $info
          ->getLabel();
      }
    }
    else {
      $graph['graph_title'] = $info
        ->getDescription();
      $graph['graph_vlabel'] = $info
        ->getValueLabel() ? $info
        ->getValueLabel() : $info
        ->getLabel();
      $graph['graph_category'] = $host;
      $graph[$munin_sensor_name . '.label'] = $info
        ->getLabel();

      // @todo - deal with other threshold types.
      if ($info
        ->getThresholdsType() == 'exceeds') {
        foreach (array(
          'warning',
          'critical',
        ) as $key) {
          $graph[$munin_sensor_name . '.' . $key] = $info
            ->getThresholdValue($key);
        }
      }
      $config[$munin_sensor_name] = $graph;
    }
  }
  return $config;
}

/**
 * Composes sensor identifier based on the current host.
 *
 * @param string $sensor_name
 *   Sensor name
 *
 * @return string
 *   Sensor identifier.
 */
function monitoring_munin_sensor_identifier($sensor_name) {
  return str_replace('-', '_', monitoring_host_key() . '__' . $sensor_name);
}

/**
 * Composes multigraph identifier.
 *
 * @param string $category
 *   Category for which to get the identifier.
 *
 * @return string
 *   The identifier.
 */
function monitoring_munin_multigraph_identifier($category) {
  return str_replace('-', '_', monitoring_host_key() . '__' . drupal_clean_css_identifier($category));
}

Functions

Namesort descending Description
monitoring_munin_config Returns the munin graphs definitions.
monitoring_munin_menu Implements hook_menu().
monitoring_munin_multigraphs Gets munin multigraphs definitions.
monitoring_munin_multigraph_delete Deletes multigraph and remove from sensor settings.
monitoring_munin_multigraph_identifier Composes multigraph identifier.
monitoring_munin_multigraph_save Saves new multigraph.
monitoring_munin_munin_default_multigraphs Implements hook_munin_default_multigraphs()
monitoring_munin_sensor_identifier Composes sensor identifier based on the current host.
_monitoring_munin_result Returns the monitoring result.