Merge branch 'collectd-5.4' into collectd-5.5
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Fri, 11 Dec 2015 20:56:40 +0000 (21:56 +0100)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Fri, 11 Dec 2015 20:56:40 +0000 (21:56 +0100)
1  2 
src/daemon/utils_ignorelist.c
src/swap.c

index 7b1c7d2,0000000..6b5e894
mode 100644,000000..100644
--- /dev/null
@@@ -1,367 -1,0 +1,346 @@@
-       int rcompile;
-       regex_t *regtemp;
-       int errsize;
-       char *regerr = NULL;
-       ignorelist_item_t *new;
 +/**
 + * collectd - src/utils_ignorelist.c
 + * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
 + * Copyright (C) 2008 Florian Forster <octo at collectd.org>
 + *
 + * This program is free software; you can redistribute it and/
 + * or modify it under the terms of the GNU General Public Li-
 + * cence as published by the Free Software Foundation; either
 + * version 2 of the Licence, or any later version.
 + *
 + * This program is distributed in the hope that it will be use-
 + * ful, but WITHOUT ANY WARRANTY; without even the implied war-
 + * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 + * See the GNU General Public Licence for more details.
 + *
 + * You should have received a copy of the GNU General Public
 + * Licence along with this program; if not, write to the Free
 + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 + * USA.
 + *
 + * Authors:
 + *   Lubos Stanek <lubek at users.sourceforge.net>
 + *   Florian Forster <octo at collectd.org>
 + **/
 +/**
 + * ignorelist handles plugin's list of configured collectable
 + * entries with global ignore action
 + **/
 +/**
 + * Usage:
 + * 
 + * Define plugin's global pointer variable of type ignorelist_t:
 + *   ignorelist_t *myconfig_ignore;
 + * If you know the state of the global ignore (IgnoreSelected),
 + * allocate the variable with:
 + *   myconfig_ignore = ignorelist_create (YourKnownIgnore);
 + * If you do not know the state of the global ignore,
 + * initialize the global variable and set the ignore flag later:
 + *   myconfig_ignore = ignorelist_init ();
 + * Append single entries in your cf_register'ed callback function:
 + *   ignorelist_add (myconfig_ignore, newentry);
 + * When you hit the IgnoreSelected config option,
 + * offer it to the list:
 + *   ignorelist_ignore (myconfig_ignore, instantly_got_value_of_ignore);
 + * That is all for the ignorelist initialization.
 + * Later during read and write (plugin's registered functions) get
 + * the information whether this entry would be collected or not:
 + *   if (ignorelist_match (myconfig_ignore, thisentry))
 + *     return;
 + **/
 +
 +#if HAVE_CONFIG_H
 +# include "config.h"
 +#endif
 +
 +#include "common.h"
 +#include "plugin.h"
 +#include "utils_ignorelist.h"
 +
 +/*
 + * private prototypes
 + */
 +struct ignorelist_item_s
 +{
 +#if HAVE_REGEX_H
 +      regex_t *rmatch;        /* regular expression entry identification */
 +#endif
 +      char *smatch;           /* string entry identification */
 +      struct ignorelist_item_s *next;
 +};
 +typedef struct ignorelist_item_s ignorelist_item_t;
 +
 +struct ignorelist_s
 +{
 +      int ignore;             /* ignore entries */
 +      ignorelist_item_t *head;        /* pointer to the first entry */
 +};
 +
 +/* *** *** *** ********************************************* *** *** *** */
 +/* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
 +/* *** *** *** ********************************************* *** *** *** */
 +
 +static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
 +{
 +      assert ((il != NULL) && (item != NULL));
 +
 +      item->next = il->head;
 +      il->head = item;
 +}
 +
 +#if HAVE_REGEX_H
 +static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
 +{
-       if ((regtemp = malloc(sizeof(regex_t))) == NULL)
++      regex_t *re;
++      ignorelist_item_t *item;
++      int status;
 +
 +      /* create buffer */
-               ERROR ("cannot allocate new config entry");
-               return (1);
++      re = malloc (sizeof (*re));
++      if (re == NULL)
 +      {
-       memset (regtemp, '\0', sizeof(regex_t));
++              ERROR ("ignorelist_append_regex: malloc failed.");
++              return ENOMEM;
 +      }
-       if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
++      memset (re, 0, sizeof (*re));
 +
 +      /* compile regex */
-               /* prepare message buffer */
-               errsize = regerror(rcompile, regtemp, NULL, 0);
-               if (errsize)
-                       regerr = smalloc(errsize);
-               /* get error message */
-               if (regerror (rcompile, regtemp, regerr, errsize))
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-                       ERROR ("Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-               }
-               else
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i",
-                                       entry, rcompile);
-                       ERROR ("Cannot compile regex %s: %i",
-                                       entry, rcompile);
-               }
++      status = regcomp (re, entry, REG_EXTENDED);
++      if (status != 0)
 +      {
-               if (errsize)
-                       sfree (regerr);
-               regfree (regtemp);
-               sfree (regtemp);
-               return (1);
++              char errbuf[1024];
 +
-       DEBUG("regex compiled: %s - %i", entry, rcompile);
++              (void) regerror (status, re, errbuf, sizeof (errbuf));
++              ERROR ("ignorelist_append_regex: Compiling regular expression \"%s\" failed: %s", entry, errbuf);
++              sfree (re);
++              return status;
 +      }
