[collectd] [PATCH] Added contextswitch plugin (for linux).

Patrik Weiskircher weiskircher at inqnet.at
Thu Oct 1 13:57:12 CEST 2009


---
 configure.in         |    4 ++
 src/Makefile.am      |    8 ++++
 src/collectd.conf.in |    1 +
 src/contextswitch.c  |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/types.db         |    1 +
 5 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 src/contextswitch.c

diff --git a/configure.in b/configure.in
index 56a5b26..d13bf6f 100644
--- a/configure.in
+++ b/configure.in
@@ -3495,6 +3495,7 @@ plugin_ascent="no"
 plugin_battery="no"
 plugin_bind="no"
 plugin_conntrack="no"
+plugin_contextswitch="no"
 plugin_cpu="no"
 plugin_cpufreq="no"
 plugin_curl_json="no"
@@ -3534,6 +3535,7 @@ then
 	plugin_conntrack="yes"
 	plugin_cpu="yes"
 	plugin_cpufreq="yes"
+	plugin_contextswitch="yes"
 	plugin_disk="yes"
 	plugin_entropy="yes"
 	plugin_interface="yes"
@@ -3767,6 +3769,7 @@ AC_PLUGIN([ascent],      [$plugin_ascent],     [AscentEmu player statistics])
 AC_PLUGIN([battery],     [$plugin_battery],    [Battery statistics])
 AC_PLUGIN([bind],        [$plugin_bind],       [ISC Bind nameserver statistics])
 AC_PLUGIN([conntrack],   [$plugin_conntrack],  [nf_conntrack statistics])
+AC_PLUGIN([contextswitch], [$plugin_contextswitch], [context switch statistics])
 AC_PLUGIN([cpufreq],     [$plugin_cpufreq],    [CPU frequency statistics])
 AC_PLUGIN([cpu],         [$plugin_cpu],        [CPU usage statistics])
 AC_PLUGIN([csv],         [yes],                [CSV output plugin])
@@ -4068,6 +4071,7 @@ Configuration:
     battery . . . . . . . $enable_battery
     bind  . . . . . . . . $enable_bind
     conntrack . . . . . . $enable_conntrack
+    contextswitch . . . . $enable_contextswitch
     cpu . . . . . . . . . $enable_cpu
     cpufreq . . . . . . . $enable_cpufreq
     csv . . . . . . . . . $enable_csv
diff --git a/src/Makefile.am b/src/Makefile.am
index 54befad..cc1dae6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -173,6 +173,14 @@ collectd_LDADD += "-dlopen" conntrack.la
 collectd_DEPENDENCIES += conntrack.la
 endif
 
+if BUILD_PLUGIN_CONTEXTSWITCH
+pkglib_LTLIBRARIES += contextswitch.la
+contextswitch_la_SOURCES = contextswitch.c
+contextswitch_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" contextswitch.la
+collectd_DEPENDENCIES += contextswitch.la
+endif
+
 if BUILD_PLUGIN_CPU
 pkglib_LTLIBRARIES += cpu.la
 cpu_la_SOURCES = cpu.c
diff --git a/src/collectd.conf.in b/src/collectd.conf.in
index e78f9dc..7af3952 100644
--- a/src/collectd.conf.in
+++ b/src/collectd.conf.in
@@ -57,6 +57,7 @@ FQDNLookup   true
 #@BUILD_PLUGIN_BATTERY_TRUE at LoadPlugin battery
 #@BUILD_PLUGIN_BIND_TRUE at LoadPlugin bind
 #@BUILD_PLUGIN_CONNTRACK_TRUE at LoadPlugin conntrack
+#@BUILD_PLUGIN_CONTEXTSWITCH_TRUE at LoadPlugin contextswitch
 @BUILD_PLUGIN_CPU_TRUE@@BUILD_PLUGIN_CPU_TRUE at LoadPlugin cpu
 #@BUILD_PLUGIN_CPUFREQ_TRUE at LoadPlugin cpufreq
 @LOAD_PLUGIN_CSV at LoadPlugin csv
diff --git a/src/contextswitch.c b/src/contextswitch.c
new file mode 100644
index 0000000..3348d55
--- /dev/null
+++ b/src/contextswitch.c
@@ -0,0 +1,95 @@
+/**
+ * collectd - src/contextswitch.c
+ * Copyright (C) 2009  Patrik Weiskircher
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Patrik Weiskircher <weiskircher at inqnet.at>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#if !KERNEL_LINUX
+# error "No applicable input method."
+#endif
+
+static void cs_submit (unsigned long context_switches)
+{
+	value_t values[1];
+	value_list_t vl = VALUE_LIST_INIT;
+
+	values[0].derive = context_switches;
+
+	vl.values = values;
+	vl.values_len = 1;
+	sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+	sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
+	sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
+
+	plugin_dispatch_values (&vl);
+}
+
+static int cs_read (void)
+{
+	FILE *fh;
+	char buffer[64];
+	int numfields;
+	char *fields[2];
+	unsigned long result = 0;
+
+	fh = fopen ("/proc/stat", "r");
+	if (fh == NULL) {
+		ERROR ("contextswitch plugin: unable to open /proc/stat: %s", 
+				sstrerror (errno, buffer, sizeof (buffer)));
+		return (-1);
+	}
+
+	while (fgets(buffer, sizeof(buffer), fh))
+	{
+		if (strncmp(buffer, "ctxt", 4))
+			continue;
+
+		numfields = strsplit(buffer, fields, 2);
+		if (numfields != 2) {
+			ERROR ("contextswitch plugin: ctxt in /proc/stat contains more than 2 fields.");
+			break;
+		}
+
+		result = strtoul(fields[1], NULL, 10);
+		if (errno == ERANGE && result == ULONG_MAX) {
+			ERROR ("contextswitch plugin: ctxt value in /proc/stat overflows.");
+			break;
+		}
+
+		break;
+	}
+	fclose(fh);
+
+	if (result == 0) {
+		ERROR ("contextswitch plugin: unable to find context switch value.");
+		return -1;
+	}
+
+	cs_submit(result);
+
+	return 0;
+}
+
+void module_register (void)
+{
+	plugin_register_read ("contextswitch", cs_read);
+} /* void module_register */
diff --git a/src/types.db b/src/types.db
index 177c57b..fb4ea7a 100644
--- a/src/types.db
+++ b/src/types.db
@@ -18,6 +18,7 @@ charge			value:GAUGE:0:U
 compression_ratio	value:GAUGE:0:2
 connections		value:COUNTER:0:U
 conntrack		entropy:GAUGE:0:4294967295
+contextswitch		contextswitches:DERIVE:0:U
 counter			value:COUNTER:U:U
 cpufreq			value:GAUGE:0:U
 cpu			value:COUNTER:0:4294967295
-- 
1.5.6.5




More information about the collectd mailing list