/**
* collectd - src/swap.c
- * Copyright (C) 2005-2010 Florian octo Forster
+ * Copyright (C) 2005-2012 Florian octo Forster
* Copyright (C) 2009 Stefan Völkel
* Copyright (C) 2009 Manuel Sanmartin
* Copyright (C) 2010 Aurélien Reynaud
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#if KERNEL_LINUX
-# define SWAP_HAVE_CONFIG 1
-/* No global variables */
+# define SWAP_HAVE_REPORT_BY_DEVICE 1
+static derive_t pagesize;
+static _Bool report_bytes = 0;
+static _Bool report_by_device = 0;
/* #endif KERNEL_LINUX */
#elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
-# define SWAP_HAVE_CONFIG 1
+# define SWAP_HAVE_REPORT_BY_DEVICE 1
static derive_t pagesize;
+static _Bool report_by_device = 0;
/* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
#elif defined(VM_SWAPUSAGE)
# error "No applicable input method."
#endif /* HAVE_LIBSTATGRAB */
-#if SWAP_HAVE_CONFIG
+static _Bool values_absolute = 1;
+static _Bool values_percentage = 0;
+
static const char *config_keys[] =
{
- "ReportByDevice"
+ "ReportBytes",
+ "ReportByDevice",
+ "ValuesAbsolute",
+ "ValuesPercentage"
};
static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-static _Bool report_by_device = 0;
-
static int swap_config (const char *key, const char *value) /* {{{ */
{
- if (strcasecmp ("ReportByDevice", key) == 0)
+ if (strcasecmp ("ReportBytes", key) == 0)
{
+#if KERNEL_LINUX
+ report_bytes = IS_TRUE (value) ? 1 : 0;
+#else
+ 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)
+ {
+#if SWAP_HAVE_REPORT_BY_DEVICE
if (IS_TRUE (value))
report_by_device = 1;
else
report_by_device = 0;
+#else
+ 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
+ values_percentage = 0;
+
+ return (0);
}
else
{
return (0);
} /* }}} int swap_config */
-#endif /* SWAP_HAVE_CONFIG */
static int swap_init (void) /* {{{ */
{
#if KERNEL_LINUX
- /* No init stuff */
+ pagesize = (derive_t) sysconf (_SC_PAGESIZE);
/* #endif KERNEL_LINUX */
#elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
swap_submit (plugin_instance, "swap", type_instance, v);
} /* }}} void swap_submit_gauge */
-#if KERNEL_LINUX
+#if KERNEL_LINUX || HAVE_PERFSTAT
static void swap_submit_derive (const char *plugin_instance, /* {{{ */
const char *type_instance, derive_t value)
{
v.derive = value;
swap_submit (plugin_instance, "swap_io", type_instance, v);
} /* }}} void swap_submit_derive */
+#endif
+#if KERNEL_LINUX
static int swap_read_separate (void) /* {{{ */
{
FILE *fh;
free = size - used;
- swap_submit_gauge (path, "used", used);
- swap_submit_gauge (path, "free", free);
+ if (values_absolute)
+ {
+ swap_submit_gauge (path, "used", used);
+ swap_submit_gauge (path, "free", free);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (path, "percent_used", (gauge_t) ((float_t) used) / (used + free) * 100);
+ swap_submit_gauge (path, "percent_free", (gauge_t) ((float_t) free) / (used + free) * 100);
+ }
}
fclose (fh);
swap_used = swap_total - (swap_free + swap_cached);
- swap_submit_gauge (NULL, "used", 1024.0 * swap_used);
- swap_submit_gauge (NULL, "free", 1024.0 * swap_free);
- swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", 1024.0 * swap_used);
+ swap_submit_gauge (NULL, "free", 1024.0 * swap_free);
+ swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap_used) / (swap_used + swap_free + swap_cached) * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap_free) / (swap_used + swap_free + swap_cached) * 100);
+ swap_submit_gauge (NULL, "percent_cached", (gauge_t) ((float_t) swap_cached) / (swap_used + swap_free + swap_cached) * 100);
+ }
return (0);
} /* }}} int swap_read_combined */
if (have_data != 0x03)
return (ENOENT);
+ if (report_bytes)
+ {
+ swap_in = swap_in * pagesize;
+ swap_out = swap_out * pagesize;
+ }
+
swap_submit_derive (NULL, "in", swap_in);
swap_submit_derive (NULL, "out", swap_out);
* 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);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", swap_alloc);
+ swap_submit_gauge (NULL, "free", swap_avail);
+ swap_submit_gauge (NULL, "reserved", swap_resv);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap_alloc) / (swap_alloc + swap_avail + swap_resv) * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap_avail) / (swap_alloc + swap_avail + swap_resv) * 100);
+ swap_submit_gauge (NULL, "percent_reserved", (gauge_t) ((float_t) swap_resv) / (swap_alloc + swap_avail + swap_resv) * 100);
+ }
return (0);
} /* }}} int swap_read_kstat */
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);
+ if (values_absolute)
+ {
+ swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail));
+ swap_submit_gauge (path, "free", (gauge_t) this_avail);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (path, "percent_used", (gauge_t) ((float_t) (this_total - this_avail)) / this_total * 100);
+ swap_submit_gauge (path, "percent_free", (gauge_t) ((float_t) this_avail) / this_total * 100);
+ }
+
} /* for (swap_num) */
if (total < avail)
* 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);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
+ swap_submit_gauge (NULL, "free", (gauge_t) avail);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) (total - avail)) / total * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) avail) / total * 100);
+ }
}
sfree (s_paths);
return (-1);
}
- swap_submit_gauge (NULL, "used", (gauge_t) used);
- swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", (gauge_t) used);
+ swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) used) / total * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) (total - used)) / total * 100);
+ }
sfree (swap_entries);
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);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
+ swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) sw_usage.xsu_used) / (sw_usage.xsu_used + sw_usage.xsu_avail) * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) sw_usage.xsu_avail) / (sw_usage.xsu_used + sw_usage.xsu_avail) * 100);
+ }
return (0);
} /* }}} int swap_read */
free = total - used;
- swap_submit_gauge (NULL, "used", (gauge_t) used);
- swap_submit_gauge (NULL, "free", (gauge_t) free);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", (gauge_t) used);
+ swap_submit_gauge (NULL, "free", (gauge_t) free);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) used) / total * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) free) / total * 100);
+ }
return (0);
} /* }}} int swap_read */
if (swap == NULL)
return (-1);
- swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
- swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
+ if (values_absolute)
+ {
+ swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
+ swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap->used) / (swap->used + swap->free) * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap->free) / (swap_used + swap->free) * 100);
+ }
return (0);
} /* }}} int swap_read */
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 );
+
+ if (values_absolute)
+ {
+ 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);
+ }
+ if (values_percentage)
+ {
+ swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) (pmemory.pgsp_total - pmemory.pgsp_free)) / pmemory.pgsp_total * 100);
+ swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) pmemory.pgsp_free) / pmemory.pgsp_total * 100);
+ swap_submit_gauge (NULL, "percent_reserved", (gauge_t) ((float_t) pmemory.pgsp_rsvd) / pmemory.pgsp_total * 100);
+ }
+ swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
+ swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
return (0);
} /* }}} int swap_read */
void module_register (void)
{
-#if SWAP_HAVE_CONFIG
- plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
-#endif
+ plugin_register_config ("swap", swap_config,
+ config_keys, config_keys_num);
plugin_register_init ("swap", swap_init);
plugin_register_read ("swap", swap_read);
} /* void module_register */