munin_api.module 5.9 KB

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