Merge remote-tracking branch 'github/pr/1879'
[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 "utils_ignorelist.h"
29
30 #if !KERNEL_LINUX
31 # error "No applicable input method."
32 #endif
33
34 /*
35  * (Module-)Global variables
36  */
37 static const char *config_keys[] =
38 {
39         "Irq",
40         "IgnoreSelected"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 static ignorelist_t *ignorelist = NULL;
45
46 /*
47  * Private functions
48  */
49 static int irq_config (const char *key, const char *value)
50 {
51         if (ignorelist == NULL)
52                 ignorelist = ignorelist_create (/* invert = */ 1);
53
54         if (strcasecmp (key, "Irq") == 0)
55         {
56                 ignorelist_add (ignorelist, value);
57         }
58         else if (strcasecmp (key, "IgnoreSelected") == 0)
59         {
60                 int invert = 1;
61                 if (IS_TRUE (value))
62                         invert = 0;
63                 ignorelist_set_invert (ignorelist, invert);
64         }
65         else
66         {
67                 return (-1);
68         }
69
70         return (0);
71 }
72
73 static void irq_submit (const char *irq_name, derive_t value)
74 {
75         value_t values[1];
76         value_list_t vl = VALUE_LIST_INIT;
77
78         if (ignorelist_match (ignorelist, irq_name) != 0)
79                 return;
80
81         values[0].derive = value;
82
83         vl.values = values;
84         vl.values_len = 1;
85         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
86         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
87         sstrncpy (vl.type, "irq", sizeof (vl.type));
88         sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance));
89
90         plugin_dispatch_values (&vl);
91 } /* void irq_submit */
92
93 static int irq_read (void)
94 {
95         FILE *fh;
96         char buffer[1024];
97         int  cpu_count;
98         char *fields[256];
99
100         /*
101          * Example content:
102          *         CPU0       CPU1       CPU2       CPU3
103          * 0:       2574          1          3          2   IO-APIC-edge      timer
104          * 1:     102553     158669     218062      70587   IO-APIC-edge      i8042
105          * 8:          0          0          0          1   IO-APIC-edge      rtc0
106          */
107         fh = fopen ("/proc/interrupts", "r");
108         if (fh == NULL)
109         {
110                 char errbuf[1024];
111                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
112                                 sstrerror (errno, errbuf, sizeof (errbuf)));
113                 return (-1);
114         }
115
116         /* Get CPU count from the first line */
117         if(fgets (buffer, sizeof (buffer), fh) != NULL) {
118                 cpu_count = strsplit (buffer, fields,
119                                 STATIC_ARRAY_SIZE (fields));
120         } else {
121                 ERROR ("irq plugin: unable to get CPU count from first line "
122                                 "of /proc/interrupts");
123                 fclose (fh);
124                 return (-1);
125         }
126
127         while (fgets (buffer, sizeof (buffer), fh) != NULL)
128         {
129                 char *irq_name;
130                 size_t irq_name_len;
131                 derive_t irq_value;
132                 int i;
133                 int fields_num;
134                 int irq_values_to_parse;
135
136                 fields_num = strsplit (buffer, fields,
137                                 STATIC_ARRAY_SIZE (fields));
138                 if (fields_num < 2)
139                         continue;
140
141                 /* Parse this many numeric fields, skip the rest
142                  * (+1 because first there is a name of irq in each line) */
143                 if (fields_num >= cpu_count + 1)
144                         irq_values_to_parse = cpu_count;
145                 else
146                         irq_values_to_parse = fields_num - 1;
147
148                 /* First field is irq name and colon */
149                 irq_name = fields[0];
150                 irq_name_len = strlen (irq_name);
151                 if (irq_name_len < 2)
152                         continue;
153
154                 /* Check if irq name ends with colon.
155                  * Otherwise it's a header. */
156                 if (irq_name[irq_name_len - 1] != ':')
157                         continue;
158
159                 /* Is it the the ARM fast interrupt (FIQ)? */
160                 if (irq_name_len == 4 && (strncmp(irq_name, "FIQ:", 4) == 0))
161                         continue;
162
163                 irq_name[irq_name_len - 1] = 0;
164                 irq_name_len--;
165
166                 irq_value = 0;
167                 for (i = 1; i <= irq_values_to_parse; i++)
168                 {
169                         /* Per-CPU value */
170                         value_t v;
171                         int status;
172
173                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
174                         if (status != 0)
175                                 break;
176
177                         irq_value += v.derive;
178                 } /* for (i) */
179
180                 /* No valid fields -> do not submit anything. */
181                 if (i <= 1)
182                         continue;
183
184                 irq_submit (irq_name, irq_value);
185         }
186
187         fclose (fh);
188
189         return (0);
190 } /* int irq_read */
191
192 void module_register (void)
193 {
194         plugin_register_config ("irq", irq_config,
195                         config_keys, config_keys_num);
196         plugin_register_read ("irq", irq_read);
197 } /* void module_register */