-       if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
 +
 +      /* create new entry */
-               ERROR ("cannot allocate new config entry");
-               regfree (regtemp);
-               sfree (regtemp);
-               return (1);
++      item = malloc (sizeof (*item));
++      if (item == NULL)
 +      {
-       memset (new, '\0', sizeof(ignorelist_item_t));
-       new->rmatch = regtemp;
++              ERROR ("ignorelist_append_regex: malloc failed.");
++              regfree (re);
++              sfree (re);
++              return ENOMEM;
 +      }
-       ignorelist_append (il, new);
++      memset (item, 0, sizeof (*item));
++      item->rmatch = re;
 +
 +      /* append new entry */
-       /* smalloc exits if it failes */
-       il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
-       memset (il, '\0', sizeof (ignorelist_t));
++      ignorelist_append (il, item);
 +
 +      return (0);
 +} /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
 +#endif
 +
 +static int ignorelist_append_string(ignorelist_t *il, const char *entry)
 +{
 +      ignorelist_item_t *new;
 +
 +      /* create new entry */
 +      if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
 +      {
 +              ERROR ("cannot allocate new entry");
 +              return (1);
 +      }
 +      memset (new, '\0', sizeof(ignorelist_item_t));
 +      new->smatch = sstrdup(entry);
 +
 +      /* append new entry */
 +      ignorelist_append (il, new);
 +
 +      return (0);
 +} /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
 +
 +#if HAVE_REGEX_H
 +/*
 + * check list for entry regex match
 + * return 1 if found
 + */
 +static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
 +{
 +      assert ((item != NULL) && (item->rmatch != NULL)
 +                      && (entry != NULL) && (strlen (entry) > 0));
 +
 +      /* match regex */
 +      if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
 +              return (1);
 +
 +      return (0);
 +} /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
 +#endif
 +
 +/*
 + * check list for entry string match
 + * return 1 if found
 + */
 +static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
 +{
 +      assert ((item != NULL) && (item->smatch != NULL)
 +                      && (entry != NULL) && (strlen (entry) > 0));
 +
 +      if (strcmp (entry, item->smatch) == 0)
 +              return (1);
 +
 +      return (0);
 +} /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
 +
 +
 +/* *** *** *** ******************************************** *** *** *** */
 +/* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
 +/* *** *** *** ******************************************** *** *** *** */
 +
 +/*
 + * create the ignorelist_t with known ignore state
 + * return pointer to ignorelist_t
 + */
 +ignorelist_t *ignorelist_create (int invert)
 +{
 +      ignorelist_t *il;
 +
-       int ret;
-       size_t entry_len;
++      il = malloc (sizeof (*il));
++      if (il == NULL)
++              return NULL;
++      memset (il, 0, sizeof (*il));
 +
 +      /*
 +       * ->ignore == 0  =>  collect
 +       * ->ignore == 1  =>  ignore
 +       */
 +      il->ignore = invert ? 0 : 1;
 +
 +      return (il);
 +} /* ignorelist_t *ignorelist_create (int ignore) */
 +
 +/*
 + * free memory used by ignorelist_t
 + */
 +void ignorelist_free (ignorelist_t *il)
 +{
 +      ignorelist_item_t *this;
 +      ignorelist_item_t *next;
 +
 +      if (il == NULL)
 +              return;
 +
 +      for (this = il->head; this != NULL; this = next)
 +      {
 +              next = this->next;
 +#if HAVE_REGEX_H
 +              if (this->rmatch != NULL)
 +              {
 +                      regfree (this->rmatch);
 +                      sfree (this->rmatch);
 +                      this->rmatch = NULL;
 +              }
 +#endif
 +              if (this->smatch != NULL)
 +              {
 +                      sfree (this->smatch);
 +                      this->smatch = NULL;
 +              }
 +              sfree (this);
 +      }
 +
 +      sfree (il);
 +      il = NULL;
 +} /* void ignorelist_destroy (ignorelist_t *il) */
 +
 +/*
 + * set ignore state of the ignorelist_t
 + */
 +void ignorelist_set_invert (ignorelist_t *il, int invert)
 +{
 +      if (il == NULL)
 +      {
 +              DEBUG("ignore call with ignorelist_t == NULL");
 +              return;
 +      }
 +
 +      il->ignore = invert ? 0 : 1;
 +} /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
 +
 +/*
 + * append entry into ignorelist_t
 + * return 1 for success
 + */
 +int ignorelist_add (ignorelist_t *il, const char *entry)
 +{
-       entry_len = strlen (entry);
++      size_t len;
 +
 +      if (il == NULL)
 +      {
 +              DEBUG ("add called with ignorelist_t == NULL");
 +              return (1);
 +      }
 +
-       if (entry_len == 0)
++      len = strlen (entry);
 +
 +      /* append nothing */
-       if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
++      if (len == 0)
 +      {
 +              DEBUG("not appending: empty entry");
 +              return (1);
 +      }
 +
 +#if HAVE_REGEX_H
 +      /* regex string is enclosed in "/.../" */
-               char *entry_copy;
-               size_t entry_copy_size;
++      if ((len > 2) && (entry[0] == '/') && entry[len - 1] == '/')
 +      {
-               /* We need to copy `entry' since it's const */
-               entry_copy_size = entry_len - 1;
-               entry_copy = smalloc (entry_copy_size);
-               sstrncpy (entry_copy, entry + 1, entry_copy_size);
++              char *copy;
++              int status;
 +
-               DEBUG("I'm about to add regex entry: %s", entry_copy);
-               ret = ignorelist_append_regex(il, entry_copy);
-               sfree (entry_copy);
++              /* skip leading slash */
++              copy = strdup (entry + 1);
++              if (copy == NULL)
++                      return ENOMEM;
 +
-       else
++              /* trim trailing slash */
++              copy[strlen (copy) - 1] = 0;
++
++              status = ignorelist_append_regex (il, copy);
++              sfree (copy);
++              return status;
 +      }
-       {
-               DEBUG("to add entry: %s", entry);
-               ret = ignorelist_append_string(il, entry);
-       }
 +#endif
-       return (ret);
 +
++      return ignorelist_append_string(il, entry);
 +} /* int ignorelist_add (ignorelist_t *il, const char *entry) */
 +
 +/*
 + * check list for entry
 + * return 1 for ignored entry
 + */
 +int ignorelist_match (ignorelist_t *il, const char *entry)
 +{
 +      ignorelist_item_t *traverse;
 +
 +      /* if no entries, collect all */
 +      if ((il == NULL) || (il->head == NULL))
 +              return (0);
 +
 +      if ((entry == NULL) || (strlen (entry) == 0))
 +              return (0);
 +
 +      /* traverse list and check entries */
 +      for (traverse = il->head; traverse != NULL; traverse = traverse->next)
 +      {
 +#if HAVE_REGEX_H
 +              if (traverse->rmatch != NULL)
 +              {
 +                      if (ignorelist_match_regex (traverse, entry))
 +                              return (il->ignore);
 +              }
 +              else
 +#endif
 +              {
 +                      if (ignorelist_match_string (traverse, entry))
 +                              return (il->ignore);
 +              }
 +      } /* for traverse */
 +
 +      return (1 - il->ignore);
 +} /* int ignorelist_match (ignorelist_t *il, const char *entry) */
 +
