123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- <?php
- // $Id$
- /**
- * @file
- * Munin API for Drupal.
- *
- * @author Frederic G. MARAND
- *
- * @copyright (c) 2011 Ouest Systèmes Informatiques
- *
- * Licensed under the General Public License version 2 or later.
- *
- * TODO Field name should be sanitized by "s/[^A-Za-z0-9_]/_/g"
- * @link http://munin-monitoring.org/wiki/notes_on_datasource_names @endlink
- */
- define('MUNIN_API_COUNTER', 'COUNTER');
- define('MUNIN_API_DERIVE', 'DERIVE');
- define('MUNIN_API_GAUGE', 'GAUGE');
- define('MUNIN_API_ABSOLUTE', 'ABSOLUTE'); // Counters reset upon reading (uncommon)
- define('MUNIN_API_DRAW_AREA', 'AREA');
- define('MUNIN_API_DRAW_LINE0', 'LINE0'); // Invisible line, but triggers graphs scaling
- define('MUNIN_API_DRAW_LINE1', 'LINE1'); // Default on Munin 2.0
- define('MUNIN_API_DRAW_LINE2', 'LINE2'); // Default on Munin < 2.0
- define('MUNIN_API_DRAW_LINE3', 'LINE3');
- define('MUNIN_API_DRAW_STACK', 'STACK');
- // Styles below are only supported on Munin >= 1.3.3
- define('MUNIN_API_DRAW_LINESTACK1', 'LINESTACK1');
- define('MUNIN_API_DRAW_LINESTACK2', 'LINESTACK2');
- define('MUNIN_API_DRAW_LINESTACK3', 'LINESTACK3');
- define('MUNIN_API_DRAW_AREASTACK', 'AREASTACK');
- // Features server for plugin stats
- define('MUNIN_API_SERVER_HOST', 'http://features.osinet.eu/');
- define('MUNIN_API_SERVER_ENDPOINT', 'xmlrpc.php');
- /**
- * 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();
- }
- /**
- * Build the site information to report on the plugin popularity page.
- *
- * The site key is a reasonably unique host identifier obtained without
- * divulging private info. This is the same method as in modules/update.module
- *
- * @see _update_refresh()
- *
- * @return array
- */
- function _munin_api_get_report_info() {
- $module = 'munin_api';
- $module_info = drupal_parse_info_file(drupal_get_path('module', $module) .'/'. $module .'.info');
- $plugins = module_implements('munin_api_info');
- $hidden = array($module);
- foreach ($plugins as $plugin_name) {
- $plugin_info = module_invoke($plugin_name, 'munin_api_info');
- if (!empty($plugin_info['#private'])) {
- $hidden[] = $plugin_name;
- }
- }
- $reported = array_diff($plugins, $hidden);
- $ret = array(
- 'module' => $module,
- 'datestamp' => check_plain($module_info['datestamp']),
- 'plugins' => $reported,
- 'site-key' => md5($GLOBALS['base_url'] . drupal_get_private_key()),
- 'version' => check_plain($module_info['version']),
- );
- return $ret;
- }
- /**
- * 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_admin_settings($form_state) {
- $default_path = variable_get('munin_api_init_path', '/^$/');
- $form = array();
- // Add the fade-in/out effects
- drupal_add_js(drupal_get_path('module', 'munin_api') .'/munin_api.js');
- drupal_add_js(array(
- 'munin_api' => array(
- 'path' => $default_path,
- )), 'setting');
- $form['watchdog'] = array(
- '#type' => 'fieldset',
- '#title' => t('Munin Watchdog'),
- '#description' => t('Drupal-level monitoring of Munin'),
- );
- $form['watchdog']['munin_api_watchdog'] = array(
- '#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.'),
- '#type' => 'radios',
- '#options' => array(
- 'none' => t('Disabled'),
- 'cron' => t('On cron runs'),
- 'init' => t('On page init'),
- ),
- '#default_value' => variable_get('munin_api_watchdog', 'cron'),
- );
- $form['watchdog']['munin_api_init_path'] = array(
- '#title' => t('Init path'),
- '#description' => t('For page init watchdog mode, specify the regular expression for the paths that should trigger a watchdog check'),
- '#type' => 'textfield',
- '#default_value' => $default_path,
- );
- $form['reporting'] = array(
- '#type' => 'fieldset',
- '#title' => t('Module reporting'),
- '#description' => t('Information reported by this module to the OSInet Features Server'),
- );
- $form['reporting']['munin_api_next_report'] = array(
- '#type' => 'checkbox',
- '#title' => t('Report usage stats'),
- '#default_value' => variable_get('munin_api_next_report', 0),
- '#return_value' => time(),
- '#description' => t('Opting in for reporting provides anonymous usage data about Munin plugins.'),
- );
- $info = _munin_api_get_report_info();
- $header = array(
- t('Property'),
- t('Value'),
- t('Explanation'),
- );
- $rows = array(
- array(
- 'module',
- $info['module'],
- t('The project name of this module'),
- ),
- array(
- 'version',
- $info['version'],
- t('The official version of this module'),
- ),
- array(
- 'datestamp',
- $info['datestamp'],
- t('Consistency check for the version'),
- ),
- array(
- 'site-key',
- $info['site-key'],
- 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.'),
- ),
- array(
- 'plugins',
- theme('item_list', $info['plugins']),
- t('This shows which are the most popular plugins. Plugins with the #private attribute are considered private and not reported here.'),
- ),
- );
- $form['reporting']['info'] = array(
- '#value' => theme('table', $header, $rows, array('class' => 'munin-api-report')),
- );
- $form = system_settings_form($form);
- return $form;
- }
- /**
- * Implements hook_cron().
- */
- function munin_api_cron() {
- // Is the Munin-node watchdog enabled in cron mode ?
- if (variable_get('munin_api_watchdog', 'cron') == 'cron') {
- _munin_api_watchdog_munin();
- }
- // Has the admin opted in for the usage report ?
- if ($next = variable_get('munin_api_next_report', 0)) {
- $now = time();
- if ($now > $next) {
- $link = l(t('Browse server'), MUNIN_API_SERVER_HOST, array('external' => TRUE));
- $ret = xmlrpc(MUNIN_API_SERVER_HOST . MUNIN_API_SERVER_ENDPOINT,
- 'ofe.usage', _munin_api_get_report_info());
- if ($ret === FALSE) {
- watchdog('munin_api', 'XML-RPC error @errno: @message', array(
- '@errno' => xmlrpc_errno(),
- '@message' => xmlrpc_error_msg(),
- ), WATCHDOG_NOTICE, $link);
- }
- else {
- watchdog('munin_api', 'Plugin usage statistics uploaded', NULL,
- WATCHDOG_INFO, $link);
- // Set next update from current value of next, not from current time,
- // to avoid slow shifts over time
- variable_set('munin_api_next_report', $next + 604800); // 7 days
- }
- }
- }
- }
- /**
- * Implements hook_init().
- *
- * Only trigger Munin API watchdog on selected pages.
- */
- function munin_api_init() {
- if (variable_get('munin_api_watchdog', 'cron') == 'init') {
- $path = $_GET['q'];
- $regex = variable_get('munin_api_init_path', '/^$/');
- $sts = preg_match($regex, $path);
- if ($sts) {
- _munin_api_watchdog_munin();
- }
- }
- }
- function munin_api_menu() {
- $items = array();
- $items['admin/settings/munin_api'] = array(
- 'title' => 'Munin',
- 'description' => 'Munin API settings',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('munin_api_admin_settings'),
- 'access arguments' => array('administer site configuration'),
- );
- $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 $module_name) {
- $module = module_invoke($module_name, 'munin_api_info');
- $graphs = element_children($module);
- $items['admin/reports/munin_api/'. $module_name] = array(
- 'type' => MENU_LOCAL_TASK,
- 'title' => $module['#title'],
- 'description' => $module['#description'],
- 'page callback' => 'munin_api_page_report_instance',
- 'page arguments' => array($module_name, $module),
- 'access arguments' => array('access site reports'),
- );
- foreach ($graphs as $graph_name) {
- $items['munin_api/'. $graph_name] = array(
- 'type' => MENU_CALLBACK,
- 'page callback' => 'munin_api_page_fetch',
- 'page arguments' => array($module_name, $module, $graph_name),
- 'access callback' => 'munin_api_access_fetch',
- 'access arguments' => array($module_name, $graph_name),
- );
- $items['munin_api/'. $graph_name .'/config'] = array(
- 'type' => MENU_CALLBACK,
- 'page callback' => 'munin_api_page_config',
- 'page arguments' => array($module_name, $graph_name),
- 'access callback' => 'munin_api_access_config',
- 'access arguments' => array($module_name, $graph_name),
- );
- }
- }
- ksort($items);
- return $items;
- }
- /**
- * Implements hook_munin_api_info().
- *
- * @return
- * An array of Munin probes informations, index by probe name.
- */
- function munin_api_munin_api_info() {
- $int = array(
- '#graph_printf' => "'%d'",
- );
- $ret = array(
- '#title' => t('Munin'),
- '#description' => t('Munin monitoring'),
- 'munin_munin' => array(
- '#title' => t('Munin-node'),
- '#info' => t('Monitor the monitoring solution.'),
- '#graph_vlabel' => t('Seconds since last probe'),
- 'munin_seconds' => $int + array(
- '#label' => t('Seconds since last probe'),
- '#type' => MUNIN_API_GAUGE,
- '#info' => t('Seconds since last probe should hover around 300. 0 means no probe found.'),
- '#warning' => '290:310',
- '#critical' => '1:600',
- ),
- ),
- );
- return $ret;
- }
- /**
- * Implements hook_munin_api_fetch().
- */
- function munin_api_munin_api_fetch($graph_name, $log = TRUE) {
- switch ($graph_name) {
- case 'munin_munin':
- $request_time = time();
- $last = variable_get('munin_api_last', 0);
- $ret['munin_seconds'] = $last ? $request_time - $last : 0;
- if ($log) {
- variable_set('munin_api_last', $request_time);
- }
- break;
- }
- return $ret;
- }
- /**
- * Page callback for munin config fetches.
- */
- function munin_api_page_config($module_name, $graph_name) {
- $module_info = module_invoke($module_name, 'munin_api_info');
- if (!is_array($module_info)) {
- return _munin_report_hook_error('munin_api_info', $module);
- }
- $info = $module_info[$graph_name];
- $config = array(
- 'graph_title' => $info['#title'],
- 'graph_info' => $info['#info'],
- 'graph_category' => 'Drupal',
- );
- foreach (element_properties($info) as $property_name) {
- if (!in_array($property_name, array('#title', '#info'))) {
- $config[drupal_substr($property_name, 1)] = $info[$property_name];
- }
- }
- foreach (element_children($info) as $field_name) {
- foreach (element_properties($info[$field_name]) as $property_name) {
- $config[$field_name .'.'. drupal_substr($property_name, 1)] = $info[$field_name][$property_name];
- }
- }
- $ret = '';
- foreach ($config as $k => $v) {
- $ret .= $k .' '. $v . PHP_EOL;
- }
- _munin_api_page_closure($ret);
- }
- /**
- * Page callback for munin data fetches.
- */
- function munin_api_page_fetch($module_name, $module, $graph_name) {
- $data = module_invoke($module_name, 'munin_api_fetch', $graph_name);
- if (!is_array($data)) {
- return _munin_report_hook_error('munin_api_fetch', $module_name);
- }
- $ret = '';
- foreach ($data as $field => $value) {
- $ret .= $field .'.value '. $value . PHP_EOL;
- }
- _munin_api_page_closure($ret);
- }
- /**
- * Page callback for Munin global report.
- *
- * @return string
- */
- function munin_api_page_report_global() {
- $header = array(
- t('Module'),
- t('Graph'),
- t('Description'),
- t('Fields'),
- );
- $rows = array();
- foreach (module_implements('munin_api_info') as $module_name) {
- $info = module_invoke($module_name, 'munin_api_info');
- $rows[] = array(
- array(
- 'data' => l($info['#title'], 'admin/reports/munin_api/'. $module_name),
- 'colspan' => 2,
- ),
- array(
- 'colspan' => 2,
- 'data' => $info['#description'],
- ),
- );
- foreach (element_children($info) as $name) {
- $title = $info[$name]['#title'] ? $info[$name]['#title'] : $name;
- $rows[] = array(
- ' ',
- $title,
- isset($info[$name]['#info']) ? $info[$name]['#info'] : t('<missing>'),
- count(element_children($info[$name])),
- );
- }
- }
- $ret = theme('table', $header, $rows);
- return $ret;
- }
- /**
- * Page callback for Munin instance report.
- *
- * @return string
- */
- function munin_api_page_report_instance($module_name, $module_info) {
- if (!is_array($module_info)) {
- return _munin_report_hook_error('munin_api_info', $module_name);
- }
- $header = array(
- t('Name'),
- t('Title / Description'),
- t('Type'),
- t('Debug'),
- );
- $error = array('class' => 'error');
- $rows = array();
- foreach (element_children($module_info) as $graph_name) {
- $data = module_invoke($module_name, 'munin_api_fetch', $graph_name, FALSE);
- if (!is_array($data)) {
- return _munin_report_hook_error('munin_api_fetch', $module_name);
- }
- $rows[] = array(
- array(
- 'data' => $module_info[$graph_name]['#title'] ? $module_info[$graph_name]['#title'] : $graph_name,
- 'colspan' => 3,
- ),
- l(t('config'), 'munin_api/'. $graph_name .'/config'),
- );
- foreach (element_children($module_info[$graph_name]) as $field_name) {
- $rows[] = array(
- ' ',
- isset($module_info[$graph_name][$field_name]['#label']) ? $module_info[$graph_name][$field_name]['#label'] : t('<missing>'),
- $module_info[$graph_name][$field_name]['#type'],
- $data[$field_name],
- );
- unset($data[$field_name]);
- }
- foreach ($data as $field_name => $field_value) {
- $rows[] = array(
- ' ',
- $error + array('data' => $field_name),
- $error + array('data' => check_plain('<unconfigured>')),
- $error + array('data' => $field_value),
- );
- }
- }
- $ret = theme('table', $header, $rows);
- return $ret;
- }
- /**
- * Munin API watchdog implementation.
- *
- * @return void
- */
- function _munin_api_watchdog_munin() {
- $seconds = munin_api_munin_api_fetch('munin_munin', FALSE);
- $seconds = reset($seconds);
- $info = munin_api_munin_api_info();
- $info = $info['munin_munin']['munin_seconds'];
- $warning = explode(':', $info['#warning']);
- $critical = explode(':', $info['#critical']);
- if (/* $seconds < $critical[0] || */ $seconds > $critical[1]) {
- $level = WATCHDOG_CRITICAL;
- }
- elseif (/* $seconds < $warning[0] || */ $seconds > $warning[1]) {
- $level = WATCHDOG_WARNING;
- }
- else {
- $level = WATCHDOG_DEBUG;
- }
- if ($level < WATCHDOG_DEBUG) { // meaning MORE urgent
- watchdog('munin_api', 'Munin last probe came @last seconds ago.', array(
- '@last' => $seconds,
- ), $level);
- }
- }
|