|  | @@ -6,8 +6,60 @@
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  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();
 | 
	
	
		
			
				|  | @@ -28,6 +80,21 @@ function munin_api_menu() {
 | 
	
		
			
				|  |  |    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'],
 | 
	
	
		
			
				|  | @@ -38,11 +105,46 @@ function munin_api_menu() {
 | 
	
		
			
				|  |  |      );
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |    return $items;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  | - * Page callback for Munin report
 | 
	
		
			
				|  |  | + * 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
 | 
	
		
			
				|  |  |   */
 | 
	
	
		
			
				|  | @@ -52,7 +154,7 @@ function munin_api_page_report_global() {
 | 
	
		
			
				|  |  |    $header = array(
 | 
	
		
			
				|  |  |      t('Module'),
 | 
	
		
			
				|  |  |      t('Description'),
 | 
	
		
			
				|  |  | -    t('Stats'),
 | 
	
		
			
				|  |  | +    t('Fields'),
 | 
	
		
			
				|  |  |    );
 | 
	
		
			
				|  |  |    $rows = array();
 | 
	
		
			
				|  |  |    foreach ($items as $name => $item) {
 | 
	
	
		
			
				|  | @@ -67,20 +169,35 @@ function munin_api_page_report_global() {
 | 
	
		
			
				|  |  |    return $ret;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * Page callback for Munin instance report.
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @return string
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  |  function munin_api_page_report_instance($module) {
 | 
	
		
			
				|  |  | -  $module = module_invoke($module, 'munin_api_info');
 | 
	
		
			
				|  |  | -  $module = reset($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) as $name) {
 | 
	
		
			
				|  |  | +  foreach (element_children($module_info) as $name) {
 | 
	
		
			
				|  |  |      $rows[] = array(
 | 
	
		
			
				|  |  |        $name,
 | 
	
		
			
				|  |  | -      isset($module[$name]['#title']) ? $module[$name]['#title'] : t('<missing>'),
 | 
	
		
			
				|  |  | -      $module[$name]['#type'],
 | 
	
		
			
				|  |  | +      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;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 |