From 11c3445e8931bbcefcb5b665a28fdae7691ebc6a Mon Sep 17 00:00:00 2001 From: Patrik Weiskircher Date: Thu, 1 Oct 2009 13:57:12 +0200 Subject: [PATCH] Added contextswitch plugin (for linux). Signed-off-by: Florian Forster --- 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(+) create mode 100644 src/contextswitch.c diff --git a/configure.in b/configure.in index 08de43b1..f091d025 100644 --- a/configure.in +++ b/configure.in @@ -3567,6 +3567,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" @@ -3604,6 +3605,7 @@ if test "x$ac_system" = "xLinux" then plugin_battery="yes" plugin_conntrack="yes" + plugin_contextswitch="yes" plugin_cpu="yes" plugin_cpufreq="yes" plugin_disk="yes" @@ -3839,6 +3841,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]) @@ -4142,6 +4145,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 2e9f265e..9bae902b 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 8f28f8f3..8d14f973 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -57,6 +57,7 @@ FQDNLookup true #@BUILD_PLUGIN_BATTERY_TRUE@LoadPlugin battery #@BUILD_PLUGIN_BIND_TRUE@LoadPlugin bind #@BUILD_PLUGIN_CONNTRACK_TRUE@LoadPlugin conntrack +#@BUILD_PLUGIN_CONTEXTSWITCH_TRUE@LoadPlugin contextswitch @BUILD_PLUGIN_CPU_TRUE@@BUILD_PLUGIN_CPU_TRUE@LoadPlugin cpu #@BUILD_PLUGIN_CPUFREQ_TRUE@LoadPlugin cpufreq @LOAD_PLUGIN_CSV@LoadPlugin csv diff --git a/src/contextswitch.c b/src/contextswitch.c new file mode 100644 index 00000000..0db727c7 --- /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 + **/ + +#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 399dbd23..0cbd4032 100644 --- a/src/types.db +++ b/src/types.db @@ -19,6 +19,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 -- 2.11.0