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