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