1aef344f596219aa3287ffabedb118c1685a9e59
[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 #define BUFSIZE 128
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 unsigned int *irq_list;
45 static unsigned int irq_list_num;
46
47 /* 
48  * irq_list_action:
49  * 0 => default is to collect selected irqs
50  * 1 => ignore selcted irqs
51  */
52 static int irq_list_action;
53
54 static int irq_config (const char *key, const char *value)
55 {
56         if (strcasecmp (key, "Irq") == 0)
57         {
58                 unsigned int *temp;
59                 unsigned int irq;
60                 char *endptr;
61
62                 temp = (unsigned int *) realloc (irq_list, (irq_list_num + 1) * sizeof (unsigned int *));
63                 if (temp == NULL)
64                 {
65                         fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
66                         ERROR ("irq plugin: Cannot allocate more memory.");
67                         return (1);
68                 }
69                 irq_list = temp;
70
71                 /* Clear errno, because we need it to see if an error occured. */
72                 errno = 0;
73
74                 irq = strtol(value, &endptr, 10);
75                 if ((endptr == value) || (errno != 0))
76                 {
77                         fprintf (stderr, "irq plugin: Irq value is not a "
78                                         "number: `%s'\n", value);
79                         ERROR ("irq plugin: Irq value is not a "
80                                         "number: `%s'", value);
81                         return (1);
82                 }
83                 irq_list[irq_list_num] = irq;
84                 irq_list_num++;
85         }
86         else if (strcasecmp (key, "IgnoreSelected") == 0)
87         {
88                 if ((strcasecmp (value, "True") == 0)
89                                 || (strcasecmp (value, "Yes") == 0)
90                                 || (strcasecmp (value, "On") == 0))
91                         irq_list_action = 1;
92                 else
93                         irq_list_action = 0;
94         }
95         else
96         {
97                 return (-1);
98         }
99         return (0);
100 }
101
102 /*
103  * Check if this interface/instance should be ignored. This is called from
104  * both, `submit' and `write' to give client and server the ability to
105  * ignore certain stuff..
106  */
107 static int check_ignore_irq (const unsigned int irq)
108 {
109         int i;
110
111         if (irq_list_num < 1)
112                 return (0);
113
114         for (i = 0; (unsigned int)i < irq_list_num; i++)
115                 if (irq == irq_list[i])
116                         return (irq_list_action);
117
118         return (1 - irq_list_action);
119 }
120
121 static void irq_submit (unsigned int irq, counter_t value)
122 {
123         value_t values[1];
124         value_list_t vl = VALUE_LIST_INIT;
125         int status;
126
127         if (check_ignore_irq (irq))
128                 return;
129
130         values[0].counter = value;
131
132         vl.values = values;
133         vl.values_len = 1;
134         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
135         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
136         sstrncpy (vl.type, "irq", sizeof (vl.type));
137
138         status = ssnprintf (vl.type_instance, sizeof (vl.type_instance),
139                         "%u", irq);
140         if ((status < 1) || ((unsigned int)status >= sizeof (vl.type_instance)))
141                 return;
142
143         plugin_dispatch_values (&vl);
144 } /* void irq_submit */
145
146 static int irq_read (void)
147 {
148 #undef BUFSIZE
149 #define BUFSIZE 256
150
151         FILE *fh;
152         char buffer[BUFSIZE];
153         unsigned int irq;
154         unsigned long long irq_value;
155         unsigned long long value;
156         char *endptr;
157         int i;
158
159         char *fields[64];
160         int fields_num;
161
162         if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
163         {
164                 char errbuf[1024];
165                 WARNING ("irq plugin: fopen (/proc/interrupts): %s",
166                                 sstrerror (errno, errbuf, sizeof (errbuf)));
167                 return (-1);
168         }
169         while (fgets (buffer, BUFSIZE, fh) != NULL)
170         {
171                 fields_num = strsplit (buffer, fields, 64);
172                 if (fields_num < 2)
173                         continue;
174
175                 errno = 0;    /* To distinguish success/failure after call */
176                 irq = strtol (fields[0], &endptr, 10);
177
178                 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
179                         continue;
180
181                 irq_value = 0;
182                 for (i = 1; i < fields_num; i++)
183                 {
184                         errno = 0;
185                         value = strtoull (fields[i], &endptr, 10);
186
187                         if ((*endptr != '\0') || (errno != 0))
188                                 break;
189
190                         irq_value += value;
191                 } /* for (i) */
192
193                 /* Force 32bit wrap-around */
194                 irq_submit (irq, irq_value % 4294967296ULL);
195         }
196
197         fclose (fh);
198
199         return (0);
200 } /* int irq_read */
201
202 void module_register (void)
203 {
204         plugin_register_config ("irq", irq_config,
205                         config_keys, config_keys_num);
206         plugin_register_read ("irq", irq_read);
207 } /* void module_register */
208
209 #undef BUFSIZE