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)
} /* }}} 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.derive = value;
- swap_submit (plugin_instance, "swap_io", type_instance, v);
+ v[0].derive = value;
+
+ 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
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 != 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, swap_free,
+ isnan (swap_cached) ? NULL : "cached", swap_cached);
return (0);
} /* }}} int swap_read_combined */
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 */
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 */