summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 99a9745)
raw | patch | inline | side by side (parent: 99a9745)
author | Florian Forster <octo@collectd.org> | |
Tue, 16 Sep 2014 05:19:09 +0000 (07:19 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Tue, 16 Sep 2014 05:22:37 +0000 (07:22 +0200) |
OpenVZ doesn't provide the line, which caused swap collection to be
skipped entirely on these systems. Instead, "cached" should simply be
ignored, as it is on other systems. Thanks to @zerkms for reporting this
problem!
Fixes: #733
skipped entirely on these systems. Instead, "cached" should simply be
ignored, as it is on other systems. Thanks to @zerkms for reporting this
problem!
Fixes: #733
src/swap.c | patch | blob | history |
diff --git a/src/swap.c b/src/swap.c
index 508f9d54cccbc04bb6fe05ce696bda18ad1a9350..0642afac78d82792318f40a57696cfe862d13ed2 100644 (file)
--- a/src/swap.c
+++ b/src/swap.c
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 */