diff --combined src/swap.c
@@@ -1,6 -1,6 +1,6 @@@
  /**
   * collectd - src/swap.c
 - * Copyright (C) 2005-2012  Florian octo Forster
 + * Copyright (C) 2005-2014  Florian octo Forster
   * Copyright (C) 2009       Stefan Völkel
   * Copyright (C) 2009       Manuel Sanmartin
   * Copyright (C) 2010       Aurélien Reynaud
@@@ -97,45 -97,48 +97,45 @@@ int kvm_pagesize
  
  #elif HAVE_PERFSTAT
  static int pagesize;
 -static perfstat_memory_total_t pmemory;
  /*# endif HAVE_PERFSTAT */
  
  #else
  # error "No applicable input method."
  #endif /* HAVE_LIBSTATGRAB */
  
 -static const char *config_keys[] =
 -{
 -      "ReportBytes",
 -      "ReportByDevice"
 -};
 -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 +static _Bool values_absolute = 1;
 +static _Bool values_percentage = 0;
  
 -static int swap_config (const char *key, const char *value) /* {{{ */
 +static int swap_config (oconfig_item_t *ci) /* {{{ */
  {
 -      if (strcasecmp ("ReportBytes", key) == 0)
 +      int i;
 +
 +      for (i = 0; i < ci->children_num; i++)
        {
 +              oconfig_item_t *child = ci->children + i;
 +              if (strcasecmp ("ReportBytes", child->key) == 0)
  #if KERNEL_LINUX
 -              report_bytes = IS_TRUE (value) ? 1 : 0;
 +                      cf_util_get_boolean (child, &report_bytes);
  #else
 -              WARNING ("swap plugin: The \"ReportBytes\" option is only "
 -                              "valid under Linux. "
 -                              "The option is going to be ignored.");
 +                      WARNING ("swap plugin: The \"ReportBytes\" option "
 +                                      "is only valid under Linux. "
 +                                      "The option is going to be ignored.");
  #endif
 -      }
 -      else if (strcasecmp ("ReportByDevice", key) == 0)
 -      {
 +              else if (strcasecmp ("ReportByDevice", child->key) == 0)
  #if SWAP_HAVE_REPORT_BY_DEVICE
 -              if (IS_TRUE (value))
 -                      report_by_device = 1;
 -              else
 -                      report_by_device = 0;
 +                      cf_util_get_boolean (child, &report_by_device);
  #else
 -              WARNING ("swap plugin: The \"ReportByDevice\" option is not "
 -                              "supported on this platform. "
 -                              "The option is going to be ignored.");
 +                      WARNING ("swap plugin: The \"ReportByDevice\" option "
 +                                      "is not supported on this platform. "
 +                                      "The option is going to be ignored.");
  #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
 -      }
 -      else
 -      {
 -              return (-1);
 +              else if (strcasecmp ("ValuesAbsolute", child->key) == 0)
 +                      cf_util_get_boolean (child, &values_absolute);
 +              else if (strcasecmp ("ValuesPercentage", child->key) == 0)
 +                      cf_util_get_boolean (child, &values_percentage);
 +              else
 +                      WARNING ("swap plugin: Unknown config option: \"%s\"",
 +                                      child->key);
        }
  
        return (0);
@@@ -187,50 -190,44 +187,50 @@@ static int swap_init (void) /* {{{ *
        return (0);
  } /* }}} int swap_init */
  
 -static void swap_submit (const char *plugin_instance, /* {{{ */
 -              const char *type, const char *type_instance,
 -              value_t value)
 +static void swap_submit_usage (char const *plugin_instance, /* {{{ */
 +              gauge_t used, gauge_t free,
 +              char const *other_name, gauge_t other_value)
  {
 +      value_t v[1];
        value_list_t vl = VALUE_LIST_INIT;
  
 -      assert (type != NULL);
 -
 -      vl.values = &value;
 -      vl.values_len = 1;
 +      vl.values = v;
 +      vl.values_len = STATIC_ARRAY_SIZE (v);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
        if (plugin_instance != NULL)
 -              sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
 -      sstrncpy (vl.type, type, sizeof (vl.type));
 -      if (type_instance != NULL)
 -              sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 -
 -      plugin_dispatch_values (&vl);
 -} /* }}} void swap_submit_inst */
 +              sstrncpy (vl.plugin_instance, plugin_instance,
 +                              sizeof (vl.plugin_instance));
 +      sstrncpy (vl.type, "swap", sizeof (vl.type));
 +
 +      if (values_absolute)
 +              plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE,
 +                              "used", used, "free", free,
 +                              other_name, other_value, NULL);
 +      if (values_percentage)
 +              plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
 +                              "used", used, "free", free,
 +                              other_name, other_value, NULL);
 +} /* }}} void swap_submit_usage */
  
 -static void swap_submit_gauge (const char *plugin_instance, /* {{{ */
 -              const char *type_instance, gauge_t value)
 +#if KERNEL_LINUX || HAVE_PERFSTAT
 +__attribute__((nonnull(1)))
 +static void swap_submit_derive (char const *type_instance, /* {{{ */
 +              derive_t value)
  {
 -      value_t v;
 +      value_list_t vl = VALUE_LIST_INIT;
 +      value_t v[1];
  
 -      v.gauge = value;
 -      swap_submit (plugin_instance, "swap", type_instance, v);
 -} /* }}} void swap_submit_gauge */
 +      v[0].derive = value;
  
 -#if KERNEL_LINUX || HAVE_PERFSTAT
 -static void swap_submit_derive (const char *plugin_instance, /* {{{ */
 -              const char *type_instance, derive_t value)
 -{
 -      value_t v;
 +      vl.values = v;
 +      vl.values_len = STATIC_ARRAY_SIZE (v);
 +      sstrncpy (vl.host, hostname_g, sizeof (vl.host));
 +      sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
 +      sstrncpy (vl.type, "swap_io", sizeof (vl.type));
 +      sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
  
 -      v.derive = value;
 -      swap_submit (plugin_instance, "swap_io", type_instance, v);
 +      plugin_dispatch_values (&vl);
  } /* }}} void swap_submit_derive */
  #endif
  
