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