9b7e61877d8c62bafc009e91e1f792c43024e63e
[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         vl.time = time (NULL);
135         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
136         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
137         sstrncpy (vl.type, "irq", sizeof (vl.type));
138
139         status = ssnprintf (vl.type_instance, sizeof (vl.type_instance),
140                         "%u", irq);
141         if ((status < 1) || ((unsigned int)status >= sizeof (vl.type_instance)))
142                 return;
143
144         plugin_dispatch_values (&vl);
145 } /* void irq_submit */
146
147 static int irq_read (void)
148 {
149 #undef BUFSIZE
150 #define BUFSIZE 256
151
152         FILE *fh;
153         char buffer[BUFSIZE];
154         unsigned int irq;
155         unsigned int irq_value;
156         long value;
157         char *endptr;
158         int i;
159
160         char *fields[64];
161         int fields_num;
162
163         if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
164         {
165                 char errbuf[1024];
166                 WARNING ("irq plugin: fopen (/proc/interrupts): %s",
167                                 sstrerror (errno, errbuf, sizeof (errbuf)));
168                 return (-1);
169         }
170         while (fgets (buffer, BUFSIZE, fh) != NULL)
171         {
172                 fields_num = strsplit (buffer, fields, 64);
173                 if (fields_num < 2)
174                         continue;
175
176                 errno = 0;    /* To distinguish success/failure after call */
177                 irq = strtol (fields[0], &endptr, 10);
178
179                 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
180                         continue;
181
182                 irq_value = 0;
183                 for (i = 1; i < fields_num; i++)
184                 {
185                         errno = 0;
186                         value = strtol (fields[i], &endptr, 10);
187
188                         if ((*endptr != '\0') || (errno != 0))
189                                 break;
190
191                         irq_value += value;
192                 } /* for (i) */
193
194                 irq_submit (irq, irq_value);
195         }
196         fclose (fh);
197
198         return (0);
199 } /* int irq_read */
200
201 void module_register (void)
202 {
203         plugin_register_config ("irq", irq_config,
204                         config_keys, config_keys_num);
205         plugin_register_read ("irq", irq_read);
206 } /* void module_register */
207
208 #undef BUFSIZE