You are here

nagios.module in Nagios Monitoring 5

Same filename and directory in other branches
  1. 8 nagios.module
  2. 6 nagios.module
  3. 7 nagios.module

File

nagios.module
View source
<?php

// Copyright 2009 Khalid Baheyeldin http://2bits.com
// Defines to be used by this modules and others that use its hook_nagios()
define('NAGIOS_STATUS_OK', 0);
define('NAGIOS_STATUS_UNKNOWN', 1);
define('NAGIOS_STATUS_WARNING', 2);
define('NAGIOS_STATUS_CRITICAL', 3);

/**
 * Mapping of defines to text strings that Nagios understands
 */
function nagios_status() {
  return array(
    NAGIOS_STATUS_OK => 'OK',
    NAGIOS_STATUS_UNKNOWN => 'UNKNOWN',
    NAGIOS_STATUS_WARNING => 'WARNING',
    NAGIOS_STATUS_CRITICAL => 'CRITICAL',
  );
}

/**
 * Functions to be performed by the base nagios module.
 */
function nagios_functions() {
  return array(
    // Checking of hook_requirements. This includes the following:
    // - module updates
    // - database schema
    // - files directory writability
    // - update.php protected
    // - Lots of other good stuff ...
    'requirements',
    // Checking whether cron has been running regularly
    'cron',
    // Checking the number of anonymous sessions
    'session_anon',
    // Checking the number of authenticated sessions
    'session_auth',
    // Checking the number of nodes and users.
    // These queries give some site statistics, and also serve to ensure the database is up
    'nodes',
    'users',
    // Modules and themes
    'modules',
    'themes',
  );
}

/**
 * Implementation of hook_menu
 */
function nagios_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/nagios',
      'type' => MENU_NORMAL_ITEM,
      'title' => t('Nagios monitoring'),
      'description' => t('Settings for Nagios monitoring'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'nagios_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
    $items[] = array(
      'path' => 'nagios',
      'type' => MENU_SUGGESTED_ITEM,
      'title' => t('Nagios status page'),
      'callback' => 'nagios_status_page',
      'access' => TRUE,
    );
  }
  return $items;
}

/**
 * Callback for the settings page
 */
function nagios_settings() {
  $group = 'modules';
  $form['nagios_ua'] = array(
    '#type' => 'textfield',
    '#title' => t('Unique ID'),
    '#default_value' => variable_get('nagios_ua', 'Nagios'),
    '#description' => t('Restrict sending information to requests identified by this Unique ID. You should change this to some unique string for your organization, and configure Nagios accordingly. This makes Nagios data less accessible to curious users. See the README.txt for more details.'),
  );
  $form[$group] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Modules'),
    '#description' => t('Select the modules that should report their data into Nagios.'),
  );
  foreach (nagios_invoke_all('nagios_info') as $module => $data) {
    $form[$group]['nagios_enable_' . $module] = array(
      '#type' => 'checkbox',
      '#title' => $data['name'] . ' (' . $module . ')',
      '#default_value' => variable_get('nagios_enable_' . $module, TRUE),
    );
  }
  foreach (nagios_invoke_all('nagios_settings') as $module => $module_settings) {
    $form[$module] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $module,
    );
    foreach ($module_settings as $element => $data) {
      $form[$module][$element] = $data;
    }
  }
  return system_settings_form($form);
}

/**
 * Callback for the nagios status page
 */
function nagios_status_page() {
  $skip_invoke = FALSE;
  header("Pragma: no-cache");
  header("Expires: 0");
  $codes = nagios_status();

  // Check the unique ID string first
  $ua = variable_get('nagios_ua', 'Nagios');
  if ($_SERVER['HTTP_USER_AGENT'] != $ua) {

    // This is not an authorized unique id, so do not send information out
    $skip_invoke = TRUE;
    $nagios_data = array(
      'nagios' => array(
        'DRUPAL' => array(
          'status' => NAGIOS_STATUS_UNKNOWN,
          'type' => 'state',
          'text' => t('Unauthorized'),
        ),
      ),
    );
  }
  if (!$skip_invoke) {

    // Not authorized, so skipping calling other modules
    $nagios_data = nagios_invoke_all('nagios');
  }

  // Find the highest level to be the overall status
  $severity = NAGIOS_STATUS_OK;
  foreach ($nagios_data as $module_name => $module_data) {
    foreach ($module_data as $key => $value) {
      $severity = max($severity, $value['status']);
    }
  }

  // Identifier that we check on the other side
  $output = "\n" . 'nagios=' . $codes[$severity] . ', ';
  $output_state = array();
  $output_perf = array();
  foreach ($nagios_data as $module_name => $module_data) {
    foreach ($module_data as $key => $value) {
      switch ($value['type']) {
        case 'state':
          $tmp_state = $key . ':' . $codes[$value['status']];
          if (!empty($value['text'])) {
            $tmp_state .= '=' . $value['text'];
          }
          $output_state[] = $tmp_state;
          break;
        case 'perf':
          $output_perf[] = $key . '=' . $value['text'];
          break;
      }
    }
  }
  $output .= implode(', ', $output_state) . ' | ' . implode(';', $output_perf) . "\n";

  // Print the output, so we test the theme
  print theme('page', $output);

  // Exit early so we do not cache the data, nor do we wrap the result in a theme
  exit;
}

/**
 * Custom invoke function
 */
function nagios_invoke_all($hook = 'nagios') {

  // This is a custom invoke function that returns a keyed array
  $return = array();
  $args = func_get_args();
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    $result = call_user_func_array($function, $args);
    $return[$module] = $result;
  }
  return $return;
}

