Merge branch 'pr/1826'
[collectd.git] / src / irq.c
1 /**
2  * collectd - src/irq.c
3  * Copyright (C) 2007  Peter Holik
4  * Copyright (C) 2011  Florian Forster
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; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Peter Holik <peter at holik.at>
22  **/
23
24 #include "collectd.h"
25
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_ignorelist.h"
30
31 #if !KERNEL_LINUX
32 # error "No applicable input method."
33 #endif
34
35 /*
36  * (Module-)Global variables
37  */
38 static const char *config_keys[] =
39 {
40         "Irq",
41         "IgnoreSelected"
42 };
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44
45 static ignorelist_t *ignorelist = NULL;
46
47 /*
48  * Private functions
49  */
50 static int irq_config (const char *key, const char *value)
51 {
52         if (ignorelist == NULL)
53                 ignorelist = ignorelist_create (/* invert = */ 1);
54
55         if (strcasecmp (key, "Irq") == 0)
56         {
57                 ignorelist_add (ignorelist, value);
58         }
59         else if (strcasecmp (key, "IgnoreSelected") == 0)
60         {
61                 int invert = 1;
62                 if (IS_TRUE (value))
63                         invert = 0;
64                 ignorelist_set_invert (ignorelist, invert);
65         }
66         else
67         {
68                 return (-1);
69         }
70
71         return (0);
72 }
73
74 static void irq_submit (const char *irq_name, derive_t value)
75 {
76         value_t values[1];
77         value_list_t vl = VALUE_LIST_INIT;
78
79         if (ignorelist_match (ignorelist, irq_name) != 0)
80                 return;
81
82         values[0].derive = value;
83
84         vl.values = values;
85         vl.values_len = 1;
86         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
87         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
88         sstrncpy (vl.type, "irq", sizeof (vl.type));
89         sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance));
90
91         plugin_dispatch_values (&vl);
92 } /* void irq_submit */
93
94 static int irq_read (void)
95 {
96         FILE *fh;
97         char buffer[1024];
98         int  cpu_count;
99         char *fields[256];
100
101         /*
102          * Example content:
103          *         CPU0       CPU1       CPU2       CPU3
104          * 0:       2574          1          3          2   IO-APIC-edge      timer
105          * 1:     102553     158669     218062      70587   IO-APIC-edge      i8042
106          * 8:          0          0          0          1   IO-APIC-edge      rtc0
107          */
108         fh = fopen ("/proc/interrupts", "r");
109         if (fh == NULL)
110         {
111                 char errbuf[1024];
112                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
113                                 sstrerror (errno, errbuf, sizeof (errbuf)));
114                 return (-1);
115         }
116
117         /* Get CPU count from the first line */
118         if(fgets (buffer, sizeof (buffer), fh) != NULL) {
119                 cpu_count = strsplit (buffer, fields,
120                                 STATIC_ARRAY_SIZE (fields));
121         } else {
122                 ERROR ("irq plugin: unable to get CPU count from first line "
123                                 "of /proc/interrupts");
124                 fclose (fh);
125                 return (-1);
126         }
127
128         while (fgets (buffer, sizeof (buffer), fh) != NULL)
129         {
130                 char *irq_name;
131                 size_t irq_name_len;
132                 derive_t irq_value;
133                 int i;
134                 int fields_num;
135                 int irq_values_to_parse;
136
137                 fields_num = strsplit (buffer, fields,
138                                 STATIC_ARRAY_SIZE (fields));
139                 if (fields_num < 2)
140                         continue;
141
142                 /* Parse this many numeric fields, skip the rest
143                  * (+1 because first there is a name of irq in each line) */
144                 if (fields_num >= cpu_count + 1)
145                         irq_values_to_parse = cpu_count;
146                 else
147                         irq_values_to_parse = fields_num - 1;
148
149                 /* First field is irq name and colon */
150                 irq_name = fields[0];
151                 irq_name_len = strlen (irq_name);
152                 if (irq_name_len < 2)
153                         continue;
154
155                 /* Check if irq name ends with colon.
156                  * Otherwise it's a header. */
157                 if (irq_name[irq_name_len - 1] != ':')
158                         continue;
159
160                 /* Is it the the ARM fast interrupt (FIQ)? */
161                 if (irq_name_len == 4 && (strncmp(irq_name, "FIQ:", 4) == 0))
162                         continue;
163
164                 irq_name[irq_name_len - 1] = 0;
165                 irq_name_len--;
166
167                 irq_value = 0;
168                 for (i = 1; i <= irq_values_to_parse; i++)
169                 {
170                         /* Per-CPU value */
171                         value_t v;
172                         int status;
173
174                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
175                         if (status != 0)
176                                 break;
177
178                         irq_value += v.derive;
179                 } /* for (i) */
180
181                 /* No valid fields -> do not submit anything. */
182                 if (i <= 1)
183                         continue;
184
185                 irq_submit (irq_name, irq_value);
186         }
187
188         fclose (fh);
189
190         return (0);
191 } /* int irq_read */
192
193 void module_register (void)
194 {
195         plugin_register_config ("irq", irq_config,
196                         config_keys, config_keys_num);
197         plugin_register_read ("irq", irq_read);
198 } /* void module_register */