[collectd] [PATCH] added swapin/out support to swap plugin

Stefan Völkel bd at bc-bd.org
Mon Aug 17 15:50:17 CEST 2009


---
 src/swap.c   |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 src/types.db |    1 +
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/src/swap.c b/src/swap.c
index de9b5f3..dccea6d 100644
--- a/src/swap.c
+++ b/src/swap.c
@@ -138,18 +138,30 @@ static int swap_init (void)
 	return (0);
 }
 
-static void swap_submit (const char *type_instance, double value)
+static void swap_submit (const char *type_instance, derive_t value, unsigned type)
 {
 	value_t values[1];
 	value_list_t vl = VALUE_LIST_INIT;
 
-	values[0].gauge = value;
+	switch (type)
+	{
+		case DS_TYPE_GAUGE:
+			values[0].gauge = value;
+			sstrncpy (vl.type, "swap", sizeof (vl.type));
+			break;
+		case DS_TYPE_DERIVE:
+			values[0].derive = value;
+			sstrncpy (vl.type, "swap_io", sizeof (vl.type));
+			break;
+		default:
+			ERROR ("swap plugin: swap_submit called with wrong"
+				" type");
+	}
 
 	vl.values = values;
 	vl.values_len = 1;
 	sstrncpy (vl.host, hostname_g, sizeof (vl.host));
 	sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
-	sstrncpy (vl.type, "swap", sizeof (vl.type));
 	sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
 	plugin_dispatch_values (&vl);
@@ -168,6 +180,8 @@ static int swap_read (void)
 	unsigned long long swap_cached = 0LL;
 	unsigned long long swap_free   = 0LL;
 	unsigned long long swap_total  = 0LL;
+	unsigned long long swap_in     = 0LL;
+	unsigned long long swap_out    = 0LL;
 
 	if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
 	{
@@ -210,9 +224,46 @@ static int swap_read (void)
 
 	swap_used = swap_total - (swap_free + swap_cached);
 
-	swap_submit ("used", swap_used);
-	swap_submit ("free", swap_free);
-	swap_submit ("cached", swap_cached);
+	if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
+	{
+		char errbuf[1024];
+		WARNING ("swap: fopen: %s",
+				sstrerror (errno, errbuf, sizeof (errbuf)));
+		return (-1);
+	}
+
+	while (fgets (buffer, 1024, fh) != NULL)
+	{
+		unsigned long long *val = NULL;
+
+		if (strncasecmp (buffer, "pswpin", 6) == 0)
+			val = &swap_in;
+		else if (strncasecmp (buffer, "pswpout", 7) == 0)
+			val = &swap_out;
+		else
+			continue;
+
+		numfields = strsplit (buffer, fields, 8);
+
+		if (numfields < 2)
+			continue;
+
+		*val = atoll (fields[1]);
+	}
+
+	if (fclose (fh))
+	{
+		char errbuf[1024];
+		WARNING ("swap: fclose: %s",
+				sstrerror (errno, errbuf, sizeof (errbuf)));
+	}
+
+	swap_submit ("used", swap_used, DS_TYPE_GAUGE);
+	swap_submit ("free", swap_free, DS_TYPE_GAUGE);
+	swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
+	swap_submit ("in", swap_in, DS_TYPE_DERIVE);
+	swap_submit ("out", swap_out, DS_TYPE_DERIVE);
+
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
diff --git a/src/types.db b/src/types.db
index ec92f4b..dd37cc1 100644
--- a/src/types.db
+++ b/src/types.db
@@ -120,6 +120,7 @@ signal_quality		value:GAUGE:0:U
 spam_check		value:GAUGE:0:U
 spam_score		value:GAUGE:U:U
 swap			value:GAUGE:0:1099511627776
+swap_io			value:DERIVE:0:1099511627776
 tcp_connections		value:GAUGE:0:4294967295
 temperature		value:GAUGE:-273.15:U
 time_dispersion		seconds:GAUGE:-1000000:1000000
-- 
1.6.3.3




More information about the collectd mailing list