/**
 * Implementation of hook_nagios_info()
 */
function nagios_nagios_info() {
  return array(
    'name' => 'Nagios monitoring',
    'id' => 'NAGIOS',
  );
}

/**
 * Implementation of hook_nagios_settings()
 */
function nagios_nagios_settings() {
  foreach (nagios_functions() as $function) {
    $var = 'nagios_func_' . $function;
    $form[$var] = array(
      '#type' => 'checkbox',
      '#title' => $function,
      '#default_value' => variable_get($var, TRUE),
    );
  }
  $group = 'thresholds';
  $form[$group] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Thresholds'),
    '#description' => t('Thresholds for reporting critical alerts to Nagios.'),
  );
  $form[$group]['nagios_cron_duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Cron duration'),
    '#default_value' => variable_get('nagios_cron_duration', 60),
    '#description' => t('Issue a critical alert when cron has not been running for this duration (in minutes). Default is 60 minutes.'),
  );
  return $form;
}

/**
 * Implementation of hook_nagios
 */
function nagios_nagios() {
  $status = array();
  foreach (nagios_functions() as $function) {
    if (variable_get('nagios_func_' . $function, TRUE)) {
      $func = 'nagios_check_' . $function;
      $result = $func();
      $status[$result['key']] = $result['data'];
    }
  }
  return $status;
}
function nagios_check_requirements() {

  // Load .install files
  include_once './includes/install.inc';
  drupal_load_updates();

  // Get the run-time requirements and status information.
  $reqs = module_invoke_all('requirements', 'runtime');

  // Check the requirements as to the most severe status
  $severity = REQUIREMENT_OK;
  foreach ($reqs as $key => $requirement) {
    if (isset($requirement['severity'])) {
      if ($requirement['severity'] > $severity) {
        $severity = $requirement['severity'];
        $desc = $requirement['title'];
      }
    }
  }

  // Create a status to pass back, and a text description too
  switch ($severity) {
    case REQUIREMENT_OK:
    case REQUIREMENT_INFO:
      $data = array(
        'status' => NAGIOS_STATUS_OK,
        'type' => 'state',
        'text' => '',
      );
      break;
    case REQUIREMENT_WARNING:
      $data = array(
        'status' => NAGIOS_STATUS_WARNING,
        'type' => 'state',
        'text' => t('@desc', array(
          '@desc' => $desc,
        )),
      );
      break;
    case REQUIREMENT_ERROR:
      $data = array(
        'status' => NAGIOS_STATUS_CRITICAL,
        'type' => 'state',
        'text' => t('@desc', array(
          '@desc' => $desc,
        )),
      );
      break;
    default:
      $data = array(
        'status' => NAGIOS_STATUS_UNKNOWN,
        'type' => 'state',
        'text' => t('severity is @severity', array(
          '@severity' => $severity,
        )),
      );
      break;
  }
  return array(
    'key' => 'ADMIN',
    'data' => $data,
  );
}
function nagios_check_cron() {
  $cron_last = variable_get('cron_last', 0);
  $mins = variable_get('nagios_cron_duration', 60);
  if (time() > $cron_last + $mins * 60) {
    $data = array(
      'status' => NAGIOS_STATUS_CRITICAL,
      'type' => 'state',
      'text' => t('cron not running @mins mins', array(
        '@mins' => $mins,
      )),
    );
  }
  else {
    $data = array(
      'status' => NAGIOS_STATUS_OK,
      'type' => 'state',
      'text' => '',
    );
  }
  return array(
    'key' => 'CRON',
    'data' => $data,
  );
}
function nagios_check_session_anon() {
  $interval = time() - 900;

  // Last 15 minutes
  $count = (int) sess_count($interval, TRUE);
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'SAN',
    'data' => $data,
  );
}
function nagios_check_session_auth() {
  $interval = time() - 900;

  // Last 15 minutes
  $count = (int) sess_count($interval, FALSE);
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'SAU',
    'data' => $data,
  );
}
function nagios_check_nodes() {

  // Include number of active nodes in the report
  $count = (int) db_result(db_query("SELECT COUNT(*) FROM {node} WHERE status = 1"));
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'NOD',
    'data' => $data,
  );
}
function nagios_check_users() {

  // Include number of active users in the report
  $count = (int) db_result(db_query("SELECT COUNT(*) FROM {users} WHERE status = 1"));
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'USR',
    'data' => $data,
  );
}
function nagios_check_modules() {
  $count = (int) db_result(db_query("SELECT COUNT(*) FROM {system} WHERE status = 1 AND type = 'module'"));
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'MOD',
    'data' => $data,
  );
}
function nagios_check_themes() {
  $count = (int) db_result(db_query("SELECT COUNT(*) FROM {system} WHERE status = 1 AND type = 'theme'"));
  $data = array(
    'status' => NAGIOS_STATUS_OK,
    'type' => 'perf',
    'text' => $count,
  );
  return array(
    'key' => 'THM',
    'data' => $data,
  );
}

Functions

Namesort descending Description
nagios_check_cron
nagios_check_modules
nagios_check_nodes
nagios_check_requirements
nagios_check_session_anon
nagios_check_session_auth
nagios_check_themes
nagios_check_users
nagios_functions Functions to be performed by the base nagios module.
nagios_invoke_all Custom invoke function
nagios_menu Implementation of hook_menu
nagios_nagios Implementation of hook_nagios
nagios_nagios_info Implementation of hook_nagios_info()
nagios_nagios_settings Implementation of hook_nagios_settings()
nagios_settings Callback for the settings page
nagios_status Mapping of defines to text strings that Nagios understands
nagios_status_page Callback for the nagios status page

Constants