Merge branch 'pr/1918'
[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_list_t vl = VALUE_LIST_INIT;
76
77         if (ignorelist_match (ignorelist, irq_name) != 0)
78                 return;
79
80         vl.values = &(value_t) { .derive = value };
81         vl.values_len = 1;
82         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
83         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
84         sstrncpy (vl.type, "irq", sizeof (vl.type));
85         sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance));
86
87         plugin_dispatch_values (&vl);
88 } /* void irq_submit */
89
90 static int irq_read (void)
91 {
92         FILE *fh;
93         char buffer[1024];
94         int  cpu_count;
95         char *fields[256];
96
97         /*
98          * Example content:
99          *         CPU0       CPU1       CPU2       CPU3
100          * 0:       2574          1          3          2   IO-APIC-edge      timer
101          * 1:     102553     158669     218062      70587   IO-APIC-edge      i8042
102          * 8:          0          0          0          1   IO-APIC-edge      rtc0
103          */
104         fh = fopen ("/proc/interrupts", "r");
105         if (fh == NULL)
106         {
107                 char errbuf[1024];
108                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
109                                 sstrerror (errno, errbuf, sizeof (errbuf)));
110                 return (-1);
111         }
112
113         /* Get CPU count from the first line */
114         if(fgets (buffer, sizeof (buffer), fh) != NULL) {
115                 cpu_count = strsplit (buffer, fields,
116                                 STATIC_ARRAY_SIZE (fields));
117         } else {
118                 ERROR ("irq plugin: unable to get CPU count from first line "
119                                 "of /proc/interrupts");
120                 fclose (fh);
121                 return (-1);
122         }
123
124         while (fgets (buffer, sizeof (buffer), fh) != NULL)
125         {
126                 char *irq_name;
127                 size_t irq_name_len;
128                 derive_t irq_value;
129                 int i;
130                 int fields_num;
131                 int irq_values_to_parse;
132
133                 fields_num = strsplit (buffer, fields,
134                                 STATIC_ARRAY_SIZE (fields));
135                 if (fields_num < 2)
136                         continue;
137
138                 /* Parse this many numeric fields, skip the rest
139                  * (+1 because first there is a name of irq in each line) */
140                 if (fields_num >= cpu_count + 1)
141                         irq_values_to_parse = cpu_count;
142                 else
143                         irq_values_to_parse = fields_num - 1;
144
145                 /* First field is irq name and colon */
146                 irq_name = fields[0];
147                 irq_name_len = strlen (irq_name);
148                 if (irq_name_len < 2)
149                         continue;
150
151                 /* Check if irq name ends with colon.
152                  * Otherwise it's a header. */
153                 if (irq_name[irq_name_len - 1] != ':')
154                         continue;
155
156                 /* Is it the the ARM fast interrupt (FIQ)? */
157                 if (irq_name_len == 4 && (strncmp(irq_name, "FIQ:", 4) == 0))
158                         continue;
159
160                 irq_name[irq_name_len - 1] = 0;
161                 irq_name_len--;
162
163                 irq_value = 0;
164                 for (i = 1; i <= irq_values_to_parse; i++)
165                 {
166                         /* Per-CPU value */
167                         value_t v;
168                         int status;
169
170                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
171                         if (status != 0)
172                                 break;
173
174                         irq_value += v.derive;
175                 } /* for (i) */
176
177                 /* No valid fields -> do not submit anything. */
178                 if (i <= 1)
179                         continue;
180
181                 irq_submit (irq_name, irq_value);
182         }
183
184         fclose (fh);
185
186         return (0);
187 } /* int irq_read */
188
189 void module_register (void)
190 {
191         plugin_register_config ("irq", irq_config,
192                         config_keys, config_keys_num);
193         plugin_register_read ("irq", irq_read);
194 } /* void module_register */