Browse Source

Munin API can now monitor the Munin probing it.

- new settings page to choose mode: none, cron, init (choose path)
- new watchdogging mechanism: only trigger if delays above munin limits
Frederic G. MARAND 13 years ago
parent
commit
d13e2fe0ca
3 changed files with 183 additions and 0 deletions
  1. 23 0
      INSTALL.txt
  2. 61 0
      munin_api.js
  3. 99 0
      munin_api.module

+ 23 - 0
INSTALL.txt

@@ -58,6 +58,7 @@ Then check their data:
  
   munin-run drupal_core
   munin-run drupal_apc  
+
   
 5 Modify configuration
 ----------------------
@@ -65,3 +66,25 @@ Then check their data:
 Any time you enable/disable a Munin submodule or modify its settings, you will
 need to force a munin-node configuration reload so that the server knows what to 
 plot.
+
+
+6 Access control
+----------------
+
+In the Drupal UI:
+
+- The Munin API settings form 
+
+  - is located at:
+    admin/settings/munin_api
+
+  - is controlled by permission: 
+    administer site configuration
+    
+- The Munin report pages
+ 
+  - start at:
+    admin/reports/munin_api
+    
+  - are controlled by the permission:
+    access site reports

+ 61 - 0
munin_api.js

@@ -0,0 +1,61 @@
+// $Id$
+
+/**
+ * @file
+ * Munin API for Drupal: settings UI eye candy
+ *
+ * @author Frederic G. MARAND
+ *
+ * @copyright (c) 2011 Ouest Systèmes Informatiques
+ *
+ * Licensed under the General Public License version 2 or later.
+ */
+
+/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true,
+  plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true,
+  indent: 2 */
+/*global Drupal,$ */
+
+/**
+ * Fade a selection in or out, depending on the parameters
+ *
+ * @param string selector
+ *   A jQuery selector
+ * @param boolean visible
+ *   Set to true to fade in, to fase to fade out
+ * @return void
+ */
+function muninApiFade(selector, visible) {
+  if (visible) {
+    $(selector).fadeIn();
+  }
+  else {
+    $(selector).fadeOut();
+  }
+}
+
+
+/**
+ * Ready handler to apply visibility to the path field on the settings form.
+ *
+ * Hide the manual path selector initially unless the current watchdog mode is
+ * set to init.
+ *
+ * @return void
+ */
+function onReadyPathVisibility() {
+  if ($('#edit-munin-api-watchdog-init').attr('checked') === false) {
+    $('#edit-munin-api-init-path-wrapper').hide();
+  }
+  $("input:radio[name='munin_api_watchdog']").click(function () {
+    muninApiFade('#edit-munin-api-init-path-wrapper',
+      $("input:radio[name='munin_api_watchdog']:checked").val() === 'init');
+  });
+}
+
+/**
+ * Assign ready handler
+ */
+$(function () {
+  onReadyPathVisibility();
+});

+ 99 - 0
munin_api.module

@@ -84,9 +84,76 @@ function munin_api_access_fetch($module) {
   return TRUE; // For now, protect at the web server level
 }
 
+function munin_api_admin_settings($form_state) {
+
+  $form = array();
+
+  $form['munin_api_watchdog'] = array(
+    '#title' => t('Munin API Watchdog'),
+    '#description' => t('Enables Drupal-level monitoring of Munin. If enabled, it will regularly make sure the time interval between Munin probes is within the expected range and raise watchdog alerts accordingly. Init should only be used on pages with a low-occurrence rate, and will not work properly if caching mode is set to "aggressive" or "external" (Pressflow).'),
+    '#type' => 'radios',
+    '#options' => array(
+      'none' => t('Disabled'),
+      'cron' => t('On cron runs'),
+      'init' => t('On page init'),
+    ),
+    '#default_value' => variable_get('munin_api_watchdog', 'cron'),
+  );
+
+  $default_path = variable_get('munin_api_init_path', '/^$/');
+  $form['munin_api_init_path'] = array(
+    '#title' => t('Init path'),
+    '#description' => t('For page init watchdog mode, specify the regular expression for the paths that should trigger a watchdog check'),
+    '#type' => 'textfield',
+    '#default_value' => $default_path,
+  );
+
+  drupal_add_js(drupal_get_path('module', 'munin_api') .'/munin_api.js');
+  drupal_add_js(array(
+    'munin_api' => array(
+      'path' => $default_path,
+    )), 'setting');
+
+  $form = system_settings_form($form);
+  return $form;
+}
+
+/**
+ * Implements hook_cron().
+ */
+function munin_api_cron() {
+  if (variable_get('munin_api_watchdog', 'cron') == 'cron') {
+    _munin_api_watchdog_munin();
+  }
+}
+
+/**
+ * Implements hook_init().
+ *
+ * Only trigger Munin API watchdog on selected pages.
+ */
+function munin_api_init() {
+  if (variable_get('munin_api_watchdog', 'cron') == 'init') {
+    $path = $_GET['q'];
+    $regex = variable_get('munin_api_init_path', '/^$/');
+    $sts = preg_match($regex, $path);
+    if ($sts) {
+      _munin_api_watchdog_munin();
+    }
+  }
+}
+
 function munin_api_menu() {
   $items = array();
 
+  $items['admin/settings/munin_api'] = array(
+    'title' => 'Munin',
+    'description' => 'Munin API settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('munin_api_admin_settings'),
+    'access arguments' => array('administer site configuration'),
+  );
+
   $items['admin/reports/munin_api'] = array(
     'title' => 'Munin',
     'description' => 'Reports about Munin data collectors and their probes',
@@ -159,6 +226,8 @@ function munin_api_munin_api_info() {
         '#label'     => t('Seconds since last probe'),
         '#type'      => MUNIN_API_GAUGE,
         '#info'      => t('Seconds since last probe should hover around 300. 0 means no probe found.'),
+        '#warning'   => '290:310',
+        '#critical'  => '1:600',
       ),
     ),
   );
@@ -331,3 +400,33 @@ function munin_api_page_report_instance($module_name, $module_info) {
   $ret = theme('table', $header, $rows);
   return $ret;
 }
+
+/**
+ * Munin API watchdog implementation.
+ *
+ * @return void
+ */
+function _munin_api_watchdog_munin() {
+  $seconds = munin_api_munin_api_fetch('munin_munin', FALSE);
+  $seconds = reset($seconds);
+
+  $info = munin_api_munin_api_info();
+  $info = $info['munin_munin']['munin_seconds'];
+  $warning  = explode(':', $info['#warning']);
+  $critical = explode(':', $info['#critical']);
+  if (/* $seconds < $critical[0] || */ $seconds > $critical[1]) {
+    $level = WATCHDOG_CRITICAL;
+  }
+  elseif (/* $seconds < $warning[0] || */ $seconds > $warning[1]) {
+    $level = WATCHDOG_WARNING;
+  }
+  else {
+    $level = WATCHDOG_DEBUG;
+  }
+
+  if ($level < WATCHDOG_DEBUG) { // meaning MORE urgent
+    watchdog('munin_api', 'Munin last probe came @last seconds ago.', array(
+      '@last' => $seconds,
+    ), $level);
+  }
+}