@@@ -256,8 -253,9 +256,8 @@@ static int swap_read_separate (void) /
                char *endptr;
  
                char path[PATH_MAX];
 -              gauge_t size;
 +              gauge_t total;
                gauge_t used;
 -              gauge_t free;
  
                numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
                if (numfields != 5)
  
                errno = 0;
                endptr = NULL;
 -              size = strtod (fields[2], &endptr);
 +              total = strtod (fields[2], &endptr);
                if ((endptr == fields[2]) || (errno != 0))
                        continue;
  
                if ((endptr == fields[3]) || (errno != 0))
                        continue;
  
 -              if (size < used)
 +              if (total < used)
                        continue;
  
 -              free = size - used;
 -
 -              swap_submit_gauge (path, "used", used);
 -              swap_submit_gauge (path, "free", free);
 +              swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0,
 +                              NULL, NAN);
        }
  
        fclose (fh);
@@@ -295,10 -295,11 +295,10 @@@ static int swap_read_combined (void) /
        FILE *fh;
        char buffer[1024];
  
 -      uint8_t have_data = 0;
 -      gauge_t swap_used   = 0.0;
 -      gauge_t swap_cached = 0.0;
 -      gauge_t swap_free   = 0.0;
 -      gauge_t swap_total  = 0.0;
 +      gauge_t swap_used   = NAN;
 +      gauge_t swap_cached = NAN;
 +      gauge_t swap_free   = NAN;
 +      gauge_t swap_total  = NAN;
  
        fh = fopen ("/proc/meminfo", "r");
        if (fh == NULL)
                        continue;
  
                if (strcasecmp (fields[0], "SwapTotal:") == 0)
 -              {
 -                      swap_total = strtod (fields[1], /* endptr = */ NULL);
 -                      have_data |= 0x01;
 -              }
 +                      strtogauge (fields[1], &swap_total);
                else if (strcasecmp (fields[0], "SwapFree:") == 0)
 -              {
 -                      swap_free = strtod (fields[1], /* endptr = */ NULL);
 -                      have_data |= 0x02;
 -              }
 +                      strtogauge (fields[1], &swap_free);
                else if (strcasecmp (fields[0], "SwapCached:") == 0)
 -              {
 -                      swap_cached = strtod (fields[1], /* endptr = */ NULL);
 -                      have_data |= 0x04;
 -              }
 +                      strtogauge (fields[1], &swap_cached);
        }
  
        fclose (fh);
  
 -      if ((have_data & 0x03) != 0x03)
 +      if (isnan (swap_total) || isnan (swap_free))
                return (ENOENT);
  
 -      if (isnan (swap_total)
 -                      || (swap_total <= 0.0)
 -                      || ((swap_free + swap_cached) > swap_total))
 -              return (EINVAL);
 -
 -      swap_used = swap_total - (swap_free + swap_cached);
 +      /* Some systems, OpenVZ for example, don't provide SwapCached. */
 +      if (isnan (swap_cached))
 +              swap_used = swap_total - swap_free;
 +      else
 +              swap_used = swap_total - (swap_free + swap_cached);
 +      assert (!isnan (swap_used));
  
 -      swap_submit_gauge (NULL, "used",   1024.0 * swap_used);
 -      swap_submit_gauge (NULL, "free",   1024.0 * swap_free);
 -      if (have_data & 0x04)
 -              swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
 +      if (swap_used < 0.0)
 +              return (EINVAL);
  
 +      swap_submit_usage (NULL, swap_used * 1024.0, swap_free * 1024.0,
 +                      isnan (swap_cached) ? NULL : "cached",
 +                      isnan (swap_cached) ? NAN : swap_cached * 1024.0);
        return (0);
  } /* }}} int swap_read_combined */
  
