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