munin_api.module 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * An API to help modules expose their instrumentation to Munin.
  6. */
  7. define('MUNIN_API_COUNTER', 'COUNTER');
  8. define('MUNIN_API_DERIVE', 'DERIVE');
  9. define('MUNIN_API_GAUGE', 'GAUGE');
  10. /**
  11. * Finalize result pages for Munin interactions.
  12. *
  13. * - text content, not HTML
  14. * - non cacheable, even for anonymous users
  15. */
  16. function _munin_api_page_closure($ret) {
  17. drupal_set_header('Content-type: text/plain');
  18. $GLOBALS['conf']['cache'] = CACHE_DISABLED; // prevent page_set_cache() in drupal_page_footer().
  19. print $ret;
  20. drupal_page_footer();
  21. exit();
  22. }
  23. /**
  24. * Display and log an incorrect hook implementation.
  25. *
  26. * @param string $hook
  27. * @param string $module
  28. *
  29. * @return string
  30. */
  31. function _munin_report_hook_error($hook, $module) {
  32. $message = t('Incorrect implementation of hook_!hook() in module @module.');
  33. $params = array(
  34. '!hook' => $hook,
  35. '@module' => $module,
  36. );
  37. drupal_set_message(strtr($message, $params), 'error');
  38. watchdog('munin_api', $message, $params, WATCHDOG_ERROR);
  39. return '<p>'. l(t('Back'), 'admin/reports/munin_api/'. $module) .'</p>';
  40. }
  41. /**
  42. * Menu access callback for Munin config fetches.
  43. *
  44. * TODO: define rules, then code
  45. */
  46. function munin_api_access_config($module) {
  47. return TRUE; // For now, protect at the web server level
  48. }
  49. /**
  50. * Menu access callback for Munin data fetches.
  51. *
  52. * TODO: define rules, then code
  53. */
  54. function munin_api_access_fetch($module) {
  55. return TRUE; // For now, protect at the web server level
  56. }
  57. function munin_api_menu() {
  58. $items = array();
  59. $items['admin/reports/munin_api'] = array(
  60. 'title' => 'Munin',
  61. 'description' => 'Reports about Munin data collectors and their probes',
  62. 'page callback' => 'munin_api_page_report_global',
  63. 'access arguments' => array('access site reports'),
  64. );
  65. $items['admin/reports/munin_api/list'] = array(
  66. 'type' => MENU_DEFAULT_LOCAL_TASK,
  67. 'title' => 'General',
  68. 'weight' => -1,
  69. );
  70. foreach (module_implements('munin_api_info') as $name) {
  71. $module = module_invoke($name, 'munin_api_info');
  72. $module = $module[$name];
  73. $items['munin_api/'. $name] = array(
  74. 'type' => MENU_CALLBACK,
  75. 'page callback' => 'munin_api_page_fetch',
  76. 'page arguments' => array(1),
  77. 'access callback' => 'munin_api_access_fetch',
  78. 'access arguments' => array($name),
  79. );
  80. $items['munin_api/'. $name .'/config'] = array(
  81. 'type' => MENU_CALLBACK,
  82. 'page callback' => 'munin_api_page_config',
  83. 'page arguments' => array(1),
  84. 'access callback' => 'munin_api_access_config',
  85. 'access arguments' => array($name),
  86. );
  87. $items['admin/reports/munin_api/'. $name] = array(
  88. 'type' => MENU_LOCAL_TASK,
  89. 'title' => $module['#title'],
  90. 'description' => $module['#description'],
  91. 'page callback' => 'munin_api_page_report_instance',
  92. 'page arguments' => array(3),
  93. 'access arguments' => array('access site reports'),
  94. );
  95. }
  96. return $items;
  97. }
  98. /**
  99. * Page callback for munin config fetches.
  100. */
  101. function munin_api_page_config($module) {
  102. $data = module_invoke($module, 'munin_api_config');
  103. if (!is_array($data)) {
  104. return _munin_report_hook_error('munin_api_config', $module);
  105. }
  106. $ret = '';
  107. foreach ($data as $attribute => $value) {
  108. $ret .= $attribute .' = '. $value . PHP_EOL;
  109. }
  110. _munin_api_page_closure($ret);
  111. }
  112. /**
  113. * Page callback for munin data fetches.
  114. */
  115. function munin_api_page_fetch($module) {
  116. $data = module_invoke($module, 'munin_api_fetch');
  117. if (!is_array($data)) {
  118. return _munin_report_hook_error('munin_api_fetch', $module);
  119. }
  120. $ret = '';
  121. foreach ($data as $field => $value) {
  122. $ret .= $module .'.'. $field .' = '. $value . PHP_EOL;
  123. }
  124. _munin_api_page_closure($ret);
  125. }
  126. /**
  127. * Page callback for Munin global report.
  128. *
  129. * @return string
  130. */
  131. function munin_api_page_report_global() {
  132. $items = module_invoke_all('munin_api_info');
  133. $header = array(
  134. t('Module'),
  135. t('Description'),
  136. t('Fields'),
  137. );
  138. $rows = array();
  139. foreach ($items as $name => $item) {
  140. $link_title = $item['#title'] ? $item['#title'] : $name;
  141. $rows[] = array(
  142. l($link_title, 'admin/reports/munin_api/'. $name),
  143. isset($item['#description']) ? $item['#description'] : t('&lt;missing&gt;'),
  144. count(element_children($item)),
  145. );
  146. }
  147. $ret = theme('table', $header, $rows);
  148. return $ret;
  149. }
  150. /**
  151. * Page callback for Munin instance report.
  152. *
  153. * @return string
  154. */
  155. function munin_api_page_report_instance($module) {
  156. $module_info = module_invoke($module, 'munin_api_info');
  157. if (!is_array($module_info) || count($module_info) != 1) {
  158. return _munin_report_hook_error('munin_api_info', $module);
  159. }
  160. $module_info = reset($module_info);
  161. if (!is_array($module_info)) {
  162. return _munin_report_hook_error('munin_api_info', $module);
  163. }
  164. $header = array(t('Name'), t('Description'), t('Type'));
  165. $rows = array();
  166. foreach (element_children($module_info) as $name) {
  167. $rows[] = array(
  168. $name,
  169. isset($module_info[$name]['#title']) ? $module_info[$name]['#title'] : t('&lt;missing&gt;'),
  170. $module_info[$name]['#type'],
  171. );
  172. }
  173. $ret = theme('table', $header, $rows);
  174. $ret .= '<h3>Munin debug (text-only)</h3><p>'
  175. . l(t('Fetch data'), 'munin_api/'. $module)
  176. . ' '
  177. . l(t('Fetch config'), 'munin_api/'. $module .'/config');
  178. return $ret;
  179. }