Code

Implemented `LoadDS' which tells plugins to only register their DataSources.
[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  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #if KERNEL_LINUX
29 # define IRQ_HAVE_READ 1
30 #else
31 # define IRQ_HAVE_READ 0
32 #endif
34 #define BUFSIZE 128
36 /*
37  * (Module-)Global variables
38  */
39 static data_source_t dsrc_irq[1] =
40 {
41         {"value", DS_TYPE_COUNTER, 0, 65535.0}
42 };
44 static data_set_t ds_irq =
45 {
46         "irq", 1, dsrc_irq
47 };
49 #if IRQ_HAVE_READ
50 static const char *config_keys[] =
51 {
52         "Irq",
53         "IgnoreSelected"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static unsigned int *irq_list;
58 static unsigned int irq_list_num;
60 /* 
61  * irq_list_action:
62  * 0 => default is to collect selected irqs
63  * 1 => ignore selcted irqs
64  */
65 static int irq_list_action;
67 static int irq_config (const char *key, const char *value)
68 {
69         if (strcasecmp (key, "Irq") == 0)
70         {
71                 unsigned int *temp;
72                 unsigned int irq;
73                 char *endptr;
75                 temp = (unsigned int *) realloc (irq_list, (irq_list_num + 1) * sizeof (unsigned int *));
76                 if (temp == NULL)
77                 {
78                         fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
79                         ERROR ("irq plugin: Cannot allocate more memory.");
80                         return (1);
81                 }
82                 irq_list = temp;
84                 /* Clear errno, because we need it to see if an error occured. */
85                 errno = 0;
87                 irq = strtol(value, &endptr, 10);
88                 if ((endptr == value) || (errno != 0))
89                 {
90                         fprintf (stderr, "irq plugin: Irq value is not a "
91                                         "number: `%s'\n", value);
92                         ERROR ("irq plugin: Irq value is not a "
93                                         "number: `%s'", value);
94                         return (1);
95                 }
96                 irq_list[irq_list_num] = irq;
97                 irq_list_num++;
98         }
99         else if (strcasecmp (key, "IgnoreSelected") == 0)
100         {
101                 if ((strcasecmp (value, "True") == 0)
102                                 || (strcasecmp (value, "Yes") == 0)
103                                 || (strcasecmp (value, "On") == 0))
104                         irq_list_action = 1;
105                 else
106                         irq_list_action = 0;
107         }
108         else
109         {
110                 return (-1);
111         }
112         return (0);
115 /*
116  * Check if this interface/instance should be ignored. This is called from
117  * both, `submit' and `write' to give client and server the ability to
118  * ignore certain stuff..
119  */
120 static int check_ignore_irq (const unsigned int irq)
122         int i;
124         if (irq_list_num < 1)
125                 return (0);
127         for (i = 0; i < irq_list_num; i++)
128                 if (irq == irq_list[i])
129                         return (irq_list_action);
131         return (1 - irq_list_action);
134 static void irq_submit (unsigned int irq, counter_t value)
136         value_t values[1];
137         value_list_t vl = VALUE_LIST_INIT;
138         int status;
140         if (check_ignore_irq (irq))
141                 return;
143         values[0].counter = value;
145         vl.values = values;
146         vl.values_len = 1;
147         vl.time = time (NULL);
148         strcpy (vl.host, hostname_g);
149         strcpy (vl.plugin, "irq");
151         status = snprintf (vl.type_instance, sizeof (vl.type_instance),
152                         "%u", irq);
153         if ((status < 1) || (status >= sizeof (vl.type_instance)))
154                 return;
156         plugin_dispatch_values ("irq", &vl);
157 } /* void irq_submit */
159 static int irq_read (void)
161 #if KERNEL_LINUX
163 #undef BUFSIZE
164 #define BUFSIZE 256
166         FILE *fh;
167         char buffer[BUFSIZE];
168         unsigned int irq;
169         unsigned int irq_value;
170         long value;
171         char *endptr;
172         int i;
174         char *fields[64];
175         int fields_num;
177         if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
178         {
179                 char errbuf[1024];
180                 WARNING ("irq plugin: fopen (/proc/interrupts): %s",
181                                 sstrerror (errno, errbuf, sizeof (errbuf)));
182                 return (-1);
183         }
184         while (fgets (buffer, BUFSIZE, fh) != NULL)
185         {
186                 fields_num = strsplit (buffer, fields, 64);
187                 if (fields_num < 2)
188                         continue;
190                 errno = 0;    /* To distinguish success/failure after call */
191                 irq = strtol (fields[0], &endptr, 10);
193                 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
194                         continue;
196                 irq_value = 0;
197                 for (i = 1; i < fields_num; i++)
198                 {
199                         errno = 0;
200                         value = strtol (fields[i], &endptr, 10);
202                         if ((*endptr != '\0') || (errno != 0))
203                                 break;
205                         irq_value += value;
206                 } /* for (i) */
208                 irq_submit (irq, irq_value);
209         }
210         fclose (fh);
211 #endif /* KERNEL_LINUX */
213         return (0);
214 } /* int irq_read */
215 #endif /* IRQ_HAVE_READ */
217 void module_register (modreg_e load)
219         if (load & MR_DATASETS)
220                 plugin_register_data_set (&ds_irq);
222 #if IRQ_HAVE_READ
223         if (load & MR_READ)
224         {
225                 plugin_register_config ("irq", irq_config,
226                                 config_keys, config_keys_num);
227                 plugin_register_read ("irq", irq_read);
228         }
229 #endif /* IRQ_HAVE_READ */
230 } /* void module_register */
232 #undef BUFSIZE