@@@ -421,8 -430,8 +421,8 @@@ static int swap_read_io (void) /* {{{ *
                swap_out = swap_out * pagesize;
        }
  
 -      swap_submit_derive (NULL, "in",  swap_in);
 -      swap_submit_derive (NULL, "out", swap_out);
 +      swap_submit_derive ("in",  swap_in);
 +      swap_submit_derive ("out", swap_out);
  
        return (0);
  } /* }}} int swap_read_io */
@@@ -453,9 -462,9 +453,9 @@@ static int swap_read (void) /* {{{ *
  /* kstat-based read function */
  static int swap_read_kstat (void) /* {{{ */
  {
 -      derive_t swap_alloc;
 -      derive_t swap_resv;
 -      derive_t swap_avail;
 +      gauge_t swap_alloc;
 +      gauge_t swap_resv;
 +      gauge_t swap_avail;
  
        struct anoninfo ai;
  
         * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
         * can suffer from a 32bit overflow.
         */
 -      swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
 -      swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
 -                      * pagesize);
 -      swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
 -
 -      swap_submit_gauge (NULL, "used", swap_alloc);
 -      swap_submit_gauge (NULL, "free", swap_avail);
 -      swap_submit_gauge (NULL, "reserved", swap_resv);
 +      swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
 +      swap_resv  = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
 +      swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
  
 +      swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
        return (0);
  } /* }}} int swap_read_kstat */
  /* #endif 0 && HAVE_LIBKSTAT */
