2 * collectd - src/contextswitch.c
3 * Copyright (C) 2009 Patrik Weiskircher
4 * Copyright (C) 2010 Kimo Rosenbaum
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Patrik Weiskircher <weiskircher at inqnet.at>
21 * Kimo Rosenbaum <http://github.com/kimor79>
29 #ifdef HAVE_SYS_SYSCTL_H
30 # include <sys/sysctl.h>
34 /* no global variables */
35 /* #endif HAVE_SYSCTLBYNAME */
38 /* no global variables */
39 /* #endif KERNEL_LINUX */
42 # include <sys/protosw.h>
43 # include <libperfstat.h>
44 /* #endif HAVE_PERFSTAT */
47 # error "No applicable input method."
50 static void cs_submit (derive_t context_switches)
53 value_list_t vl = VALUE_LIST_INIT;
55 values[0].derive = (derive_t) context_switches;
59 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
60 sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
61 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
63 plugin_dispatch_values (&vl);
66 static int cs_read (void)
70 size_t value_len = sizeof (value);
73 status = sysctlbyname ("vm.stats.sys.v_swtch",
75 /* new pointer = */ NULL, /* new length = */ 0);
78 ERROR("contextswitch plugin: sysctlbyname "
79 "(vm.stats.sys.v_swtch) failed");
84 /* #endif HAVE_SYSCTLBYNAME */
94 fh = fopen ("/proc/stat", "r");
96 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
97 sstrerror (errno, buffer, sizeof (buffer)));
101 while (fgets(buffer, sizeof(buffer), fh) != NULL)
105 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
109 if (strcmp("ctxt", fields[0]) != 0)
114 result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
115 if ((endptr == fields[1]) || (errno != 0)) {
116 ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
129 ERROR ("contextswitch plugin: Unable to find context switch value.");
130 /* #endif KERNEL_LINUX */
134 perfstat_cpu_total_t perfcputotal;
136 status = perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1);
140 ERROR ("contextswitch plugin: perfstat_cpu_total: %s",
141 sstrerror (errno, errbuf, sizeof (errbuf)));
145 cs_submit(perfcputotal.pswitch);
147 #endif /* defined(HAVE_PERFSTAT) */
152 void module_register (void)
154 plugin_register_read ("contextswitch", cs_read);
155 } /* void module_register */