munin_api.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. // Features server for plugin stats
  32. define('MUNIN_API_SERVER_HOST', 'http://features.osinet.eu/');
  33. define('MUNIN_API_SERVER_ENDPOINT', 'xmlrpc.php');
  34. /**
  35. * Finalize result pages for Munin interactions.
  36. *
  37. * - text content, not HTML
  38. * - non cacheable, even for anonymous users
  39. */
  40. function _munin_api_page_closure($ret) {
  41. drupal_set_header('Content-type: text/plain');
  42. $GLOBALS['conf']['cache'] = CACHE_DISABLED; // prevent page_set_cache() in drupal_page_footer().
  43. print $ret;
  44. drupal_page_footer();
  45. exit();
  46. }
  47. /**
  48. * Build the site information to report on the plugin popularity page.
  49. *
  50. * The site key is a reasonably unique host identifier obtained without
  51. * divulging private info. This is the same method as in modules/update.module
  52. *
  53. * @see _update_refresh()
  54. *
  55. * @return array
  56. */
  57. function _munin_api_get_report_info() {
  58. $module = 'munin_api';
  59. $module_info = drupal_parse_info_file(drupal_get_path('module', $module) .'/'. $module .'.info');
  60. $plugins = module_implements('munin_api_info');
  61. $hidden = array($module);
  62. foreach ($plugins as $plugin_name) {
  63. $plugin_info = module_invoke($plugin_name, 'munin_api_info');
  64. if (!empty($plugin_info['#private'])) {
  65. $hidden[] = $plugin_name;
  66. }
  67. }
  68. $reported = array_diff($plugins, $hidden);
  69. $ret = array(
  70. 'module' => $module,
  71. 'datestamp' => check_plain($module_info['datestamp']),
  72. 'plugins' => $reported,
  73. 'site-key' => md5($GLOBALS['base_url'] . drupal_get_private_key()),
  74. 'version' => check_plain($module_info['version']),
  75. );
  76. return $ret;
  77. }
  78. /**
  79. * Display and log an incorrect hook implementation.
  80. *
  81. * @param string $hook
  82. * @param string $module
  83. *
  84. * @return string
  85. */
  86. function _munin_report_hook_error($hook, $module) {
  87. $message = t('Incorrect implementation of hook_!hook() in module @module.');
  88. $params = array(
  89. '!hook' => $hook,
  90. '@module' => $module,
  91. );
  92. drupal_set_message(strtr($message, $params), 'error');
  93. watchdog('munin_api', $message, $params, WATCHDOG_ERROR);
  94. return '<p>'. l(t('Back'), 'admin/reports/munin_api/'. $module) .'</p>';
  95. }
  96. /**
  97. * Menu access callback for Munin config fetches.
  98. *
  99. * TODO: define rules, then code
  100. */
  101. function munin_api_access_config($module) {
  102. return TRUE; // For now, protect at the web server level
  103. }
  104. /**
  105. * Menu access callback for Munin data fetches.
  106. *
  107. * TODO: define rules, then code
  108. */
  109. function munin_api_access_fetch($module) {
  110. return TRUE; // For now, protect at the web server level
  111. }
  112. function munin_api_admin_settings($form_state) {
  113. $default_path = variable_get('munin_api_init_path', '/^$/');
  114. $form = array();
  115. // Add the fade-in/out effects
  116. drupal_add_js(drupal_get_path('module', 'munin_api') .'/munin_api.js');
  117. drupal_add_js(array(
  118. 'munin_api' => array(
  119. 'path' => $default_path,
  120. )), 'setting');
  121. $form['watchdog'] = array(
  122. '#type' => 'fieldset',
  123. '#title' => t('Munin Watchdog'),
  124. '#description' => t('Drupal-level monitoring of Munin'),
  125. );
  126. $form['watchdog']['munin_api_watchdog'] = array(
  127. '#description' => t('If enabled, the module will regularly make sure the time interval between Munin probes is within the expected range and raise watchdog alerts accordingly. "Cron" mode is recommended. "Page init" mode should only be used on pages with a steady occurrence rate below 5 minutes but not too high, and will not work properly for anonymous users if caching mode is set to "aggressive" or "external" (Pressflow). This mechanism is most useful when watchdog is implemented by syslog, not dblog.'),
  128. '#type' => 'radios',
  129. '#options' => array(
  130. 'none' => t('Disabled'),
  131. 'cron' => t('On cron runs'),
  132. 'init' => t('On page init'),
  133. ),
  134. '#default_value' => variable_get('munin_api_watchdog', 'cron'),
  135. );
  136. $form['watchdog']['munin_api_init_path'] = array(
  137. '#title' => t('Init path'),
  138. '#description' => t('For page init watchdog mode, specify the regular expression for the paths that should trigger a watchdog check'),
  139. '#type' => 'textfield',
  140. '#default_value' => $default_path,
  141. );
  142. $form['reporting'] = array(
  143. '#type' => 'fieldset',
  144. '#title' => t('Module reporting'),
  145. '#description' => t('Information reported by this module to the OSInet Features Server'),
  146. );
  147. $form['reporting']['munin_api_next_report'] = array(
  148. '#type' => 'checkbox',
  149. '#title' => t('Report usage stats'),
  150. '#default_value' => variable_get('munin_api_next_report', 0),
  151. '#return_value' => time(),
  152. '#description' => t('Opting in for reporting provides anonymous usage data about Munin plugins.'),
  153. );
  154. $info = _munin_api_get_report_info();
  155. $header = array(
  156. t('Property'),
  157. t('Value'),
  158. t('Explanation'),
  159. );
  160. $rows = array(
  161. array(
  162. 'module',
  163. $info['module'],
  164. t('The project name of this module'),
  165. ),
  166. array(
  167. 'version',
  168. $info['version'],
  169. t('The official version of this module'),
  170. ),
  171. array(
  172. 'datestamp',
  173. $info['datestamp'],
  174. t('Consistency check for the version'),
  175. ),
  176. array(
  177. 'site-key',
  178. $info['site-key'],
  179. t('Your site unique key. This is the same identifier you are sending to drupal.org via update.module: it provides a reasonably unique identifier to count sites without reporting any identifiable information.'),
  180. ),
  181. array(
  182. 'plugins',
  183. theme('item_list', $info['plugins']),
  184. t('This shows which are the most popular plugins. Plugins with the #private attribute are considered private and not reported here.'),
  185. ),
  186. );
  187. $form['reporting']['info'] = array(
  188. '#value' => theme('table', $header, $rows, array('class' => 'munin-api-report')),
  189. );
  190. $form = system_settings_form($form);
  191. return $form;
  192. }
  193. /**
  194. * Implements hook_cron().
  195. */
  196. function munin_api_cron() {
  197. // Is the Munin-node watchdog enabled in cron mode ?
  198. if (variable_get('munin_api_watchdog', 'cron') == 'cron') {
  199. _munin_api_watchdog_munin();
  200. }
  201. // Has the admin opted in for the usage report ?
  202. if ($next = variable_get('munin_api_next_report', 0)) {
  203. $now = time();
  204. if ($now > $next) {
  205. $link = l(t('Browse server'), MUNIN_API_SERVER_HOST, array('external' => TRUE));
  206. $ret = xmlrpc(MUNIN_API_SERVER_HOST . MUNIN_API_SERVER_ENDPOINT,
  207. 'ofe.usage', _munin_api_get_report_info());
  208. if ($ret === FALSE) {
  209. watchdog('munin_api', 'XML-RPC error @errno: @message', array(
  210. '@errno' => xmlrpc_errno(),
  211. '@message' => xmlrpc_error_msg(),
  212. ), WATCHDOG_NOTICE, $link);
  213. }
  214. else {
  215. watchdog('munin_api', 'Plugin usage statistics uploaded', NULL,
  216. WATCHDOG_INFO, $link);
  217. // Set next update from current value of next, not from current time,
  218. // to avoid slow shifts over time
  219. variable_set('munin_api_next_report', $next + 604800); // 7 days
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Implements hook_init().
  226. *
  227. * Only trigger Munin API watchdog on selected pages.
  228. */
  229. function munin_api_init() {
  230. if (variable_get('munin_api_watchdog', 'cron') == 'init') {
  231. $path = $_GET['q'];
  232. $regex = variable_get('munin_api_init_path', '/^$/');
  233. $sts = preg_match($regex, $path);
  234. if ($sts) {
  235. _munin_api_watchdog_munin();
  236. }
  237. }
  238. }
  239. function munin_api_menu() {
  240. $items = array();
  241. $items['admin/settings/munin_api'] = array(
  242. 'title' => 'Munin',
  243. 'description' => 'Munin API settings',
  244. 'page callback' => 'drupal_get_form',
  245. 'page arguments' => array('munin_api_admin_settings'),
  246. 'access arguments' => array('administer site configuration'),
  247. );
  248. $items['admin/reports/munin_api'] = array(
  249. 'title' => 'Munin',
  250. 'description' => 'Reports about Munin data collectors and their probes',
  251. 'page callback' => 'munin_api_page_report_global',
  252. 'access arguments' => array('access site reports'),
  253. );
  254. $items['admin/reports/munin_api/list'] = array(
  255. 'type' => MENU_DEFAULT_LOCAL_TASK,
  256. 'title' => 'General',
  257. 'weight' => -1,
  258. );
  259. foreach (module_implements('munin_api_info') as $module_name) {
  260. $module = module_invoke($module_name, 'munin_api_info');
  261. $graphs = element_children($module);
  262. $items['admin/reports/munin_api/'. $module_name] = array(
  263. 'type' => MENU_LOCAL_TASK,
  264. 'title' => $module['#title'],
  265. 'description' => $module['#description'],
  266. 'page callback' => 'munin_api_page_report_instance',
  267. 'page arguments' => array($module_name, $module),
  268. 'access arguments' => array('access site reports'),
  269. );
  270. foreach ($graphs as $graph_name) {
  271. $items['munin_api/'. $graph_name] = array(
  272. 'type' => MENU_CALLBACK,
  273. 'page callback' => 'munin_api_page_fetch',
  274. 'page arguments' => array($module_name, $module, $graph_name),
  275. 'access callback' => 'munin_api_access_fetch',
  276. 'access arguments' => array($module_name, $graph_name),
  277. );
  278. $items['munin_api/'. $graph_name .'/config'] = array(
  279. 'type' => MENU_CALLBACK,
  280. 'page callback' => 'munin_api_page_config',
  281. 'page arguments' => array($module_name, $graph_name),
  282. 'access callback' => 'munin_api_access_config',
  283. 'access arguments' => array($module_name, $graph_name),
  284. );
  285. }
  286. }
  287. ksort($items);
  288. return $items;
  289. }
  290. /**
  291. * Implements hook_munin_api_info().
  292. *
  293. * @return
  294. * An array of Munin probes informations, index by probe name.
  295. */
  296. function munin_api_munin_api_info() {
  297. $int = array(
  298. '#graph_printf' => "'%d'",
  299. );
  300. $ret = array(
  301. '#title' => t('Munin'),
  302. '#description' => t('Munin monitoring'),
  303. 'munin_munin' => array(
  304. '#title' => t('Munin-node'),
  305. '#info' => t('Monitor the monitoring solution.'),
  306. '#graph_vlabel' => t('Seconds since last probe'),
  307. 'munin_seconds' => $int + array(
  308. '#label' => t('Seconds since last probe'),
  309. '#type' => MUNIN_API_GAUGE,
  310. '#info' => t('Seconds since last probe should hover around 300. 0 means no probe found.'),
  311. '#warning' => '290:310',
  312. '#critical' => '1:600',
  313. ),
  314. ),
  315. );
  316. return $ret;
  317. }
  318. /**
  319. * Implements hook_munin_api_fetch().
  320. */
  321. function munin_api_munin_api_fetch($graph_name, $log = TRUE) {
  322. switch ($graph_name) {
  323. case 'munin_munin':
  324. $request_time = time();
  325. $last = variable_get('munin_api_last', 0);
  326. $ret['munin_seconds'] = $last ? $request_time - $last : 0;
  327. if ($log) {
  328. variable_set('munin_api_last', $request_time);
  329. }
  330. break;
  331. }
  332. return $ret;
  333. }
  334. /**
  335. * Page callback for munin config fetches.
  336. */
  337. function munin_api_page_config($module_name, $graph_name) {
  338. $module_info = module_invoke($module_name, 'munin_api_info');
  339. if (!is_array($module_info)) {
  340. return _munin_report_hook_error('munin_api_info', $module);
  341. }
  342. $info = $module_info[$graph_name];
  343. $config = array(
  344. 'graph_title' => $info['#title'],
  345. 'graph_info' => $info['#info'],
  346. 'graph_category' => 'Drupal',
  347. );
  348. foreach (element_properties($info) as $property_name) {
  349. if (!in_array($property_name, array('#title', '#info'))) {
  350. $config[drupal_substr($property_name, 1)] = $info[$property_name];
  351. }
  352. }
  353. foreach (element_children($info) as $field_name) {
  354. foreach (element_properties($info[$field_name]) as $property_name) {
  355. $config[$field_name .'.'. drupal_substr($property_name, 1)] = $info[$field_name][$property_name];
  356. }
  357. }
  358. $ret = '';
  359. foreach ($config as $k => $v) {
  360. $ret .= $k .' '. $v . PHP_EOL;
  361. }
  362. _munin_api_page_closure($ret);
  363. }
  364. /**
  365. * Page callback for munin data fetches.
  366. */
  367. function munin_api_page_fetch($module_name, $module, $graph_name) {
  368. $data = module_invoke($module_name, 'munin_api_fetch', $graph_name);
  369. if (!is_array($data)) {
  370. return _munin_report_hook_error('munin_api_fetch', $module_name);
  371. }
  372. $ret = '';
  373. foreach ($data as $field => $value) {
  374. $ret .= $field .'.value '. $value . PHP_EOL;
  375. }
  376. _munin_api_page_closure($ret);
  377. }
  378. /**
  379. * Page callback for Munin global report.
  380. *
  381. * @return string
  382. */
  383. function munin_api_page_report_global() {
  384. $header = array(
  385. t('Module'),
  386. t('Graph'),
  387. t('Description'),
  388. t('Fields'),
  389. );
  390. $rows = array();
  391. foreach (module_implements('munin_api_info') as $module_name) {
  392. $info = module_invoke($module_name, 'munin_api_info');
  393. $rows[] = array(
  394. array(
  395. 'data' => l($info['#title'], 'admin/reports/munin_api/'. $module_name),
  396. 'colspan' => 2,
  397. ),
  398. array(
  399. 'colspan' => 2,
  400. 'data' => $info['#description'],
  401. ),
  402. );
  403. foreach (element_children($info) as $name) {
  404. $title = $info[$name]['#title'] ? $info[$name]['#title'] : $name;
  405. $rows[] = array(
  406. '&nbsp;',
  407. $title,
  408. isset($info[$name]['#info']) ? $info[$name]['#info'] : t('&lt;missing&gt;'),
  409. count(element_children($info[$name])),
  410. );
  411. }
  412. }
  413. $ret = theme('table', $header, $rows);
  414. return $ret;
  415. }
  416. /**
  417. * Page callback for Munin instance report.
  418. *
  419. * @return string
  420. */
  421. function munin_api_page_report_instance($module_name, $module_info) {
  422. if (!is_array($module_info)) {
  423. return _munin_report_hook_error('munin_api_info', $module_name);
  424. }
  425. $header = array(
  426. t('Name'),
  427. t('Title / Description'),
  428. t('Type'),
  429. t('Debug'),
  430. );
  431. $error = array('class' => 'error');
  432. $rows = array();
  433. foreach (element_children($module_info) as $graph_name) {
  434. $data = module_invoke($module_name, 'munin_api_fetch', $graph_name, FALSE);
  435. if (!is_array($data)) {
  436. return _munin_report_hook_error('munin_api_fetch', $module_name);
  437. }
  438. $rows[] = array(
  439. array(
  440. 'data' => $module_info[$graph_name]['#title'] ? $module_info[$graph_name]['#title'] : $graph_name,
  441. 'colspan' => 3,
  442. ),
  443. l(t('config'), 'munin_api/'. $graph_name .'/config'),
  444. );
  445. foreach (element_children($module_info[$graph_name]) as $field_name) {
  446. $rows[] = array(
  447. '&nbsp;',
  448. isset($module_info[$graph_name][$field_name]['#label']) ? $module_info[$graph_name][$field_name]['#label'] : t('&lt;missing&gt;'),
  449. $module_info[$graph_name][$field_name]['#type'],
  450. $data[$field_name],
  451. );
  452. unset($data[$field_name]);
  453. }
  454. foreach ($data as $field_name => $field_value) {
  455. $rows[] = array(
  456. '&nbsp;',
  457. $error + array('data' => $field_name),
  458. $error + array('data' => check_plain('<unconfigured>')),
  459. $error + array('data' => $field_value),
  460. );
  461. }
  462. }
  463. $ret = theme('table', $header, $rows);
  464. return $ret;
  465. }
  466. /**
  467. * Munin API watchdog implementation.
  468. *
  469. * @return void
  470. */
  471. function _munin_api_watchdog_munin() {
  472. $seconds = munin_api_munin_api_fetch('munin_munin', FALSE);
  473. $seconds = reset($seconds);
  474. $info = munin_api_munin_api_info();
  475. $info = $info['munin_munin']['munin_seconds'];
  476. $warning = explode(':', $info['#warning']);
  477. $critical = explode(':', $info['#critical']);
  478. if (/* $seconds < $critical[0] || */ $seconds > $critical[1]) {
  479. $level = WATCHDOG_CRITICAL;
  480. }
  481. elseif (/* $seconds < $warning[0] || */ $seconds > $warning[1]) {
  482. $level = WATCHDOG_WARNING;
  483. }
  484. else {
  485. $level = WATCHDOG_DEBUG;
  486. }
  487. if ($level < WATCHDOG_DEBUG) { // meaning MORE urgent
  488. watchdog('munin_api', 'Munin last probe came @last seconds ago.', array(
  489. '@last' => $seconds,
  490. ), $level);
  491. }
  492. }