You are here

health.module in Health Status 7

Contains the basic hooks and function used by the Health system.

File

health.module
View source
<?php

/**
 * @file
 * Contains the basic hooks and function used by the Health system.
 */
define("HEALTH_OKAY", 'okay');
define("HEALTH_WARNING", 'warning');
define("HEALTH_ERROR", 'error');
define("HEALTH_UNKNOWN", 'unknown');

/**
 * Implements hook_init().
 */
function health_init() {

  // Add admin stylesheet if on the dashboard.
  if (arg(1) == 'reports' && arg(2) == 'health') {
    drupal_add_css(drupal_get_path('module', 'health') . '/css/health.css', array(
      'type' => 'file',
    ));
  }
}

/**
 * Implements hook_permissions().
 */
function health_permission() {
  return array(
    'access health report' => array(
      'title' => t('Access the system health report.'),
      'description' => t('Allows a user to see all information in the health report'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function health_menu() {

  // Health settings page for modifying generic functionality.
  $items['admin/config/system/health'] = array(
    'title' => 'Health settings',
    'description' => 'Manage your Health page and API settings.',
    'type' => MENU_NORMAL_ITEM,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'health_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'health.admin.inc',
  );

  // Health dashboard, for viewing how your site is doing.
  $items['admin/reports/health/dashboard'] = array(
    'title' => 'Health dashboard',
    'description' => 'View the health of your site over the past few days.',
    'type' => MENU_NORMAL_ITEM,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'health_admin_dashboard_form',
    ),
    'access arguments' => array(
      'access health report',
    ),
    'file' => 'health.admin.inc',
  );

  // Health API callback.
  $items['health/api/%'] = array(
    'title' => 'Health API',
    'description' => 'Health API callback.',
    'type' => MENU_CALLBACK,
    'page callback' => 'health_api_callback',
    'page arguments' => array(
      2,
    ),
    'access callback' => 'health_api_access',
    'file' => 'health.service.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function health_theme() {
  return array(
    'health_status_email' => array(
      'template' => 'templates/health_status_email',
      'variables' => array(),
    ),
    'health_help_page' => array(
      'template' => 'templates/health_help_page',
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_cron().
 */
function health_cron() {

  // Check to see if we need to send e-mails right now.
  $last_run = variable_get('health_cron_last', 0);
  $frequency = variable_get('health_email_frequency');
  if ($frequency !== "never" && ($frequency === "cron" || REQUEST_TIME - $frequency < $last_run)) {
    if (health_email_send()) {
      health_cron_email_send();
    }
  }
}

/**
 * Determines if the Health status e-mail should be sent.
 *
 * The e-mail should only be sent if health status monitors report
 * a threshold that the user has requested to receive e-mails for.
 */
function health_email_send() {
  $thresholds = array_keys(array_filter(variable_get('health_email_threshold', array())));
  $data = health_get_data();
  foreach ($data as $group => $results) {
    foreach ($results as $monitor => $r) {
      if (in_array($r['status'], $thresholds)) {

        // User needs to receive e-mail for this threshold.
        return TRUE;
      }
    }
  }

  // User does not need to receive e-mail.
  return FALSE;
}

/**
 * Sends a health status on cron run when necessary.
 */
function health_cron_email_send() {

  // Need to include admin file for the table functions.
  module_load_include('inc', 'health', 'health.admin');
  $emails = explode(PHP_EOL, variable_get('health_email_users'));
  $data = health_get_data();
  $monitors = health_get_monitors();
  $tables = array();
  foreach ($data as $group => $results) {
    $tables[$group] = theme('table', array(
      'rows' => health_admin_dashboard_rows($monitors[$group], $results),
      'header' => health_admin_dashboard_headers(),
    ));
  }
  $params = array(
    'site_name' => variable_get('site_name'),
    'data' => health_get_data(),
    'tables' => $tables,
  );
  foreach ($emails as $to) {
    drupal_mail('health', 'health_status', $to, language_default(), $params, variable_get('site_email'), TRUE);
  }
}

/**
 * Implements hook_mail().
 */
function health_mail($key, &$message, $params) {
  switch ($key) {
    case "health_status":
      $message['subject'] = variable_get('health_email_subject');
      $message['body'][] = theme('health_status_email', $params);
      $message['headers']['Content-Type'] = "text/html; charset=iso-8859-1";
      $message['headers']['Mime-Version'] = "1.0";
      break;
  }
}

/**
 * Gets all the health status data from modules that hook into it.
 *
 * @return array
 *   Results from all the health monitors.
 */
function health_get_data() {
  $monitors = health_get_monitors();
  foreach ($monitors as $group => $checks) {

    // Run hooks for the name of the monitor.
    foreach ($checks as $key => $check) {
      $results[$group][$key] = module_invoke_all('health_monitor_' . $key, $check['args']);
    }
  }
  drupal_alter('health_data', $results);
  return (array) $results;
}

/**
 * Gets monitors that have been registered.
 *
 * @return array
 *   Monitors supplied by hooks.
 */
function health_get_monitors() {
  $monitors = module_invoke_all('health_monitors');

  // Organize monitors by group.
  $grouped = array();
  foreach ($monitors as $key => $m) {
    $grouped[$m['group']][$key] = $m;
  }
  drupal_alter('health_monitors', $grouped);
  return $grouped;
}

/**
 * Returns a properly formatted message from a monitor.
 *
 * @param string $status
 *   Status should be HEALTH_OKAY, HEALTH_WARNING, HEALTH_ERROR, or HEALTH_UNKNOWN.
 * @param string $message
 *   A message to show on the Health Status page.
 *
 * @return array
 *   Returns a properly formatted response for the Health monitor.
 */
function health_monitor_status($status, $message) {
  return array(
    'status' => $status,
    'message' => $message,
  );
}

/**
 * Access callback for seeing if the user can view the Health API.
 *
 * The Health API requires a secret key be posted to it.
 * The key can be set in the admin/config/system/health settings page.
 */
function health_api_access() {

  // Make sure they sent a key.
  if (!isset($_POST['key']) || empty($_POST['key'])) {
    return FALSE;
  }

  // Check if we are requiring HTTPS.
  if (variable_get('health_api_require_https', TRUE) && $_SERVER['HTTPS'] !== "on") {

    // User is not using HTTPS.
    return FALSE;
  }
  $key = $_POST['key'];

  // Check the key versus the one we have stored.
  if ($key !== variable_get('health_api_access_key', FALSE)) {

    // Wrong key sent.
    return FALSE;
  }

  // All checks passed, allow access.
  return TRUE;
}

/**
 * Implements hook_help().
 *
 * @todo: Fill this in.
 */
function health_help($path, $arg) {
  switch ($path) {
    case "admin/help#health":
      return theme('health_help_page');
  }
}

Functions

Namesort descending Description
health_api_access Access callback for seeing if the user can view the Health API.
health_cron Implements hook_cron().
health_cron_email_send Sends a health status on cron run when necessary.
health_email_send Determines if the Health status e-mail should be sent.
health_get_data Gets all the health status data from modules that hook into it.
health_get_monitors Gets monitors that have been registered.
health_help Implements hook_help().
health_init Implements hook_init().
health_mail Implements hook_mail().
health_menu Implements hook_menu().
health_monitor_status Returns a properly formatted message from a monitor.
health_permission Implements hook_permissions().
health_theme Implements hook_theme().

Constants

Namesort descending Description
HEALTH_ERROR
HEALTH_OKAY @file Contains the basic hooks and function used by the Health system.
HEALTH_UNKNOWN
HEALTH_WARNING