Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / swap.c
index 564d606..a8276c7 100644 (file)
@@ -106,61 +106,36 @@ static int pagesize;
 static _Bool values_absolute = 1;
 static _Bool values_percentage = 0;
 
-static const char *config_keys[] =
+static int swap_config (oconfig_item_t *ci) /* {{{ */
 {
-       "ReportBytes",
-       "ReportByDevice",
-       "ValuesAbsolute",
-       "ValuesPercentage"
-};
-static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
-static int swap_config (const char *key, const char *value) /* {{{ */
-{
-       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 if (strcasecmp (key, "ValuesAbsolute") == 0)
-       {
-               if (IS_TRUE (value))
-                       values_absolute = 1;
-               else
-                       values_absolute = 0;
-
-               return (0);
-       }
-       else if (strcasecmp (key, "ValuesPercentage") == 0)
-       {
-               if (IS_TRUE (value))
-                       values_percentage = 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
-                       values_percentage = 0;
-
-               return (0);
-       }
-       else
-       {
-               return (-1);
+                       WARNING ("swap plugin: Unknown config option: \"%s\"",
+                                       child->key);
        }
 
        return (0);
@@ -182,6 +157,8 @@ static int swap_init (void) /* {{{ */
 /* #endif defined(VM_SWAPUSAGE) */
 
 #elif HAVE_LIBKVM_GETSWAPINFO
+       char errbuf[_POSIX2_LINE_MAX];
+
        if (kvm_obj != NULL)
        {
                kvm_close (kvm_obj);
@@ -190,14 +167,11 @@ static int swap_init (void) /* {{{ */
 
        kvm_pagesize = getpagesize ();
 
-       if ((kvm_obj = kvm_open (NULL, /* execfile */
-                                       NULL, /* corefile */
-                                       NULL, /* swapfile */
-                                       O_RDONLY, /* flags */
-                                       NULL)) /* errstr */
-                       == NULL)
+       kvm_obj = kvm_openfiles (NULL, "/dev/null", NULL, O_RDONLY, errbuf);
+
+       if (kvm_obj == NULL)
        {
-               ERROR ("swap plugin: kvm_open failed.");
+               ERROR ("swap plugin: kvm_openfiles failed, %s", errbuf);
                return (-1);
        }
 /* #endif HAVE_LIBKVM_GETSWAPINFO */
@@ -213,27 +187,6 @@ 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)
-{
-       value_list_t vl = VALUE_LIST_INIT;
-
-       assert (type != NULL);
-
-       vl.values = &value;
-       vl.values_len = 1;
-       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 */
-
 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
                gauge_t used, gauge_t free,
                char const *other_name, gauge_t other_value)
@@ -251,23 +204,33 @@ static void swap_submit_usage (char const *plugin_instance, /* {{{ */
        sstrncpy (vl.type, "swap", sizeof (vl.type));
 
        if (values_absolute)
-               plugin_dispatch_multivalue (&vl, 0,
+               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,
+               plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
                                "used", used, "free", free,
                                other_name, other_value, NULL);
 } /* }}} void swap_submit_usage */
 
 #if KERNEL_LINUX || HAVE_PERFSTAT
-static void swap_submit_derive (const char *plugin_instance, /* {{{ */
-               const char *type_instance, derive_t value)
+__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[0].derive = value;
 
-       v.derive = value;
-       swap_submit (plugin_instance, "swap_io", type_instance, 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));
+
+       plugin_dispatch_values (&vl);
 } /* }}} void swap_submit_derive */
 #endif
 
@@ -331,11 +294,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)
@@ -356,36 +318,30 @@ static int swap_read_combined (void) /* {{{ */
                        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 != 0x07)
+       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);
+       /* 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_used = swap_total - (swap_free + swap_cached);
+       if (swap_used < 0.0)
+               return (EINVAL);
 
-       INFO ("swap plugin: used = %g, free = %g, cached = %g", swap_used, swap_free, swap_cached);
-       swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
+       swap_submit_usage (NULL, swap_used, swap_free,
+                       isnan (swap_cached) ? NULL : "cached", swap_cached);
        return (0);
 } /* }}} int swap_read_combined */
 
@@ -463,8 +419,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 */
@@ -821,8 +777,8 @@ static int swap_read (void) /* {{{ */
        reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
 
        swap_submit_usage (NULL, total - free, free, "reserved", reserved);
-       swap_submit_derive (NULL, "in",  (derive_t) pmemory.pgspins * pagesize);
-       swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
+       swap_submit_derive ("in",  (derive_t) pmemory.pgspins * pagesize);
+       swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
 
        return (0);
 } /* }}} int swap_read */
@@ -830,8 +786,7 @@ static int swap_read (void) /* {{{ */
 
 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 */