Various plugins: Convert more plugins to use "derive" instead of "counter".
[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 (IS_TRUE (value))
89                         irq_list_action = 1;
90                 else
91                         irq_list_action = 0;
92         }
93         else
94         {
95                 return (-1);
96         }
97         return (0);
98 }
99
100 /*
101  * Check if this interface/instance should be ignored. This is called from
102  * both, `submit' and `write' to give client and server the ability to
103  * ignore certain stuff..
104  */
105 static int check_ignore_irq (const unsigned int irq)
106 {
107         int i;
108
109         if (irq_list_num < 1)
110                 return (0);
111
112         for (i = 0; (unsigned int)i < irq_list_num; i++)
113                 if (irq == irq_list[i])
114                         return (irq_list_action);
115
116         return (1 - irq_list_action);
117 }
118
119 static void irq_submit (unsigned int irq, derive_t value)
120 {
121         value_t values[1];
122         value_list_t vl = VALUE_LIST_INIT;
123         int status;
124
125         if (check_ignore_irq (irq))
126                 return;
127
128         values[0].derive = value;
129
130         vl.values = values;
131         vl.values_len = 1;
132         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
133         sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
134         sstrncpy (vl.type, "irq", sizeof (vl.type));
135
136         status = ssnprintf (vl.type_instance, sizeof (vl.type_instance),
137                         "%u", irq);
138         if ((status < 1) || ((unsigned int)status >= sizeof (vl.type_instance)))
139                 return;
140
141         plugin_dispatch_values (&vl);
142 } /* void irq_submit */
143
144 static int irq_read (void)
145 {
146         FILE *fh;
147         char buffer[1024];
148
149         fh = fopen ("/proc/interrupts", "r");
150         if (fh == NULL)
151         {
152                 char errbuf[1024];
153                 ERROR ("irq plugin: fopen (/proc/interrupts): %s",
154                                 sstrerror (errno, errbuf, sizeof (errbuf)));
155                 return (-1);
156         }
157
158         while (fgets (buffer, BUFSIZE, fh) != NULL)
159         {
160                 unsigned int irq;
161                 derive_t irq_value;
162                 char *endptr;
163                 int i;
164
165                 char *fields[64];
166                 int fields_num;
167
168                 fields_num = strsplit (buffer, fields, 64);
169                 if (fields_num < 2)
170                         continue;
171
172                 errno = 0;    /* To distinguish success/failure after call */
173                 irq = (unsigned int) strtoul (fields[0], &endptr, /* base = */ 10);
174
175                 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
176                         continue;
177
178                 irq_value = 0;
179                 for (i = 1; i < fields_num; i++)
180                 {
181                         /* Per-CPU value */
182                         value_t v;
183                         int status;
184
185                         status = parse_value (fields[i], &v, DS_TYPE_DERIVE);
186                         if (status != 0)
187                                 break;
188
189                         irq_value += v.derive;
190                 } /* for (i) */
191
192                 if (i < fields_num)
193                         continue;
194
195                 irq_submit (irq, irq_value);
196         }
197
198         fclose (fh);
199
200         return (0);
201 } /* int irq_read */
202
203 void module_register (void)
204 {
205         plugin_register_config ("irq", irq_config,
206                         config_keys, config_keys_num);
207         plugin_register_read ("irq", irq_read);
208 } /* void module_register */
209
210 #undef BUFSIZE