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 #elif HAVE_PERFSTAT
41 # include <sys/protosw.h>
42 # include <libperfstat.h>
43 /* #endif HAVE_PERFSTAT */
45 #else
46 # error "No applicable input method."
47 #endif
49 static void cs_submit (derive_t context_switches)
50 {
51 value_t values[1];
52 value_list_t vl = VALUE_LIST_INIT;
54 values[0].derive = (derive_t) context_switches;
56 vl.values = values;
57 vl.values_len = 1;
58 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
59 sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
60 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
62 plugin_dispatch_values (&vl);
63 }
65 static int cs_read (void)
66 {
67 #if HAVE_SYSCTLBYNAME
68 int value = 0;
69 size_t value_len = sizeof (value);
70 int status;
72 status = sysctlbyname ("vm.stats.sys.v_swtch",
73 &value, &value_len,
74 /* new pointer = */ NULL, /* new length = */ 0);
75 if (status != 0)
76 {
77 ERROR("contextswitch plugin: sysctlbyname "
78 "(vm.stats.sys.v_swtch) failed");
79 return (-1);
80 }
82 cs_submit (value);
83 /* #endif HAVE_SYSCTLBYNAME */
85 #elif KERNEL_LINUX
86 FILE *fh;
87 char buffer[64];
88 int numfields;
89 char *fields[3];
90 derive_t result = 0;
91 int status = -2;
93 fh = fopen ("/proc/stat", "r");
94 if (fh == NULL) {
95 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
96 sstrerror (errno, buffer, sizeof (buffer)));
97 return (-1);
98 }
100 while (fgets(buffer, sizeof(buffer), fh) != NULL)
101 {
102 char *endptr;
104 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
105 if (numfields != 2)
106 continue;
108 if (strcmp("ctxt", fields[0]) != 0)
109 continue;
111 errno = 0;
112 endptr = NULL;
113 result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
114 if ((endptr == fields[1]) || (errno != 0)) {
115 ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
116 fields[1]);
117 status = -1;
118 break;
119 }
121 cs_submit(result);
122 status = 0;
123 break;
124 }
125 fclose(fh);
127 if (status == -2)
128 ERROR ("contextswitch plugin: Unable to find context switch value.");
129 /* #endif KERNEL_LINUX */
131 #elif HAVE_PERFSTAT
132 int status = 0;
133 perfstat_cpu_total_t perfcputotal;
135 status = perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1);
136 if (status < 0)
137 {
138 char errbuf[1024];
139 ERROR ("contextswitch plugin: perfstat_cpu_total: %s",
140 sstrerror (errno, errbuf, sizeof (errbuf)));
141 return (-1);
142 }
144 cs_submit(perfcputotal.pswitch);
145 status = 0;
146 #endif /* defined(HAVE_PERFSTAT) */
148 return status;
149 }
151 void module_register (void)
152 {
153 plugin_register_read ("contextswitch", cs_read);
154 } /* void module_register */