Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / swap.c
index 508f9d5..e4c5d24 100644 (file)
@@ -157,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);
@@ -165,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 */
@@ -205,11 +204,11 @@ 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 */
@@ -282,7 +281,8 @@ static int swap_read_separate (void) /* {{{ */
                if (total < used)
                        continue;
 
-               swap_submit_usage (path, used, total - used, NULL, NAN);
+               swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0,
+                               NULL, NAN);
        }
 
        fclose (fh);
@@ -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)
@@ -320,35 +319,31 @@ 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);
 
-       swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
+       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 */
 
@@ -526,10 +521,10 @@ static int swap_read (void) /* {{{ */
                 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);
         }
 
@@ -539,7 +534,7 @@ static int swap_read (void) /* {{{ */
        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);
        }