contextswitches, processes plugins: Don't print an error if number of fields don...
[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 #if !KERNEL_LINUX
27 # error "No applicable input method."
28 #endif
29
30 static void cs_submit (unsigned long context_switches)
31 {
32         value_t values[1];
33         value_list_t vl = VALUE_LIST_INIT;
34
35         values[0].derive = (derive_t) context_switches;
36
37         vl.values = values;
38         vl.values_len = 1;
39         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
40         sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
41         sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
42
43         plugin_dispatch_values (&vl);
44 }
45
46 static int cs_read (void)
47 {
48         FILE *fh;
49         char buffer[64];
50         int numfields;
51         char *fields[3];
52         unsigned long result = 0;
53         int status = -2;
54
55         fh = fopen ("/proc/stat", "r");
56         if (fh == NULL) {
57                 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
58                                 sstrerror (errno, buffer, sizeof (buffer)));
59                 return (-1);
60         }
61
62         while (fgets(buffer, sizeof(buffer), fh) != NULL)
63         {
64                 char *endptr;
65
66                 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
67                 if (numfields != 2)
68                         continue;
69
70                 if (strcmp("ctxt", fields[0]) != 0)
71                         continue;
72
73                 errno = 0;
74                 endptr = NULL;
75                 result = strtoul(fields[1], &endptr, 10);
76                 if ((endptr == fields[1]) || (errno != 0)) {
77                         ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
78                                         fields[1]);
79                         status = -1;
80                         break;
81                 }
82
83                 cs_submit(result);
84                 status = 0;
85                 break;
86         }
87         fclose(fh);
88
89         if (status == -2)
90                 ERROR ("contextswitch plugin: Unable to find context switch value.");
91
92         return status;
93 }
94
95 void module_register (void)
96 {
97         plugin_register_read ("contextswitch", cs_read);
98 } /* void module_register */