@@@ -507,8 -520,8 +507,8 @@@ static int swap_read (void) /* {{{ *
          int status;
          int i;
  
 -        derive_t avail = 0;
 -        derive_t total = 0;
 +        gauge_t avail = 0;
 +        gauge_t total = 0;
  
          swap_num = swapctl (SC_GETNSWP, NULL);
          if (swap_num < 0)
                  return (0);
  
        /* Allocate and initialize the swaptbl_t structure */
-         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
+         s = malloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
          if (s == NULL)
          {
-                 ERROR ("swap plugin: smalloc failed.");
+                 ERROR ("swap plugin: malloc failed.");
                  return (-1);
          }
  
        s_paths = calloc (swap_num, PATH_MAX);
        if (s_paths == NULL)
        {
-               ERROR ("swap plugin: malloc failed.");
+               ERROR ("swap plugin: calloc failed.");
                sfree (s);
                return (-1);
        }
          for (i = 0; i < swap_num; i++)
          {
                char path[PATH_MAX];
 -              derive_t this_total;
 -              derive_t this_avail;
 +              gauge_t this_total;
 +              gauge_t this_avail;
  
                  if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
                          continue;
  
 -              this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
 -              this_avail = ((derive_t) s->swt_ent[i].ste_free)  * pagesize;
 +              this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
 +              this_avail = (gauge_t) (s->swt_ent[i].ste_free  * pagesize);
  
                /* Shortcut for the "combined" setting (default) */
                if (!report_by_device)
                sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
                escape_slashes (path, sizeof (path));
  
 -              swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail));
 -              swap_submit_gauge (path, "free", (gauge_t) this_avail);
 +              swap_submit_usage (path, this_total - this_avail, this_avail,
 +                              NULL, NAN);
          } /* for (swap_num) */
  
          if (total < avail)
          {
 -                ERROR ("swap plugin: Total swap space (%"PRIi64") "
 -                                "is less than free swap space (%"PRIi64").",
 +                ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
                                  total, avail);
                sfree (s_paths);
                  sfree (s);
                  return (-1);
          }
  
 -      /* If the "separate" option was specified (report_by_device == 2), all
 +      /* If the "separate" option was specified (report_by_device == 1), all
         * values have already been dispatched from within the loop. */
        if (!report_by_device)
 -      {
 -              swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
 -              swap_submit_gauge (NULL, "free", (gauge_t) avail);
 -      }
 +              swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
  
        sfree (s_paths);
          sfree (s);
