Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / contextswitch.c
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  **/
23
24 #include "collectd.h"
25
26 #include "common.h"
27 #include "plugin.h"
28
29 #ifdef HAVE_SYS_SYSCTL_H
30 # include <sys/sysctl.h>
31 #endif
32
33 #if HAVE_SYSCTLBYNAME
34 /* no global variables */
35 /* #endif HAVE_SYSCTLBYNAME */
36
37 #elif KERNEL_LINUX
38 /* no global variables */
39 /* #endif KERNEL_LINUX */
40
41 #elif HAVE_PERFSTAT
42 # include <sys/protosw.h>
43 # include <libperfstat.h>
44 /* #endif HAVE_PERFSTAT */
45
46 #else
47 # error "No applicable input method."
48 #endif
49
50 static void cs_submit (derive_t context_switches)
51 {
52         value_t values[1];
53         value_list_t vl = VALUE_LIST_INIT;
54
55         values[0].derive = (derive_t) context_switches;
56
57         vl.values = values;
58         vl.values_len = 1;
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));
62
63         plugin_dispatch_values (&vl);
64 }
65
66 static int cs_read (void)
67 {
68 #if HAVE_SYSCTLBYNAME
69         int value = 0;
70         size_t value_len = sizeof (value);
71         int status;
72
73         status = sysctlbyname ("vm.stats.sys.v_swtch",
74                         &value, &value_len,
75                         /* new pointer = */ NULL, /* new length = */ 0);
76         if (status != 0)
77         {
78                 ERROR("contextswitch plugin: sysctlbyname "
79                                 "(vm.stats.sys.v_swtch) failed");
80                 return (-1);
81         }
82
83         cs_submit (value);
84 /* #endif HAVE_SYSCTLBYNAME */
85
86 #elif KERNEL_LINUX
87         FILE *fh;
88         char buffer[64];
89         int numfields;
90         char *fields[3];
91         derive_t result = 0;
92         int status = -2;
93
94         fh = fopen ("/proc/stat", "r");
95         if (fh == NULL) {
96                 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
97                                 sstrerror (errno, buffer, sizeof (buffer)));
98                 return (-1);
99         }
100
101         while (fgets(buffer, sizeof(buffer), fh) != NULL)
102         {
103                 char *endptr;
104
105                 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
106                 if (numfields != 2)
107                         continue;
108
109                 if (strcmp("ctxt", fields[0]) != 0)
110                         continue;
111
112                 errno = 0;
113                 endptr = NULL;
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",
117                                         fields[1]);
118                         status = -1;
119                         break;
120                 }
121
122                 cs_submit(result);
123                 status = 0;
124                 break;
125         }
126         fclose(fh);
127
128         if (status == -2)
129                 ERROR ("contextswitch plugin: Unable to find context switch value.");
130 /* #endif  KERNEL_LINUX */
131
132 #elif HAVE_PERFSTAT
133         int status = 0;
134         perfstat_cpu_total_t perfcputotal;
135
136         status = perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1);
137         if (status < 0)
138         {
139                 char errbuf[1024];
140                 ERROR ("contextswitch plugin: perfstat_cpu_total: %s",
141                         sstrerror (errno, errbuf, sizeof (errbuf)));
142                 return (-1);
143         }
144
145         cs_submit(perfcputotal.pswitch);
146         status = 0;
147 #endif /* defined(HAVE_PERFSTAT) */
148
149         return status;
150 }
151
152 void module_register (void)
153 {
154         plugin_register_read ("contextswitch", cs_read);
155 } /* void module_register */