2 * collectd - src/contextswitch.c
3 * Copyright (C) 2009 Patrik Weiskircher
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Patrik Weiskircher <weiskircher at inqnet.at>
27 # error "No applicable input method."
30 static void cs_submit (unsigned long context_switches)
33 value_list_t vl = VALUE_LIST_INIT;
35 values[0].derive = context_switches;
39 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
40 sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
41 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
43 plugin_dispatch_values (&vl);
46 static int cs_read (void)
52 unsigned long result = 0;
54 fh = fopen ("/proc/stat", "r");
56 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
57 sstrerror (errno, buffer, sizeof (buffer)));
61 while (fgets(buffer, sizeof(buffer), fh))
63 if (strncmp(buffer, "ctxt", 4))
66 numfields = strsplit(buffer, fields, 2);
68 ERROR ("contextswitch plugin: ctxt in /proc/stat contains more than 2 fields.");
72 result = strtoul(fields[1], NULL, 10);
73 if (errno == ERANGE && result == ULONG_MAX) {
74 ERROR ("contextswitch plugin: ctxt value in /proc/stat overflows.");
83 ERROR ("contextswitch plugin: unable to find context switch value.");
92 void module_register (void)
94 plugin_register_read ("contextswitch", cs_read);
95 } /* void module_register */