@@@ -623,8 -640,8 +623,8 @@@ static int swap_read (void) /* {{{ *
        int status;
        int i;
  
 -      derive_t used  = 0;
 -      derive_t total = 0;
 +      gauge_t used  = 0;
 +      gauge_t total = 0;
  
        swap_num = swapctl (SWAP_NSWAP, NULL, 0);
        if (swap_num < 0)
        }
  
  #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
 -# define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
 +# define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
  #else
 -# define C_SWAP_BLOCK_SIZE ((derive_t) 512)
 +# define C_SWAP_BLOCK_SIZE 512.0
  #endif
  
 +      /* TODO: Report per-device stats. The path name is available from
 +       * swap_entries[i].se_path */
        for (i = 0; i < swap_num; i++)
        {
                if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
                        continue;
  
 -              used  += ((derive_t) swap_entries[i].se_inuse)
 -                      * C_SWAP_BLOCK_SIZE;
 -              total += ((derive_t) swap_entries[i].se_nblks)
 -                      * C_SWAP_BLOCK_SIZE;
 +              used  += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
 +              total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
        }
  
        if (total < used)
        {
 -              ERROR ("swap plugin: Total swap space (%"PRIu64") "
 -                              "is less than used swap space (%"PRIu64").",
 +              ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
                                total, used);
                return (-1);
        }
  
 -      swap_submit_gauge (NULL, "used", (gauge_t) used);
 -      swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
 +      swap_submit_usage (NULL, used, total - used, NULL, NAN);
  
        sfree (swap_entries);
 -
        return (0);
  } /* }}} int swap_read */
  /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
