123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- // $Id$
- /**
- * @file
- * An API to help modules expose their instrumentation to Munin.
- */
- 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();
- $items['admin/reports/munin_api'] = array(
- 'title' => 'Munin',
- 'description' => 'Reports about Munin data collectors and their probes',
- 'page callback' => 'munin_api_page_report_global',
- 'access arguments' => array('access site reports'),
- );
- $items['admin/reports/munin_api/list'] = array(
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'title' => 'General',
- 'weight' => -1,
- );
- 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'],
- 'description' => $module['#description'],
- 'page callback' => 'munin_api_page_report_instance',
- 'page arguments' => array(3),
- 'access arguments' => array('access site reports'),
- );
- }
- return $items;
- }
- /**
- * 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
- */
- function munin_api_page_report_global() {
- $items = module_invoke_all('munin_api_info');
- $header = array(
- t('Module'),
- t('Description'),
- t('Fields'),
- );
- $rows = array();
- foreach ($items as $name => $item) {
- $link_title = $item['#title'] ? $item['#title'] : $name;
- $rows[] = array(
- l($link_title, 'admin/reports/munin_api/'. $name),
- isset($item['#description']) ? $item['#description'] : t('<missing>'),
- count(element_children($item)),
- );
- }
- $ret = theme('table', $header, $rows);
- return $ret;
- }
- /**
- * Page callback for Munin instance report.
- *
- * @return string
- */
- function munin_api_page_report_instance($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_info) as $name) {
- $rows[] = array(
- $name,
- isset($module_info[$name]['#title']) ? $module_info[$name]['#title'] : t('<missing>'),
- $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;
- }
|