1 /**
2 * collectd - src/contextswitch.c
3 * Copyright (C) 2009 Patrik Weiskircher
4 * Copyright (C) 2010 Kimo Rosenbaum
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Patrik Weiskircher <weiskircher at inqnet.at>
21 * Kimo Rosenbaum <http://github.com/kimor79>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
30 #endif
32 #if HAVE_SYSCTLBYNAME
33 /* no global variables */
34 /* #endif HAVE_SYSCTLBYNAME */
36 #elif KERNEL_LINUX
37 /* no global variables */
38 /* #endif KERNEL_LINUX */
40 #else
41 # error "No applicable input method."
42 #endif
44 static void cs_submit (derive_t context_switches)
45 {
46 value_t values[1];
47 value_list_t vl = VALUE_LIST_INIT;
49 values[0].derive = (derive_t) context_switches;
51 vl.values = values;
52 vl.values_len = 1;
53 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
54 sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
55 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
57 plugin_dispatch_values (&vl);
58 }
60 static int cs_read (void)
61 {
62 #if HAVE_SYSCTLBYNAME
63 int value = 0;
64 size_t value_len = sizeof (value);
65 int status;
67 status = sysctlbyname ("vm.stats.sys.v_swtch",
68 &value, &value_len,
69 /* new pointer = */ NULL, /* new length = */ 0);
70 if (status != 0)
71 {
72 ERROR("contextswitch plugin: sysctlbyname "
73 "(vm.stats.sys.v_swtch) failed");
74 return (-1);
75 }
77 cs_submit (value);
78 /* #endif HAVE_SYSCTLBYNAME */
80 #elif KERNEL_LINUX
81 FILE *fh;
82 char buffer[64];
83 int numfields;
84 char *fields[3];
85 derive_t result = 0;
86 int status = -2;
88 fh = fopen ("/proc/stat", "r");
89 if (fh == NULL) {
90 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
91 sstrerror (errno, buffer, sizeof (buffer)));
92 return (-1);
93 }
95 while (fgets(buffer, sizeof(buffer), fh) != NULL)
96 {
97 char *endptr;
99 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
100 if (numfields != 2)
101 continue;
103 if (strcmp("ctxt", fields[0]) != 0)
104 continue;
106 errno = 0;
107 endptr = NULL;
108 result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
109 if ((endptr == fields[1]) || (errno != 0)) {
110 ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
111 fields[1]);
112 status = -1;
113 break;
114 }
116 cs_submit(result);
117 status = 0;
118 break;
119 }
120 fclose(fh);
122 if (status == -2)
123 ERROR ("contextswitch plugin: Unable to find context switch value.");
124 #endif /* KERNEL_LINUX */
126 return status;
127 }
129 void module_register (void)
130 {
131 plugin_register_read ("contextswitch", cs_read);
132 } /* void module_register */