@@@ -701,9 -721,8 +701,9 @@@ static int swap_read (void) /* {{{ *
                return (-1);
  
        /* The returned values are bytes. */
 -      swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
 -      swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
 +      swap_submit_usage (NULL,
 +                      (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
 +                      NULL, NAN);
  
        return (0);
  } /* }}} int swap_read */
@@@ -715,8 -734,9 +715,8 @@@ static int swap_read (void) /* {{{ *
        struct kvm_swap data_s;
        int             status;
  
 -      derive_t used;
 -      derive_t free;
 -      derive_t total;
 +      gauge_t used;
 +      gauge_t total;
  
        if (kvm_obj == NULL)
                return (-1);
        if (status == -1)
                return (-1);
  
 -      total = (derive_t) data_s.ksw_total;
 -      used  = (derive_t) data_s.ksw_used;
 -
 -      total *= (derive_t) kvm_pagesize;
 -      used  *= (derive_t) kvm_pagesize;
 +      total = (gauge_t) data_s.ksw_total;
 +      used  = (gauge_t) data_s.ksw_used;
  
 -      free = total - used;
 +      total *= (gauge_t) kvm_pagesize;
 +      used  *= (gauge_t) kvm_pagesize;
  
 -      swap_submit_gauge (NULL, "used", (gauge_t) used);
 -      swap_submit_gauge (NULL, "free", (gauge_t) free);
 +      swap_submit_usage (NULL, used, total - used, NULL, NAN);
  
        return (0);
  } /* }}} int swap_read */
@@@ -744,11 -767,12 +744,11 @@@ static int swap_read (void) /* {{{ *
        sg_swap_stats *swap;
  
        swap = sg_get_swap_stats ();
 -
        if (swap == NULL)
                return (-1);
  
 -      swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
 -      swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
 +      swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
 +                      NULL, NAN);
  
        return (0);
  } /* }}} int swap_read */
  #elif HAVE_PERFSTAT
  static int swap_read (void) /* {{{ */
  {
 -        if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
 +      perfstat_memory_total_t pmemory;
 +      int status;
 +
 +      gauge_t total;
 +      gauge_t free;
 +      gauge_t reserved;
 +
 +      memset (&pmemory, 0, sizeof (pmemory));
 +        status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
 +      if (status < 0)
        {
                  char errbuf[1024];
 -                WARNING ("memory plugin: perfstat_memory_total failed: %s",
 +                WARNING ("swap plugin: perfstat_memory_total failed: %s",
                          sstrerror (errno, errbuf, sizeof (errbuf)));
                  return (-1);
          }
  
 -      swap_submit_gauge (NULL, "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
 -      swap_submit_gauge (NULL, "free", (gauge_t) pmemory.pgsp_free * pagesize );
 -      swap_submit_gauge (NULL, "reserved", (gauge_t) pmemory.pgsp_rsvd * pagesize);
 -      swap_submit_derive (NULL, "in",  (derive_t) pmemory.pgspins * pagesize);
 -      swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
 +      total    = (gauge_t) (pmemory.pgsp_total * pagesize);
 +      free     = (gauge_t) (pmemory.pgsp_free * pagesize);
 +      reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
 +
 +      swap_submit_usage (NULL, total - free, free, "reserved", reserved);
 +      swap_submit_derive ("in",  (derive_t) pmemory.pgspins * pagesize);
 +      swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
  
        return (0);
  } /* }}} int swap_read */
  
  void module_register (void)
  {
 -      plugin_register_config ("swap", swap_config,
 -                      config_keys, config_keys_num);
 +      plugin_register_complex_config ("swap", swap_config);
        plugin_register_init ("swap", swap_init);
        plugin_register_read ("swap", swap_read);
  } /* void module_register */