Merge pull request #19 from bostjan/master
[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 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.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[64];
99
100         fh = fopen ("/proc/interrupts", "r");
101         if (fh == NULL)
102         {
103                 char errbuf[1024];
104                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
105                                 sstrerror (errno, errbuf, sizeof (errbuf)));
106                 return (-1);
107         }
108
109         /* Get CPU count from the first line */
110         if(fgets (buffer, sizeof (buffer), fh) != NULL) {
111                 cpu_count = strsplit (buffer, fields, 64);
112         } else {
113                 ERROR ("irq plugin: unable to get CPU count from first line of /proc/interrupts");
114                 return (-1);
115         }
116
117         while (fgets (buffer, sizeof (buffer), fh) != NULL)
118         {
119                 char *irq_name;
120                 size_t irq_name_len;
121                 derive_t irq_value;
122                 int i;
123                 int fields_num;
124                 int irq_values_to_parse;
125
126                 fields_num = strsplit (buffer, fields, 64);
127                 if (fields_num < 2)
128                         continue;
129
130                 /* Parse this many numeric fields, skip the rest
131                  * (+1 because first there is a name of irq in each line) */
132                 if (fields_num >= cpu_count+1) {
133                         irq_values_to_parse = cpu_count;
134                 } else {
135                         irq_values_to_parse = fields_num - 1;
136                 }
137
138                 /* First field is irq name */
139                 irq_name = fields[0];
140                 irq_name_len = strlen (irq_name);
141                 if (irq_name_len < 2)
142                         continue;
143
144                 /* Check if irq name ends with colon.
145                  * Otherwise it's a header. */
146                 if (irq_name[irq_name_len - 1] != ':')
147                         continue;
148
149                 irq_name[irq_name_len - 1] = 0;
150                 irq_name_len--;
151
152                 irq_value = 0;
153                 for (i = 1; i <= irq_values_to_parse; i++)
154                 {
155                         /* Per-CPU value */
156                         value_t v;
157                         int status;
158
159                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
160                         if (status != 0)
161                                 break;
162
163                         irq_value += v.derive;
164                 } /* for (i) */
165
166                 /* No valid fields -> do not submit anything. */
167                 if (i <= 1)
168                         continue;
169
170                 irq_submit (irq_name, irq_value);
171         }
172
173         fclose (fh);
174
175         return (0);
176 } /* int irq_read */
177
178 void module_register (void)
179 {
180         plugin_register_config ("irq", irq_config,
181                         config_keys, config_keys_num);
182         plugin_register_read ("irq", irq_read);
183 } /* void module_register */