f571d39c39201c174d0cfea1ba68054a6565ad0d
[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 #include "common.h"
26 #include "plugin.h"
27
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
30 #endif
31
32 #if HAVE_SYSCTLBYNAME
33 /* no global variables */
34 /* #endif HAVE_SYSCTLBYNAME */
35
36 #elif KERNEL_LINUX
37 /* no global variables */
38 /* #endif KERNEL_LINUX */
39
40 #else
41 # error "No applicable input method."
42 #endif
43
44 static void cs_submit (derive_t context_switches)
45 {
46         value_t values[1];
47         value_list_t vl = VALUE_LIST_INIT;
48
49         values[0].derive = (derive_t) context_switches;
50
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));
56
57         plugin_dispatch_values (&vl);
58 }
59
60 static int cs_read (void)
61 {
62         int status = -2;
63 #if HAVE_SYSCTLBYNAME
64         int value;
65         size_t value_len = sizeof (value);
66
67         if (sysctlbyname ("vm.stats.sys.v_swtch", (void *) &value, &value_len,
68                         NULL, 0) == 0)
69         {
70                 cs_submit(value);
71                 status = 0;
72         }
73         else
74         {
75                 ERROR("contextswitch plugin: sysctlbyname failed");
76         }
77
78 /* #endif HAVE_SYSCTLBYNAME */
79 #elif KERNEL_LINUX
80         FILE *fh;
81         char buffer[64];
82         int numfields;
83         char *fields[3];
84         derive_t result = 0;
85
86         fh = fopen ("/proc/stat", "r");
87         if (fh == NULL) {
88                 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
89                                 sstrerror (errno, buffer, sizeof (buffer)));
90                 return (-1);
91         }
92
93         while (fgets(buffer, sizeof(buffer), fh) != NULL)
94         {
95                 char *endptr;
96
97                 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
98                 if (numfields != 2)
99                         continue;
100
101                 if (strcmp("ctxt", fields[0]) != 0)
102                         continue;
103
104                 errno = 0;
105                 endptr = NULL;
106                 result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
107                 if ((endptr == fields[1]) || (errno != 0)) {
108                         ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
109                                         fields[1]);
110                         status = -1;
111                         break;
112                 }
113
114                 cs_submit(result);
115                 status = 0;
116                 break;
117         }
118         fclose(fh);
119
120         if (status == -2)
121                 ERROR ("contextswitch plugin: Unable to find context switch value.");
122 #endif /* KERNEL_LINUX */
123
124         return status;
125 }
126
127 void module_register (void)
128 {
129         plugin_register_read ("contextswitch", cs_read);
130 } /* void module_register */