Merge branch 'collectd-4.10' into collectd-5.0
[collectd.git] / src / irq.c
1 /**
2  * collectd - src/irq.c
3  * Copyright (C) 2007  Peter Holik
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #if !KERNEL_LINUX
29 # error "No applicable input method."
30 #endif
31
32 /*
33  * (Module-)Global variables
34  */
35 static const char *config_keys[] =
36 {
37         "Irq",
38         "IgnoreSelected"
39 };
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41
42 static char         **irq_list;
43 static unsigned int   irq_list_num = 0;
44
45 /* 
46  * irq_list_action:
47  * 0 => default is to collect selected irqs
48  * 1 => ignore selcted irqs
49  */
50 static int irq_list_action;
51
52 static int irq_config (const char *key, const char *value)
53 {
54         if (strcasecmp (key, "Irq") == 0)
55         {
56                 char **temp;
57
58                 temp = realloc (irq_list, (irq_list_num + 1) * sizeof (*irq_list));
59                 if (temp == NULL)
60                 {
61                         fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
62                         ERROR ("irq plugin: Cannot allocate more memory.");
63                         return (1);
64                 }
65                 irq_list = temp;
66
67                 irq_list[irq_list_num] = strdup (value);
68                 if (irq_list[irq_list_num] == NULL)
69                 {
70                         ERROR ("irq plugin: strdup(3) failed.");
71                         return (1);
72                 }
73
74                 irq_list_num++;
75         }
76         else if (strcasecmp (key, "IgnoreSelected") == 0)
77         {
78                 if (IS_TRUE (value))
79                         irq_list_action = 1;
80                 else
81                         irq_list_action = 0;
82         }
83         else
84         {
85                 return (-1);
86         }
87         return (0);
88 }
89
90 /*
91  * Check if this interface/instance should be ignored. This is called from
92  * both, `submit' and `write' to give client and server the ability to
93  * ignore certain stuff..
94  */
95 static int check_ignore_irq (const char *irq)
96 {
97         unsigned int i;
98
99         if (irq_list_num < 1)
100                 return (0);
101
102         for (i = 0; i < irq_list_num; i++)
103                 if (strcmp (irq, irq_list[i]) == 0)
104                         return (irq_list_action);
105
106         return (1 - irq_list_action);
107 }
108
109 static void irq_submit (const char *irq_name, derive_t value)
110 {
111         value_t values[1];
112         value_list_t vl = VALUE_LIST_INIT;
113
114         if (check_ignore_irq (irq_name))
115                 return;
116
117         values[0].derive = value;
118
119         vl.values = values;
120         vl.values_len = 1;
121         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
122         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
123         sstrncpy (vl.type, "irq", sizeof (vl.type));
124         sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance));
125
126         plugin_dispatch_values (&vl);
127 } /* void irq_submit */
128
129 static int irq_read (void)
130 {
131         FILE *fh;
132         char buffer[1024];
133
134         fh = fopen ("/proc/interrupts", "r");
135         if (fh == NULL)
136         {
137                 char errbuf[1024];
138                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
139                                 sstrerror (errno, errbuf, sizeof (errbuf)));
140                 return (-1);
141         }
142
143         while (fgets (buffer, sizeof (buffer), fh) != NULL)
144         {
145                 char *irq_name;
146                 size_t irq_name_len;
147                 derive_t irq_value;
148                 int i;
149
150                 char *fields[64];
151                 int fields_num;
152
153                 fields_num = strsplit (buffer, fields, 64);
154                 if (fields_num < 2)
155                         continue;
156
157                 irq_name = fields[0];
158                 irq_name_len = strlen (irq_name);
159                 if (irq_name_len < 2)
160                         continue;
161
162                 /* Check if irq name ends with colon.
163                  * Otherwise it's a header. */
164                 if (irq_name[irq_name_len - 1] != ':')
165                         continue;
166
167                 irq_name[irq_name_len - 1] = 0;
168                 irq_name_len--;
169
170                 irq_value = 0;
171                 for (i = 1; i < fields_num; i++)
172                 {
173                         /* Per-CPU value */
174                         value_t v;
175                         int status;
176
177                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
178                         if (status != 0)
179                                 break;
180
181                         irq_value += v.derive;
182                 } /* for (i) */
183
184                 /* No valid fields -> do not submit anything. */
185                 if (i <= 1)
186                         continue;
187
188                 irq_submit (irq_name, irq_value);
189         }
190
191         fclose (fh);
192
193         return (0);
194 } /* int irq_read */
195
196 void module_register (void)
197 {
198         plugin_register_config ("irq", irq_config,
199                         config_keys, config_keys_num);
200         plugin_register_read ("irq", irq_read);
201 } /* void module_register */