diff --git a/munin.css b/munin.css
deleted file mode 100644
index 9a4e84a..0000000
--- a/munin.css
+++ /dev/null
@@ -1,6 +0,0 @@
-div .munin_script {
-  background-color: #000;
-  color: #fff;
-  border: 1px solid #EEE;
-  padding: 5px;
-}
diff --git a/munin_api.admin.inc b/munin_api.admin.inc
new file mode 100644
index 0000000..66dd105
--- /dev/null
+++ b/munin_api.admin.inc
@@ -0,0 +1,276 @@
+<?php
+/**
+ * @file
+ *  Munin API Admin forms.
+ */
+
+
+/**
+ * Settings form.
+ *
+ * @return array
+ *  System settings form array.
+ */
+function munin_api_settings_form() {
+  $form = array();
+
+  // Helper.
+  $form['help'] = array(
+    '#value' => '<p>' . t('Save this form, then go to the !plugins tab to select and configure your plugins.',
+        array('!plugins' => l(t('Plugins'), 'admin/settings/munin/plugins'))) . '</p>',
+  );
+
+  // General settings.
+  $form['general'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('General'),
+  );
+  $form['general'][MUNIN_API_VAR_CATEGORY] = array(
+    '#type' => 'textfield',
+    '#title' => t('The category name for the site'),
+    '#default_value' => variable_get(MUNIN_API_VAR_CATEGORY, ''),
+    '#required' => FALSE,
+    '#description' => t('Leave this blank to use the absolute URL to the root of the site. Recommended.'),
+  );
+  $form['general'][MUNIN_API_VAR_TIMEFRAME] = array(
+    '#type' => 'textfield',
+    '#title' => t('The timeframe used for delta graphs measuring change over time.'),
+    '#default_value' => variable_get(MUNIN_API_VAR_TIMEFRAME, ''),
+    '#required' => FALSE,
+    '#description' => t('Set the timeframe in seconds for sampling.'),
+  );
+
+  // Drush settings.
+  $form['drush'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Drush'),
+  );
+  $form['drush'][MUNIN_API_VAR_DRUSH_PATH] = array(
+    '#type' => 'textfield',
+    '#title' => t('Drush executable'),
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_PATH, 'drush'),
+    '#required' => TRUE,
+    '#description' => t('Path to Drush executable'),
+  );
+  $drush_users = array('www-data' => 'www-data', 'apache' => 'apache', 'aegir' => 'aegir', 'custom' => 'custom');
+  $form['drush'][MUNIN_API_VAR_DRUSH_USER] = array(
+    '#type' => 'select',
+    '#title' => t('Drush user'),
+    '#options' => $drush_users,
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_USER, 'www-data'),
+    '#required' => TRUE,
+    '#description' => t('The user drush should run as.'),
+  );
+  $form['drush'][MUNIN_API_VAR_DRUSH_USER_CUSTOM] = array(
+    '#type' => 'textfield',
+    '#title' => t('Custom drush user'),
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_USER_CUSTOM, ''),
+    '#required' => FALSE,
+    '#description' => t('A custom drush user.'),
+    '#states' => array(
+      'visible' => array(
+       ':input[name="' . MUNIN_API_VAR_DRUSH_USER . '"]' => array('value' => 'custom'),
+      ),
+    )
+  );
+  $form['drush'][MUNIN_API_VAR_DRUSH_ALIAS] = array(
+    '#type' => 'textfield',
+    '#title' => t('Drush alias'),
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_ALIAS, ''),
+    '#description' => t('Will override the values set for root and uri. Helpful when the root changes, such as on Aegir systems. Omit the @. Make sure the alias is available to the user running the script.'),
+  );
+  $form['drush'][MUNIN_API_VAR_DRUSH_ROOT] = array(
+    '#type' => 'textfield',
+    '#title' => t('Drush root'),
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_ROOT,
+        (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '')),
+    '#description' => t('As passed to <strong>--root<strong> parameter'),
+    '#states' => array(
+      'invisible' => array(
+       ':input[name="' . MUNIN_API_VAR_DRUSH_ALIAS . '"]' => array('filled' => TRUE),
+      ),
+    )
+  );
+  $form['drush'][MUNIN_API_VAR_DRUSH_URI] = array(
+    '#type' => 'textfield',
+    '#title' => t('Drush URI'),
+    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_URI, 'default'),
+    '#description' => t('As passed to <strong>--uri<strong> parameter'),
+    '#states' => array(
+      'invisible' => array(
+       ':input[name="' . MUNIN_API_VAR_DRUSH_ALIAS . '"]' => array('filled' => TRUE),
+      ),
+    )
+  );
+
+  // Go to the scripts after saving.
+  $form['#redirect'] = 'admin/settings/munin/plugins';
+
+  // Save settings automatically.
+  return system_settings_form($form);
+}
+
+/**
+ * Validate the munin_settings_form.
+ */
+function munin_api_settings_form_validate($form, $form_state) {
+  $values = $form_state['values'];
+  // Check that a custom value has been selected if custom user.
+  if ($values[MUNIN_API_VAR_DRUSH_USER] == 'custom' && empty($values[MUNIN_API_VAR_DRUSH_USER_CUSTOM])) {
+    form_set_error(MUNIN_API_VAR_DRUSH_USER_CUSTOM, t('Custom user field is missing.'));
+  }
+  // Check that uri and root have been selected if there is no alias.
+  if (empty($values[MUNIN_API_VAR_DRUSH_ALIAS])) {
+    if (empty($values[MUNIN_API_VAR_DRUSH_ROOT])) {
+      form_set_error(MUNIN_API_VAR_DRUSH_ROOT, t('Complete the Drush Root field if not using an alias.'));
+    }
+    if (empty($values[MUNIN_API_VAR_DRUSH_URI])) {
+      form_set_error(MUNIN_API_VAR_DRUSH_URI, t('Complete the Drush URI field if not using an alias.'));
+    }
+  }
+}
+
+/**
+ * Settings form.
+ *
+ * @return array
+ *  System settings form array.
+ */
+function munin_api_plugins_form() {
+  $form = array();
+
+  // Helper.
+  $form['help'] = array(
+    '#value' => '<p>' . t('Save this form, then go to the !scripts tab to copy the shell script code.',
+        array('!scripts' => l(t('Scripts'), 'admin/settings/munin/scripts'))) . '</p>',
+  );
+
+  // Saved plugin configs [on/off].
+  $plugin_defaults = variable_get(MUNIN_API_VAR_PLUGINS, array());
+  // Full plugin definitions.
+  $plugins = munin_api_plugin_definitions();
+
+  if (count($plugins) == 0) {
+    $form['message'] = array(
+      '#type' => 'markup',
+      '#value' => t('There are currently no munin plugin definitions available. Please enable a module that defines some munin plugin configurations.'),
+    );
+  }
+  else {
+    $form['munin_plugins'] = array(
+      '#tree' => TRUE,
+      '#type' => 'vertical_tabs'
+    );
+    foreach ($plugins as $category => $definition) {
+      // There are some meta elements describing the category All remaining
+      // elements are definitions.
+      $form['munin_plugins'][$category] = array(
+        '#type' => 'fieldset',
+        '#title' => check_plain($definition['#title']),
+        '#description' => check_plain($definition['#description']),
+        '#collabsible' => TRUE,
+        '#collapsed' => TRUE,
+      );
+      foreach ($definition as $key => $value) {
+        // Each plugin may define multiple stats in their category. Each of
+        // them represents an item in the munin graph element. Let the user
+        // choose.
+        if (drupal_substr($key, 0, 1) != '#') {
+          $form['munin_plugins'][$category][$key] = array(
+            '#type' => 'fieldset',
+            '#title' => check_plain($value['#label']),
+            '#description' => empty($value['#description']) ? '': check_plain($value['#description']),
+            '#collabsible' => TRUE,
+            '#collapsed' => FALSE,
+            '#tree' => TRUE,
+          );
+          $form['munin_plugins'][$category][$key]['enable'] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Enable'),
+            '#default_value' => isset($plugin_defaults[$category][$key]['enable'])?
+              $plugin_defaults[$category][$key]['enable']:'',
+          );
+          $form['munin_plugins'][$category][$key]['warning'] = array(
+            '#type' => 'textfield',
+            '#title' => t('Warning level'),
+            '#description' => t('Warning threshold level in Munin reports.'),
+            '#size' => 5,
+            '#default_value' => _munin_plugin_default($category, $key, 'warning', $plugin_defaults, $plugins),
+          );
+          $form['munin_plugins'][$category][$key]['critical'] = array(
+            '#type' => 'textfield',
+            '#title' => t('Critical level'),
+            '#description' => t('Critical threshold level in Munin reports.'),
+            '#size' => 5,
+            '#default_value' => _munin_plugin_default($category, $key, 'critical', $plugin_defaults, $plugins),
+          );
+        }
+      }
+    }
+  }
+
+  // Go to the scripts after saving.
+  $form['#redirect'] = 'admin/settings/munin/scripts';
+
+  // Save settings automatically.
+  return system_settings_form($form);
+}
+
+/**
+ * Page callback of results.
+ */
+function munin_api_results_page() {
+  drupal_add_library('system', 'drupal.collapse');
+  // Loop through the plugins
+  $s = array();
+  foreach (munin_api_plugin_definitions(TRUE) as $plugin_name => $plugin) {
+    $s[] = '<fieldset class="collapsible collapsed">' .
+      '<legend><span class="fieldset-legend"><a href="#">' . check_plain($plugin['#title']) . '</a></span></legend>' .
+        '<div class="fieldset-wrapper">' .
+        '<h3>' . t('Configuration') . '</h3>' .
+        '<pre>$ drush munin-run ' . check_plain($plugin_name) . ' --get-config</pre>' .
+        '<pre>' . munin_api_get_config($plugin, $plugin_name) . '</pre>' .
+        '<h3>' . t('Values') . '</h3>' .
+        '<pre>$ drush munin-run ' . check_plain($plugin_name) . '</pre>' .
+        '<pre>' . munin_api_get_values($plugin, $plugin_name) . '</pre>' .
+      '</div>' .
+    '</fieldset>';
+  }
+  return implode($s);
+}
+
+/**
+ * Page callback of rendered script.
+ */
+function munin_api_script_page() {
+  $output = '<h2>' . t('1. Manual installation') . '</h2>' .
+    '<p>' . t('Follow the step by step instructions from the README and install this file.') . '</p>' .
+    '<pre>' . munin_api_script() . '</pre>';
+  $output .= '<h2>' . t('2. Install with help from drush (recommended)') . '</h2>' .
+    '<p>' . t('Pipe a script generated by Drush into the shell. This allows to to run the script as root without involving Drush. You should review the contents of the script before running it.') . '</p>' .
+    '<pre>' .
+    "$ drush munin-setup | sudo sh\n" .
+    '</pre>';
+  return $output;
+}
+
+/**
+ * Helper which looks for a default value for vars in the form. It will first
+ * try the munin_plugins variable. If that fails, it will try the actual plugin
+ * definition. This ensures that defaults for "critical" and "warning" which are
+ * set in the plugin are availabe to the user for editing on the first time they
+ * come to the form. If the user wants to blank it out, they can.
+ *
+ */
+function _munin_plugin_default($category, $key, $var, $plugin_defaults, $plugins) {
+  // If there is a saved value (non blank) then that is the defaut
+  if (!empty($plugin_defaults[$category][$key][$var])) {
+    return $plugin_defaults[$category][$key][$var];
+  }
+  // If the form has been saved before (blank value) and plugin has a value
+  if (!isset($plugin_defaults[$category][$key][$var]) && !empty($plugins[$category][$key]['#config'][$var])) {
+    return $plugins[$category][$key]['#config'][$var];
+  }
+  // A blank value has been saved before.
+  return '';
+}
diff --git a/munin_api.drush.inc b/munin_api.drush.inc
index f033f48..0b3f3bd 100644
--- a/munin_api.drush.inc
+++ b/munin_api.drush.inc
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * @file
  * Contains the hook definitions necessary to enable drush function munin-run.
