munin_api.module 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. * TODO Field name should be sanitized by "s/[^A-Za-z0-9_]/_/g"
  14. * @link http://munin-monitoring.org/wiki/notes_on_datasource_names @endlink
  15. */
  16. define('MUNIN_API_COUNTER', 'COUNTER');
  17. define('MUNIN_API_DERIVE', 'DERIVE');
  18. define('MUNIN_API_GAUGE', 'GAUGE');
  19. define('MUNIN_API_ABSOLUTE', 'ABSOLUTE'); // Counters reset upon reading (uncommon)
  20. define('MUNIN_API_DRAW_AREA', 'AREA');
  21. define('MUNIN_API_DRAW_LINE0', 'LINE0'); // Invisible line, but triggers graphs scaling
  22. define('MUNIN_API_DRAW_LINE1', 'LINE1'); // Default on Munin 2.0
  23. define('MUNIN_API_DRAW_LINE2', 'LINE2'); // Default on Munin < 2.0
  24. define('MUNIN_API_DRAW_LINE3', 'LINE3');
  25. define('MUNIN_API_DRAW_STACK', 'STACK');
  26. // Styles below are only supported on Munin >= 1.3.3
  27. define('MUNIN_API_DRAW_LINESTACK1', 'LINESTACK1');
  28. define('MUNIN_API_DRAW_LINESTACK2', 'LINESTACK2');
  29. define('MUNIN_API_DRAW_LINESTACK3', 'LINESTACK3');
  30. define('MUNIN_API_DRAW_AREASTACK', 'AREASTACK');
  31. /**
  32. * Finalize result pages for Munin interactions.
  33. *
  34. * - text content, not HTML
  35. * - non cacheable, even for anonymous users
  36. */
  37. function _munin_api_page_closure($ret) {
  38. drupal_set_header('Content-type: text/plain');
  39. $GLOBALS['conf']['cache'] = CACHE_DISABLED; // prevent page_set_cache() in drupal_page_footer().
  40. print $ret;
  41. drupal_page_footer();
  42. exit();
  43. }
  44. /**
  45. * Display and log an incorrect hook implementation.
  46. *
  47. * @param string $hook
  48. * @param string $module
  49. *
  50. * @return string
  51. */
  52. function _munin_report_hook_error($hook, $module) {
  53. $message = t('Incorrect implementation of hook_!hook() in module @module.');
  54. $params = array(
  55. '!hook' => $hook,
  56. '@module' => $module,
  57. );
  58. drupal_set_message(strtr($message, $params), 'error');
  59. watchdog('munin_api', $message, $params, WATCHDOG_ERROR);
  60. return '<p>'. l(t('Back'), 'admin/reports/munin_api/'. $module) .'</p>';
  61. }
  62. /**
  63. * Menu access callback for Munin config fetches.
  64. *
  65. * TODO: define rules, then code
  66. */
  67. function munin_api_access_config($module) {
  68. return TRUE; // For now, protect at the web server level
  69. }
  70. /**
  71. * Menu access callback for Munin data fetches.
  72. *
  73. * TODO: define rules, then code
  74. */
  75. function munin_api_access_fetch($module) {
  76. return TRUE; // For now, protect at the web server level
  77. }
  78. function munin_api_menu() {
  79. $items = array();
  80. $items['admin/reports/munin_api'] = array(
  81. 'title' => 'Munin',
  82. 'description' => 'Reports about Munin data collectors and their probes',
  83. 'page callback' => 'munin_api_page_report_global',
  84. 'access arguments' => array('access site reports'),
  85. );
  86. $items['admin/reports/munin_api/list'] = array(
  87. 'type' => MENU_DEFAULT_LOCAL_TASK,
  88. 'title' => 'General',
  89. 'weight' => -1,
  90. );
  91. foreach (module_implements('munin_api_info') as $module_name) {
  92. $module = module_invoke($module_name, 'munin_api_info');
  93. $graphs = element_children($module);
  94. $items['admin/reports/munin_api/'. $module_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($module_name, $module),
  100. 'access arguments' => array('access site reports'),
  101. );
  102. foreach ($graphs as $graph_name) {
  103. $items['munin_api/'. $graph_name] = array(
  104. 'type' => MENU_CALLBACK,
  105. 'page callback' => 'munin_api_page_fetch',
  106. 'page arguments' => array($module_name, $module, $graph_name),
  107. 'access callback' => 'munin_api_access_fetch',
  108. 'access arguments' => array($module_name, $graph_name),
  109. );
  110. $items['munin_api/'. $graph_name .'/config'] = array(
  111. 'type' => MENU_CALLBACK,
  112. 'page callback' => 'munin_api_page_config',
  113. 'page arguments' => array($module_name, $graph_name),
  114. 'access callback' => 'munin_api_access_config',
  115. 'access arguments' => array($module_name, $graph_name),
  116. );
  117. }
  118. }
  119. ksort($items);
  120. return $items;
  121. }
  122. /**
  123. * Implements hook_munin_api_info().
  124. *
  125. * @return
  126. * An array of Munin probes informations, index by probe name.
  127. */
  128. function munin_api_munin_api_info() {
  129. $int = array(
  130. '#graph_printf' => "'%d'",
  131. );
  132. $ret = array(
  133. '#title' => t('Munin'),
  134. '#description' => t('Munin monitoring'),
  135. 'munin_munin' => array(
  136. '#title' => t('Munin-node'),
  137. '#info' => t('Monitor the monitoring solution.'),
  138. '#graph_vlabel' => t('Seconds since last probe'),
  139. 'munin_seconds' => $int + array(
  140. '#label' => t('Seconds since last probe'),
  141. '#type' => MUNIN_API_GAUGE,
  142. '#info' => t('Seconds since last probe should hover around 300. 0 means no probe found.'),
  143. ),
  144. ),
  145. );
  146. return $ret;
  147. }
  148. /**
  149. * Implements hook_munin_api_fetch().
  150. */
  151. function munin_api_munin_api_fetch($graph_name, $log = TRUE) {
  152. switch ($graph_name) {
  153. case 'munin_munin':
  154. $request_time = time();
  155. $last = variable_get('munin_api_last', 0);
  156. $ret['munin_seconds'] = $last ? $request_time - $last : 0;
  157. if ($log) {
  158. variable_set('munin_api_last', $request_time);
  159. }
  160. break;
  161. }
  162. return $ret;
  163. }
  164. /**
  165. * Page callback for munin config fetches.
  166. */
  167. function munin_api_page_config($module_name, $graph_name) {
  168. $module_info = module_invoke($module_name, 'munin_api_info');
  169. if (!is_array($module_info)) {
  170. return _munin_report_hook_error('munin_api_info', $module);
  171. }
  172. $info = $module_info[$graph_name];
  173. $config = array(
  174. 'graph_title' => $info['#title'],
  175. 'graph_info' => $info['#info'],
  176. 'graph_category' => 'Drupal',
  177. );
  178. foreach (element_properties($info) as $property_name) {
  179. if (!in_array($property_name, array('#title', '#info'))) {
  180. $config[drupal_substr($property_name, 1)] = $info[$property_name];
  181. }
  182. }
  183. foreach (element_children($info) as $field_name) {
  184. foreach (element_properties($info[$field_name]) as $property_name) {
  185. $config[$field_name .'.'. drupal_substr($property_name, 1)] = $info[$field_name][$property_name];
  186. }
  187. }
  188. $ret = '';
  189. foreach ($config as $k => $v) {
  190. $ret .= $k .' '. $v . PHP_EOL;
  191. }
  192. _munin_api_page_closure($ret);
  193. }
  194. /**
  195. * Page callback for munin data fetches.
  196. */
  197. function munin_api_page_fetch($module_name, $module, $graph_name) {
  198. $data = module_invoke($module_name, 'munin_api_fetch', $graph_name);
  199. if (!is_array($data)) {
  200. return _munin_report_hook_error('munin_api_fetch', $module_name);
  201. }
  202. $ret = '';
  203. foreach ($data as $field => $value) {
  204. $ret .= $field .'.value '. $value . PHP_EOL;
  205. }
  206. _munin_api_page_closure($ret);
  207. }
  208. /**
  209. * Page callback for Munin global report.
  210. *
  211. * @return string
  212. */
  213. function munin_api_page_report_global() {
  214. $header = array(
  215. t('Module'),
  216. t('Graph'),
  217. t('Description'),
  218. t('Fields'),
  219. );
  220. $rows = array();
  221. foreach (module_implements('munin_api_info') as $module_name) {
  222. $info = module_invoke($module_name, 'munin_api_info');
  223. $rows[] = array(
  224. array(
  225. 'data' => l($info['#title'], 'admin/reports/munin_api/'. $module_name),
  226. 'colspan' => 2,
  227. ),
  228. array(
  229. 'colspan' => 2,
  230. 'data' => $info['#description'],
  231. ),
  232. );
  233. foreach (element_children($info) as $name) {
  234. $title = $info[$name]['#title'] ? $info[$name]['#title'] : $name;
  235. $rows[] = array(
  236. '&nbsp;',
  237. $title,
  238. isset($info[$name]['#info']) ? $info[$name]['#info'] : t('&lt;missing&gt;'),
  239. count(element_children($info[$name])),
  240. );
  241. }
  242. }
  243. $ret = theme('table', $header, $rows);
  244. return $ret;
  245. }
  246. /**
  247. * Page callback for Munin instance report.
  248. *
  249. * @return string
  250. */
  251. function munin_api_page_report_instance($module_name, $module_info) {
  252. if (!is_array($module_info)) {
  253. return _munin_report_hook_error('munin_api_info', $module_name);
  254. }
  255. $header = array(
  256. t('Name'),
  257. t('Title / Description'),
  258. t('Type'),
  259. t('Debug'),
  260. );
  261. $error = array('class' => 'error');
  262. $rows = array();
  263. foreach (element_children($module_info) as $graph_name) {
  264. $data = module_invoke($module_name, 'munin_api_fetch', $graph_name, FALSE);
  265. if (!is_array($data)) {
  266. return _munin_report_hook_error('munin_api_fetch', $module_name);
  267. }
  268. $rows[] = array(
  269. array(
  270. 'data' => $module_info[$graph_name]['#title'] ? $module_info[$graph_name]['#title'] : $graph_name,
  271. 'colspan' => 3,
  272. ),
  273. l(t('config'), 'munin_api/'. $graph_name .'/config'),
  274. );
  275. foreach (element_children($module_info[$graph_name]) as $field_name) {
  276. $rows[] = array(
  277. '&nbsp;',
  278. isset($module_info[$graph_name][$field_name]['#label']) ? $module_info[$graph_name][$field_name]['#label'] : t('&lt;missing&gt;'),
  279. $module_info[$graph_name][$field_name]['#type'],
  280. $data[$field_name],
  281. );
  282. unset($data[$field_name]);
  283. }
  284. foreach ($data as $field_name => $field_value) {
  285. $rows[] = array(
  286. '&nbsp;',
  287. $error + array('data' => $field_name),
  288. $error + array('data' => check_plain('<unconfigured>')),
  289. $error + array('data' => $field_value),
  290. );
  291. }
  292. }
  293. $ret = theme('table', $header, $rows);
  294. return $ret;
  295. }