Parcourir la source

Implemented hook_munin_api_fetch() and hook_munin_api_config()

- hook call pages in munin_api.module
- hook implementations in munin_apc.module
FGM il y a 13 ans
Parent
commit
51091296e9
2 fichiers modifiés avec 163 ajouts et 25 suppressions
  1. 39 18
      modules/munin_apc/munin_apc.module
  2. 124 7
      munin_api.module

+ 39 - 18
modules/munin_apc/munin_apc.module

@@ -16,40 +16,61 @@ function munin_apc_munin_api_info() {
     'munin_apc' => array(
       '#title' => t('PHP APC'),
       '#description' => t('Collect cache information for the Alternative PHP Cache'),
+      'uptime'     => array(
+        '#title'     => t('Up-time of APC instance'),
+        '#type'      => MUNIN_API_GAUGE
+      ),
       'total_size' => array(
-        '#title'    => t('Number of bytes allocated to APC'),
-        '#type'     => MUNIN_API_COUNTER,
+        '#title'     => t('Number of bytes allocated to APC'),
+        '#type'      => MUNIN_API_COUNTER,
       ),
-      'used_size' => array(
-        '#title'    => t('Number of bytes currently used by APC, either cached or deleted'),
-        '#type'     => MUNIN_API_COUNTER,
+      'used_size'  => array(
+        '#title'     => t('Number of bytes currently used by APC, either cached or deleted'),
+        '#type'      => MUNIN_API_COUNTER,
       ),
-      'num_hits' => array(
-        '#title'    => t('The number of hits since the last restart'),
-        '#type'     => MUNIN_API_COUNTER,
+      'num_hits'   => array(
+        '#title'     => t('The number of hits since the last restart'),
+        '#type'      => MUNIN_API_COUNTER,
       ),
       'num_misses' => array(
-        '#title'    => t('The number of misses since the last restart'),
-        '#type'     => MUNIN_API_COUNTER,
+        '#title'     => t('The number of misses since the last restart'),
+        '#type'      => MUNIN_API_COUNTER,
       ),
       'hit_rate'   => array(
-        '#title'    => t('This hit/miss ration since the last restart'),
-        '#type'     => MUNIN_API_GAUGE,
+        '#title'     => t('This hit/miss ratio since the last restart'),
+        '#type'      => MUNIN_API_GAUGE,
       ),
       'num_insertions' => array(
-        '#title'    => t('The number of insertions since the last restart'),
-        '#type'     => MUNIN_API_COUNTER,
+        '#title'     => t('The number of insertions since the last restart'),
+        '#type'      => MUNIN_API_COUNTER,
       ),
       'cache_entries' => array(
-        '#title'    => t('The number of cache entries'),
-        '#type'     => MUNIN_API_GAUGE,
+        '#title'     => t('The number of cache entries'),
+        '#type'      => MUNIN_API_GAUGE,
       ),
       'deleted_entries' => array(
-        '#title'    => t('The number of deleted entries lingering'),
-        '#type'     => MUNIN_API_GAUGE,
+        '#title'     => t('The number of deleted entries lingering'),
+        '#type'      => MUNIN_API_GAUGE,
       ),
     ),
   );
 
   return $ret;
 }
+
+/**
+ * Implements hook_munin_api_fetch().
+ */
+function munin_apc_munin_api_fetch() {
+  return array(
+    'uptime' => 1,
+  );
+}
+
+/**
+ * Implements hook_munin_api_config().
+ */
+function munin_apc_munin_api_config() {
+  return array(
+  );
+}

+ 124 - 7
munin_api.module

@@ -6,8 +6,60 @@
  */
 
 define('MUNIN_API_COUNTER', 'COUNTER');
+define('MUNIN_API_DERIVE',  'DERIVE');
 define('MUNIN_API_GAUGE',   'GAUGE');
 
+/**
+ * Finalize result pages for Munin interactions.
+ *
+ * - text content, not HTML
+ * - non cacheable, even for anonymous users
+ */
+function _munin_api_page_closure($ret) {
+  drupal_set_header('Content-type: text/plain');
+  $GLOBALS['conf']['cache'] = CACHE_DISABLED; // prevent page_set_cache() in drupal_page_footer().
+
+  print $ret;
+  drupal_page_footer();
+  exit();
+}
+
+/**
+ * Display and log an incorrect hook implementation.
+ *
+ * @param string $hook
+ * @param string $module
+ *
+ * @return string
+ */
+function _munin_report_hook_error($hook, $module) {
+  $message = t('Incorrect implementation of hook_!hook() in module @module.');
+  $params = array(
+    '!hook' => $hook,
+    '@module' => $module,
+  );
+  drupal_set_message(strtr($message, $params), 'error');
+  watchdog('munin_api', $message, $params, WATCHDOG_ERROR);
+  return '<p>'. l(t('Back'), 'admin/reports/munin_api/'. $module) .'</p>';
+}
+
+/**
+ * Menu access callback for Munin config fetches.
+ *
+ * TODO: define rules, then code
+ */
+function munin_api_access_config($module) {
+  return TRUE; // For now, protect at the web server level
+}
+
+/**
+ * Menu access callback for Munin data fetches.
+ *
+ * TODO: define rules, then code
+ */
+function munin_api_access_fetch($module) {
+  return TRUE; // For now, protect at the web server level
+}
 
 function munin_api_menu() {
   $items = array();
@@ -28,6 +80,21 @@ function munin_api_menu() {
   foreach (module_implements('munin_api_info') as $name) {
     $module = module_invoke($name, 'munin_api_info');
     $module = $module[$name];
+    $items['munin_api/'. $name] = array(
+      'type' => MENU_CALLBACK,
+      'page callback' => 'munin_api_page_fetch',
+      'page arguments' => array(1),
+      'access callback' => 'munin_api_access_fetch',
+      'access arguments' => array($name),
+    );
+    $items['munin_api/'. $name .'/config'] = array(
+      'type' => MENU_CALLBACK,
+      'page callback' => 'munin_api_page_config',
+      'page arguments' => array(1),
+      'access callback' => 'munin_api_access_config',
+      'access arguments' => array($name),
+    );
+
     $items['admin/reports/munin_api/'. $name] = array(
       'type' => MENU_LOCAL_TASK,
       'title' => $module['#title'],
@@ -38,11 +105,46 @@ function munin_api_menu() {
     );
   }
 
+
   return $items;
 }
 
 /**
- * Page callback for Munin report
+ * Page callback for munin config fetches.
+ */
+function munin_api_page_config($module) {
+  $data = module_invoke($module, 'munin_api_config');
+  if (!is_array($data)) {
+    return _munin_report_hook_error('munin_api_config', $module);
+  }
+
+  $ret = '';
+  foreach ($data as $attribute => $value) {
+    $ret .= $attribute .' = '. $value . PHP_EOL;
+  }
+
+  _munin_api_page_closure($ret);
+}
+
+/**
+ * Page callback for munin data fetches.
+ */
+function munin_api_page_fetch($module) {
+  $data = module_invoke($module, 'munin_api_fetch');
+  if (!is_array($data)) {
+    return _munin_report_hook_error('munin_api_fetch', $module);
+  }
+
+  $ret = '';
+  foreach ($data as $field => $value) {
+    $ret .= $module .'.'. $field .' = '. $value . PHP_EOL;
+  }
+
+  _munin_api_page_closure($ret);
+}
+
+/**
+ * Page callback for Munin global report.
  *
  * @return string
  */
@@ -52,7 +154,7 @@ function munin_api_page_report_global() {
   $header = array(
     t('Module'),
     t('Description'),
-    t('Stats'),
+    t('Fields'),
   );
   $rows = array();
   foreach ($items as $name => $item) {
@@ -67,20 +169,35 @@ function munin_api_page_report_global() {
   return $ret;
 }
 
+/**
+ * Page callback for Munin instance report.
+ *
+ * @return string
+ */
 function munin_api_page_report_instance($module) {
-  $module = module_invoke($module, 'munin_api_info');
-  $module = reset($module);
+  $module_info = module_invoke($module, 'munin_api_info');
+  if (!is_array($module_info) || count($module_info) != 1) {
+    return _munin_report_hook_error('munin_api_info', $module);
+  }
+  $module_info = reset($module_info);
+  if (!is_array($module_info)) {
+    return _munin_report_hook_error('munin_api_info', $module);
+  }
 
   $header = array(t('Name'), t('Description'), t('Type'));
   $rows = array();
-  foreach (element_children($module) as $name) {
+  foreach (element_children($module_info) as $name) {
     $rows[] = array(
       $name,
-      isset($module[$name]['#title']) ? $module[$name]['#title'] : t('&lt;missing&gt;'),
-      $module[$name]['#type'],
+      isset($module_info[$name]['#title']) ? $module_info[$name]['#title'] : t('&lt;missing&gt;'),
+      $module_info[$name]['#type'],
     );
   }
   $ret = theme('table', $header, $rows);
+  $ret .= '<h3>Munin debug (text-only)</h3><p>'
+    . l(t('Fetch data'), 'munin_api/'. $module)
+    . ' '
+    . l(t('Fetch config'), 'munin_api/'. $module .'/config');
   return $ret;
 }