@@ -12,7 +11,7 @@ function munin_api_drush_command() {
   $items = array();
 
   $items['munin-run'] = array(
-    'callback' => 'munin_api_drush_callback',
+    'callback' => 'munin_api_run_drush_callback',
     'arguments' => array(
       'plugin' => 'Run munin plugin.',
     ),
@@ -26,11 +25,111 @@ function munin_api_drush_command() {
     'drupal dependencies' => array('munin_api'),
   );
 
+  $items['munin-setup'] = array(
+    'callback' => 'munin_api_setup_drush_callback',
+    'description' => 'Produces script output which can be saved and the run as root.',
+    'examples' => array(
+      'drush munin-setup' => 'Produces a setup script.',
+      'drush munin-setup | sudo sh' => 'Run the setup script as root.',
+    ),
+    'drupal dependencies' => array('munin_api'),
+  );
+
+  $items['munin-cleanup'] = array(
+    'callback' => 'munin_api_cleanup_drush_callback',
+    'description' => 'Cleans up scripts and symlinks.',
+    'examples' => array(
+      'drush munin-cleanup' => 'Produces a cleanup script.',
+      'drush munin-cleanup | sudo sh' => 'Run the cleanup script as root.',
+    ),
+    'drupal dependencies' => array('munin_api'),
+  );
+
   return $items;
 }
 
 /**
- *  Callback function that creats the response for munin to read and create the
+ * Drush callback function which will output the commands to setup munin.
+ */
+function munin_api_setup_drush_callback() {
+
+//  cat <<EOF > {$script_file}
+//{$script_contents}
+//EOF
+
+  // Targets
+  $site = check_plain(munin_api_site());
+  $script_dir = '/usr/local/munin/plugins';
+  $script_file = "$script_dir/$site";
+  $plugins_dir = '/etc/munin/plugins';
+  $conf_dir = '/etc/munin/plugin-conf.d';
+  $script_contents = munin_api_script();
+  $the_plugins = "{$plugins_dir}/{$site}_*";
+  $drush_user = munin_api_drush_user();
+  $restart = '/etc/init.d/munin-node restart';
+  // Symlinks
+  $syms = '';
+  foreach (munin_api_plugin_definitions(TRUE) as $plugin_name => $plugin_parts) {
+    $syms .= "echo 'Create symlink: {$site}_{$plugin_name}'\nln -s {$script_file} {$plugins_dir}/{$site}_{$plugin_name}\n";
+  }
+  // Build it
+  return <<<COMMANDS
+#! /bin/bash
+# Munin setup script
+# Review the contents of this file and run as root.
+echo 'Making script directory: {$script_dir}'
+mkdir -p {$script_dir}
+echo 'Writing the script: {$script_file}'
+echo '{$script_contents}' > {$script_file}
+echo 'Configuring to run as correct user: {$drush_user}'
+cat <<EOF > $conf_dir/{$site}
+[{$site}_*]
+user {$drush_user}
+EOF
+echo 'Making executable: {$script_file}'
+chmod a+x {$script_file}
+echo 'Deleting symlinks: {$the_plugins}'
+rm -f {$the_plugins}
+{$syms}
+echo 'Restarting munin-node.'
+$restart
+echo 'Munin setup completed. Happy monitoring.'
+
+COMMANDS;
+}
+
+/**
+ * Drush callback function which will output the commands to setup munin.
+ */
+function munin_api_cleanup_drush_callback() {
+  // Targets
+  $site = check_plain(munin_api_site());
+  $script_dir = '/usr/local/munin/plugins';
+  $script_file = "$script_dir/$site";
+  $plugins_dir = '/etc/munin/plugins';
+  $conf_dir = '/etc/munin/plugin-conf.d';
+  $the_plugins = "{$plugins_dir}/{$site}_*";
+  $restart = '/etc/init.d/munin-node restart';
+  // Build it
+  return <<<COMMANDS
+#! /bin/bash
+# Munin cleanup script
+# Review the contents of this file and run as root.
+echo 'Deleting symlinks: {$the_plugins}'
+rm -f {$the_plugins}
+echo 'Deleting script: {$script_file}'
+rm -f {$script_file}
+echo 'Deleting conf: $conf_dir/{$site}'
+rm -f $conf_dir/{$site}
+echo 'Restarting munin-node.'
+$restart
+echo 'Munin cleanup completed. You can restore anytime by running drush munin-setup again. Thanks for monitoring.'
+
+COMMANDS;
+}
+
+/**
+ * Callback function that creats the response for munin to read and create the
  * graphs. This function is executed when drush gets munin-run argument (See
  * munin_api_drush_command).
  *
@@ -38,22 +137,17 @@ function munin_api_drush_command() {
  *   The name of the called plugin. Can contain or not drupal_ prefix (it will
  * be detected and removed).
  */
-function munin_api_drush_callback($plugin_name = NULL) {
-  $definitions = _munin_plugin_definitions(TRUE);
+function munin_api_run_drush_callback($plugin_name = NULL) {
+  $definitions = munin_api_plugin_definitions(TRUE);
   $plugins = array();
 
   if ($plugin_name) {
-    // If $plugin_name starts with drupal_ prefix we trim it here. This is the
-    // case for calls through drush as I didn't find an option yet to trim it in
-    // the bash script.
-    $plugin_name = preg_replace("/^(drupal_?)?([a-z0-9_\-])?/i", "$2", $plugin_name);
-
+    $plugin_name = array_pop(explode('_', $plugin_name));
     if ($plugin_name) {
       $plugins = array($plugin_name);
     }
   }
-
-  if (!count($plugins)) {
+  elseif (!count($plugins)) {
     $plugins = array_keys($definitions);
   }
 
@@ -64,99 +158,12 @@ function munin_api_drush_callback($plugin_name = NULL) {
     else {
       // We are requested to print cofiguration settings for this plugin.
       if (drush_get_option('get-config', FALSE)) {
-        _munin_api_get_config($definitions[$plugin_name], $plugin_name);
+        drush_print(munin_api_get_config($definitions[$plugin_name], $plugin_name));
       }
       // We are requested to print the value for this plugin.
       else {
-        _munin_api_get_values($definitions[$plugin_name], $plugin_name);
+        drush_print(munin_api_get_values($definitions[$plugin_name], $plugin_name));
       }
     }
   }
 }
-
-function _munin_api_get_config($plugin, $plugin_name) {
-  $config = $plugin['#config'];
-
-  // It might not be set at all in which case we use the bellow defaults.
-  !is_array($config) ? $config = array() : 0;
-
-  // We set here some defaults.
-  if (!$config['title']) {
-    $config['title'] = $plugin['#title'];
-  }
-  if (!$config['vlabel']) {
-    $config['vlabel'] = $plugin['#vlabel'];
-  }
-  if (!$config['category']) {
-    $config['category'] = 'Drupal';
-  }
-
-  drush_print('multigraph ' . drupal_clean_css_identifier($plugin_name));
-
-  // Every config set in #config array will overwrite the defaults.
-  foreach ($config as $prop => $value) {
-    drush_print('graph_' . $prop . ' ' . $value);
-  }
-
-  foreach ($plugin as $name => $props) {
-    if (is_array($props)) {
-      $config = $props['#config'];
-      // We set here some defaults.
-      if (!$config['type']) {
-        $config['type'] = 'GAUGE';
-      }
-      if (!$config['label']) {
-        $config['label'] = $props['#label'] ? $props['#label'] :
-            $props['#title'];
-      }
-
-      // Every config set in #config array will overwrite the defaults.
-      foreach ($config as $prop => $value) {
-        drush_print($name . '.' . $prop . ' ' . $value);
-      }
-    }
-  }
-
-  drush_print("\n");
-}
-
-function _munin_api_get_values($definition, $plugin_name) {
-  drush_print('multigraph ' . drupal_clean_css_identifier($plugin_name));
-  foreach ($definition as $name => $plugin) {
-    if (is_array($plugin)) {
-      // Statement and sql are the same thing. We left both for backwards
-      // compatibility.
-      if ($plugin['#statement']) {
-        $value = db_query($plugin['#statement'])->fetchField();
-      }
-      elseif ($plugin['#sql']) {
-        $value = db_query($plugin['#sql'])->fetchField();
-      }
-      elseif ($plugin['#callback']) {
-        // User function is called instead of sql command.
-        if ($plugin['#file']) {
-          // File property is set so we include it.
-          $file = DRUPAL_ROOT . '/' . $plugin['#file'];
-          if (is_file($plugin['#file'])) {
-            require_once $file;
-          }
-        }
-
-        if (function_exists($plugin['#callback'])) {
-          try {
-            // Call custom callback.
-            $value = call_user_func_array($plugin['#callback'], $plugin['#callback_arguments']);
-          } catch (Exception $e) {
-            // Let's cach possible errors from callbacks.
-            drush_log($e->getMessage(), 'error');
-            $value = NULL;
-          }
-        }
-      }
-
-      // And we print everything to console.
-      drush_print($name . '.value ' . $value);
-    }
-  }
-  drush_print("\n");
-}
diff --git a/munin_api.install b/munin_api.install
index c1ce17d..1169009 100644
--- a/munin_api.install
+++ b/munin_api.install
@@ -1,11 +1,42 @@
 <?php
+/**
+ * @file
+ * Uninstall and update functions.
+ */
 
 /**
  * Implements hook_uninstall().
  */
 function munin_api_uninstall() {
   variable_del('munin_drush_path');
+  variable_del('munin_drush_user');
+  variable_del('munin_drush_user_custom');
+  variable_del('munin_drush_alias');
   variable_del('munin_drush_root');
   variable_del('munin_drush_uri');
+  variable_del('munin_category');
+  variable_del('munin_timeframe');
   variable_del('munin_plugins');
 }
+
+/**
+ * Add and tweak config variables for better admin interface.
+ */
+function munin_api_update_7001() {
+  // Move the hardcoded category name into a variable
+  variable_set('munin_category', 'Drupal');
+  // Set a timeframe so that all plugins can reuse the same value
+  variable_set('munin_timeframe', '300');
+  // Tweak the munin plugins flag to be one of many possible values.
+  // This allows for the addition of critical and warning in the UI.
+  $old_plugins = variable_get('munin_plugins', array());
+  $new_plugins = array();
+  foreach ($old_plugins as $old_plugin_key => $old_plugin) {
+    $new_plugin = array();
+    foreach ($old_plugin as $name => $value) {
+      $new_plugin[$name] = array('enable' => $value);
+    }
+    $new_plugins[$old_plugin_key] = $new_plugin;
+  }
+  variable_set('munin_plugins', $new_plugins);
+}
diff --git a/munin_api.module b/munin_api.module
index a722e32..04f9a2f 100644
--- a/munin_api.module
+++ b/munin_api.module
@@ -13,6 +13,18 @@
  */
 define('MUNIN_API_VAR_DRUSH_PATH', 'munin_drush_path');
 /**
+ * Variable name that holds the drush user.
+ */
+define('MUNIN_API_VAR_DRUSH_USER', 'munin_drush_user');
+/**
+ * Variable name that holds the drush user.
+ */
+define('MUNIN_API_VAR_DRUSH_USER_CUSTOM', 'munin_drush_user_custom');
+/**
+ * Variable name that holds the drush alias.
+ */
+define('MUNIN_API_VAR_DRUSH_ALIAS', 'munin_drush_alias');
+/**
  * Variable name that holds the drush root.
  */
 define('MUNIN_API_VAR_DRUSH_ROOT', 'munin_drush_root');
@@ -21,6 +33,14 @@ define('MUNIN_API_VAR_DRUSH_ROOT', 'munin_drush_root');
  */
 define('MUNIN_API_VAR_DRUSH_URI', 'munin_drush_uri');
 /**
+ * Variable name that holds the category for the site.
+ */
+define('MUNIN_API_VAR_CATEGORY', 'munin_category');
+/**
+ * Variable name that holds the timeframe (seconds).
+ */
+define('MUNIN_API_VAR_TIMEFRAME', '300');
+/**
  * Variable name that holds plugin definitions.
  */
 define('MUNIN_API_VAR_PLUGINS', 'munin_plugins');
@@ -48,24 +68,43 @@ function munin_api_menu() {
   $items = array();
   $items['admin/config/development/munin'] = array(
     'title' => 'Munin Plugin',
-    'description' => "Generate a munin plugin shell script with this gui.",
+    'description' => "Generate a munin plugin shell script with this GUI.",
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('munin_settings_form'),
-    'access arguments' => array('generate munin plugin'),
+    'page arguments' => array('munin_api_settings_form'),
+    'access arguments' => array('configure munin plugin'),
+    'file' => 'munin_api.admin.inc',
     'type' => MENU_NORMAL_ITEM,
   );
-
+  $items['admin/config/development/munin/settings'] = array(
+    'title' => 'Settings',
+    'access arguments' => array('configure munin plugin'),
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'weight' => 1,
+  );
   $items['admin/config/development/munin/plugins'] = array(
     'title' => 'Plugins',
-    'access arguments' => array('generate munin plugin'),
-    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('munin_api_plugins_form'),
+    'access arguments' => array('configure munin plugin'),
+    'file' => 'munin_api.admin.inc',
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 2,
+  );
+  $items['admin/config/development/munin/results'] = array(
+    'title' => 'Results',
+    'page callback' => 'munin_api_results_page',
+    'access arguments' => array('configure munin plugin'),
+    'file' => 'munin_api.admin.inc',
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 3,
   );
-
   $items['admin/config/development/munin/script'] = array(
     'title' => 'Script',
-    'page callback' => 'munin_api_script',
-    'access arguments' => array('configure munin plugin'),
+    'page callback' => 'munin_api_script_page',
+    'access arguments' => array('generate munin plugin'),
+    'file' => 'munin_api.admin.inc',
     'type' => MENU_LOCAL_TASK,
+    'weight' => 4,
   );
 
   return $items;
@@ -82,6 +121,184 @@ function munin_api_perm() {
   );
 }
 
+// Functions
+
+/**
+ * The script which is symlinked to.
+ */
+function munin_api_script() {
+  // Command
+  $drush_command = _munin_api_drush_cmd();
+  // User
+  $user = variable_get(MUNIN_API_VAR_DRUSH_USER, 'www-data');
+  if ($user == 'custom') {
+    $user = variable_get(MUNIN_API_VAR_DRUSH_USER, '');
+  }
+  // String
+  $s = <<<EOF
+#!/bin/bash
+# Drupal munin plugin - This script was generated by the munin Drupal module.
+
+# The name of this script is the param passed to drush
+NAME=`basename $0`
+
+# Export the HOME dir of the user so that drush aliases can be found.
+HOME=~
+export HOME
+
+# The drush command to run
+DRUSH="{$drush_command}"
+
+# Pass config as a param to trigger the --get-config option
+# Config settings
+case $1 in
+  config)
+    DRUSH="\${DRUSH} --get-config"
+esac
+
+\$DRUSH
+EOF;
+  return $s;
+}
+
+/**
+ * Build up a config string to return to Munin.
+ * The presumption is that a value set in the UI will rule over a value which is
+ * defined in the plugin. The user has ultimate control.
+ * Generally, plugin writers should avoid using "category" as that is a site
+ * specific value. Plugin values for "critical" and "warning" will be used as
+ * defaults in the UI and can be overridden by the user to suit their preferences.
+ * Other values for chart drawing etc are untouched.
+ */
+function munin_api_get_config($plugin, $plugin_name) {
+  $out = array();
+
+  // Vars which are user configurable
+  $config_vars = array('warning', 'critical');
+
+  // Config starts out as what is set in the plugin.
+  $config = empty($plugin['#config']) ? array() : $plugin['#config'];
+
+  // The munin plugin variables (as configured by user in the admin UI)
+  // These can be used to override the values in $config.
+  $munin_plugins = variable_get(MUNIN_API_VAR_PLUGINS, array());
+
+  // We set here some defaults.
+  if (empty($config['title'])) {
+    $config['title'] = $plugin['#title'];
+  }
+  if (empty($config['vlabel'])) {
+    $config['vlabel'] = $plugin['#vlabel'];
+  }
+  // Category.
+  $config['category'] = munin_api_site();
+
+  $out[] = 'multigraph ' . drupal_clean_css_identifier($plugin_name);
+
+  // Every config set in #config array will overwrite the defaults.
+  foreach ($config as $prop => $value) {
+    $out[] = 'graph_' . $prop . ' ' . $value;
+  }
+
+  foreach ($plugin as $part_name => $props) {
+    if (is_array($props)) {
+      $config = empty($props['#config']) ? array() : $props['#config'];
+      // We set here some defaults.
+      if (empty($config['type'])) {
+        $config['type'] = 'GAUGE';
+      }
+      if (empty($config['label'])) {
+        $config['label'] = $props['#label'] ? $props['#label'] : $props['#title'];
+      }
+      // User configurable variables.
+      foreach ($config_vars as $config_var) {
+        // The saved value from the form.
+        if (!empty($munin_plugins[$plugin_name][$part_name][$config_var])) {
+          // The user has set a non null value.
+         $config[$config_var] = $munin_plugins[$plugin_name][$part_name][$config_var];
+        }
+        elseif (isset($munin_plugins[$plugin_name][$part_name][$config_var])) {
+          // The user has explicitly blanked the value
+          unset($config[$config_var]);
+        }
+        // Otherwise we leave the default as defined in the plugin
+      }
+      // Add the config line.
+      foreach ($config as $prop => $value) {
+        $out[] = $part_name . '.' . $prop . ' ' . $value;
+      }
+    }
+  }
+  return implode("\n", $out);
+}
+
+/**
+ * Lookup the value to return to Munin. This can be done with a #statement or a
+ * #callback.
+ */
+function munin_api_get_values($definition, $plugin_name) {
+  $out = array();
+  $out[] = 'multigraph ' . drupal_clean_css_identifier($plugin_name);
+  foreach ($definition as $name => $plugin) {
+    if (is_array($plugin)) {
+      // Args are used for safe and easy substitution for #statement and #sql.
+      $statement_arguments = array();
+      if (!empty($plugin['#statement_arguments'])) {
+        $statement_arguments = $plugin['#statement_arguments'];
+      }
+      // Statement and sql are the same thing. We left both for backwards
+      // compatibility.
+      if (!empty($plugin['#statement'])) {
+        $value = db_query($plugin['#statement'], $statement_arguments)->fetchField();
+      }
+      elseif (!empty($plugin['#sql'])) {
+        $value = db_query($plugin['#sql'], $statement_arguments)->fetchField();
+      }
+      elseif ($plugin['#callback']) {
+        // User function is called instead of sql command.
+        if (!empty($plugin['#file'])) {
+          // File property is set so we include it.
+          $file = DRUPAL_ROOT . '/' . $plugin['#file'];
+          if (is_file($plugin['#file'])) {
+            require_once $file;
+          }
+        }
+        if (function_exists($plugin['#callback'])) {
+          try {
+            // Gracefully handle empty arguments
+            $args = !empty($plugin['#callback_arguments']) ? $plugin['#callback_arguments'] : array();
+            // Call custom callback.
+            $value = call_user_func_array($plugin['#callback'], $args);
+          } catch (Exception $e) {
+            // Let's catch possible errors from callbacks.
+            drush_log($e->getMessage(), 'error');
+            $value = NULL;
+          }
+        }
+      }
+      // Add the value
+      $out[] = $name . '.value ' . $value;
+    }
+  }
+  return implode("\n", $out);
+}
+
+/**
+ * Clean up the URL to remove the http, the trailing slash, and any other
+ * unwanted characters.
+ */
+function munin_api_site() {
+  $category = variable_get(MUNIN_API_VAR_CATEGORY, '');
+  if (empty($category)) {
+    $url = url('', array('absolute' => TRUE));
+    $url = preg_replace('#^https?://#', '', $url);
+    $url = preg_replace('#/$#', '', $url);
+    $url = preg_replace('#[:@/ ]#', '_', $url);
+    $category = 'drupal_' . $url;
+  }
+  return $category;
+}
+
 /**
  * Load the plugin definitions.
  *
@@ -91,157 +308,71 @@ function munin_api_perm() {
  * @return array
  *  Array of plugin definitions.
  */
-function _munin_plugin_definitions($only_active = FALSE) {
+function munin_api_plugin_definitions($only_active = FALSE) {
   $plugins = module_invoke_all('muninplugin');
   // Let other modules alter the definition.
   drupal_alter('muninplugin', $plugins);
 
   if ($only_active) {
     $plugin_defaults = variable_get(MUNIN_API_VAR_PLUGINS, array());
-
     foreach ($plugin_defaults as $key => $value) {
-      foreach ($value as $name => $active) {
-        // Remove if inactive.
-        if ($active != 1) {
-          unset($plugins[$key][$name]);
-        }
-      }
-      // Remove group if none left.
-      if (count($plugins[$key]) == 0) {
+      // Be defensive and check for an array
+      if (!is_array($value)) {
         unset($plugins[$key]);
       }
+      else {
+        $at_least_one = FALSE;
+        foreach ($value as $name => $properties) {
+          // Remove if not enabled.
+          if (empty($properties['enable'])) {
+            unset($plugins[$key][$name]);
+          }
+          else {
+            $at_least_one = TRUE;
+          }
+        }
+        // Remove group if not at least one.
+        if (!$at_least_one) {
+          unset($plugins[$key]);
+        }
+      }
     }
   }
-
   return $plugins;
 }
 
 /**
- * Settings form.
- *
- * @return array
- *  System settings form array.
+ * Returns drush user depending on whether a custom vale has been entered.
  */
-function munin_settings_form() {
-  $form = array();
-
-  // Helper.
-  $form['help'] = array(
-    '#value' => '<p>' . t('Save this form, then go to the !scripts tab to copy the shell script code.', 
-        array('!scripts' => l('Scripts', 'admin/settings/munin/scripts'))) . '</p>',
-  );
-
-  // Saved plugin configs [on/off].
-  $plugin_defaults = variable_get(MUNIN_API_VAR_PLUGINS, array());
-  // Full plugin definitions.
-  $plugins = _munin_plugin_definitions();
-
-  if (count($plugins) == 0) {
-    $form['message'] = array(
-      '#type' => 'markup',
-      '#value' => t('There are currently no munin plugin definitions available. Please enable a module that defines some munin plugin configurations.'),
-    );
+function munin_api_drush_user() {
+  if (variable_get(MUNIN_API_VAR_DRUSH_USER, '') == 'custom') {
+    return variable_get(MUNIN_API_VAR_DRUSH_USER_CUSTOM, '');
   }
   else {
-    $form['munin_plugins'] = array('#tree' => TRUE);
-    foreach($plugins as $category => $definition) {
-      // There are some meta elements describing the category All remaining
-      // elements are definitions.
-      $form['munin_plugins'][$category] = array(
-        '#type' => 'fieldset',
-        '#title' => $definition['#title'],
-        '#description' => $definition['#description'],
-        '#collabsible' => TRUE,
-        '#collapsed' => TRUE,
-      );
-      foreach ($definition as $key => $value) {
-        // Each plugin may define multiple stats in their category. Each of
-        // them represents an item in the munin graph element. Let the user
-        // choose.
-        if (drupal_substr($key, 0 ,1) != '#') {
-          $form['munin_plugins'][$category][$key] = array(
-            '#type' => 'checkbox',
-            '#title' => $value['#label'],
-            '#description' => $value['#description'],
-            '#default_value' => isset($plugin_defaults[$category][$key])?
-              $plugin_defaults[$category][$key]:'',
-          );
-        }
-      }
-    }
+    return variable_get(MUNIN_API_VAR_DRUSH_USER, '');
   }
-
-  // Drush settings.
-  $form['drush'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Drush'),
-  );
-  $form['drush'][MUNIN_API_VAR_DRUSH_PATH] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path to Drush executable'),
-    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_PATH, 'drush'),
-    '#required' => TRUE,
-  );
-  $form['drush'][MUNIN_API_VAR_DRUSH_ROOT] = array(
-    '#type' => 'textfield',
-    '#title' => t('Drush root'),
-    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_ROOT,
-        (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '')),
-    '#required' => TRUE,
-    '#description' => t('As passed to <strong>--root<strong> parameter'),
-  );
-  $form['drush'][MUNIN_API_VAR_DRUSH_URI] = array(
-    '#type' => 'textfield',
-    '#title' => t('Drush URI'),
-    '#default_value' => variable_get(MUNIN_API_VAR_DRUSH_URI, 'default'),
-    '#required' => TRUE,
-    '#description' => t('As passed to <strong>--uri<strong> parameter'),
-  );
-
-
-  // Go to the scripts after saving.
-  $form['#redirect'] = 'admin/settings/munin/scripts';
-
-  // Save settings automatically.
-  return system_settings_form($form);
 }
 
+// Helpers
+
 /**
  * Build the drush command for the scripts.
  */
 function _munin_api_drush_cmd() {
-  $cmd = variable_get(MUNIN_API_VAR_DRUSH_PATH, 'drush');
-  $root = variable_get(MUNIN_API_VAR_DRUSH_ROOT,
-      (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : ''));
-  $uri = variable_get(MUNIN_API_VAR_DRUSH_URI, 'default');
-
-  return $cmd . ' --root=' . $root . ' --uri=' . $uri . ' munin-run $NAME';
-}
-
-/**
- * Page callback of rendered script.
- */
-function munin_api_script() {
-  $css_path = drupal_get_path('module', 'munin_api') . '/munin.css';
-  drupal_add_css($css_path, 'module');
-
-  $output = '<h4>Copy the bellow code into a new bash script and save it as <code>/etc/munin/plugins/drupal</code> and make it executable (<code>chmod +x drupal</code>).</h4>';
-  $output .=  '<div class="munin_script"><pre>
-#!/bin/bash
-# Drupal munin plugin - This script was generated by the munin Drupal module.
-
-# Variables
-NAME=`basename $0`
-DRUSH="' . _munin_api_drush_cmd() . '"
-
-# Config settings
-case $1 in
-  config)
-    DRUSH=$DRUSH" --get-config"
-esac
-
-$DRUSH
-</pre></div>';
-
-  return $output;
+  $script = variable_get(MUNIN_API_VAR_DRUSH_PATH, 'drush');
+  $alias = variable_get(MUNIN_API_VAR_DRUSH_ALIAS, '');
+  $user = variable_get(MUNIN_API_VAR_DRUSH_USER, '');
+  // Check for an alias
+  if (!empty($alias)) {
+    // An alias wins
+    $cmd = $script . ' @' . $alias . ' munin-run $NAME';
+  }
+  else {
+    // Fall back to the root and uri.
+    $root = variable_get(MUNIN_API_VAR_DRUSH_ROOT,
+        (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : ''));
+    $uri = variable_get(MUNIN_API_VAR_DRUSH_URI, 'default');
+    $cmd = $script . ' --root=' . $root . ' --uri=' . $uri . ' munin-run $NAME';
+  }
+  return $cmd;
 }
diff --git a/munin_default.module b/munin_default.module
index 401fd81..7867ecb 100644
--- a/munin_default.module
+++ b/munin_default.module
@@ -13,22 +13,22 @@
 function munin_default_muninplugin() {
   $plugins = array();
 
-  // Set the category. There will be a munin element for each category.
   $plugins['user'] = array(
     '#title' => t('User information'),
     '#description' => t('Information about the users on your site.'),
     '#vlabel' => t('Number'),
   );
-  // Add the data that fits into that category.
   $plugins['user']['curuser'] = array(
     '#label' => t('Currently logged in user'),
     '#description' => t('This item shows the number of currently logged in users.'),
-    '#statement' => 'SELECT COUNT(uid) FROM {users} WHERE UNIX_TIMESTAMP() - access < (60*5)',
+    '#statement' => 'SELECT COUNT(uid) FROM {users} WHERE UNIX_TIMESTAMP() - access < :timeframe',
+    '#statement_arguments' => array(':timeframe' => variable_get(MUNIN_API_VAR_TIMEFRAME, '300')),
   );
   $plugins['user']['newuser'] = array(
     '#label' => t('New users'),
     '#description' => t('This item shows the number of users that have been created.'),
-    '#statement' => 'SELECT COUNT(uid) FROM {users} WHERE UNIX_TIMESTAMP() - created < (60*5)',
+    '#statement' => 'SELECT COUNT(uid) FROM {users} WHERE UNIX_TIMESTAMP() - created < :timeframe',
+    '#statement_arguments' => array(':timeframe' => variable_get(MUNIN_API_VAR_TIMEFRAME, '300')),
   );
 
   $plugins['content'] = array(
@@ -49,12 +49,14 @@ function munin_default_muninplugin() {
   $plugins['content']['newnodes'] = array(
     '#label' => t('New nodes'),
     '#description' => t('Number of new nodes on this site.'),
-    '#statement' => 'SELECT COUNT(nid) FROM {node} WHERE UNIX_TIMESTAMP() - created < (60*5)',
+    '#statement' => 'SELECT COUNT(nid) FROM {node} WHERE UNIX_TIMESTAMP() - created < :timeframe',
+    '#statement_arguments' => array(':timeframe' => variable_get(MUNIN_API_VAR_TIMEFRAME, '300')),
   );
   $plugins['content']['newcmnt'] = array(
     '#label' => t('New Comments'),
     '#description' => t('Number of new comments on this site.'),
-    '#statement' => 'SELECT COUNT(cid) FROM {comment} WHERE UNIX_TIMESTAMP() - created < (60*5)',
+    '#statement' => 'SELECT COUNT(cid) FROM {comment} WHERE UNIX_TIMESTAMP() - created < :timeframe',
+    '#statement_arguments' => array(':timeframe' => variable_get(MUNIN_API_VAR_TIMEFRAME, '300')),
   );
 
   $watchdog_severity_levels = watchdog_severity_levels();
@@ -68,13 +70,14 @@ function munin_default_muninplugin() {
   foreach ($watchdog_severity_levels as $id => $severity) {
     $plugins['errors'][$severity] = array(
       '#label' => t('!severity', array('!severity' => ucfirst($severity))),
-      '#description' => t('Number errors of !severity severity.'),
+      '#description' => t('Number errors of !severity severity.', array('!severity' => $severity)),
       '#config' => array(
         'type' => 'DERIVE',
         'min' => 0,
         'draw' => 'LINE1'
       ),
-      '#statement' => 'SELECT COUNT(wid) FROM {watchdog} WHERE severity = ' . $id . ';',
+      '#statement' => 'SELECT COUNT(wid) FROM {watchdog} WHERE severity = :id;',
+      '#statement_arguments' => array(':id' => $id),
     );
   }
 
@@ -129,6 +132,6 @@ function _munin_default_read_pageload_timer() {
  */
 function munin_default_exit($destination = NULL) {
   $page_counter = timer_stop('munin');
-  
+
   cache_set('munin:default_pageload', $page_counter['time']);
 }
